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