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