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