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