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