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