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