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