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