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