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