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