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