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