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