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