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