]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/command.c
Fri Mar 24 12:10:38 2000 glen mccready <gkm@pobox.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, 1998, 2000 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., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #include "defs.h"
20 #include "gdbcmd.h"
21 #include "symtab.h"
22 #include "value.h"
23 #include <ctype.h>
24 #include "gdb_string.h"
25 #ifdef UI_OUT
26 #include "ui-out.h"
27 #endif
28
29 #include "gdb_wait.h"
30 #include "gnu-regex.h"
31 /* FIXME: this should be auto-configured! */
32 #ifdef __MSDOS__
33 # define CANT_FORK
34 #endif
35
36 /* Prototypes for local functions */
37
38 static void undef_cmd_error PARAMS ((char *, char *));
39
40 static void show_user PARAMS ((char *, int));
41
42 static void show_user_1 (struct cmd_list_element *, struct ui_file *);
43
44 static void make_command PARAMS ((char *, int));
45
46 static void shell_escape PARAMS ((char *, int));
47
48 static int parse_binary_operation PARAMS ((char *));
49
50 static void print_doc_line (struct ui_file *, char *);
51
52 static struct cmd_list_element *find_cmd PARAMS ((char *command,
53 int len,
54 struct cmd_list_element * clist,
55 int ignore_help_classes,
56 int *nfound));
57 static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *,
58 struct re_pattern_buffer *, char *);
59
60 void apropos_command (char *, int);
61
62 void _initialize_command PARAMS ((void));
63
64 /* Add element named NAME.
65 CLASS is the top level category into which commands are broken down
66 for "help" purposes.
67 FUN should be the function to execute the command;
68 it will get a character string as argument, with leading
69 and trailing blanks already eliminated.
70
71 DOC is a documentation string for the command.
72 Its first line should be a complete sentence.
73 It should start with ? for a command that is an abbreviation
74 or with * for a command that most users don't need to know about.
75
76 Add this command to command list *LIST.
77
78 Returns a pointer to the added command (not necessarily the head
79 of *LIST). */
80
81 struct cmd_list_element *
82 add_cmd (name, class, fun, doc, list)
83 char *name;
84 enum command_class class;
85 void (*fun) PARAMS ((char *, int));
86 char *doc;
87 struct cmd_list_element **list;
88 {
89 register struct cmd_list_element *c
90 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
91 struct cmd_list_element *p;
92
93 delete_cmd (name, list);
94
95 if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
96 {
97 c->next = *list;
98 *list = c;
99 }
100 else
101 {
102 p = *list;
103 while (p->next && STRCMP (p->next->name, name) <= 0)
104 {
105 p = p->next;
106 }
107 c->next = p->next;
108 p->next = c;
109 }
110
111 c->name = name;
112 c->class = class;
113 c->function.cfunc = fun;
114 c->doc = doc;
115 c->flags = 0;
116 c->replacement = NULL;
117 c->hook = NULL;
118 c->prefixlist = NULL;
119 c->prefixname = NULL;
120 c->allow_unknown = 0;
121 c->abbrev_flag = 0;
122 c->completer = make_symbol_completion_list;
123 c->type = not_set_cmd;
124 c->var = NULL;
125 c->var_type = var_boolean;
126 c->enums = NULL;
127 c->user_commands = NULL;
128 c->hookee = NULL;
129 c->cmd_pointer = NULL;
130
131 return c;
132 }
133
134
135 /* Deprecates a command CMD.
136 REPLACEMENT is the name of the command which should be used in place
137 of this command, or NULL if no such command exists.
138
139 This function does not check to see if command REPLACEMENT exists
140 since gdb may not have gotten around to adding REPLACEMENT when this
141 function is called.
142
143 Returns a pointer to the deprecated command. */
144
145 struct cmd_list_element *
146 deprecate_cmd (cmd, replacement)
147 struct cmd_list_element *cmd;
148 char *replacement;
149 {
150 cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
151
152 if (replacement != NULL)
153 cmd->replacement = replacement;
154 else
155 cmd->replacement = NULL;
156
157 return cmd;
158 }
159
160
161 /* Same as above, except that the abbrev_flag is set. */
162
163 #if 0 /* Currently unused */
164
165 struct cmd_list_element *
166 add_abbrev_cmd (name, class, fun, doc, list)
167 char *name;
168 enum command_class class;
169 void (*fun) PARAMS ((char *, int));
170 char *doc;
171 struct cmd_list_element **list;
172 {
173 register struct cmd_list_element *c
174 = add_cmd (name, class, fun, doc, list);
175
176 c->abbrev_flag = 1;
177 return c;
178 }
179
180 #endif
181
182 struct cmd_list_element *
183 add_alias_cmd (name, oldname, class, abbrev_flag, list)
184 char *name;
185 char *oldname;
186 enum command_class class;
187 int abbrev_flag;
188 struct cmd_list_element **list;
189 {
190 /* Must do this since lookup_cmd tries to side-effect its first arg */
191 char *copied_name;
192 register struct cmd_list_element *old;
193 register struct cmd_list_element *c;
194 copied_name = (char *) alloca (strlen (oldname) + 1);
195 strcpy (copied_name, oldname);
196 old = lookup_cmd (&copied_name, *list, "", 1, 1);
197
198 if (old == 0)
199 {
200 delete_cmd (name, list);
201 return 0;
202 }
203
204 c = add_cmd (name, class, old->function.cfunc, old->doc, list);
205 c->prefixlist = old->prefixlist;
206 c->prefixname = old->prefixname;
207 c->allow_unknown = old->allow_unknown;
208 c->abbrev_flag = abbrev_flag;
209 c->cmd_pointer = old;
210 return c;
211 }
212
213 /* Like add_cmd but adds an element for a command prefix:
214 a name that should be followed by a subcommand to be looked up
215 in another command list. PREFIXLIST should be the address
216 of the variable containing that list. */
217
218 struct cmd_list_element *
219 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
220 allow_unknown, list)
221 char *name;
222 enum command_class class;
223 void (*fun) PARAMS ((char *, int));
224 char *doc;
225 struct cmd_list_element **prefixlist;
226 char *prefixname;
227 int allow_unknown;
228 struct cmd_list_element **list;
229 {
230 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
231 c->prefixlist = prefixlist;
232 c->prefixname = prefixname;
233 c->allow_unknown = allow_unknown;
234 return c;
235 }
236
237 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
238
239 struct cmd_list_element *
240 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
241 allow_unknown, list)
242 char *name;
243 enum command_class class;
244 void (*fun) PARAMS ((char *, int));
245 char *doc;
246 struct cmd_list_element **prefixlist;
247 char *prefixname;
248 int allow_unknown;
249 struct cmd_list_element **list;
250 {
251 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
252 c->prefixlist = prefixlist;
253 c->prefixname = prefixname;
254 c->allow_unknown = allow_unknown;
255 c->abbrev_flag = 1;
256 return c;
257 }
258
259 /* This is an empty "cfunc". */
260 void
261 not_just_help_class_command (args, from_tty)
262 char *args;
263 int from_tty;
264 {
265 }
266
267 /* This is an empty "sfunc". */
268 static void empty_sfunc PARAMS ((char *, int, struct cmd_list_element *));
269
270 static void
271 empty_sfunc (args, from_tty, c)
272 char *args;
273 int from_tty;
274 struct cmd_list_element *c;
275 {
276 }
277
278 /* Add element named NAME to command list LIST (the list for set
279 or some sublist thereof).
280 CLASS is as in add_cmd.
281 VAR_TYPE is the kind of thing we are setting.
282 VAR is address of the variable being controlled by this command.
283 DOC is the documentation string. */
284
285 struct cmd_list_element *
286 add_set_cmd (name, class, var_type, var, doc, list)
287 char *name;
288 enum command_class class;
289 var_types var_type;
290 char *var;
291 char *doc;
292 struct cmd_list_element **list;
293 {
294 struct cmd_list_element *c
295 = add_cmd (name, class, NO_FUNCTION, doc, list);
296
297 c->type = set_cmd;
298 c->var_type = var_type;
299 c->var = var;
300 /* This needs to be something besides NO_FUNCTION so that this isn't
301 treated as a help class. */
302 c->function.sfunc = empty_sfunc;
303 return c;
304 }
305
306 /* Add element named NAME to command list LIST (the list for set
307 or some sublist thereof).
308 CLASS is as in add_cmd.
309 ENUMLIST is a list of strings which may follow NAME.
310 VAR is address of the variable which will contain the matching string
311 (from ENUMLIST).
312 DOC is the documentation string. */
313
314 struct cmd_list_element *
315 add_set_enum_cmd (name, class, enumlist, var, doc, list)
316 char *name;
317 enum command_class class;
318 char *enumlist[];
319 char *var;
320 char *doc;
321 struct cmd_list_element **list;
322 {
323 struct cmd_list_element *c
324 = add_set_cmd (name, class, var_enum, var, doc, list);
325 c->enums = enumlist;
326
327 return c;
328 }
329
330 /* Where SETCMD has already been added, add the corresponding show
331 command to LIST and return a pointer to the added command (not
332 necessarily the head of LIST). */
333 struct cmd_list_element *
334 add_show_from_set (setcmd, list)
335 struct cmd_list_element *setcmd;
336 struct cmd_list_element **list;
337 {
338 struct cmd_list_element *showcmd =
339 (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
340 struct cmd_list_element *p;
341
342 memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
343 delete_cmd (showcmd->name, list);
344 showcmd->type = show_cmd;
345
346 /* Replace "set " at start of docstring with "show ". */
347 if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
348 && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
349 showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
350 else
351 fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
352
353 if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
354 {
355 showcmd->next = *list;
356 *list = showcmd;
357 }
358 else
359 {
360 p = *list;
361 while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
362 {
363 p = p->next;
364 }
365 showcmd->next = p->next;
366 p->next = showcmd;
367 }
368
369 return showcmd;
370 }
371
372 /* Remove the command named NAME from the command list. */
373
374 void
375 delete_cmd (name, list)
376 char *name;
377 struct cmd_list_element **list;
378 {
379 register struct cmd_list_element *c;
380 struct cmd_list_element *p;
381
382 while (*list && STREQ ((*list)->name, name))
383 {
384 if ((*list)->hookee)
385 (*list)->hookee->hook = 0; /* Hook slips out of its mouth */
386 p = (*list)->next;
387 free ((PTR) * list);
388 *list = p;
389 }
390
391 if (*list)
392 for (c = *list; c->next;)
393 {
394 if (STREQ (c->next->name, name))
395 {
396 if (c->next->hookee)
397 c->next->hookee->hook = 0; /* hooked cmd gets away. */
398 p = c->next->next;
399 free ((PTR) c->next);
400 c->next = p;
401 }
402 else
403 c = c->next;
404 }
405 }
406 /* Recursively walk the commandlist structures, and print out the
407 documentation of commands that match our regex in either their
408 name, or their documentation.
409 */
410 static void
411 apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist,
412 struct re_pattern_buffer *regex, char *prefix)
413 {
414 register struct cmd_list_element *c;
415 int returnvalue=1; /*Needed to avoid double printing*/
416 /* Walk through the commands */
417 for (c=commandlist;c;c=c->next)
418 {
419 if (c->name != NULL)
420 {
421 /* Try to match against the name*/
422 returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
423 if (returnvalue >= 0)
424 {
425 /* Stolen from help_cmd_list. We don't directly use
426 * help_cmd_list because it doesn't let us print out
427 * single commands
428 */
429 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
430 print_doc_line (stream, c->doc);
431 fputs_filtered ("\n", stream);
432 returnvalue=0; /*Set this so we don't print it again.*/
433 }
434 }
435 if (c->doc != NULL && returnvalue != 0)
436 {
437 /* Try to match against documentation */
438 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
439 {
440 /* Stolen from help_cmd_list. We don't directly use
441 * help_cmd_list because it doesn't let us print out
442 * single commands
443 */
444 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
445 print_doc_line (stream, c->doc);
446 fputs_filtered ("\n", stream);
447 }
448 }
449 /* Check if this command has subcommands */
450 if (c->prefixlist != NULL)
451 {
452 /* Recursively call ourselves on the subcommand list,
453 passing the right prefix in.
454 */
455 apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname);
456 }
457 }
458 }
459 /* Search through names of commands and documentations for a certain
460 regular expression.
461 */
462 void
463 apropos_command (char *searchstr, int from_tty)
464 {
465 extern struct cmd_list_element *cmdlist; /*This is the main command list*/
466 regex_t pattern;
467 char *pattern_fastmap;
468 char errorbuffer[512];
469 pattern_fastmap=calloc(256,sizeof(char));
470 if (searchstr == NULL)
471 error("REGEXP string is empty");
472
473 if (regcomp(&pattern,searchstr,REG_ICASE) == 0)
474 {
475 pattern.fastmap=pattern_fastmap;
476 re_compile_fastmap(&pattern);
477 apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,"");
478 }
479 else
480 {
481 regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512);
482 error("Error in regular expression:%s",errorbuffer);
483 }
484 free(pattern_fastmap);
485 }
486
487
488 /* This command really has to deal with two things:
489 * 1) I want documentation on *this string* (usually called by
490 * "help commandname").
491 * 2) I want documentation on *this list* (usually called by
492 * giving a command that requires subcommands. Also called by saying
493 * just "help".)
494 *
495 * I am going to split this into two seperate comamnds, help_cmd and
496 * help_list.
497 */
498
499 void
500 help_cmd (command, stream)
501 char *command;
502 struct ui_file *stream;
503 {
504 struct cmd_list_element *c;
505 extern struct cmd_list_element *cmdlist;
506
507 if (!command)
508 {
509 help_list (cmdlist, "", all_classes, stream);
510 return;
511 }
512
513 if (strcmp (command, "all") == 0)
514 {
515 help_all (stream);
516 return;
517 }
518
519 c = lookup_cmd (&command, cmdlist, "", 0, 0);
520
521 if (c == 0)
522 return;
523
524 /* There are three cases here.
525 If c->prefixlist is nonzero, we have a prefix command.
526 Print its documentation, then list its subcommands.
527
528 If c->function is nonzero, we really have a command.
529 Print its documentation and return.
530
531 If c->function is zero, we have a class name.
532 Print its documentation (as if it were a command)
533 and then set class to the number of this class
534 so that the commands in the class will be listed. */
535
536 fputs_filtered (c->doc, stream);
537 fputs_filtered ("\n", stream);
538
539 if (c->prefixlist == 0 && c->function.cfunc != NULL)
540 return;
541 fprintf_filtered (stream, "\n");
542
543 /* If this is a prefix command, print it's subcommands */
544 if (c->prefixlist)
545 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
546
547 /* If this is a class name, print all of the commands in the class */
548 if (c->function.cfunc == NULL)
549 help_list (cmdlist, "", c->class, stream);
550
551 if (c->hook)
552 fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
553 c->hook->name);
554 }
555
556 /*
557 * Get a specific kind of help on a command list.
558 *
559 * LIST is the list.
560 * CMDTYPE is the prefix to use in the title string.
561 * CLASS is the class with which to list the nodes of this list (see
562 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
563 * everything, ALL_CLASSES for just classes, and non-negative for only things
564 * in a specific class.
565 * and STREAM is the output stream on which to print things.
566 * If you call this routine with a class >= 0, it recurses.
567 */
568 void
569 help_list (list, cmdtype, class, stream)
570 struct cmd_list_element *list;
571 char *cmdtype;
572 enum command_class class;
573 struct ui_file *stream;
574 {
575 int len;
576 char *cmdtype1, *cmdtype2;
577
578 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
579 len = strlen (cmdtype);
580 cmdtype1 = (char *) alloca (len + 1);
581 cmdtype1[0] = 0;
582 cmdtype2 = (char *) alloca (len + 4);
583 cmdtype2[0] = 0;
584 if (len)
585 {
586 cmdtype1[0] = ' ';
587 strncpy (cmdtype1 + 1, cmdtype, len - 1);
588 cmdtype1[len] = 0;
589 strncpy (cmdtype2, cmdtype, len - 1);
590 strcpy (cmdtype2 + len - 1, " sub");
591 }
592
593 if (class == all_classes)
594 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
595 else
596 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
597
598 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
599
600 if (class == all_classes)
601 fprintf_filtered (stream, "\n\
602 Type \"help%s\" followed by a class name for a list of commands in that class.",
603 cmdtype1);
604
605 fprintf_filtered (stream, "\n\
606 Type \"help%s\" followed by %scommand name for full documentation.\n\
607 Command name abbreviations are allowed if unambiguous.\n",
608 cmdtype1, cmdtype2);
609 }
610
611 static void
612 help_all (stream)
613 struct ui_file *stream;
614 {
615 struct cmd_list_element *c;
616 extern struct cmd_list_element *cmdlist;
617
618 for (c = cmdlist; c; c = c->next)
619 {
620 if (c->abbrev_flag)
621 continue;
622 /* If this is a prefix command, print it's subcommands */
623 if (c->prefixlist)
624 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
625
626 /* If this is a class name, print all of the commands in the class */
627 else if (c->function.cfunc == NULL)
628 help_cmd_list (cmdlist, c->class, "", 0, stream);
629 }
630 }
631
632 /* Print only the first line of STR on STREAM. */
633 static void
634 print_doc_line (stream, str)
635 struct ui_file *stream;
636 char *str;
637 {
638 static char *line_buffer = 0;
639 static int line_size;
640 register char *p;
641
642 if (!line_buffer)
643 {
644 line_size = 80;
645 line_buffer = (char *) xmalloc (line_size);
646 }
647
648 p = str;
649 while (*p && *p != '\n' && *p != '.' && *p != ',')
650 p++;
651 if (p - str > line_size - 1)
652 {
653 line_size = p - str + 1;
654 free ((PTR) line_buffer);
655 line_buffer = (char *) xmalloc (line_size);
656 }
657 strncpy (line_buffer, str, p - str);
658 line_buffer[p - str] = '\0';
659 if (islower (line_buffer[0]))
660 line_buffer[0] = toupper (line_buffer[0]);
661 #ifdef UI_OUT
662 ui_out_text (uiout, line_buffer);
663 #else
664 fputs_filtered (line_buffer, stream);
665 #endif
666 }
667
668 /*
669 * Implement a help command on command list LIST.
670 * RECURSE should be non-zero if this should be done recursively on
671 * all sublists of LIST.
672 * PREFIX is the prefix to print before each command name.
673 * STREAM is the stream upon which the output should be written.
674 * CLASS should be:
675 * A non-negative class number to list only commands in that
676 * class.
677 * ALL_COMMANDS to list all commands in list.
678 * ALL_CLASSES to list all classes in list.
679 *
680 * Note that RECURSE will be active on *all* sublists, not just the
681 * ones selected by the criteria above (ie. the selection mechanism
682 * is at the low level, not the high-level).
683 */
684 void
685 help_cmd_list (list, class, prefix, recurse, stream)
686 struct cmd_list_element *list;
687 enum command_class class;
688 char *prefix;
689 int recurse;
690 struct ui_file *stream;
691 {
692 register struct cmd_list_element *c;
693
694 for (c = list; c; c = c->next)
695 {
696 if (c->abbrev_flag == 0 &&
697 (class == all_commands
698 || (class == all_classes && c->function.cfunc == NULL)
699 || (class == c->class && c->function.cfunc != NULL)))
700 {
701 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
702 print_doc_line (stream, c->doc);
703 fputs_filtered ("\n", stream);
704 }
705 if (recurse
706 && c->prefixlist != 0
707 && c->abbrev_flag == 0)
708 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
709 }
710 }
711 \f
712
713 /* Search the input clist for 'command'. Return the command if
714 found (or NULL if not), and return the number of commands
715 found in nfound */
716
717 static struct cmd_list_element *
718 find_cmd (command, len, clist, ignore_help_classes, nfound)
719 char *command;
720 int len;
721 struct cmd_list_element *clist;
722 int ignore_help_classes;
723 int *nfound;
724 {
725 struct cmd_list_element *found, *c;
726
727 found = (struct cmd_list_element *) NULL;
728 *nfound = 0;
729 for (c = clist; c; c = c->next)
730 if (!strncmp (command, c->name, len)
731 && (!ignore_help_classes || c->function.cfunc))
732 {
733 found = c;
734 (*nfound)++;
735 if (c->name[len] == '\0')
736 {
737 *nfound = 1;
738 break;
739 }
740 }
741 return found;
742 }
743
744 /* This routine takes a line of TEXT and a CLIST in which to start the
745 lookup. When it returns it will have incremented the text pointer past
746 the section of text it matched, set *RESULT_LIST to point to the list in
747 which the last word was matched, and will return a pointer to the cmd
748 list element which the text matches. It will return NULL if no match at
749 all was possible. It will return -1 (cast appropriately, ick) if ambigous
750 matches are possible; in this case *RESULT_LIST will be set to point to
751 the list in which there are ambiguous choices (and *TEXT will be set to
752 the ambiguous text string).
753
754 If the located command was an abbreviation, this routine returns the base
755 command of the abbreviation.
756
757 It does no error reporting whatsoever; control will always return
758 to the superior routine.
759
760 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
761 at the prefix_command (ie. the best match) *or* (special case) will be NULL
762 if no prefix command was ever found. For example, in the case of "info a",
763 "info" matches without ambiguity, but "a" could be "args" or "address", so
764 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
765 RESULT_LIST should not be interpeted as a pointer to the beginning of a
766 list; it simply points to a specific command. In the case of an ambiguous
767 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
768 "info t" can be "info types" or "info target"; upon return *TEXT has been
769 advanced past "info ").
770
771 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
772 affect the operation).
773
774 This routine does *not* modify the text pointed to by TEXT.
775
776 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
777 are actually help classes rather than commands (i.e. the function field of
778 the struct cmd_list_element is NULL). */
779
780 struct cmd_list_element *
781 lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
782 char **text;
783 struct cmd_list_element *clist, **result_list;
784 int ignore_help_classes;
785 {
786 char *p, *command;
787 int len, tmp, nfound;
788 struct cmd_list_element *found, *c;
789 char *line = *text;
790
791 while (**text == ' ' || **text == '\t')
792 (*text)++;
793
794 /* Treating underscores as part of command words is important
795 so that "set args_foo()" doesn't get interpreted as
796 "set args _foo()". */
797 for (p = *text;
798 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
799 (tui_version &&
800 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
801 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
802 p++)
803 ;
804
805 /* If nothing but whitespace, return 0. */
806 if (p == *text)
807 return 0;
808
809 len = p - *text;
810
811 /* *text and p now bracket the first command word to lookup (and
812 it's length is len). We copy this into a local temporary */
813
814
815 command = (char *) alloca (len + 1);
816 for (tmp = 0; tmp < len; tmp++)
817 {
818 char x = (*text)[tmp];
819 command[tmp] = x;
820 }
821 command[len] = '\0';
822
823 /* Look it up. */
824 found = 0;
825 nfound = 0;
826 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
827
828 /*
829 ** We didn't find the command in the entered case, so lower case it
830 ** and search again.
831 */
832 if (!found || nfound == 0)
833 {
834 for (tmp = 0; tmp < len; tmp++)
835 {
836 char x = command[tmp];
837 command[tmp] = isupper (x) ? tolower (x) : x;
838 }
839 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
840 }
841
842 /* If nothing matches, we have a simple failure. */
843 if (nfound == 0)
844 return 0;
845
846 if (nfound > 1)
847 {
848 if (result_list != NULL)
849 /* Will be modified in calling routine
850 if we know what the prefix command is. */
851 *result_list = 0;
852 return (struct cmd_list_element *) -1; /* Ambiguous. */
853 }
854
855 /* We've matched something on this list. Move text pointer forward. */
856
857 *text = p;
858
859 if (found->cmd_pointer)
860 {
861 /* We drop the alias (abbreviation) in favor of the command it is
862 pointing to. If the alias is deprecated, though, we need to
863 warn the user about it before we drop it. Note that while we
864 are warning about the alias, we may also warn about the command
865 itself and we will adjust the appropriate DEPRECATED_WARN_USER
866 flags */
867
868 if (found->flags & DEPRECATED_WARN_USER)
869 deprecated_cmd_warning (&line);
870 found = found->cmd_pointer;
871 }
872 /* If we found a prefix command, keep looking. */
873
874 if (found->prefixlist)
875 {
876 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
877 ignore_help_classes);
878 if (!c)
879 {
880 /* Didn't find anything; this is as far as we got. */
881 if (result_list != NULL)
882 *result_list = clist;
883 return found;
884 }
885 else if (c == (struct cmd_list_element *) -1)
886 {
887 /* We've gotten this far properly, but the next step
888 is ambiguous. We need to set the result list to the best
889 we've found (if an inferior hasn't already set it). */
890 if (result_list != NULL)
891 if (!*result_list)
892 /* This used to say *result_list = *found->prefixlist
893 If that was correct, need to modify the documentation
894 at the top of this function to clarify what is supposed
895 to be going on. */
896 *result_list = found;
897 return c;
898 }
899 else
900 {
901 /* We matched! */
902 return c;
903 }
904 }
905 else
906 {
907 if (result_list != NULL)
908 *result_list = clist;
909 return found;
910 }
911 }
912
913 /* All this hair to move the space to the front of cmdtype */
914
915 static void
916 undef_cmd_error (cmdtype, q)
917 char *cmdtype, *q;
918 {
919 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
920 cmdtype,
921 q,
922 *cmdtype ? " " : "",
923 strlen (cmdtype) - 1,
924 cmdtype);
925 }
926
927 /* Look up the contents of *LINE as a command in the command list LIST.
928 LIST is a chain of struct cmd_list_element's.
929 If it is found, return the struct cmd_list_element for that command
930 and update *LINE to point after the command name, at the first argument.
931 If not found, call error if ALLOW_UNKNOWN is zero
932 otherwise (or if error returns) return zero.
933 Call error if specified command is ambiguous,
934 unless ALLOW_UNKNOWN is negative.
935 CMDTYPE precedes the word "command" in the error message.
936
937 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
938 elements which are actually help classes rather than commands (i.e.
939 the function field of the struct cmd_list_element is 0). */
940
941 struct cmd_list_element *
942 lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
943 char **line;
944 struct cmd_list_element *list;
945 char *cmdtype;
946 int allow_unknown;
947 int ignore_help_classes;
948 {
949 struct cmd_list_element *last_list = 0;
950 struct cmd_list_element *c =
951 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
952 #if 0
953 /* This is wrong for complete_command. */
954 char *ptr = (*line) + strlen (*line) - 1;
955
956 /* Clear off trailing whitespace. */
957 while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
958 ptr--;
959 *(ptr + 1) = '\0';
960 #endif
961
962 if (!c)
963 {
964 if (!allow_unknown)
965 {
966 if (!*line)
967 error ("Lack of needed %scommand", cmdtype);
968 else
969 {
970 char *p = *line, *q;
971
972 while (isalnum (*p) || *p == '-')
973 p++;
974
975 q = (char *) alloca (p - *line + 1);
976 strncpy (q, *line, p - *line);
977 q[p - *line] = '\0';
978 undef_cmd_error (cmdtype, q);
979 }
980 }
981 else
982 return 0;
983 }
984 else if (c == (struct cmd_list_element *) -1)
985 {
986 /* Ambigous. Local values should be off prefixlist or called
987 values. */
988 int local_allow_unknown = (last_list ? last_list->allow_unknown :
989 allow_unknown);
990 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
991 struct cmd_list_element *local_list =
992 (last_list ? *(last_list->prefixlist) : list);
993
994 if (local_allow_unknown < 0)
995 {
996 if (last_list)
997 return last_list; /* Found something. */
998 else
999 return 0; /* Found nothing. */
1000 }
1001 else
1002 {
1003 /* Report as error. */
1004 int amb_len;
1005 char ambbuf[100];
1006
1007 for (amb_len = 0;
1008 ((*line)[amb_len] && (*line)[amb_len] != ' '
1009 && (*line)[amb_len] != '\t');
1010 amb_len++)
1011 ;
1012
1013 ambbuf[0] = 0;
1014 for (c = local_list; c; c = c->next)
1015 if (!strncmp (*line, c->name, amb_len))
1016 {
1017 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
1018 {
1019 if (strlen (ambbuf))
1020 strcat (ambbuf, ", ");
1021 strcat (ambbuf, c->name);
1022 }
1023 else
1024 {
1025 strcat (ambbuf, "..");
1026 break;
1027 }
1028 }
1029 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1030 *line, ambbuf);
1031 return 0; /* lint */
1032 }
1033 }
1034 else
1035 {
1036 /* We've got something. It may still not be what the caller
1037 wants (if this command *needs* a subcommand). */
1038 while (**line == ' ' || **line == '\t')
1039 (*line)++;
1040
1041 if (c->prefixlist && **line && !c->allow_unknown)
1042 undef_cmd_error (c->prefixname, *line);
1043
1044 /* Seems to be what he wants. Return it. */
1045 return c;
1046 }
1047 return 0;
1048 }
1049
1050 /* We are here presumably because an alias or command in *TEXT is
1051 deprecated and a warning message should be generated. This function
1052 decodes *TEXT and potentially generates a warning message as outlined
1053 below.
1054
1055 Example for 'set endian big' which has a fictitious alias 'seb'.
1056
1057 If alias wasn't used in *TEXT, and the command is deprecated:
1058 "warning: 'set endian big' is deprecated."
1059
1060 If alias was used, and only the alias is deprecated:
1061 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1062
1063 If alias was used and command is deprecated (regardless of whether the
1064 alias itself is deprecated:
1065
1066 "warning: 'set endian big' (seb) is deprecated."
1067
1068 After the message has been sent, clear the appropriate flags in the
1069 command and/or the alias so the user is no longer bothered.
1070
1071 */
1072 void
1073 deprecated_cmd_warning (char **text)
1074 {
1075 struct cmd_list_element *alias = NULL;
1076 struct cmd_list_element *prefix_cmd = NULL;
1077 struct cmd_list_element *cmd = NULL;
1078 struct cmd_list_element *c;
1079 char *type;
1080
1081 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1082 /* return if text doesn't evaluate to a command */
1083 return;
1084
1085 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1086 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1087 /* return if nothing is deprecated */
1088 return;
1089
1090 printf_filtered ("Warning:");
1091
1092 if (alias && !(cmd->flags & CMD_DEPRECATED))
1093 printf_filtered (" '%s', an alias for the", alias->name);
1094
1095 printf_filtered (" command '");
1096
1097 if (prefix_cmd)
1098 printf_filtered ("%s", prefix_cmd->prefixname);
1099
1100 printf_filtered ("%s", cmd->name);
1101
1102 if (alias && (cmd->flags & CMD_DEPRECATED))
1103 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1104 else
1105 printf_filtered ("' is deprecated.\n");
1106
1107
1108 /* if it is only the alias that is deprecated, we want to indicate the
1109 new alias, otherwise we'll indicate the new command */
1110
1111 if (alias && !(cmd->flags & CMD_DEPRECATED))
1112 {
1113 if (alias->replacement)
1114 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1115 else
1116 printf_filtered ("No alternative known.\n\n");
1117 }
1118 else
1119 {
1120 if (cmd->replacement)
1121 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1122 else
1123 printf_filtered ("No alternative known.\n\n");
1124 }
1125
1126 /* We've warned you, now we'll keep quiet */
1127 if (alias)
1128 alias->flags &= ~DEPRECATED_WARN_USER;
1129
1130 cmd->flags &= ~DEPRECATED_WARN_USER;
1131 }
1132
1133
1134
1135 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1136 Return 1 on success, 0 on failure.
1137
1138 If LINE refers to an alias, *alias will point to that alias.
1139
1140 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1141 command) set *prefix_cmd.
1142
1143 Set *cmd to point to the command LINE indicates.
1144
1145 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1146 exist, they are NULL when we return.
1147
1148 */
1149 int
1150 lookup_cmd_composition (char *text,
1151 struct cmd_list_element **alias,
1152 struct cmd_list_element **prefix_cmd,
1153 struct cmd_list_element **cmd)
1154 {
1155 char *p, *command;
1156 int len, tmp, nfound;
1157 struct cmd_list_element *cur_list;
1158 struct cmd_list_element *prev_cmd;
1159 *alias = NULL;
1160 *prefix_cmd = NULL;
1161 *cmd = NULL;
1162
1163 cur_list = cmdlist;
1164
1165 while (1)
1166 {
1167 /* Go through as many command lists as we need to
1168 to find the command TEXT refers to. */
1169
1170 prev_cmd = *cmd;
1171
1172 while (*text == ' ' || *text == '\t')
1173 (text)++;
1174
1175 /* Treating underscores as part of command words is important
1176 so that "set args_foo()" doesn't get interpreted as
1177 "set args _foo()". */
1178 for (p = text;
1179 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1180 (tui_version &&
1181 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1182 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1183 p++)
1184 ;
1185
1186 /* If nothing but whitespace, return. */
1187 if (p == text)
1188 return 0;
1189
1190 len = p - text;
1191
1192 /* text and p now bracket the first command word to lookup (and
1193 it's length is len). We copy this into a local temporary */
1194
1195 command = (char *) alloca (len + 1);
1196 for (tmp = 0; tmp < len; tmp++)
1197 {
1198 char x = text[tmp];
1199 command[tmp] = x;
1200 }
1201 command[len] = '\0';
1202
1203 /* Look it up. */
1204 *cmd = 0;
1205 nfound = 0;
1206 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1207
1208 /* We didn't find the command in the entered case, so lower case it
1209 and search again.
1210 */
1211 if (!*cmd || nfound == 0)
1212 {
1213 for (tmp = 0; tmp < len; tmp++)
1214 {
1215 char x = command[tmp];
1216 command[tmp] = isupper (x) ? tolower (x) : x;
1217 }
1218 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1219 }
1220
1221 if (*cmd == (struct cmd_list_element *) -1)
1222 {
1223 return 0; /* ambiguous */
1224 }
1225
1226 if (*cmd == NULL)
1227 return 0; /* nothing found */
1228 else
1229 {
1230 if ((*cmd)->cmd_pointer)
1231 {
1232 /* cmd was actually an alias, we note that an alias was used
1233 (by assigning *alais) and we set *cmd.
1234 */
1235 *alias = *cmd;
1236 *cmd = (*cmd)->cmd_pointer;
1237 }
1238 *prefix_cmd = prev_cmd;
1239 }
1240 if ((*cmd)->prefixlist)
1241 cur_list = *(*cmd)->prefixlist;
1242 else
1243 return 1;
1244
1245 text = p;
1246 }
1247 }
1248
1249
1250
1251
1252 #if 0
1253 /* Look up the contents of *LINE as a command in the command list LIST.
1254 LIST is a chain of struct cmd_list_element's.
1255 If it is found, return the struct cmd_list_element for that command
1256 and update *LINE to point after the command name, at the first argument.
1257 If not found, call error if ALLOW_UNKNOWN is zero
1258 otherwise (or if error returns) return zero.
1259 Call error if specified command is ambiguous,
1260 unless ALLOW_UNKNOWN is negative.
1261 CMDTYPE precedes the word "command" in the error message. */
1262
1263 struct cmd_list_element *
1264 lookup_cmd (line, list, cmdtype, allow_unknown)
1265 char **line;
1266 struct cmd_list_element *list;
1267 char *cmdtype;
1268 int allow_unknown;
1269 {
1270 register char *p;
1271 register struct cmd_list_element *c, *found;
1272 int nfound;
1273 char ambbuf[100];
1274 char *processed_cmd;
1275 int i, cmd_len;
1276
1277 /* Skip leading whitespace. */
1278
1279 while (**line == ' ' || **line == '\t')
1280 (*line)++;
1281
1282 /* Clear out trailing whitespace. */
1283
1284 p = *line + strlen (*line);
1285 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
1286 p--;
1287 *p = 0;
1288
1289 /* Find end of command name. */
1290
1291 p = *line;
1292 while (*p == '-' || isalnum (*p))
1293 p++;
1294
1295 /* Look up the command name.
1296 If exact match, keep that.
1297 Otherwise, take command abbreviated, if unique. Note that (in my
1298 opinion) a null string does *not* indicate ambiguity; simply the
1299 end of the argument. */
1300
1301 if (p == *line)
1302 {
1303 if (!allow_unknown)
1304 error ("Lack of needed %scommand", cmdtype);
1305 return 0;
1306 }
1307
1308 /* Copy over to a local buffer, converting to lowercase on the way.
1309 This is in case the command being parsed is a subcommand which
1310 doesn't match anything, and that's ok. We want the original
1311 untouched for the routine of the original command. */
1312
1313 processed_cmd = (char *) alloca (p - *line + 1);
1314 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
1315 {
1316 char x = (*line)[cmd_len];
1317 if (isupper (x))
1318 processed_cmd[cmd_len] = tolower (x);
1319 else
1320 processed_cmd[cmd_len] = x;
1321 }
1322 processed_cmd[cmd_len] = '\0';
1323
1324 /* Check all possibilities in the current command list. */
1325 found = 0;
1326 nfound = 0;
1327 for (c = list; c; c = c->next)
1328 {
1329 if (!strncmp (processed_cmd, c->name, cmd_len))
1330 {
1331 found = c;
1332 nfound++;
1333 if (c->name[cmd_len] == 0)
1334 {
1335 nfound = 1;
1336 break;
1337 }
1338 }
1339 }
1340
1341 /* Report error for undefined command name. */
1342
1343 if (nfound != 1)
1344 {
1345 if (nfound > 1 && allow_unknown >= 0)
1346 {
1347 ambbuf[0] = 0;
1348 for (c = list; c; c = c->next)
1349 if (!strncmp (processed_cmd, c->name, cmd_len))
1350 {
1351 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
1352 {
1353 if (strlen (ambbuf))
1354 strcat (ambbuf, ", ");
1355 strcat (ambbuf, c->name);
1356 }
1357 else
1358 {
1359 strcat (ambbuf, "..");
1360 break;
1361 }
1362 }
1363 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
1364 processed_cmd, ambbuf);
1365 }
1366 else if (!allow_unknown)
1367 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
1368 return 0;
1369 }
1370
1371 /* Skip whitespace before the argument. */
1372
1373 while (*p == ' ' || *p == '\t')
1374 p++;
1375 *line = p;
1376
1377 if (found->prefixlist && *p)
1378 {
1379 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
1380 found->allow_unknown);
1381 if (c)
1382 return c;
1383 }
1384
1385 return found;
1386 }
1387 #endif
1388
1389 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1390
1391 /* Return a vector of char pointers which point to the different
1392 possible completions in LIST of TEXT.
1393
1394 WORD points in the same buffer as TEXT, and completions should be
1395 returned relative to this position. For example, suppose TEXT is "foo"
1396 and we want to complete to "foobar". If WORD is "oo", return
1397 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1398
1399 char **
1400 complete_on_cmdlist (list, text, word)
1401 struct cmd_list_element *list;
1402 char *text;
1403 char *word;
1404 {
1405 struct cmd_list_element *ptr;
1406 char **matchlist;
1407 int sizeof_matchlist;
1408 int matches;
1409 int textlen = strlen (text);
1410
1411 sizeof_matchlist = 10;
1412 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1413 matches = 0;
1414
1415 for (ptr = list; ptr; ptr = ptr->next)
1416 if (!strncmp (ptr->name, text, textlen)
1417 && !ptr->abbrev_flag
1418 && (ptr->function.cfunc
1419 || ptr->prefixlist))
1420 {
1421 if (matches == sizeof_matchlist)
1422 {
1423 sizeof_matchlist *= 2;
1424 matchlist = (char **) xrealloc ((char *) matchlist,
1425 (sizeof_matchlist
1426 * sizeof (char *)));
1427 }
1428
1429 matchlist[matches] = (char *)
1430 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1431 if (word == text)
1432 strcpy (matchlist[matches], ptr->name);
1433 else if (word > text)
1434 {
1435 /* Return some portion of ptr->name. */
1436 strcpy (matchlist[matches], ptr->name + (word - text));
1437 }
1438 else
1439 {
1440 /* Return some of text plus ptr->name. */
1441 strncpy (matchlist[matches], word, text - word);
1442 matchlist[matches][text - word] = '\0';
1443 strcat (matchlist[matches], ptr->name);
1444 }
1445 ++matches;
1446 }
1447
1448 if (matches == 0)
1449 {
1450 free ((PTR) matchlist);
1451 matchlist = 0;
1452 }
1453 else
1454 {
1455 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1456 * sizeof (char *)));
1457 matchlist[matches] = (char *) 0;
1458 }
1459
1460 return matchlist;
1461 }
1462
1463 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1464
1465 /* Return a vector of char pointers which point to the different
1466 possible completions in CMD of TEXT.
1467
1468 WORD points in the same buffer as TEXT, and completions should be
1469 returned relative to this position. For example, suppose TEXT is "foo"
1470 and we want to complete to "foobar". If WORD is "oo", return
1471 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1472
1473 char **
1474 complete_on_enum (enumlist, text, word)
1475 char **enumlist;
1476 char *text;
1477 char *word;
1478 {
1479 char **matchlist;
1480 int sizeof_matchlist;
1481 int matches;
1482 int textlen = strlen (text);
1483 int i;
1484 char *name;
1485
1486 sizeof_matchlist = 10;
1487 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1488 matches = 0;
1489
1490 for (i = 0; (name = enumlist[i]) != NULL; i++)
1491 if (strncmp (name, text, textlen) == 0)
1492 {
1493 if (matches == sizeof_matchlist)
1494 {
1495 sizeof_matchlist *= 2;
1496 matchlist = (char **) xrealloc ((char *) matchlist,
1497 (sizeof_matchlist
1498 * sizeof (char *)));
1499 }
1500
1501 matchlist[matches] = (char *)
1502 xmalloc (strlen (word) + strlen (name) + 1);
1503 if (word == text)
1504 strcpy (matchlist[matches], name);
1505 else if (word > text)
1506 {
1507 /* Return some portion of name. */
1508 strcpy (matchlist[matches], name + (word - text));
1509 }
1510 else
1511 {
1512 /* Return some of text plus name. */
1513 strncpy (matchlist[matches], word, text - word);
1514 matchlist[matches][text - word] = '\0';
1515 strcat (matchlist[matches], name);
1516 }
1517 ++matches;
1518 }
1519
1520 if (matches == 0)
1521 {
1522 free ((PTR) matchlist);
1523 matchlist = 0;
1524 }
1525 else
1526 {
1527 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1528 * sizeof (char *)));
1529 matchlist[matches] = (char *) 0;
1530 }
1531
1532 return matchlist;
1533 }
1534
1535 static int
1536 parse_binary_operation (arg)
1537 char *arg;
1538 {
1539 int length;
1540
1541 if (!arg || !*arg)
1542 return 1;
1543
1544 length = strlen (arg);
1545
1546 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
1547 length--;
1548
1549 if (!strncmp (arg, "on", length)
1550 || !strncmp (arg, "1", length)
1551 || !strncmp (arg, "yes", length))
1552 return 1;
1553 else if (!strncmp (arg, "off", length)
1554 || !strncmp (arg, "0", length)
1555 || !strncmp (arg, "no", length))
1556 return 0;
1557 else
1558 {
1559 error ("\"on\" or \"off\" expected.");
1560 return 0;
1561 }
1562 }
1563
1564 /* Do a "set" or "show" command. ARG is NULL if no argument, or the text
1565 of the argument, and FROM_TTY is nonzero if this command is being entered
1566 directly by the user (i.e. these are just like any other
1567 command). C is the command list element for the command. */
1568 void
1569 do_setshow_command (arg, from_tty, c)
1570 char *arg;
1571 int from_tty;
1572 struct cmd_list_element *c;
1573 {
1574 if (c->type == set_cmd)
1575 {
1576 switch (c->var_type)
1577 {
1578 case var_string:
1579 {
1580 char *new;
1581 char *p;
1582 char *q;
1583 int ch;
1584
1585 if (arg == NULL)
1586 arg = "";
1587 new = (char *) xmalloc (strlen (arg) + 2);
1588 p = arg;
1589 q = new;
1590 while ((ch = *p++) != '\000')
1591 {
1592 if (ch == '\\')
1593 {
1594 /* \ at end of argument is used after spaces
1595 so they won't be lost. */
1596 /* This is obsolete now that we no longer strip
1597 trailing whitespace and actually, the backslash
1598 didn't get here in my test, readline or
1599 something did something funky with a backslash
1600 right before a newline. */
1601 if (*p == 0)
1602 break;
1603 ch = parse_escape (&p);
1604 if (ch == 0)
1605 break; /* C loses */
1606 else if (ch > 0)
1607 *q++ = ch;
1608 }
1609 else
1610 *q++ = ch;
1611 }
1612 #if 0
1613 if (*(p - 1) != '\\')
1614 *q++ = ' ';
1615 #endif
1616 *q++ = '\0';
1617 new = (char *) xrealloc (new, q - new);
1618 if (*(char **) c->var != NULL)
1619 free (*(char **) c->var);
1620 *(char **) c->var = new;
1621 }
1622 break;
1623 case var_string_noescape:
1624 if (arg == NULL)
1625 arg = "";
1626 if (*(char **) c->var != NULL)
1627 free (*(char **) c->var);
1628 *(char **) c->var = savestring (arg, strlen (arg));
1629 break;
1630 case var_filename:
1631 if (arg == NULL)
1632 error_no_arg ("filename to set it to.");
1633 if (*(char **) c->var != NULL)
1634 free (*(char **) c->var);
1635 *(char **) c->var = tilde_expand (arg);
1636 break;
1637 case var_boolean:
1638 *(int *) c->var = parse_binary_operation (arg);
1639 break;
1640 case var_uinteger:
1641 if (arg == NULL)
1642 error_no_arg ("integer to set it to.");
1643 *(unsigned int *) c->var = parse_and_eval_address (arg);
1644 if (*(unsigned int *) c->var == 0)
1645 *(unsigned int *) c->var = UINT_MAX;
1646 break;
1647 case var_integer:
1648 {
1649 unsigned int val;
1650 if (arg == NULL)
1651 error_no_arg ("integer to set it to.");
1652 val = parse_and_eval_address (arg);
1653 if (val == 0)
1654 *(int *) c->var = INT_MAX;
1655 else if (val >= INT_MAX)
1656 error ("integer %u out of range", val);
1657 else
1658 *(int *) c->var = val;
1659 break;
1660 }
1661 case var_zinteger:
1662 if (arg == NULL)
1663 error_no_arg ("integer to set it to.");
1664 *(int *) c->var = parse_and_eval_address (arg);
1665 break;
1666 case var_enum:
1667 {
1668 int i;
1669 int len;
1670 int nmatches;
1671 char *match = NULL;
1672 char *p;
1673
1674 /* if no argument was supplied, print an informative error message */
1675 if (arg == NULL)
1676 {
1677 char msg[1024];
1678 strcpy (msg, "Requires an argument. Valid arguments are ");
1679 for (i = 0; c->enums[i]; i++)
1680 {
1681 if (i != 0)
1682 strcat (msg, ", ");
1683 strcat (msg, c->enums[i]);
1684 }
1685 strcat (msg, ".");
1686 error (msg);
1687 }
1688
1689 p = strchr (arg, ' ');
1690
1691 if (p)
1692 len = p - arg;
1693 else
1694 len = strlen (arg);
1695
1696 nmatches = 0;
1697 for (i = 0; c->enums[i]; i++)
1698 if (strncmp (arg, c->enums[i], len) == 0)
1699 {
1700 match = c->enums[i];
1701 nmatches++;
1702 }
1703
1704 if (nmatches <= 0)
1705 error ("Undefined item: \"%s\".", arg);
1706
1707 if (nmatches > 1)
1708 error ("Ambiguous item \"%s\".", arg);
1709
1710 *(char **) c->var = match;
1711 }
1712 break;
1713 default:
1714 error ("gdb internal error: bad var_type in do_setshow_command");
1715 }
1716 }
1717 else if (c->type == show_cmd)
1718 {
1719 #ifdef UI_OUT
1720 struct cleanup *old_chain;
1721 struct ui_stream *stb;
1722 int quote;
1723
1724 stb = ui_out_stream_new (uiout);
1725 old_chain = make_cleanup ((make_cleanup_func) ui_out_stream_delete, stb);
1726 #endif /* UI_OUT */
1727
1728 /* Print doc minus "show" at start. */
1729 print_doc_line (gdb_stdout, c->doc + 5);
1730
1731 #ifdef UI_OUT
1732 ui_out_text (uiout, " is ");
1733 ui_out_wrap_hint (uiout, " ");
1734 quote = 0;
1735 switch (c->var_type)
1736 {
1737 case var_string:
1738 {
1739 unsigned char *p;
1740
1741 if (*(unsigned char **) c->var)
1742 fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
1743 quote = 1;
1744 }
1745 break;
1746 case var_string_noescape:
1747 case var_filename:
1748 case var_enum:
1749 if (*(char **) c->var)
1750 fputs_filtered (*(char **) c->var, stb->stream);
1751 quote = 1;
1752 break;
1753 case var_boolean:
1754 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
1755 break;
1756 case var_uinteger:
1757 if (*(unsigned int *) c->var == UINT_MAX)
1758 {
1759 fputs_filtered ("unlimited", stb->stream);
1760 break;
1761 }
1762 /* else fall through */
1763 case var_zinteger:
1764 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
1765 break;
1766 case var_integer:
1767 if (*(int *) c->var == INT_MAX)
1768 {
1769 fputs_filtered ("unlimited", stb->stream);
1770 }
1771 else
1772 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
1773 break;
1774
1775 default:
1776 error ("gdb internal error: bad var_type in do_setshow_command");
1777 }
1778 if (quote)
1779 ui_out_text (uiout, "\"");
1780 ui_out_field_stream (uiout, "value", stb);
1781 if (quote)
1782 ui_out_text (uiout, "\"");
1783 ui_out_text (uiout, ".\n");
1784 do_cleanups (old_chain);
1785 #else
1786 fputs_filtered (" is ", gdb_stdout);
1787 wrap_here (" ");
1788 switch (c->var_type)
1789 {
1790 case var_string:
1791 {
1792 fputs_filtered ("\"", gdb_stdout);
1793 if (*(unsigned char **) c->var)
1794 fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
1795 fputs_filtered ("\"", gdb_stdout);
1796 }
1797 break;
1798 case var_string_noescape:
1799 case var_filename:
1800 case var_enum:
1801 fputs_filtered ("\"", gdb_stdout);
1802 if (*(char **) c->var)
1803 fputs_filtered (*(char **) c->var, gdb_stdout);
1804 fputs_filtered ("\"", gdb_stdout);
1805 break;
1806 case var_boolean:
1807 fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
1808 break;
1809 case var_uinteger:
1810 if (*(unsigned int *) c->var == UINT_MAX)
1811 {
1812 fputs_filtered ("unlimited", gdb_stdout);
1813 break;
1814 }
1815 /* else fall through */
1816 case var_zinteger:
1817 fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
1818 break;
1819 case var_integer:
1820 if (*(int *) c->var == INT_MAX)
1821 {
1822 fputs_filtered ("unlimited", gdb_stdout);
1823 }
1824 else
1825 fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
1826 break;
1827
1828 default:
1829 error ("gdb internal error: bad var_type in do_setshow_command");
1830 }
1831 fputs_filtered (".\n", gdb_stdout);
1832 #endif
1833 }
1834 else
1835 error ("gdb internal error: bad cmd_type in do_setshow_command");
1836 (*c->function.sfunc) (NULL, from_tty, c);
1837 if (c->type == set_cmd && set_hook)
1838 set_hook (c);
1839 }
1840
1841 /* Show all the settings in a list of show commands. */
1842
1843 void
1844 cmd_show_list (list, from_tty, prefix)
1845 struct cmd_list_element *list;
1846 int from_tty;
1847 char *prefix;
1848 {
1849 #ifdef UI_OUT
1850 ui_out_list_begin (uiout, "showlist");
1851 #endif
1852 for (; list != NULL; list = list->next)
1853 {
1854 /* If we find a prefix, run its list, prefixing our output by its
1855 prefix (with "show " skipped). */
1856 #ifdef UI_OUT
1857 if (list->prefixlist && !list->abbrev_flag)
1858 {
1859 ui_out_list_begin (uiout, "optionlist");
1860 ui_out_field_string (uiout, "prefix", list->prefixname + 5);
1861 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1862 ui_out_list_end (uiout);
1863 }
1864 if (list->type == show_cmd)
1865 {
1866 ui_out_list_begin (uiout, "option");
1867 ui_out_text (uiout, prefix);
1868 ui_out_field_string (uiout, "name", list->name);
1869 ui_out_text (uiout, ": ");
1870 do_setshow_command ((char *) NULL, from_tty, list);
1871 ui_out_list_end (uiout);
1872 }
1873 #else
1874 if (list->prefixlist && !list->abbrev_flag)
1875 cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
1876 if (list->type == show_cmd)
1877 {
1878 fputs_filtered (prefix, gdb_stdout);
1879 fputs_filtered (list->name, gdb_stdout);
1880 fputs_filtered (": ", gdb_stdout);
1881 do_setshow_command ((char *) NULL, from_tty, list);
1882 }
1883 #endif
1884 }
1885 #ifdef UI_OUT
1886 ui_out_list_end (uiout);
1887 #endif
1888 }
1889
1890 /* ARGSUSED */
1891 static void
1892 shell_escape (arg, from_tty)
1893 char *arg;
1894 int from_tty;
1895 {
1896 #ifdef CANT_FORK
1897 /* If ARG is NULL, they want an inferior shell, but `system' just
1898 reports if the shell is available when passed a NULL arg. */
1899 int rc = system (arg ? arg : "");
1900
1901 if (!arg)
1902 arg = "inferior shell";
1903
1904 if (rc == -1)
1905 {
1906 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
1907 safe_strerror (errno));
1908 gdb_flush (gdb_stderr);
1909 }
1910 else if (rc)
1911 {
1912 fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
1913 gdb_flush (gdb_stderr);
1914 }
1915 #ifdef __DJGPP__
1916 /* Make sure to return to the directory GDB thinks it is, in case the
1917 shell command we just ran changed it. */
1918 chdir (current_directory);
1919 #endif
1920 #else /* Can fork. */
1921 int rc, status, pid;
1922 char *p, *user_shell;
1923
1924 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
1925 user_shell = "/bin/sh";
1926
1927 /* Get the name of the shell for arg0 */
1928 if ((p = strrchr (user_shell, '/')) == NULL)
1929 p = user_shell;
1930 else
1931 p++; /* Get past '/' */
1932
1933 if ((pid = fork ()) == 0)
1934 {
1935 if (!arg)
1936 execl (user_shell, p, 0);
1937 else
1938 execl (user_shell, p, "-c", arg, 0);
1939
1940 fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
1941 safe_strerror (errno));
1942 gdb_flush (gdb_stderr);
1943 _exit (0177);
1944 }
1945
1946 if (pid != -1)
1947 while ((rc = wait (&status)) != pid && rc != -1)
1948 ;
1949 else
1950 error ("Fork failed");
1951 #endif /* Can fork. */
1952 }
1953
1954 static void
1955 make_command (arg, from_tty)
1956 char *arg;
1957 int from_tty;
1958 {
1959 char *p;
1960
1961 if (arg == 0)
1962 p = "make";
1963 else
1964 {
1965 p = xmalloc (sizeof ("make ") + strlen (arg));
1966 strcpy (p, "make ");
1967 strcpy (p + sizeof ("make ") - 1, arg);
1968 }
1969
1970 shell_escape (p, from_tty);
1971 }
1972
1973 static void
1974 show_user_1 (c, stream)
1975 struct cmd_list_element *c;
1976 struct ui_file *stream;
1977 {
1978 register struct command_line *cmdlines;
1979
1980 cmdlines = c->user_commands;
1981 if (!cmdlines)
1982 return;
1983 fputs_filtered ("User command ", stream);
1984 fputs_filtered (c->name, stream);
1985 fputs_filtered (":\n", stream);
1986
1987 #ifdef UI_OUT
1988 print_command_lines (uiout, cmdlines, 1);
1989 fputs_filtered ("\n", stream);
1990 #else
1991 while (cmdlines)
1992 {
1993 print_command_line (cmdlines, 4, stream);
1994 cmdlines = cmdlines->next;
1995 }
1996 fputs_filtered ("\n", stream);
1997 #endif
1998 }
1999
2000 /* ARGSUSED */
2001 static void
2002 show_user (args, from_tty)
2003 char *args;
2004 int from_tty;
2005 {
2006 struct cmd_list_element *c;
2007 extern struct cmd_list_element *cmdlist;
2008
2009 if (args)
2010 {
2011 c = lookup_cmd (&args, cmdlist, "", 0, 1);
2012 if (c->class != class_user)
2013 error ("Not a user command.");
2014 show_user_1 (c, gdb_stdout);
2015 }
2016 else
2017 {
2018 for (c = cmdlist; c; c = c->next)
2019 {
2020 if (c->class == class_user)
2021 show_user_1 (c, gdb_stdout);
2022 }
2023 }
2024 }
2025
2026 void
2027 _initialize_command ()
2028 {
2029 add_com ("shell", class_support, shell_escape,
2030 "Execute the rest of the line as a shell command. \n\
2031 With no arguments, run an inferior shell.");
2032
2033 /* NOTE: cagney/2000-03-20: Being able to enter ``(gdb) !ls'' would
2034 be a really useful feature. Unfortunatly, the below wont do
2035 this. Instead it adds support for the form ``(gdb) ! ls''
2036 (i.e. the space is required). If the ``!'' command below is
2037 added the complains about no ``!'' command would be replaced by
2038 complains about how the ``!'' command is broken :-) */
2039 if (xdb_commands)
2040 add_com_alias ("!", "shell", class_support, 0);
2041
2042 add_com ("make", class_support, make_command,
2043 "Run the ``make'' program using the rest of the line as arguments.");
2044 add_cmd ("user", no_class, show_user,
2045 "Show definitions of user defined commands.\n\
2046 Argument is the name of the user defined command.\n\
2047 With no argument, show definitions of all user defined commands.", &showlist);
2048 add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");
2049 }