]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/command.c
import gdb-1999-05-25 snapshot
[thirdparty/binutils-gdb.git] / gdb / command.c
1 /* Handle lists of commands, their decoding and documentation, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1998 Free Software Foundation, Inc.
3
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.
8
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.
13
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. */
17
18 #include "defs.h"
19 #include "gdbcmd.h"
20 #include "symtab.h"
21 #include "value.h"
22 #include <ctype.h>
23 #include "gdb_string.h"
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #ifdef HAVE_WAIT_H
29 # include <wait.h>
30 #else
31 # ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
33 # endif
34 #endif
35
36 #include "wait.h"
37
38 /* Prototypes for local functions */
39
40 static void undef_cmd_error PARAMS ((char *, char *));
41
42 static void show_user PARAMS ((char *, int));
43
44 static void show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
45
46 static void make_command PARAMS ((char *, int));
47
48 static void shell_escape PARAMS ((char *, int));
49
50 static int parse_binary_operation PARAMS ((char *));
51
52 static void print_doc_line PARAMS ((GDB_FILE *, char *));
53
54 static struct cmd_list_element *find_cmd PARAMS ((char *command,
55 int len,
56 struct cmd_list_element *clist,
57 int ignore_help_classes,
58 int *nfound));
59
60 void _initialize_command PARAMS ((void));
61
62 /* Add element named NAME.
63 CLASS is the top level category into which commands are broken down
64 for "help" purposes.
65 FUN should be the function to execute the command;
66 it will get a character string as argument, with leading
67 and trailing blanks already eliminated.
68
69 DOC is a documentation string for the command.
70 Its first line should be a complete sentence.
71 It should start with ? for a command that is an abbreviation
72 or with * for a command that most users don't need to know about.
73
74 Add this command to command list *LIST.
75
76 Returns a pointer to the added command (not necessarily the head
77 of *LIST). */
78
79 struct cmd_list_element *
80 add_cmd (name, class, fun, doc, list)
81 char *name;
82 enum command_class class;
83 void (*fun) PARAMS ((char *, int));
84 char *doc;
85 struct cmd_list_element **list;
86 {
87 register struct cmd_list_element *c
88 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
89 struct cmd_list_element *p;
90
91 delete_cmd (name, list);
92
93 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
94 {
95 c->next = *list;
96 *list = c;
97 }
98 else
99 {
100 p = *list;
101 while (p->next && STRCMP (p->next->name, name) <= 0)
102 {
103 p = p->next;
104 }
105 c->next = p->next;
106 p->next = c;
107 }
108
109 c->name = name;
110 c->class = class;
111 c->function.cfunc = fun;
112 c->doc = doc;
113 c->hook = NULL;
114 c->prefixlist = NULL;
115 c->prefixname = NULL;
116 c->allow_unknown = 0;
117 c->abbrev_flag = 0;
118 c->completer = make_symbol_completion_list;
119 c->type = not_set_cmd;
120 c->var = NULL;
121 c->var_type = var_boolean;
122 c->enums = NULL;
123 c->user_commands = NULL;
124 c->hookee = NULL;
125 c->cmd_pointer = NULL;
126
127 return c;
128 }
129
130 /* Same as above, except that the abbrev_flag is set. */
131
132 #if 0 /* Currently unused */
133
134 struct cmd_list_element *
135 add_abbrev_cmd (name, class, fun, doc, list)
136 char *name;
137 enum command_class class;
138 void (*fun) PARAMS ((char *, int));
139 char *doc;
140 struct cmd_list_element **list;
141 {
142 register struct cmd_list_element *c
143 = add_cmd (name, class, fun, doc, list);
144
145 c->abbrev_flag = 1;
146 return c;
147 }
148
149 #endif
150
151 struct cmd_list_element *
152 add_alias_cmd (name, oldname, class, abbrev_flag, list)
153 char *name;
154 char *oldname;
155 enum command_class class;
156 int abbrev_flag;
157 struct cmd_list_element **list;
158 {
159 /* Must do this since lookup_cmd tries to side-effect its first arg */
160 char *copied_name;
161 register struct cmd_list_element *old;
162 register struct cmd_list_element *c;
163 copied_name = (char *) alloca (strlen (oldname) + 1);
164 strcpy (copied_name, oldname);
165 old = lookup_cmd (&copied_name, *list, "", 1, 1);
166
167 if (old == 0)
168 {
169 delete_cmd (name, list);
170 return 0;
171 }
172
173 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
174 c->prefixlist = old->prefixlist;
175 c->prefixname = old->prefixname;
176 c->allow_unknown = old->allow_unknown;
177 c->abbrev_flag = abbrev_flag;
178 c->cmd_pointer = old;
179 return c;
180 }
181
182 /* Like add_cmd but adds an element for a command prefix:
183 a name that should be followed by a subcommand to be looked up
184 in another command list. PREFIXLIST should be the address
185 of the variable containing that list. */
186
187 struct cmd_list_element *
188 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
189 allow_unknown, list)
190 char *name;
191 enum command_class class;
192 void (*fun) PARAMS ((char *, int));
193 char *doc;
194 struct cmd_list_element **prefixlist;
195 char *prefixname;
196 int allow_unknown;
197 struct cmd_list_element **list;
198 {
199 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
200 c->prefixlist = prefixlist;
201 c->prefixname = prefixname;
202 c->allow_unknown = allow_unknown;
203 return c;
204 }
205
206 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
207
208 struct cmd_list_element *
209 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
210 allow_unknown, list)
211 char *name;
212 enum command_class class;
213 void (*fun) PARAMS ((char *, int));
214 char *doc;
215 struct cmd_list_element **prefixlist;
216 char *prefixname;
217 int allow_unknown;
218 struct cmd_list_element **list;
219 {
220 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
221 c->prefixlist = prefixlist;
222 c->prefixname = prefixname;
223 c->allow_unknown = allow_unknown;
224 c->abbrev_flag = 1;
225 return c;
226 }
227
228 /* This is an empty "cfunc". */
229 void
230 not_just_help_class_command (args, from_tty)
231 char *args;
232 int from_tty;
233 {
234 }
235
236 /* This is an empty "sfunc". */
237 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
238
239 static void
240 empty_sfunc (args, from_tty, c)
241 char *args;
242 int from_tty;
243 struct cmd_list_element *c;
244 {
245 }
246
247 /* Add element named NAME to command list LIST (the list for set
248 or some sublist thereof).
249 CLASS is as in add_cmd.
250 VAR_TYPE is the kind of thing we are setting.
251 VAR is address of the variable being controlled by this command.
252 DOC is the documentation string. */
253
254 struct cmd_list_element *
255 add_set_cmd (name, class, var_type, var, doc, list)
256 char *name;
257 enum command_class class;
258 var_types var_type;
259 char *var;
260 char *doc;
261 struct cmd_list_element **list;
262 {
263 struct cmd_list_element *c
264 = add_cmd (name, class, NO_FUNCTION, doc, list);
265
266 c->type = set_cmd;
267 c->var_type = var_type;
268 c->var = var;
269 /* This needs to be something besides NO_FUNCTION so that this isn't
270 treated as a help class. */
271 c->function.sfunc = empty_sfunc;
272 return c;
273 }
274
275 /* Add element named NAME to command list LIST (the list for set
276 or some sublist thereof).
277 CLASS is as in add_cmd.
278 ENUMLIST is a list of strings which may follow NAME.
279 VAR is address of the variable which will contain the matching string
280 (from ENUMLIST).
281 DOC is the documentation string. */
282
283 struct cmd_list_element *
284 add_set_enum_cmd (name, class, enumlist, var, doc, list)
285 char *name;
286 enum command_class class;
287 char *enumlist[];
288 char *var;
289 char *doc;
290 struct cmd_list_element **list;
291 {
292 struct cmd_list_element *c
293 = add_set_cmd (name, class, var_enum, var, doc, list);
294 c->enums = enumlist;
295
296 return c;
297 }
298
299 /* Where SETCMD has already been added, add the corresponding show
300 command to LIST and return a pointer to the added command (not
301 necessarily the head of LIST). */
302 struct cmd_list_element *
303 add_show_from_set (setcmd, list)
304 struct cmd_list_element *setcmd;
305 struct cmd_list_element **list;
306 {
307 struct cmd_list_element *showcmd =
308 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
309 struct cmd_list_element *p;
310
311 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
312 delete_cmd (showcmd->name, list);
313 showcmd->type = show_cmd;
314
315 /* Replace "set " at start of docstring with "show ". */
316 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
317 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
318 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
319 else
320 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
321
322 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
323 {
324 showcmd->next = *list;
325 *list = showcmd;
326 }
327 else
328 {
329 p = *list;
330 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
331 {
332 p = p->next;
333 }
334 showcmd->next = p->next;
335 p->next = showcmd;
336 }
337
338 return showcmd;
339 }
340
341 /* Remove the command named NAME from the command list. */
342
343 void
344 delete_cmd (name, list)
345 char *name;
346 struct cmd_list_element **list;
347 {
348 register struct cmd_list_element *c;
349 struct cmd_list_element *p;
350
351 while (*list && STREQ ((*list)->name, name))
352 {
353 if ((*list)->hookee)
354 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
355 p = (*list)->next;
356 free ((PTR)*list);
357 *list = p;
358 }
359
360 if (*list)
361 for (c = *list; c->next;)
362 {
363 if (STREQ (c->next->name, name))
364 {
365 if (c->next->hookee)
366 c->next->hookee->hook = 0; /* hooked cmd gets away. */
367 p = c->next->next;
368 free ((PTR)c->next);
369 c->next = p;
370 }
371 else
372 c = c->next;
373 }
374 }
375
376 /* This command really has to deal with two things:
377 * 1) I want documentation on *this string* (usually called by
378 * "help commandname").
379 * 2) I want documentation on *this list* (usually called by
380 * giving a command that requires subcommands. Also called by saying
381 * just "help".)
382 *
383 * I am going to split this into two seperate comamnds, help_cmd and
384 * help_list.
385 */
386
387 void
388 help_cmd (command, stream)
389 char *command;
390 GDB_FILE *stream;
391 {
392 struct cmd_list_element *c;
393 extern struct cmd_list_element *cmdlist;
394
395 if (!command)
396 {
397 help_list (cmdlist, "", all_classes, stream);
398 return;
399 }
400
401 c = lookup_cmd (&command, cmdlist, "", 0, 0);
402
403 if (c == 0)
404 return;
405
406 /* There are three cases here.
407 If c->prefixlist is nonzero, we have a prefix command.
408 Print its documentation, then list its subcommands.
409
410 If c->function is nonzero, we really have a command.
411 Print its documentation and return.
412
413 If c->function is zero, we have a class name.
414 Print its documentation (as if it were a command)
415 and then set class to the number of this class
416 so that the commands in the class will be listed. */
417
418 fputs_filtered (c->doc, stream);
419 fputs_filtered ("\n", stream);
420
421 if (c->prefixlist == 0 && c->function.cfunc != NULL)
422 return;
423 fprintf_filtered (stream, "\n");
424
425 /* If this is a prefix command, print it's subcommands */
426 if (c->prefixlist)
427 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
428
429 /* If this is a class name, print all of the commands in the class */
430 if (c->function.cfunc == NULL)
431 help_list (cmdlist, "", c->class, stream);
432
433 if (c->hook)
434 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
435 c->hook->name);
436 }
437
438 /*
439 * Get a specific kind of help on a command list.
440 *
441 * LIST is the list.
442 * CMDTYPE is the prefix to use in the title string.
443 * CLASS is the class with which to list the nodes of this list (see
444 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
445 * everything, ALL_CLASSES for just classes, and non-negative for only things
446 * in a specific class.
447 * and STREAM is the output stream on which to print things.
448 * If you call this routine with a class >= 0, it recurses.
449 */
450 void
451 help_list (list, cmdtype, class, stream)
452 struct cmd_list_element *list;
453 char *cmdtype;
454 enum command_class class;
455 GDB_FILE *stream;
456 {
457 int len;
458 char *cmdtype1, *cmdtype2;
459
460 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
461 len = strlen (cmdtype);
462 cmdtype1 = (char *) alloca (len + 1);
463 cmdtype1[0] = 0;
464 cmdtype2 = (char *) alloca (len + 4);
465 cmdtype2[0] = 0;
466 if (len)
467 {
468 cmdtype1[0] = ' ';
469 strncpy (cmdtype1 + 1, cmdtype, len - 1);
470 cmdtype1[len] = 0;
471 strncpy (cmdtype2, cmdtype, len - 1);
472 strcpy (cmdtype2 + len - 1, " sub");
473 }
474
475 if (class == all_classes)
476 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
477 else
478 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
479
480 help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
481
482 if (class == all_classes)
483 fprintf_filtered (stream, "\n\
484 Type \"help%s\" followed by a class name for a list of commands in that class.",
485 cmdtype1);
486
487 fprintf_filtered (stream, "\n\
488 Type \"help%s\" followed by %scommand name for full documentation.\n\
489 Command name abbreviations are allowed if unambiguous.\n",
490 cmdtype1, cmdtype2);
491 }
492
493 /* Print only the first line of STR on STREAM. */
494 static void
495 print_doc_line (stream, str)
496 GDB_FILE *stream;
497 char *str;
498 {
499 static char *line_buffer = 0;
500 static int line_size;
501 register char *p;
502
503 if (!line_buffer)
504 {
505 line_size = 80;
506 line_buffer = (char *) xmalloc (line_size);
507 }
508
509 p = str;
510 while (*p && *p != '\n' && *p != '.' && *p != ',')
511 p++;
512 if (p - str > line_size - 1)
513 {
514 line_size = p - str + 1;
515 free ((PTR)line_buffer);
516 line_buffer = (char *) xmalloc (line_size);
517 }
518 strncpy (line_buffer, str, p - str);
519 line_buffer[p - str] = '\0';
520 if (islower (line_buffer[0]))
521 line_buffer[0] = toupper (line_buffer[0]);
522 fputs_filtered (line_buffer, stream);
523 }
524
525 /*
526 * Implement a help command on command list LIST.
527 * RECURSE should be non-zero if this should be done recursively on
528 * all sublists of LIST.
529 * PREFIX is the prefix to print before each command name.
530 * STREAM is the stream upon which the output should be written.
531 * CLASS should be:
532 * A non-negative class number to list only commands in that
533 * class.
534 * ALL_COMMANDS to list all commands in list.
535 * ALL_CLASSES to list all classes in list.
536 *
537 * Note that RECURSE will be active on *all* sublists, not just the
538 * ones selected by the criteria above (ie. the selection mechanism
539 * is at the low level, not the high-level).
540 */
541 void
542 help_cmd_list (list, class, prefix, recurse, stream)
543 struct cmd_list_element *list;
544 enum command_class class;
545 char *prefix;
546 int recurse;
547 GDB_FILE *stream;
548 {
549 register struct cmd_list_element *c;
550
551 for (c = list; c; c = c->next)
552 {
553 if (c->abbrev_flag == 0 &&
554 (class == all_commands
555 || (class == all_classes && c->function.cfunc == NULL)
556 || (class == c->class && c->function.cfunc != NULL)))
557 {
558 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
559 print_doc_line (stream, c->doc);
560 fputs_filtered ("\n", stream);
561 }
562 if (recurse
563 && c->prefixlist != 0
564 && c->abbrev_flag == 0)
565 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
566 }
567 }
568
569 \f
570 /* Search the input clist for 'command'. Return the command if
571 found (or NULL if not), and return the number of commands
572 found in nfound */
573
574 static struct cmd_list_element *
575 find_cmd(command, len, clist, ignore_help_classes, nfound)
576 char *command;
577 int len;
578 struct cmd_list_element *clist;
579 int ignore_help_classes;
580 int *nfound;
581 {
582 struct cmd_list_element *found, *c;
583
584 found = (struct cmd_list_element *)NULL;
585 *nfound = 0;
586 for (c = clist; c; c = c->next)
587 if (!strncmp (command, c->name, len)
588 && (!ignore_help_classes || c->function.cfunc))
589 {
590 found = c;
591 (*nfound)++;
592 if (c->name[len] == '\0')
593 {
594 *nfound = 1;
595 break;
596 }
597 }
598 return found;
599 }
600
601 /* This routine takes a line of TEXT and a CLIST in which to start the
602 lookup. When it returns it will have incremented the text pointer past
603 the section of text it matched, set *RESULT_LIST to point to the list in
604 which the last word was matched, and will return a pointer to the cmd
605 list element which the text matches. It will return NULL if no match at
606 all was possible. It will return -1 (cast appropriately, ick) if ambigous
607 matches are possible; in this case *RESULT_LIST will be set to point to
608 the list in which there are ambiguous choices (and *TEXT will be set to
609 the ambiguous text string).
610
611 If the located command was an abbreviation, this routine returns the base
612 command of the abbreviation.
613
614 It does no error reporting whatsoever; control will always return
615 to the superior routine.
616
617 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
618 at the prefix_command (ie. the best match) *or* (special case) will be NULL
619 if no prefix command was ever found. For example, in the case of "info a",
620 "info" matches without ambiguity, but "a" could be "args" or "address", so
621 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
622 RESULT_LIST should not be interpeted as a pointer to the beginning of a
623 list; it simply points to a specific command. In the case of an ambiguous
624 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
625 "info t" can be "info types" or "info target"; upon return *TEXT has been
626 advanced past "info ").
627
628 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
629 affect the operation).
630
631 This routine does *not* modify the text pointed to by TEXT.
632
633 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
634 are actually help classes rather than commands (i.e. the function field of
635 the struct cmd_list_element is NULL). */
636
637 struct cmd_list_element *
638 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
639 char **text;
640 struct cmd_list_element *clist, **result_list;
641 int ignore_help_classes;
642 {
643 char *p, *command;
644 int len, tmp, nfound;
645 struct cmd_list_element *found, *c;
646
647 while (**text == ' ' || **text == '\t')
648 (*text)++;
649
650 /* Treating underscores as part of command words is important
651 so that "set args_foo()" doesn't get interpreted as
652 "set args _foo()". */
653 for (p = *text;
654 *p && (isalnum(*p) || *p == '-' || *p == '_' ||
655 (tui_version &&
656 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
657 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
658 p++)
659 ;
660
661 /* If nothing but whitespace, return 0. */
662 if (p == *text)
663 return 0;
664
665 len = p - *text;
666
667 /* *text and p now bracket the first command word to lookup (and
668 it's length is len). We copy this into a local temporary */
669
670
671 command = (char *) alloca (len + 1);
672 for (tmp = 0; tmp < len; tmp++)
673 {
674 char x = (*text)[tmp];
675 command[tmp] = x;
676 }
677 command[len] = '\0';
678
679 /* Look it up. */
680 found = 0;
681 nfound = 0;
682 found = find_cmd(command, len, clist, ignore_help_classes, &nfound);
683
684 /*
685 ** We didn't find the command in the entered case, so lower case it
686 ** and search again.
687 */
688 if (!found || nfound == 0)
689 {
690 for (tmp = 0; tmp < len; tmp++)
691 {
692 char x = command[tmp];
693 command[tmp] = isupper(x) ? tolower(x) : x;
694 }
695 found = find_cmd(command, len, clist, ignore_help_classes, &nfound);
696 }
697
698 /* If nothing matches, we have a simple failure. */
699 if (nfound == 0)
700 return 0;
701
702 if (nfound > 1)
703 {
704 if (result_list != NULL)
705 /* Will be modified in calling routine
706 if we know what the prefix command is. */
707 *result_list = 0;
708 return (struct cmd_list_element *) -1; /* Ambiguous. */
709 }
710
711 /* We've matched something on this list. Move text pointer forward. */
712
713 *text = p;
714
715 /* If this was an abbreviation, use the base command instead. */
716
717 if (found->cmd_pointer)
718 found = found->cmd_pointer;
719
720 /* If we found a prefix command, keep looking. */
721
722 if (found->prefixlist)
723 {
724 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
725 ignore_help_classes);
726 if (!c)
727 {
728 /* Didn't find anything; this is as far as we got. */
729 if (result_list != NULL)
730 *result_list = clist;
731 return found;
732 }
733 else if (c == (struct cmd_list_element *) -1)
734 {
735 /* We've gotten this far properly, but the next step
736 is ambiguous. We need to set the result list to the best
737 we've found (if an inferior hasn't already set it). */
738 if (result_list != NULL)
739 if (!*result_list)
740 /* This used to say *result_list = *found->prefixlist
741 If that was correct, need to modify the documentation
742 at the top of this function to clarify what is supposed
743 to be going on. */
744 *result_list = found;
745 return c;
746 }
747 else
748 {
749 /* We matched! */
750 return c;
751 }
752 }
753 else
754 {
755 if (result_list != NULL)
756 *result_list = clist;
757 return found;
758 }
759 }
760
761 /* All this hair to move the space to the front of cmdtype */
762
763 static void
764 undef_cmd_error (cmdtype, q)
765 char *cmdtype, *q;
766 {
767 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
768 cmdtype,
769 q,
770 *cmdtype? " ": "",
771 strlen(cmdtype)-1,
772 cmdtype);
773 }
774
775 /* Look up the contents of *LINE as a command in the command list LIST.
776 LIST is a chain of struct cmd_list_element's.
777 If it is found, return the struct cmd_list_element for that command
778 and update *LINE to point after the command name, at the first argument.
779 If not found, call error if ALLOW_UNKNOWN is zero
780 otherwise (or if error returns) return zero.
781 Call error if specified command is ambiguous,
782 unless ALLOW_UNKNOWN is negative.
783 CMDTYPE precedes the word "command" in the error message.
784
785 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
786 elements which are actually help classes rather than commands (i.e.
787 the function field of the struct cmd_list_element is 0). */
788
789 struct cmd_list_element *
790 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
791 char **line;
792 struct cmd_list_element *list;
793 char *cmdtype;
794 int allow_unknown;
795 int ignore_help_classes;
796 {
797 struct cmd_list_element *last_list = 0;
798 struct cmd_list_element *c =
799 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
800 #if 0
801 /* This is wrong for complete_command. */
802 char *ptr = (*line) + strlen (*line) - 1;
803
804 /* Clear off trailing whitespace. */
805 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
806 ptr--;
807 *(ptr + 1) = '\0';
808 #endif
809
810 if (!c)
811 {
812 if (!allow_unknown)
813 {
814 if (!*line)
815 error ("Lack of needed %scommand", cmdtype);
816 else
817 {
818 char *p = *line, *q;
819
820 while (isalnum(*p) || *p == '-')
821 p++;
822
823 q = (char *) alloca (p - *line + 1);
824 strncpy (q, *line, p - *line);
825 q[p - *line] = '\0';
826 undef_cmd_error (cmdtype, q);
827 }
828 }
829 else
830 return 0;
831 }
832 else if (c == (struct cmd_list_element *) -1)
833 {
834 /* Ambigous. Local values should be off prefixlist or called
835 values. */
836 int local_allow_unknown = (last_list ? last_list->allow_unknown :
837 allow_unknown);
838 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
839 struct cmd_list_element *local_list =
840 (last_list ? *(last_list->prefixlist) : list);
841
842 if (local_allow_unknown < 0)
843 {
844 if (last_list)
845 return last_list; /* Found something. */
846 else
847 return 0; /* Found nothing. */
848 }
849 else
850 {
851 /* Report as error. */
852 int amb_len;
853 char ambbuf[100];
854
855 for (amb_len = 0;
856 ((*line)[amb_len] && (*line)[amb_len] != ' '
857 && (*line)[amb_len] != '\t');
858 amb_len++)
859 ;
860
861 ambbuf[0] = 0;
862 for (c = local_list; c; c = c->next)
863 if (!strncmp (*line, c->name, amb_len))
864 {
865 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
866 {
867 if (strlen (ambbuf))
868 strcat (ambbuf, ", ");
869 strcat (ambbuf, c->name);
870 }
871 else
872 {
873 strcat (ambbuf, "..");
874 break;
875 }
876 }
877 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
878 *line, ambbuf);
879 return 0; /* lint */
880 }
881 }
882 else
883 {
884 /* We've got something. It may still not be what the caller
885 wants (if this command *needs* a subcommand). */
886 while (**line == ' ' || **line == '\t')
887 (*line)++;
888
889 if (c->prefixlist && **line && !c->allow_unknown)
890 undef_cmd_error (c->prefixname, *line);
891
892 /* Seems to be what he wants. Return it. */
893 return c;
894 }
895 return 0;
896 }
897
898 #if 0
899 /* Look up the contents of *LINE as a command in the command list LIST.
900 LIST is a chain of struct cmd_list_element's.
901 If it is found, return the struct cmd_list_element for that command
902 and update *LINE to point after the command name, at the first argument.
903 If not found, call error if ALLOW_UNKNOWN is zero
904 otherwise (or if error returns) return zero.
905 Call error if specified command is ambiguous,
906 unless ALLOW_UNKNOWN is negative.
907 CMDTYPE precedes the word "command" in the error message. */
908
909 struct cmd_list_element *
910 lookup_cmd (line, list, cmdtype, allow_unknown)
911 char **line;
912 struct cmd_list_element *list;
913 char *cmdtype;
914 int allow_unknown;
915 {
916 register char *p;
917 register struct cmd_list_element *c, *found;
918 int nfound;
919 char ambbuf[100];
920 char *processed_cmd;
921 int i, cmd_len;
922
923 /* Skip leading whitespace. */
924
925 while (**line == ' ' || **line == '\t')
926 (*line)++;
927
928 /* Clear out trailing whitespace. */
929
930 p = *line + strlen (*line);
931 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
932 p--;
933 *p = 0;
934
935 /* Find end of command name. */
936
937 p = *line;
938 while (*p == '-' || isalnum(*p))
939 p++;
940
941 /* Look up the command name.
942 If exact match, keep that.
943 Otherwise, take command abbreviated, if unique. Note that (in my
944 opinion) a null string does *not* indicate ambiguity; simply the
945 end of the argument. */
946
947 if (p == *line)
948 {
949 if (!allow_unknown)
950 error ("Lack of needed %scommand", cmdtype);
951 return 0;
952 }
953
954 /* Copy over to a local buffer, converting to lowercase on the way.
955 This is in case the command being parsed is a subcommand which
956 doesn't match anything, and that's ok. We want the original
957 untouched for the routine of the original command. */
958
959 processed_cmd = (char *) alloca (p - *line + 1);
960 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
961 {
962 char x = (*line)[cmd_len];
963 if (isupper(x))
964 processed_cmd[cmd_len] = tolower(x);
965 else
966 processed_cmd[cmd_len] = x;
967 }
968 processed_cmd[cmd_len] = '\0';
969
970 /* Check all possibilities in the current command list. */
971 found = 0;
972 nfound = 0;
973 for (c = list; c; c = c->next)
974 {
975 if (!strncmp (processed_cmd, c->name, cmd_len))
976 {
977 found = c;
978 nfound++;
979 if (c->name[cmd_len] == 0)
980 {
981 nfound = 1;
982 break;
983 }
984 }
985 }
986
987 /* Report error for undefined command name. */
988
989 if (nfound != 1)
990 {
991 if (nfound > 1 && allow_unknown >= 0)
992 {
993 ambbuf[0] = 0;
994 for (c = list; c; c = c->next)
995 if (!strncmp (processed_cmd, c->name, cmd_len))
996 {
997 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
998 {
999 if (strlen (ambbuf))
1000 strcat (ambbuf, ", ");
1001 strcat (ambbuf, c->name);
1002 }
1003 else
1004 {
1005 strcat (ambbuf, "..");
1006 break;
1007 }
1008 }
1009 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1010 processed_cmd, ambbuf);
1011 }
1012 else if (!allow_unknown)
1013 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1014 return 0;
1015 }
1016
1017 /* Skip whitespace before the argument. */
1018
1019 while (*p == ' ' || *p == '\t') p++;
1020 *line = p;
1021
1022 if (found->prefixlist && *p)
1023 {
1024 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1025 found->allow_unknown);
1026 if (c)
1027 return c;
1028 }
1029
1030 return found;
1031 }
1032 #endif
1033
1034 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1035
1036 /* Return a vector of char pointers which point to the different
1037 possible completions in LIST of TEXT.
1038
1039 WORD points in the same buffer as TEXT, and completions should be
1040 returned relative to this position. For example, suppose TEXT is "foo"
1041 and we want to complete to "foobar". If WORD is "oo", return
1042 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1043
1044 char **
1045 complete_on_cmdlist (list, text, word)
1046 struct cmd_list_element *list;
1047 char *text;
1048 char *word;
1049 {
1050 struct cmd_list_element *ptr;
1051 char **matchlist;
1052 int sizeof_matchlist;
1053 int matches;
1054 int textlen = strlen (text);
1055
1056 sizeof_matchlist = 10;
1057 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1058 matches = 0;
1059
1060 for (ptr = list; ptr; ptr = ptr->next)
1061 if (!strncmp (ptr->name, text, textlen)
1062 && !ptr->abbrev_flag
1063 && (ptr->function.cfunc
1064 || ptr->prefixlist))
1065 {
1066 if (matches == sizeof_matchlist)
1067 {
1068 sizeof_matchlist *= 2;
1069 matchlist = (char **) xrealloc ((char *)matchlist,
1070 (sizeof_matchlist
1071 * sizeof (char *)));
1072 }
1073
1074 matchlist[matches] = (char *)
1075 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1076 if (word == text)
1077 strcpy (matchlist[matches], ptr->name);
1078 else if (word > text)
1079 {
1080 /* Return some portion of ptr->name. */
1081 strcpy (matchlist[matches], ptr->name + (word - text));
1082 }
1083 else
1084 {
1085 /* Return some of text plus ptr->name. */
1086 strncpy (matchlist[matches], word, text - word);
1087 matchlist[matches][text - word] = '\0';
1088 strcat (matchlist[matches], ptr->name);
1089 }
1090 ++matches;
1091 }
1092
1093 if (matches == 0)
1094 {
1095 free ((PTR)matchlist);
1096 matchlist = 0;
1097 }
1098 else
1099 {
1100 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1101 * sizeof (char *)));
1102 matchlist[matches] = (char *) 0;
1103 }
1104
1105 return matchlist;
1106 }
1107
1108 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1109
1110 /* Return a vector of char pointers which point to the different
1111 possible completions in CMD of TEXT.
1112
1113 WORD points in the same buffer as TEXT, and completions should be
1114 returned relative to this position. For example, suppose TEXT is "foo"
1115 and we want to complete to "foobar". If WORD is "oo", return
1116 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1117
1118 char **
1119 complete_on_enum (enumlist, text, word)
1120 char **enumlist;
1121 char *text;
1122 char *word;
1123 {
1124 char **matchlist;
1125 int sizeof_matchlist;
1126 int matches;
1127 int textlen = strlen (text);
1128 int i;
1129 char *name;
1130
1131 sizeof_matchlist = 10;
1132 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1133 matches = 0;
1134
1135 for (i = 0; (name = enumlist[i]) != NULL; i++)
1136 if (strncmp (name, text, textlen) == 0)
1137 {
1138 if (matches == sizeof_matchlist)
1139 {
1140 sizeof_matchlist *= 2;
1141 matchlist = (char **) xrealloc ((char *)matchlist,
1142 (sizeof_matchlist
1143 * sizeof (char *)));
1144 }
1145
1146 matchlist[matches] = (char *)
1147 xmalloc (strlen (word) + strlen (name) + 1);
1148 if (word == text)
1149 strcpy (matchlist[matches], name);
1150 else if (word > text)
1151 {
1152 /* Return some portion of name. */
1153 strcpy (matchlist[matches], name + (word - text));
1154 }
1155 else
1156 {
1157 /* Return some of text plus name. */
1158 strncpy (matchlist[matches], word, text - word);
1159 matchlist[matches][text - word] = '\0';
1160 strcat (matchlist[matches], name);
1161 }
1162 ++matches;
1163 }
1164
1165 if (matches == 0)
1166 {
1167 free ((PTR)matchlist);
1168 matchlist = 0;
1169 }
1170 else
1171 {
1172 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1173 * sizeof (char *)));
1174 matchlist[matches] = (char *) 0;
1175 }
1176
1177 return matchlist;
1178 }
1179
1180 static int
1181 parse_binary_operation (arg)
1182 char *arg;
1183 {
1184 int length;
1185
1186 if (!arg || !*arg)
1187 return 1;
1188
1189 length = strlen (arg);
1190
1191 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1192 length--;
1193
1194 if (!strncmp (arg, "on", length)
1195 || !strncmp (arg, "1", length)
1196 || !strncmp (arg, "yes", length))
1197 return 1;
1198 else
1199 if (!strncmp (arg, "off", length)
1200 || !strncmp (arg, "0", length)
1201 || !strncmp (arg, "no", length))
1202 return 0;
1203 else
1204 {
1205 error ("\"on\" or \"off\" expected.");
1206 return 0;
1207 }
1208 }
1209
1210 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1211 of the argument, and FROM_TTY is nonzero if this command is being entered
1212 directly by the user (i.e. these are just like any other
1213 command). C is the command list element for the command. */
1214 void
1215 do_setshow_command (arg, from_tty, c)
1216 char *arg;
1217 int from_tty;
1218 struct cmd_list_element *c;
1219 {
1220 if (c->type == set_cmd)
1221 {
1222 switch (c->var_type)
1223 {
1224 case var_string:
1225 {
1226 char *new;
1227 char *p;
1228 char *q;
1229 int ch;
1230
1231 if (arg == NULL)
1232 arg = "";
1233 new = (char *) xmalloc (strlen (arg) + 2);
1234 p = arg; q = new;
1235 while ((ch = *p++) != '\000')
1236 {
1237 if (ch == '\\')
1238 {
1239 /* \ at end of argument is used after spaces
1240 so they won't be lost. */
1241 /* This is obsolete now that we no longer strip
1242 trailing whitespace and actually, the backslash
1243 didn't get here in my test, readline or
1244 something did something funky with a backslash
1245 right before a newline. */
1246 if (*p == 0)
1247 break;
1248 ch = parse_escape (&p);
1249 if (ch == 0)
1250 break; /* C loses */
1251 else if (ch > 0)
1252 *q++ = ch;
1253 }
1254 else
1255 *q++ = ch;
1256 }
1257 #if 0
1258 if (*(p - 1) != '\\')
1259 *q++ = ' ';
1260 #endif
1261 *q++ = '\0';
1262 new = (char *) xrealloc (new, q - new);
1263 if (*(char **)c->var != NULL)
1264 free (*(char **)c->var);
1265 *(char **) c->var = new;
1266 }
1267 break;
1268 case var_string_noescape:
1269 if (arg == NULL)
1270 arg = "";
1271 if (*(char **)c->var != NULL)
1272 free (*(char **)c->var);
1273 *(char **) c->var = savestring (arg, strlen (arg));
1274 break;
1275 case var_filename:
1276 if (arg == NULL)
1277 error_no_arg ("filename to set it to.");
1278 if (*(char **)c->var != NULL)
1279 free (*(char **)c->var);
1280 *(char **)c->var = tilde_expand (arg);
1281 break;
1282 case var_boolean:
1283 *(int *) c->var = parse_binary_operation (arg);
1284 break;
1285 case var_uinteger:
1286 if (arg == NULL)
1287 error_no_arg ("integer to set it to.");
1288 *(unsigned int *) c->var = parse_and_eval_address (arg);
1289 if (*(unsigned int *) c->var == 0)
1290 *(unsigned int *) c->var = UINT_MAX;
1291 break;
1292 case var_integer:
1293 {
1294 unsigned int val;
1295 if (arg == NULL)
1296 error_no_arg ("integer to set it to.");
1297 val = parse_and_eval_address (arg);
1298 if (val == 0)
1299 *(int *) c->var = INT_MAX;
1300 else if (val >= INT_MAX)
1301 error ("integer %u out of range", val);
1302 else
1303 *(int *) c->var = val;
1304 break;
1305 }
1306 case var_zinteger:
1307 if (arg == NULL)
1308 error_no_arg ("integer to set it to.");
1309 *(int *) c->var = parse_and_eval_address (arg);
1310 break;
1311 case var_enum:
1312 {
1313 int i;
1314 int len;
1315 int nmatches;
1316 char *match = NULL;
1317 char *p;
1318
1319 /* if no argument was supplied, print an informative error message */
1320 if (arg == NULL)
1321 {
1322 char msg[1024];
1323 strcpy (msg, "Requires an argument. Valid arguments are ");
1324 for (i = 0; c->enums[i]; i++)
1325 {
1326 if (i != 0)
1327 strcat (msg, ", ");
1328 strcat (msg, c->enums[i]);
1329 }
1330 strcat (msg, ".");
1331 error (msg);
1332 }
1333
1334 p = strchr (arg, ' ');
1335
1336 if (p)
1337 len = p - arg;
1338 else
1339 len = strlen (arg);
1340
1341 nmatches = 0;
1342 for (i = 0; c->enums[i]; i++)
1343 if (strncmp (arg, c->enums[i], len) == 0)
1344 {
1345 match = c->enums[i];
1346 nmatches++;
1347 }
1348
1349 if (nmatches <= 0)
1350 error ("Undefined item: \"%s\".", arg);
1351
1352 if (nmatches > 1)
1353 error ("Ambiguous item \"%s\".", arg);
1354
1355 *(char **)c->var = match;
1356 }
1357 break;
1358 default:
1359 error ("gdb internal error: bad var_type in do_setshow_command");
1360 }
1361 }
1362 else if (c->type == show_cmd)
1363 {
1364 /* Print doc minus "show" at start. */
1365 print_doc_line (gdb_stdout, c->doc + 5);
1366
1367 fputs_filtered (" is ", gdb_stdout);
1368 wrap_here (" ");
1369 switch (c->var_type)
1370 {
1371 case var_string:
1372 {
1373 unsigned char *p;
1374
1375 fputs_filtered ("\"", gdb_stdout);
1376 if (*(unsigned char **)c->var)
1377 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1378 gdb_printchar (*p, gdb_stdout, '"');
1379 fputs_filtered ("\"", gdb_stdout);
1380 }
1381 break;
1382 case var_string_noescape:
1383 case var_filename:
1384 case var_enum:
1385 fputs_filtered ("\"", gdb_stdout);
1386 if (*(char **)c->var)
1387 fputs_filtered (*(char **) c->var, gdb_stdout);
1388 fputs_filtered ("\"", gdb_stdout);
1389 break;
1390 case var_boolean:
1391 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1392 break;
1393 case var_uinteger:
1394 if (*(unsigned int *) c->var == UINT_MAX) {
1395 fputs_filtered ("unlimited", gdb_stdout);
1396 break;
1397 }
1398 /* else fall through */
1399 case var_zinteger:
1400 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1401 break;
1402 case var_integer:
1403 if (*(int *) c->var == INT_MAX)
1404 {
1405 fputs_filtered ("unlimited", gdb_stdout);
1406 }
1407 else
1408 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1409 break;
1410
1411 default:
1412 error ("gdb internal error: bad var_type in do_setshow_command");
1413 }
1414 fputs_filtered (".\n", gdb_stdout);
1415 }
1416 else
1417 error ("gdb internal error: bad cmd_type in do_setshow_command");
1418 (*c->function.sfunc) (NULL, from_tty, c);
1419 }
1420
1421 /* Show all the settings in a list of show commands. */
1422
1423 void
1424 cmd_show_list (list, from_tty, prefix)
1425 struct cmd_list_element *list;
1426 int from_tty;
1427 char *prefix;
1428 {
1429 for (; list != NULL; list = list->next) {
1430 /* If we find a prefix, run its list, prefixing our output by its
1431 prefix (with "show " skipped). */
1432 if (list->prefixlist && !list->abbrev_flag)
1433 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1434 if (list->type == show_cmd)
1435 {
1436 fputs_filtered (prefix, gdb_stdout);
1437 fputs_filtered (list->name, gdb_stdout);
1438 fputs_filtered (": ", gdb_stdout);
1439 do_setshow_command ((char *)NULL, from_tty, list);
1440 }
1441 }
1442 }
1443
1444 /* ARGSUSED */
1445 static void
1446 shell_escape (arg, from_tty)
1447 char *arg;
1448 int from_tty;
1449 {
1450 #ifdef CANT_FORK
1451 /* FIXME: what about errors (I don't know how GO32 system() handles
1452 them)? */
1453 system (arg);
1454 #else /* Can fork. */
1455 int rc, status, pid;
1456 char *p, *user_shell;
1457
1458 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1459 user_shell = "/bin/sh";
1460
1461 /* Get the name of the shell for arg0 */
1462 if ((p = strrchr (user_shell, '/')) == NULL)
1463 p = user_shell;
1464 else
1465 p++; /* Get past '/' */
1466
1467 if ((pid = fork()) == 0)
1468 {
1469 if (!arg)
1470 execl (user_shell, p, 0);
1471 else
1472 execl (user_shell, p, "-c", arg, 0);
1473
1474 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1475 safe_strerror (errno));
1476 gdb_flush (gdb_stderr);
1477 _exit (0177);
1478 }
1479
1480 if (pid != -1)
1481 while ((rc = wait (&status)) != pid && rc != -1)
1482 ;
1483 else
1484 error ("Fork failed");
1485 #endif /* Can fork. */
1486 }
1487
1488 static void
1489 make_command (arg, from_tty)
1490 char *arg;
1491 int from_tty;
1492 {
1493 char *p;
1494
1495 if (arg == 0)
1496 p = "make";
1497 else
1498 {
1499 p = xmalloc (sizeof("make ") + strlen(arg));
1500 strcpy (p, "make ");
1501 strcpy (p + sizeof("make ")-1, arg);
1502 }
1503
1504 shell_escape (p, from_tty);
1505 }
1506
1507 static void
1508 show_user_1 (c, stream)
1509 struct cmd_list_element *c;
1510 GDB_FILE *stream;
1511 {
1512 register struct command_line *cmdlines;
1513
1514 cmdlines = c->user_commands;
1515 if (!cmdlines)
1516 return;
1517 fputs_filtered ("User command ", stream);
1518 fputs_filtered (c->name, stream);
1519 fputs_filtered (":\n", stream);
1520
1521 while (cmdlines)
1522 {
1523 print_command_line (cmdlines, 4, stream);
1524 cmdlines = cmdlines->next;
1525 }
1526 fputs_filtered ("\n", stream);
1527 }
1528
1529 /* ARGSUSED */
1530 static void
1531 show_user (args, from_tty)
1532 char *args;
1533 int from_tty;
1534 {
1535 struct cmd_list_element *c;
1536 extern struct cmd_list_element *cmdlist;
1537
1538 if (args)
1539 {
1540 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1541 if (c->class != class_user)
1542 error ("Not a user command.");
1543 show_user_1 (c, gdb_stdout);
1544 }
1545 else
1546 {
1547 for (c = cmdlist; c; c = c->next)
1548 {
1549 if (c->class == class_user)
1550 show_user_1 (c, gdb_stdout);
1551 }
1552 }
1553 }
1554
1555 void
1556 _initialize_command ()
1557 {
1558 add_com ("shell", class_support, shell_escape,
1559 "Execute the rest of the line as a shell command. \n\
1560 With no arguments, run an inferior shell.");
1561
1562 if (xdb_commands)
1563 add_com_alias("!", "shell", class_support, 0);
1564
1565 add_com ("make", class_support, make_command,
1566 "Run the ``make'' program using the rest of the line as arguments.");
1567 add_cmd ("user", no_class, show_user,
1568 "Show definitions of user defined commands.\n\
1569 Argument is the name of the user defined command.\n\
1570 With no argument, show definitions of all user defined commands.", &showlist);
1571 }