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