Sat Nov 7 18:30:20 1998 Peter Schauer <peter.schauer@regent.e-technik.tu-muenchen.de>
[deliverable/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 \f
563 /* This routine takes a line of TEXT and a CLIST in which to start the
564 lookup. When it returns it will have incremented the text pointer past
565 the section of text it matched, set *RESULT_LIST to point to the list in
566 which the last word was matched, and will return a pointer to the cmd
567 list element which the text matches. It will return NULL if no match at
568 all was possible. It will return -1 (cast appropriately, ick) if ambigous
569 matches are possible; in this case *RESULT_LIST will be set to point to
570 the list in which there are ambiguous choices (and *TEXT will be set to
571 the ambiguous text string).
572
573 If the located command was an abbreviation, this routine returns the base
574 command of the abbreviation.
575
576 It does no error reporting whatsoever; control will always return
577 to the superior routine.
578
579 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
580 at the prefix_command (ie. the best match) *or* (special case) will be NULL
581 if no prefix command was ever found. For example, in the case of "info a",
582 "info" matches without ambiguity, but "a" could be "args" or "address", so
583 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
584 RESULT_LIST should not be interpeted as a pointer to the beginning of a
585 list; it simply points to a specific command. In the case of an ambiguous
586 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
587 "info t" can be "info types" or "info target"; upon return *TEXT has been
588 advanced past "info ").
589
590 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
591 affect the operation).
592
593 This routine does *not* modify the text pointed to by TEXT.
594
595 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
596 are actually help classes rather than commands (i.e. the function field of
597 the struct cmd_list_element is NULL). */
598
599 struct cmd_list_element *
600 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
601 char **text;
602 struct cmd_list_element *clist, **result_list;
603 int ignore_help_classes;
604 {
605 char *p, *command;
606 int len, tmp, nfound;
607 struct cmd_list_element *found, *c;
608
609 while (**text == ' ' || **text == '\t')
610 (*text)++;
611
612 /* Treating underscores as part of command words is important
613 so that "set args_foo()" doesn't get interpreted as
614 "set args _foo()". */
615 for (p = *text;
616 *p && (isalnum(*p) || *p == '-' || *p == '_');
617 p++)
618 ;
619
620 /* If nothing but whitespace, return 0. */
621 if (p == *text)
622 return 0;
623
624 len = p - *text;
625
626 /* *text and p now bracket the first command word to lookup (and
627 it's length is len). We copy this into a local temporary,
628 converting to lower case as we go. */
629
630 command = (char *) alloca (len + 1);
631 for (tmp = 0; tmp < len; tmp++)
632 {
633 char x = (*text)[tmp];
634 command[tmp] = isupper(x) ? tolower(x) : x;
635 }
636 command[len] = '\0';
637
638 /* Look it up. */
639 found = 0;
640 nfound = 0;
641 for (c = clist; c; c = c->next)
642 if (!strncmp (command, c->name, len)
643 && (!ignore_help_classes || c->function.cfunc))
644 {
645 found = c;
646 nfound++;
647 if (c->name[len] == '\0')
648 {
649 nfound = 1;
650 break;
651 }
652 }
653
654 /* If nothing matches, we have a simple failure. */
655 if (nfound == 0)
656 return 0;
657
658 if (nfound > 1)
659 {
660 if (result_list != NULL)
661 /* Will be modified in calling routine
662 if we know what the prefix command is. */
663 *result_list = 0;
664 return (struct cmd_list_element *) -1; /* Ambiguous. */
665 }
666
667 /* We've matched something on this list. Move text pointer forward. */
668
669 *text = p;
670
671 /* If this was an abbreviation, use the base command instead. */
672
673 if (found->cmd_pointer)
674 found = found->cmd_pointer;
675
676 /* If we found a prefix command, keep looking. */
677
678 if (found->prefixlist)
679 {
680 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
681 ignore_help_classes);
682 if (!c)
683 {
684 /* Didn't find anything; this is as far as we got. */
685 if (result_list != NULL)
686 *result_list = clist;
687 return found;
688 }
689 else if (c == (struct cmd_list_element *) -1)
690 {
691 /* We've gotten this far properly, but the next step
692 is ambiguous. We need to set the result list to the best
693 we've found (if an inferior hasn't already set it). */
694 if (result_list != NULL)
695 if (!*result_list)
696 /* This used to say *result_list = *found->prefixlist
697 If that was correct, need to modify the documentation
698 at the top of this function to clarify what is supposed
699 to be going on. */
700 *result_list = found;
701 return c;
702 }
703 else
704 {
705 /* We matched! */
706 return c;
707 }
708 }
709 else
710 {
711 if (result_list != NULL)
712 *result_list = clist;
713 return found;
714 }
715 }
716
717 /* All this hair to move the space to the front of cmdtype */
718
719 static void
720 undef_cmd_error (cmdtype, q)
721 char *cmdtype, *q;
722 {
723 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
724 cmdtype,
725 q,
726 *cmdtype? " ": "",
727 strlen(cmdtype)-1,
728 cmdtype);
729 }
730
731 /* Look up the contents of *LINE as a command in the command list LIST.
732 LIST is a chain of struct cmd_list_element's.
733 If it is found, return the struct cmd_list_element for that command
734 and update *LINE to point after the command name, at the first argument.
735 If not found, call error if ALLOW_UNKNOWN is zero
736 otherwise (or if error returns) return zero.
737 Call error if specified command is ambiguous,
738 unless ALLOW_UNKNOWN is negative.
739 CMDTYPE precedes the word "command" in the error message.
740
741 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
742 elements which are actually help classes rather than commands (i.e.
743 the function field of the struct cmd_list_element is 0). */
744
745 struct cmd_list_element *
746 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
747 char **line;
748 struct cmd_list_element *list;
749 char *cmdtype;
750 int allow_unknown;
751 int ignore_help_classes;
752 {
753 struct cmd_list_element *last_list = 0;
754 struct cmd_list_element *c =
755 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
756 #if 0
757 /* This is wrong for complete_command. */
758 char *ptr = (*line) + strlen (*line) - 1;
759
760 /* Clear off trailing whitespace. */
761 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
762 ptr--;
763 *(ptr + 1) = '\0';
764 #endif
765
766 if (!c)
767 {
768 if (!allow_unknown)
769 {
770 if (!*line)
771 error ("Lack of needed %scommand", cmdtype);
772 else
773 {
774 char *p = *line, *q;
775
776 while (isalnum(*p) || *p == '-')
777 p++;
778
779 q = (char *) alloca (p - *line + 1);
780 strncpy (q, *line, p - *line);
781 q[p - *line] = '\0';
782 undef_cmd_error (cmdtype, q);
783 }
784 }
785 else
786 return 0;
787 }
788 else if (c == (struct cmd_list_element *) -1)
789 {
790 /* Ambigous. Local values should be off prefixlist or called
791 values. */
792 int local_allow_unknown = (last_list ? last_list->allow_unknown :
793 allow_unknown);
794 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
795 struct cmd_list_element *local_list =
796 (last_list ? *(last_list->prefixlist) : list);
797
798 if (local_allow_unknown < 0)
799 {
800 if (last_list)
801 return last_list; /* Found something. */
802 else
803 return 0; /* Found nothing. */
804 }
805 else
806 {
807 /* Report as error. */
808 int amb_len;
809 char ambbuf[100];
810
811 for (amb_len = 0;
812 ((*line)[amb_len] && (*line)[amb_len] != ' '
813 && (*line)[amb_len] != '\t');
814 amb_len++)
815 ;
816
817 ambbuf[0] = 0;
818 for (c = local_list; c; c = c->next)
819 if (!strncmp (*line, c->name, amb_len))
820 {
821 if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
822 {
823 if (strlen (ambbuf))
824 strcat (ambbuf, ", ");
825 strcat (ambbuf, c->name);
826 }
827 else
828 {
829 strcat (ambbuf, "..");
830 break;
831 }
832 }
833 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
834 *line, ambbuf);
835 return 0; /* lint */
836 }
837 }
838 else
839 {
840 /* We've got something. It may still not be what the caller
841 wants (if this command *needs* a subcommand). */
842 while (**line == ' ' || **line == '\t')
843 (*line)++;
844
845 if (c->prefixlist && **line && !c->allow_unknown)
846 undef_cmd_error (c->prefixname, *line);
847
848 /* Seems to be what he wants. Return it. */
849 return c;
850 }
851 return 0;
852 }
853
854 #if 0
855 /* Look up the contents of *LINE as a command in the command list LIST.
856 LIST is a chain of struct cmd_list_element's.
857 If it is found, return the struct cmd_list_element for that command
858 and update *LINE to point after the command name, at the first argument.
859 If not found, call error if ALLOW_UNKNOWN is zero
860 otherwise (or if error returns) return zero.
861 Call error if specified command is ambiguous,
862 unless ALLOW_UNKNOWN is negative.
863 CMDTYPE precedes the word "command" in the error message. */
864
865 struct cmd_list_element *
866 lookup_cmd (line, list, cmdtype, allow_unknown)
867 char **line;
868 struct cmd_list_element *list;
869 char *cmdtype;
870 int allow_unknown;
871 {
872 register char *p;
873 register struct cmd_list_element *c, *found;
874 int nfound;
875 char ambbuf[100];
876 char *processed_cmd;
877 int i, cmd_len;
878
879 /* Skip leading whitespace. */
880
881 while (**line == ' ' || **line == '\t')
882 (*line)++;
883
884 /* Clear out trailing whitespace. */
885
886 p = *line + strlen (*line);
887 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
888 p--;
889 *p = 0;
890
891 /* Find end of command name. */
892
893 p = *line;
894 while (*p == '-' || isalnum(*p))
895 p++;
896
897 /* Look up the command name.
898 If exact match, keep that.
899 Otherwise, take command abbreviated, if unique. Note that (in my
900 opinion) a null string does *not* indicate ambiguity; simply the
901 end of the argument. */
902
903 if (p == *line)
904 {
905 if (!allow_unknown)
906 error ("Lack of needed %scommand", cmdtype);
907 return 0;
908 }
909
910 /* Copy over to a local buffer, converting to lowercase on the way.
911 This is in case the command being parsed is a subcommand which
912 doesn't match anything, and that's ok. We want the original
913 untouched for the routine of the original command. */
914
915 processed_cmd = (char *) alloca (p - *line + 1);
916 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
917 {
918 char x = (*line)[cmd_len];
919 if (isupper(x))
920 processed_cmd[cmd_len] = tolower(x);
921 else
922 processed_cmd[cmd_len] = x;
923 }
924 processed_cmd[cmd_len] = '\0';
925
926 /* Check all possibilities in the current command list. */
927 found = 0;
928 nfound = 0;
929 for (c = list; c; c = c->next)
930 {
931 if (!strncmp (processed_cmd, c->name, cmd_len))
932 {
933 found = c;
934 nfound++;
935 if (c->name[cmd_len] == 0)
936 {
937 nfound = 1;
938 break;
939 }
940 }
941 }
942
943 /* Report error for undefined command name. */
944
945 if (nfound != 1)
946 {
947 if (nfound > 1 && allow_unknown >= 0)
948 {
949 ambbuf[0] = 0;
950 for (c = list; c; c = c->next)
951 if (!strncmp (processed_cmd, c->name, cmd_len))
952 {
953 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
954 {
955 if (strlen (ambbuf))
956 strcat (ambbuf, ", ");
957 strcat (ambbuf, c->name);
958 }
959 else
960 {
961 strcat (ambbuf, "..");
962 break;
963 }
964 }
965 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
966 processed_cmd, ambbuf);
967 }
968 else if (!allow_unknown)
969 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
970 return 0;
971 }
972
973 /* Skip whitespace before the argument. */
974
975 while (*p == ' ' || *p == '\t') p++;
976 *line = p;
977
978 if (found->prefixlist && *p)
979 {
980 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
981 found->allow_unknown);
982 if (c)
983 return c;
984 }
985
986 return found;
987 }
988 #endif
989
990 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
991
992 /* Return a vector of char pointers which point to the different
993 possible completions in LIST of TEXT.
994
995 WORD points in the same buffer as TEXT, and completions should be
996 returned relative to this position. For example, suppose TEXT is "foo"
997 and we want to complete to "foobar". If WORD is "oo", return
998 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
999
1000 char **
1001 complete_on_cmdlist (list, text, word)
1002 struct cmd_list_element *list;
1003 char *text;
1004 char *word;
1005 {
1006 struct cmd_list_element *ptr;
1007 char **matchlist;
1008 int sizeof_matchlist;
1009 int matches;
1010 int textlen = strlen (text);
1011
1012 sizeof_matchlist = 10;
1013 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1014 matches = 0;
1015
1016 for (ptr = list; ptr; ptr = ptr->next)
1017 if (!strncmp (ptr->name, text, textlen)
1018 && !ptr->abbrev_flag
1019 && (ptr->function.cfunc
1020 || ptr->prefixlist))
1021 {
1022 if (matches == sizeof_matchlist)
1023 {
1024 sizeof_matchlist *= 2;
1025 matchlist = (char **) xrealloc ((char *)matchlist,
1026 (sizeof_matchlist
1027 * sizeof (char *)));
1028 }
1029
1030 matchlist[matches] = (char *)
1031 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1032 if (word == text)
1033 strcpy (matchlist[matches], ptr->name);
1034 else if (word > text)
1035 {
1036 /* Return some portion of ptr->name. */
1037 strcpy (matchlist[matches], ptr->name + (word - text));
1038 }
1039 else
1040 {
1041 /* Return some of text plus ptr->name. */
1042 strncpy (matchlist[matches], word, text - word);
1043 matchlist[matches][text - word] = '\0';
1044 strcat (matchlist[matches], ptr->name);
1045 }
1046 ++matches;
1047 }
1048
1049 if (matches == 0)
1050 {
1051 free ((PTR)matchlist);
1052 matchlist = 0;
1053 }
1054 else
1055 {
1056 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1057 * sizeof (char *)));
1058 matchlist[matches] = (char *) 0;
1059 }
1060
1061 return matchlist;
1062 }
1063
1064 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1065
1066 /* Return a vector of char pointers which point to the different
1067 possible completions in CMD of TEXT.
1068
1069 WORD points in the same buffer as TEXT, and completions should be
1070 returned relative to this position. For example, suppose TEXT is "foo"
1071 and we want to complete to "foobar". If WORD is "oo", return
1072 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1073
1074 char **
1075 complete_on_enum (enumlist, text, word)
1076 char **enumlist;
1077 char *text;
1078 char *word;
1079 {
1080 char **matchlist;
1081 int sizeof_matchlist;
1082 int matches;
1083 int textlen = strlen (text);
1084 int i;
1085 char *name;
1086
1087 sizeof_matchlist = 10;
1088 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1089 matches = 0;
1090
1091 for (i = 0; (name = enumlist[i]) != NULL; i++)
1092 if (strncmp (name, text, textlen) == 0)
1093 {
1094 if (matches == sizeof_matchlist)
1095 {
1096 sizeof_matchlist *= 2;
1097 matchlist = (char **) xrealloc ((char *)matchlist,
1098 (sizeof_matchlist
1099 * sizeof (char *)));
1100 }
1101
1102 matchlist[matches] = (char *)
1103 xmalloc (strlen (word) + strlen (name) + 1);
1104 if (word == text)
1105 strcpy (matchlist[matches], name);
1106 else if (word > text)
1107 {
1108 /* Return some portion of name. */
1109 strcpy (matchlist[matches], name + (word - text));
1110 }
1111 else
1112 {
1113 /* Return some of text plus name. */
1114 strncpy (matchlist[matches], word, text - word);
1115 matchlist[matches][text - word] = '\0';
1116 strcat (matchlist[matches], name);
1117 }
1118 ++matches;
1119 }
1120
1121 if (matches == 0)
1122 {
1123 free ((PTR)matchlist);
1124 matchlist = 0;
1125 }
1126 else
1127 {
1128 matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
1129 * sizeof (char *)));
1130 matchlist[matches] = (char *) 0;
1131 }
1132
1133 return matchlist;
1134 }
1135
1136 static int
1137 parse_binary_operation (arg)
1138 char *arg;
1139 {
1140 int length;
1141
1142 if (!arg || !*arg)
1143 return 1;
1144
1145 length = strlen (arg);
1146
1147 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1148 length--;
1149
1150 if (!strncmp (arg, "on", length)
1151 || !strncmp (arg, "1", length)
1152 || !strncmp (arg, "yes", length))
1153 return 1;
1154 else
1155 if (!strncmp (arg, "off", length)
1156 || !strncmp (arg, "0", length)
1157 || !strncmp (arg, "no", length))
1158 return 0;
1159 else
1160 {
1161 error ("\"on\" or \"off\" expected.");
1162 return 0;
1163 }
1164 }
1165
1166 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1167 of the argument, and FROM_TTY is nonzero if this command is being entered
1168 directly by the user (i.e. these are just like any other
1169 command). C is the command list element for the command. */
1170 void
1171 do_setshow_command (arg, from_tty, c)
1172 char *arg;
1173 int from_tty;
1174 struct cmd_list_element *c;
1175 {
1176 if (c->type == set_cmd)
1177 {
1178 switch (c->var_type)
1179 {
1180 case var_string:
1181 {
1182 char *new;
1183 char *p;
1184 char *q;
1185 int ch;
1186
1187 if (arg == NULL)
1188 arg = "";
1189 new = (char *) xmalloc (strlen (arg) + 2);
1190 p = arg; q = new;
1191 while ((ch = *p++) != '\000')
1192 {
1193 if (ch == '\\')
1194 {
1195 /* \ at end of argument is used after spaces
1196 so they won't be lost. */
1197 /* This is obsolete now that we no longer strip
1198 trailing whitespace and actually, the backslash
1199 didn't get here in my test, readline or
1200 something did something funky with a backslash
1201 right before a newline. */
1202 if (*p == 0)
1203 break;
1204 ch = parse_escape (&p);
1205 if (ch == 0)
1206 break; /* C loses */
1207 else if (ch > 0)
1208 *q++ = ch;
1209 }
1210 else
1211 *q++ = ch;
1212 }
1213 #if 0
1214 if (*(p - 1) != '\\')
1215 *q++ = ' ';
1216 #endif
1217 *q++ = '\0';
1218 new = (char *) xrealloc (new, q - new);
1219 if (*(char **)c->var != NULL)
1220 free (*(char **)c->var);
1221 *(char **) c->var = new;
1222 }
1223 break;
1224 case var_string_noescape:
1225 if (arg == NULL)
1226 arg = "";
1227 if (*(char **)c->var != NULL)
1228 free (*(char **)c->var);
1229 *(char **) c->var = savestring (arg, strlen (arg));
1230 break;
1231 case var_filename:
1232 if (arg == NULL)
1233 error_no_arg ("filename to set it to.");
1234 if (*(char **)c->var != NULL)
1235 free (*(char **)c->var);
1236 *(char **)c->var = tilde_expand (arg);
1237 break;
1238 case var_boolean:
1239 *(int *) c->var = parse_binary_operation (arg);
1240 break;
1241 case var_uinteger:
1242 if (arg == NULL)
1243 error_no_arg ("integer to set it to.");
1244 *(unsigned int *) c->var = parse_and_eval_address (arg);
1245 if (*(unsigned int *) c->var == 0)
1246 *(unsigned int *) c->var = UINT_MAX;
1247 break;
1248 case var_integer:
1249 {
1250 unsigned int val;
1251 if (arg == NULL)
1252 error_no_arg ("integer to set it to.");
1253 val = parse_and_eval_address (arg);
1254 if (val == 0)
1255 *(int *) c->var = INT_MAX;
1256 else if (val >= INT_MAX)
1257 error ("integer %u out of range", val);
1258 else
1259 *(int *) c->var = val;
1260 break;
1261 }
1262 case var_zinteger:
1263 if (arg == NULL)
1264 error_no_arg ("integer to set it to.");
1265 *(int *) c->var = parse_and_eval_address (arg);
1266 break;
1267 case var_enum:
1268 {
1269 int i;
1270 int len;
1271 int nmatches;
1272 char *match = NULL;
1273 char *p;
1274
1275 /* if no argument was supplied, print an informative error message */
1276 if (arg == NULL)
1277 {
1278 char msg[1024];
1279 strcpy (msg, "Requires an argument. Valid arguments are ");
1280 for (i = 0; c->enums[i]; i++)
1281 {
1282 if (i != 0)
1283 strcat (msg, ", ");
1284 strcat (msg, c->enums[i]);
1285 }
1286 strcat (msg, ".");
1287 error (msg);
1288 }
1289
1290 p = strchr (arg, ' ');
1291
1292 if (p)
1293 len = p - arg;
1294 else
1295 len = strlen (arg);
1296
1297 nmatches = 0;
1298 for (i = 0; c->enums[i]; i++)
1299 if (strncmp (arg, c->enums[i], len) == 0)
1300 {
1301 match = c->enums[i];
1302 nmatches++;
1303 }
1304
1305 if (nmatches <= 0)
1306 error ("Undefined item: \"%s\".", arg);
1307
1308 if (nmatches > 1)
1309 error ("Ambiguous item \"%s\".", arg);
1310
1311 *(char **)c->var = match;
1312 }
1313 break;
1314 default:
1315 error ("gdb internal error: bad var_type in do_setshow_command");
1316 }
1317 }
1318 else if (c->type == show_cmd)
1319 {
1320 /* Print doc minus "show" at start. */
1321 print_doc_line (gdb_stdout, c->doc + 5);
1322
1323 fputs_filtered (" is ", gdb_stdout);
1324 wrap_here (" ");
1325 switch (c->var_type)
1326 {
1327 case var_string:
1328 {
1329 unsigned char *p;
1330
1331 fputs_filtered ("\"", gdb_stdout);
1332 if (*(unsigned char **)c->var)
1333 for (p = *(unsigned char **) c->var; *p != '\0'; p++)
1334 gdb_printchar (*p, gdb_stdout, '"');
1335 fputs_filtered ("\"", gdb_stdout);
1336 }
1337 break;
1338 case var_string_noescape:
1339 case var_filename:
1340 case var_enum:
1341 fputs_filtered ("\"", gdb_stdout);
1342 if (*(char **)c->var)
1343 fputs_filtered (*(char **) c->var, gdb_stdout);
1344 fputs_filtered ("\"", gdb_stdout);
1345 break;
1346 case var_boolean:
1347 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1348 break;
1349 case var_uinteger:
1350 if (*(unsigned int *) c->var == UINT_MAX) {
1351 fputs_filtered ("unlimited", gdb_stdout);
1352 break;
1353 }
1354 /* else fall through */
1355 case var_zinteger:
1356 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1357 break;
1358 case var_integer:
1359 if (*(int *) c->var == INT_MAX)
1360 {
1361 fputs_filtered ("unlimited", gdb_stdout);
1362 }
1363 else
1364 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1365 break;
1366
1367 default:
1368 error ("gdb internal error: bad var_type in do_setshow_command");
1369 }
1370 fputs_filtered (".\n", gdb_stdout);
1371 }
1372 else
1373 error ("gdb internal error: bad cmd_type in do_setshow_command");
1374 (*c->function.sfunc) (NULL, from_tty, c);
1375 }
1376
1377 /* Show all the settings in a list of show commands. */
1378
1379 void
1380 cmd_show_list (list, from_tty, prefix)
1381 struct cmd_list_element *list;
1382 int from_tty;
1383 char *prefix;
1384 {
1385 for (; list != NULL; list = list->next) {
1386 /* If we find a prefix, run its list, prefixing our output by its
1387 prefix (with "show " skipped). */
1388 if (list->prefixlist && !list->abbrev_flag)
1389 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1390 if (list->type == show_cmd)
1391 {
1392 fputs_filtered (prefix, gdb_stdout);
1393 fputs_filtered (list->name, gdb_stdout);
1394 fputs_filtered (": ", gdb_stdout);
1395 do_setshow_command ((char *)NULL, from_tty, list);
1396 }
1397 }
1398 }
1399
1400 /* ARGSUSED */
1401 static void
1402 shell_escape (arg, from_tty)
1403 char *arg;
1404 int from_tty;
1405 {
1406 #ifdef CANT_FORK
1407 /* FIXME: what about errors (I don't know how GO32 system() handles
1408 them)? */
1409 system (arg);
1410 #else /* Can fork. */
1411 int rc, status, pid;
1412 char *p, *user_shell;
1413
1414 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1415 user_shell = "/bin/sh";
1416
1417 /* Get the name of the shell for arg0 */
1418 if ((p = strrchr (user_shell, '/')) == NULL)
1419 p = user_shell;
1420 else
1421 p++; /* Get past '/' */
1422
1423 if ((pid = fork()) == 0)
1424 {
1425 if (!arg)
1426 execl (user_shell, p, 0);
1427 else
1428 execl (user_shell, p, "-c", arg, 0);
1429
1430 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1431 safe_strerror (errno));
1432 gdb_flush (gdb_stderr);
1433 _exit (0177);
1434 }
1435
1436 if (pid != -1)
1437 while ((rc = wait (&status)) != pid && rc != -1)
1438 ;
1439 else
1440 error ("Fork failed");
1441 #endif /* Can fork. */
1442 }
1443
1444 static void
1445 make_command (arg, from_tty)
1446 char *arg;
1447 int from_tty;
1448 {
1449 char *p;
1450
1451 if (arg == 0)
1452 p = "make";
1453 else
1454 {
1455 p = xmalloc (sizeof("make ") + strlen(arg));
1456 strcpy (p, "make ");
1457 strcpy (p + sizeof("make ")-1, arg);
1458 }
1459
1460 shell_escape (p, from_tty);
1461 }
1462
1463 static void
1464 show_user_1 (c, stream)
1465 struct cmd_list_element *c;
1466 GDB_FILE *stream;
1467 {
1468 register struct command_line *cmdlines;
1469
1470 cmdlines = c->user_commands;
1471 if (!cmdlines)
1472 return;
1473 fputs_filtered ("User command ", stream);
1474 fputs_filtered (c->name, stream);
1475 fputs_filtered (":\n", stream);
1476
1477 while (cmdlines)
1478 {
1479 print_command_line (cmdlines, 4);
1480 cmdlines = cmdlines->next;
1481 }
1482 fputs_filtered ("\n", stream);
1483 }
1484
1485 /* ARGSUSED */
1486 static void
1487 show_user (args, from_tty)
1488 char *args;
1489 int from_tty;
1490 {
1491 struct cmd_list_element *c;
1492 extern struct cmd_list_element *cmdlist;
1493
1494 if (args)
1495 {
1496 c = lookup_cmd (&args, cmdlist, "", 0, 1);
1497 if (c->class != class_user)
1498 error ("Not a user command.");
1499 show_user_1 (c, gdb_stdout);
1500 }
1501 else
1502 {
1503 for (c = cmdlist; c; c = c->next)
1504 {
1505 if (c->class == class_user)
1506 show_user_1 (c, gdb_stdout);
1507 }
1508 }
1509 }
1510
1511 void
1512 _initialize_command ()
1513 {
1514 add_com ("shell", class_support, shell_escape,
1515 "Execute the rest of the line as a shell command. \n\
1516 With no arguments, run an inferior shell.");
1517 add_com ("make", class_support, make_command,
1518 "Run the ``make'' program using the rest of the line as arguments.");
1519 add_cmd ("user", no_class, show_user,
1520 "Show definitions of user defined commands.\n\
1521 Argument is the name of the user defined command.\n\
1522 With no argument, show definitions of all user defined commands.", &showlist);
1523 }
This page took 0.139257 seconds and 4 git commands to generate.