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