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