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