1 /* Handle lists of commands, their decoding and documentation, for GDB.
3 Copyright (c) 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002, 2004, 2007,
4 2008, 2009 Free Software Foundation, Inc.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_regex.h"
23 #include "gdb_string.h"
24 #include "completer.h"
27 #include "cli/cli-cmds.h"
28 #include "cli/cli-decode.h"
31 #include "tui/tui.h" /* For tui_active et.al. */
34 #include "gdb_assert.h"
36 /* Prototypes for local functions */
38 static void undef_cmd_error (char *, char *);
40 static struct cmd_list_element
*delete_cmd (char *name
,
41 struct cmd_list_element
**list
,
42 struct cmd_list_element
**prehook
,
43 struct cmd_list_element
**prehookee
,
44 struct cmd_list_element
**posthook
,
45 struct cmd_list_element
**posthookee
);
47 static struct cmd_list_element
*find_cmd (char *command
,
49 struct cmd_list_element
*clist
,
50 int ignore_help_classes
,
53 static void help_all (struct ui_file
*stream
);
56 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
57 struct ui_file
*stream
);
60 /* Set the callback function for the specified command. For each both
61 the commands callback and func() are set. The latter set to a
62 bounce function (unless cfunc / sfunc is NULL that is). */
65 do_cfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
67 c
->function
.cfunc (args
, from_tty
); /* Ok. */
71 set_cmd_cfunc (struct cmd_list_element
*cmd
, cmd_cfunc_ftype
*cfunc
)
77 cmd
->function
.cfunc
= cfunc
; /* Ok. */
81 do_sfunc (struct cmd_list_element
*c
, char *args
, int from_tty
)
83 c
->function
.sfunc (args
, from_tty
, c
); /* Ok. */
87 set_cmd_sfunc (struct cmd_list_element
*cmd
, cmd_sfunc_ftype
*sfunc
)
93 cmd
->function
.sfunc
= sfunc
; /* Ok. */
97 cmd_cfunc_eq (struct cmd_list_element
*cmd
,
98 void (*cfunc
) (char *args
, int from_tty
))
100 return cmd
->func
== do_cfunc
&& cmd
->function
.cfunc
== cfunc
;
104 set_cmd_context (struct cmd_list_element
*cmd
, void *context
)
106 cmd
->context
= context
;
110 get_cmd_context (struct cmd_list_element
*cmd
)
116 cmd_type (struct cmd_list_element
*cmd
)
122 set_cmd_completer (struct cmd_list_element
*cmd
,
123 char **(*completer
) (struct cmd_list_element
*self
,
124 char *text
, char *word
))
126 cmd
->completer
= completer
; /* Ok. */
130 /* Add element named NAME.
131 CLASS is the top level category into which commands are broken down
133 FUN should be the function to execute the command;
134 it will get a character string as argument, with leading
135 and trailing blanks already eliminated.
137 DOC is a documentation string for the command.
138 Its first line should be a complete sentence.
139 It should start with ? for a command that is an abbreviation
140 or with * for a command that most users don't need to know about.
142 Add this command to command list *LIST.
144 Returns a pointer to the added command (not necessarily the head
147 struct cmd_list_element
*
148 add_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
149 char *doc
, struct cmd_list_element
**list
)
151 struct cmd_list_element
*c
152 = (struct cmd_list_element
*) xmalloc (sizeof (struct cmd_list_element
));
153 struct cmd_list_element
*p
, *iter
;
155 /* Turn each alias of the old command into an alias of the new
157 c
->aliases
= delete_cmd (name
, list
, &c
->hook_pre
, &c
->hookee_pre
,
158 &c
->hook_post
, &c
->hookee_post
);
159 for (iter
= c
->aliases
; iter
; iter
= iter
->alias_chain
)
160 iter
->cmd_pointer
= c
;
162 c
->hook_pre
->hookee_pre
= c
;
164 c
->hookee_pre
->hook_pre
= c
;
166 c
->hook_post
->hookee_post
= c
;
168 c
->hookee_post
->hook_post
= c
;
170 if (*list
== NULL
|| strcmp ((*list
)->name
, name
) >= 0)
178 while (p
->next
&& strcmp (p
->next
->name
, name
) <= 0)
188 set_cmd_cfunc (c
, fun
);
189 set_cmd_context (c
, NULL
);
192 c
->replacement
= NULL
;
193 c
->pre_show_hook
= NULL
;
195 c
->prefixlist
= NULL
;
196 c
->prefixname
= NULL
;
197 c
->allow_unknown
= 0;
199 set_cmd_completer (c
, make_symbol_completion_list_fn
);
201 c
->type
= not_set_cmd
;
203 c
->var_type
= var_boolean
;
205 c
->user_commands
= NULL
;
206 c
->cmd_pointer
= NULL
;
207 c
->alias_chain
= NULL
;
212 /* Deprecates a command CMD.
213 REPLACEMENT is the name of the command which should be used in place
214 of this command, or NULL if no such command exists.
216 This function does not check to see if command REPLACEMENT exists
217 since gdb may not have gotten around to adding REPLACEMENT when this
220 Returns a pointer to the deprecated command. */
222 struct cmd_list_element
*
223 deprecate_cmd (struct cmd_list_element
*cmd
, char *replacement
)
225 cmd
->flags
|= (CMD_DEPRECATED
| DEPRECATED_WARN_USER
);
227 if (replacement
!= NULL
)
228 cmd
->replacement
= replacement
;
230 cmd
->replacement
= NULL
;
235 struct cmd_list_element
*
236 add_alias_cmd (char *name
, char *oldname
, enum command_class
class,
237 int abbrev_flag
, struct cmd_list_element
**list
)
239 /* Must do this since lookup_cmd tries to side-effect its first arg */
241 struct cmd_list_element
*old
;
242 struct cmd_list_element
*c
;
243 copied_name
= (char *) alloca (strlen (oldname
) + 1);
244 strcpy (copied_name
, oldname
);
245 old
= lookup_cmd (&copied_name
, *list
, "", 1, 1);
249 struct cmd_list_element
*prehook
, *prehookee
, *posthook
, *posthookee
;
250 struct cmd_list_element
*aliases
= delete_cmd (name
, list
,
251 &prehook
, &prehookee
,
252 &posthook
, &posthookee
);
253 /* If this happens, it means a programmer error somewhere. */
254 gdb_assert (!aliases
&& !prehook
&& !prehookee
255 && !posthook
&& ! posthookee
);
259 c
= add_cmd (name
, class, NULL
, old
->doc
, list
);
260 /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
262 c
->function
= old
->function
;
263 c
->prefixlist
= old
->prefixlist
;
264 c
->prefixname
= old
->prefixname
;
265 c
->allow_unknown
= old
->allow_unknown
;
266 c
->abbrev_flag
= abbrev_flag
;
267 c
->cmd_pointer
= old
;
268 c
->alias_chain
= old
->aliases
;
273 /* Like add_cmd but adds an element for a command prefix:
274 a name that should be followed by a subcommand to be looked up
275 in another command list. PREFIXLIST should be the address
276 of the variable containing that list. */
278 struct cmd_list_element
*
279 add_prefix_cmd (char *name
, enum command_class
class, void (*fun
) (char *, int),
280 char *doc
, struct cmd_list_element
**prefixlist
,
281 char *prefixname
, int allow_unknown
,
282 struct cmd_list_element
**list
)
284 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
285 c
->prefixlist
= prefixlist
;
286 c
->prefixname
= prefixname
;
287 c
->allow_unknown
= allow_unknown
;
291 /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
293 struct cmd_list_element
*
294 add_abbrev_prefix_cmd (char *name
, enum command_class
class,
295 void (*fun
) (char *, int), char *doc
,
296 struct cmd_list_element
**prefixlist
, char *prefixname
,
297 int allow_unknown
, struct cmd_list_element
**list
)
299 struct cmd_list_element
*c
= add_cmd (name
, class, fun
, doc
, list
);
300 c
->prefixlist
= prefixlist
;
301 c
->prefixname
= prefixname
;
302 c
->allow_unknown
= allow_unknown
;
307 /* This is an empty "cfunc". */
309 not_just_help_class_command (char *args
, int from_tty
)
313 /* This is an empty "sfunc". */
314 static void empty_sfunc (char *, int, struct cmd_list_element
*);
317 empty_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
321 /* Add element named NAME to command list LIST (the list for set/show
322 or some sublist thereof).
323 TYPE is set_cmd or show_cmd.
324 CLASS is as in add_cmd.
325 VAR_TYPE is the kind of thing we are setting.
326 VAR is address of the variable being controlled by this command.
327 DOC is the documentation string. */
329 static struct cmd_list_element
*
330 add_set_or_show_cmd (char *name
,
332 enum command_class
class,
336 struct cmd_list_element
**list
)
338 struct cmd_list_element
*c
= add_cmd (name
, class, NULL
, doc
, list
);
339 gdb_assert (type
== set_cmd
|| type
== show_cmd
);
341 c
->var_type
= var_type
;
343 /* This needs to be something besides NULL so that this isn't
344 treated as a help class. */
345 set_cmd_sfunc (c
, empty_sfunc
);
349 /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
350 CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
351 setting. VAR is address of the variable being controlled by this
352 command. SET_FUNC and SHOW_FUNC are the callback functions (if
353 non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
354 strings. PRINT the format string to print the value. SET_RESULT
355 and SHOW_RESULT, if not NULL, are set to the resulting command
359 add_setshow_cmd_full (char *name
,
360 enum command_class
class,
361 var_types var_type
, void *var
,
362 const char *set_doc
, const char *show_doc
,
363 const char *help_doc
,
364 cmd_sfunc_ftype
*set_func
,
365 show_value_ftype
*show_func
,
366 struct cmd_list_element
**set_list
,
367 struct cmd_list_element
**show_list
,
368 struct cmd_list_element
**set_result
,
369 struct cmd_list_element
**show_result
)
371 struct cmd_list_element
*set
;
372 struct cmd_list_element
*show
;
376 if (help_doc
!= NULL
)
378 full_set_doc
= xstrprintf ("%s\n%s", set_doc
, help_doc
);
379 full_show_doc
= xstrprintf ("%s\n%s", show_doc
, help_doc
);
383 full_set_doc
= xstrdup (set_doc
);
384 full_show_doc
= xstrdup (show_doc
);
386 set
= add_set_or_show_cmd (name
, set_cmd
, class, var_type
, var
,
387 full_set_doc
, set_list
);
388 if (set_func
!= NULL
)
389 set_cmd_sfunc (set
, set_func
);
390 show
= add_set_or_show_cmd (name
, show_cmd
, class, var_type
, var
,
391 full_show_doc
, show_list
);
392 show
->show_value_func
= show_func
;
394 if (set_result
!= NULL
)
396 if (show_result
!= NULL
)
400 /* Add element named NAME to command list LIST (the list for set or
401 some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
402 of strings which may follow NAME. VAR is address of the variable
403 which will contain the matching string (from ENUMLIST). */
406 add_setshow_enum_cmd (char *name
,
407 enum command_class
class,
408 const char *enumlist
[],
411 const char *show_doc
,
412 const char *help_doc
,
413 cmd_sfunc_ftype
*set_func
,
414 show_value_ftype
*show_func
,
415 struct cmd_list_element
**set_list
,
416 struct cmd_list_element
**show_list
)
418 struct cmd_list_element
*c
;
419 add_setshow_cmd_full (name
, class, var_enum
, var
,
420 set_doc
, show_doc
, help_doc
,
427 /* Add an auto-boolean command named NAME to both the set and show
428 command list lists. CLASS is as in add_cmd. VAR is address of the
429 variable which will contain the value. DOC is the documentation
430 string. FUNC is the corresponding callback. */
432 add_setshow_auto_boolean_cmd (char *name
,
433 enum command_class
class,
434 enum auto_boolean
*var
,
435 const char *set_doc
, const char *show_doc
,
436 const char *help_doc
,
437 cmd_sfunc_ftype
*set_func
,
438 show_value_ftype
*show_func
,
439 struct cmd_list_element
**set_list
,
440 struct cmd_list_element
**show_list
)
442 static const char *auto_boolean_enums
[] = { "on", "off", "auto", NULL
};
443 struct cmd_list_element
*c
;
444 add_setshow_cmd_full (name
, class, var_auto_boolean
, var
,
445 set_doc
, show_doc
, help_doc
,
449 c
->enums
= auto_boolean_enums
;
452 /* Add element named NAME to both the set and show command LISTs (the
453 list for set/show or some sublist thereof). CLASS is as in
454 add_cmd. VAR is address of the variable which will contain the
455 value. SET_DOC and SHOW_DOC are the documentation strings. */
457 add_setshow_boolean_cmd (char *name
, enum command_class
class, int *var
,
458 const char *set_doc
, const char *show_doc
,
459 const char *help_doc
,
460 cmd_sfunc_ftype
*set_func
,
461 show_value_ftype
*show_func
,
462 struct cmd_list_element
**set_list
,
463 struct cmd_list_element
**show_list
)
465 static const char *boolean_enums
[] = { "on", "off", NULL
};
466 struct cmd_list_element
*c
;
467 add_setshow_cmd_full (name
, class, var_boolean
, var
,
468 set_doc
, show_doc
, help_doc
,
472 c
->enums
= boolean_enums
;
475 /* Add element named NAME to both the set and show command LISTs (the
476 list for set/show or some sublist thereof). */
478 add_setshow_filename_cmd (char *name
, enum command_class
class,
480 const char *set_doc
, const char *show_doc
,
481 const char *help_doc
,
482 cmd_sfunc_ftype
*set_func
,
483 show_value_ftype
*show_func
,
484 struct cmd_list_element
**set_list
,
485 struct cmd_list_element
**show_list
)
487 struct cmd_list_element
*set_result
;
488 add_setshow_cmd_full (name
, class, var_filename
, var
,
489 set_doc
, show_doc
, help_doc
,
493 set_cmd_completer (set_result
, filename_completer
);
496 /* Add element named NAME to both the set and show command LISTs (the
497 list for set/show or some sublist thereof). Return the new "set"
499 struct cmd_list_element
*
500 add_setshow_string_cmd (char *name
, enum command_class
class,
502 const char *set_doc
, const char *show_doc
,
503 const char *help_doc
,
504 cmd_sfunc_ftype
*set_func
,
505 show_value_ftype
*show_func
,
506 struct cmd_list_element
**set_list
,
507 struct cmd_list_element
**show_list
)
509 struct cmd_list_element
*cmd
;
510 add_setshow_cmd_full (name
, class, var_string
, var
,
511 set_doc
, show_doc
, help_doc
,
518 /* Add element named NAME to both the set and show command LISTs (the
519 list for set/show or some sublist thereof). Return the new "set"
521 struct cmd_list_element
*
522 add_setshow_string_noescape_cmd (char *name
, enum command_class
class,
524 const char *set_doc
, const char *show_doc
,
525 const char *help_doc
,
526 cmd_sfunc_ftype
*set_func
,
527 show_value_ftype
*show_func
,
528 struct cmd_list_element
**set_list
,
529 struct cmd_list_element
**show_list
)
531 struct cmd_list_element
*cmd
;
532 add_setshow_cmd_full (name
, class, var_string_noescape
, var
,
533 set_doc
, show_doc
, help_doc
,
540 /* Add element named NAME to both the set and show command LISTs (the
541 list for set/show or some sublist thereof). */
543 add_setshow_optional_filename_cmd (char *name
, enum command_class
class,
545 const char *set_doc
, const char *show_doc
,
546 const char *help_doc
,
547 cmd_sfunc_ftype
*set_func
,
548 show_value_ftype
*show_func
,
549 struct cmd_list_element
**set_list
,
550 struct cmd_list_element
**show_list
)
552 struct cmd_list_element
*set_result
;
554 add_setshow_cmd_full (name
, class, var_optional_filename
, var
,
555 set_doc
, show_doc
, help_doc
,
560 set_cmd_completer (set_result
, filename_completer
);
564 /* Add element named NAME to both the set and show command LISTs (the
565 list for set/show or some sublist thereof). CLASS is as in
566 add_cmd. VAR is address of the variable which will contain the
567 value. SET_DOC and SHOW_DOC are the documentation strings. Return
568 the new "set" command. */
569 struct cmd_list_element
*
570 add_setshow_integer_cmd (char *name
, enum command_class
class,
572 const char *set_doc
, const char *show_doc
,
573 const char *help_doc
,
574 cmd_sfunc_ftype
*set_func
,
575 show_value_ftype
*show_func
,
576 struct cmd_list_element
**set_list
,
577 struct cmd_list_element
**show_list
)
579 struct cmd_list_element
*cmd
;
580 add_setshow_cmd_full (name
, class, var_integer
, var
,
581 set_doc
, show_doc
, help_doc
,
588 /* Add element named NAME to both the set and show command LISTs (the
589 list for set/show or some sublist thereof). CLASS is as in
590 add_cmd. VAR is address of the variable which will contain the
591 value. SET_DOC and SHOW_DOC are the documentation strings. Return
592 the new "set" command. */
593 struct cmd_list_element
*
594 add_setshow_uinteger_cmd (char *name
, enum command_class
class,
596 const char *set_doc
, const char *show_doc
,
597 const char *help_doc
,
598 cmd_sfunc_ftype
*set_func
,
599 show_value_ftype
*show_func
,
600 struct cmd_list_element
**set_list
,
601 struct cmd_list_element
**show_list
)
603 struct cmd_list_element
*cmd
;
604 add_setshow_cmd_full (name
, class, var_uinteger
, var
,
605 set_doc
, show_doc
, help_doc
,
612 /* Add element named NAME to both the set and show command LISTs (the
613 list for set/show or some sublist thereof). CLASS is as in
614 add_cmd. VAR is address of the variable which will contain the
615 value. SET_DOC and SHOW_DOC are the documentation strings. Return
616 the new "set" command. */
617 struct cmd_list_element
*
618 add_setshow_zinteger_cmd (char *name
, enum command_class
class,
620 const char *set_doc
, const char *show_doc
,
621 const char *help_doc
,
622 cmd_sfunc_ftype
*set_func
,
623 show_value_ftype
*show_func
,
624 struct cmd_list_element
**set_list
,
625 struct cmd_list_element
**show_list
)
627 struct cmd_list_element
*cmd
;
628 add_setshow_cmd_full (name
, class, var_zinteger
, var
,
629 set_doc
, show_doc
, help_doc
,
636 /* Add element named NAME to both the set and show command LISTs (the
637 list for set/show or some sublist thereof). CLASS is as in
638 add_cmd. VAR is address of the variable which will contain the
639 value. SET_DOC and SHOW_DOC are the documentation strings. Return
640 the new "set" command. */
641 struct cmd_list_element
*
642 add_setshow_zuinteger_cmd (char *name
, enum command_class
class,
644 const char *set_doc
, const char *show_doc
,
645 const char *help_doc
,
646 cmd_sfunc_ftype
*set_func
,
647 show_value_ftype
*show_func
,
648 struct cmd_list_element
**set_list
,
649 struct cmd_list_element
**show_list
)
651 struct cmd_list_element
*cmd
;
652 add_setshow_cmd_full (name
, class, var_zuinteger
, var
,
653 set_doc
, show_doc
, help_doc
,
660 /* Remove the command named NAME from the command list. Return the
661 list commands which were aliased to the deleted command. If the
662 command had no aliases, return NULL. The various *HOOKs are set to
663 the pre- and post-hook commands for the deleted command. If the
664 command does not have a hook, the corresponding out parameter is
667 static struct cmd_list_element
*
668 delete_cmd (char *name
, struct cmd_list_element
**list
,
669 struct cmd_list_element
**prehook
,
670 struct cmd_list_element
**prehookee
,
671 struct cmd_list_element
**posthook
,
672 struct cmd_list_element
**posthookee
)
674 struct cmd_list_element
*iter
;
675 struct cmd_list_element
**previous_chain_ptr
;
676 struct cmd_list_element
*aliases
= NULL
;
682 previous_chain_ptr
= list
;
684 for (iter
= *previous_chain_ptr
; iter
; iter
= *previous_chain_ptr
)
686 if (strcmp (iter
->name
, name
) == 0)
689 iter
->destroyer (iter
, iter
->context
);
690 if (iter
->hookee_pre
)
691 iter
->hookee_pre
->hook_pre
= 0;
692 *prehook
= iter
->hook_pre
;
693 *prehookee
= iter
->hookee_pre
;
694 if (iter
->hookee_post
)
695 iter
->hookee_post
->hook_post
= 0;
696 *posthook
= iter
->hook_post
;
697 *posthookee
= iter
->hookee_post
;
699 /* Update the link. */
700 *previous_chain_ptr
= iter
->next
;
702 aliases
= iter
->aliases
;
704 /* If this command was an alias, remove it from the list of
706 if (iter
->cmd_pointer
)
708 struct cmd_list_element
**prevp
= &iter
->cmd_pointer
->aliases
;
709 struct cmd_list_element
*a
= *prevp
;
713 prevp
= &a
->alias_chain
;
716 *prevp
= iter
->alias_chain
;
721 /* We won't see another command with the same name. */
725 previous_chain_ptr
= &iter
->next
;
731 /* Shorthands to the commands above. */
733 /* Add an element to the list of info subcommands. */
735 struct cmd_list_element
*
736 add_info (char *name
, void (*fun
) (char *, int), char *doc
)
738 return add_cmd (name
, no_class
, fun
, doc
, &infolist
);
741 /* Add an alias to the list of info subcommands. */
743 struct cmd_list_element
*
744 add_info_alias (char *name
, char *oldname
, int abbrev_flag
)
746 return add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
749 /* Add an element to the list of commands. */
751 struct cmd_list_element
*
752 add_com (char *name
, enum command_class
class, void (*fun
) (char *, int),
755 return add_cmd (name
, class, fun
, doc
, &cmdlist
);
758 /* Add an alias or abbreviation command to the list of commands. */
760 struct cmd_list_element
*
761 add_com_alias (char *name
, char *oldname
, enum command_class
class,
764 return add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
767 /* Recursively walk the commandlist structures, and print out the
768 documentation of commands that match our regex in either their
769 name, or their documentation.
772 apropos_cmd (struct ui_file
*stream
, struct cmd_list_element
*commandlist
,
773 struct re_pattern_buffer
*regex
, char *prefix
)
775 struct cmd_list_element
*c
;
777 /* Walk through the commands */
778 for (c
=commandlist
;c
;c
=c
->next
)
780 returnvalue
= -1; /*Needed to avoid double printing*/
783 /* Try to match against the name*/
784 returnvalue
=re_search(regex
,c
->name
,strlen(c
->name
),0,strlen(c
->name
),NULL
);
785 if (returnvalue
>= 0)
787 print_help_for_command (c
, prefix
,
788 0 /* don't recurse */, stream
);
791 if (c
->doc
!= NULL
&& returnvalue
< 0)
793 /* Try to match against documentation */
794 if (re_search(regex
,c
->doc
,strlen(c
->doc
),0,strlen(c
->doc
),NULL
) >=0)
796 print_help_for_command (c
, prefix
,
797 0 /* don't recurse */, stream
);
800 /* Check if this command has subcommands and is not an abbreviation.
801 We skip listing subcommands of abbreviations in order to avoid
802 duplicates in the output.
804 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
806 /* Recursively call ourselves on the subcommand list,
807 passing the right prefix in.
809 apropos_cmd (stream
,*c
->prefixlist
,regex
,c
->prefixname
);
814 /* This command really has to deal with two things:
815 * 1) I want documentation on *this string* (usually called by
816 * "help commandname").
817 * 2) I want documentation on *this list* (usually called by
818 * giving a command that requires subcommands. Also called by saying
821 * I am going to split this into two seperate comamnds, help_cmd and
826 help_cmd (char *command
, struct ui_file
*stream
)
828 struct cmd_list_element
*c
;
829 extern struct cmd_list_element
*cmdlist
;
833 help_list (cmdlist
, "", all_classes
, stream
);
837 if (strcmp (command
, "all") == 0)
843 c
= lookup_cmd (&command
, cmdlist
, "", 0, 0);
848 /* There are three cases here.
849 If c->prefixlist is nonzero, we have a prefix command.
850 Print its documentation, then list its subcommands.
852 If c->func is non NULL, we really have a command. Print its
853 documentation and return.
855 If c->func is NULL, we have a class name. Print its
856 documentation (as if it were a command) and then set class to the
857 number of this class so that the commands in the class will be
860 fputs_filtered (c
->doc
, stream
);
861 fputs_filtered ("\n", stream
);
863 if (c
->prefixlist
== 0 && c
->func
!= NULL
)
865 fprintf_filtered (stream
, "\n");
867 /* If this is a prefix command, print it's subcommands */
869 help_list (*c
->prefixlist
, c
->prefixname
, all_commands
, stream
);
871 /* If this is a class name, print all of the commands in the class */
873 help_list (cmdlist
, "", c
->class, stream
);
875 if (c
->hook_pre
|| c
->hook_post
)
876 fprintf_filtered (stream
,
877 "\nThis command has a hook (or hooks) defined:\n");
880 fprintf_filtered (stream
,
881 "\tThis command is run after : %s (pre hook)\n",
884 fprintf_filtered (stream
,
885 "\tThis command is run before : %s (post hook)\n",
890 * Get a specific kind of help on a command list.
893 * CMDTYPE is the prefix to use in the title string.
894 * CLASS is the class with which to list the nodes of this list (see
895 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
896 * everything, ALL_CLASSES for just classes, and non-negative for only things
897 * in a specific class.
898 * and STREAM is the output stream on which to print things.
899 * If you call this routine with a class >= 0, it recurses.
902 help_list (struct cmd_list_element
*list
, char *cmdtype
,
903 enum command_class
class, struct ui_file
*stream
)
906 char *cmdtype1
, *cmdtype2
;
908 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
909 len
= strlen (cmdtype
);
910 cmdtype1
= (char *) alloca (len
+ 1);
912 cmdtype2
= (char *) alloca (len
+ 4);
917 strncpy (cmdtype1
+ 1, cmdtype
, len
- 1);
919 strncpy (cmdtype2
, cmdtype
, len
- 1);
920 strcpy (cmdtype2
+ len
- 1, " sub");
923 if (class == all_classes
)
924 fprintf_filtered (stream
, "List of classes of %scommands:\n\n", cmdtype2
);
926 fprintf_filtered (stream
, "List of %scommands:\n\n", cmdtype2
);
928 help_cmd_list (list
, class, cmdtype
, (int) class >= 0, stream
);
930 if (class == all_classes
)
932 fprintf_filtered (stream
, "\n\
933 Type \"help%s\" followed by a class name for a list of commands in ",
936 fprintf_filtered (stream
, "that class.");
938 fprintf_filtered (stream
, "\n\
939 Type \"help all\" for the list of all commands.");
942 fprintf_filtered (stream
, "\nType \"help%s\" followed by %scommand name ",
945 fputs_filtered ("for ", stream
);
947 fputs_filtered ("full ", stream
);
949 fputs_filtered ("documentation.\n", stream
);
950 fputs_filtered ("Type \"apropos word\" to search "
951 "for commands related to \"word\".\n", stream
);
952 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
957 help_all (struct ui_file
*stream
)
959 struct cmd_list_element
*c
;
960 extern struct cmd_list_element
*cmdlist
;
961 int seen_unclassified
= 0;
963 for (c
= cmdlist
; c
; c
= c
->next
)
967 /* If this is a class name, print all of the commands in the class */
971 fprintf_filtered (stream
, "\nCommand class: %s\n\n", c
->name
);
972 help_cmd_list (cmdlist
, c
->class, "", 1, stream
);
976 /* While it's expected that all commands are in some class,
977 as a safety measure, we'll print commands outside of any
980 for (c
= cmdlist
; c
; c
= c
->next
)
985 if (c
->class == no_class
)
987 if (!seen_unclassified
)
989 fprintf_filtered (stream
, "\nUnclassified commands\n\n");
990 seen_unclassified
= 1;
992 print_help_for_command (c
, "", 1, stream
);
998 /* Print only the first line of STR on STREAM. */
1000 print_doc_line (struct ui_file
*stream
, char *str
)
1002 static char *line_buffer
= 0;
1003 static int line_size
;
1009 line_buffer
= (char *) xmalloc (line_size
);
1013 while (*p
&& *p
!= '\n' && *p
!= '.' && *p
!= ',')
1015 if (p
- str
> line_size
- 1)
1017 line_size
= p
- str
+ 1;
1018 xfree (line_buffer
);
1019 line_buffer
= (char *) xmalloc (line_size
);
1021 strncpy (line_buffer
, str
, p
- str
);
1022 line_buffer
[p
- str
] = '\0';
1023 if (islower (line_buffer
[0]))
1024 line_buffer
[0] = toupper (line_buffer
[0]);
1025 ui_out_text (uiout
, line_buffer
);
1028 /* Print one-line help for command C.
1029 If RECURSE is non-zero, also print one-line descriptions
1030 of all prefixed subcommands. */
1032 print_help_for_command (struct cmd_list_element
*c
, char *prefix
, int recurse
,
1033 struct ui_file
*stream
)
1035 fprintf_filtered (stream
, "%s%s -- ", prefix
, c
->name
);
1036 print_doc_line (stream
, c
->doc
);
1037 fputs_filtered ("\n", stream
);
1040 && c
->prefixlist
!= 0
1041 && c
->abbrev_flag
== 0)
1042 /* Subcommands of a prefix command typically have 'all_commands'
1043 as class. If we pass CLASS to recursive invocation,
1044 most often we won't see anything. */
1045 help_cmd_list (*c
->prefixlist
, all_commands
, c
->prefixname
, 1, stream
);
1049 * Implement a help command on command list LIST.
1050 * RECURSE should be non-zero if this should be done recursively on
1051 * all sublists of LIST.
1052 * PREFIX is the prefix to print before each command name.
1053 * STREAM is the stream upon which the output should be written.
1055 * A non-negative class number to list only commands in that
1057 * ALL_COMMANDS to list all commands in list.
1058 * ALL_CLASSES to list all classes in list.
1060 * Note that RECURSE will be active on *all* sublists, not just the
1061 * ones selected by the criteria above (ie. the selection mechanism
1062 * is at the low level, not the high-level).
1065 help_cmd_list (struct cmd_list_element
*list
, enum command_class
class,
1066 char *prefix
, int recurse
, struct ui_file
*stream
)
1068 struct cmd_list_element
*c
;
1070 for (c
= list
; c
; c
= c
->next
)
1072 if (c
->abbrev_flag
== 0 &&
1073 (class == all_commands
1074 || (class == all_classes
&& c
->func
== NULL
)
1075 || (class == c
->class && c
->func
!= NULL
)))
1077 print_help_for_command (c
, prefix
, recurse
, stream
);
1079 else if (c
->abbrev_flag
== 0 && recurse
1080 && class == class_user
&& c
->prefixlist
!= NULL
)
1081 /* User-defined commands may be subcommands. */
1082 help_cmd_list (*c
->prefixlist
, class, c
->prefixname
, recurse
, stream
);
1087 /* Search the input clist for 'command'. Return the command if
1088 found (or NULL if not), and return the number of commands
1091 static struct cmd_list_element
*
1092 find_cmd (char *command
, int len
, struct cmd_list_element
*clist
,
1093 int ignore_help_classes
, int *nfound
)
1095 struct cmd_list_element
*found
, *c
;
1097 found
= (struct cmd_list_element
*) NULL
;
1099 for (c
= clist
; c
; c
= c
->next
)
1100 if (!strncmp (command
, c
->name
, len
)
1101 && (!ignore_help_classes
|| c
->func
))
1105 if (c
->name
[len
] == '\0')
1115 find_command_name_length (const char *text
)
1117 const char *p
= text
;
1119 /* Treating underscores as part of command words is important
1120 so that "set args_foo()" doesn't get interpreted as
1121 "set args _foo()". */
1122 /* Some characters are only used for TUI specific commands. However, they
1123 are always allowed for the sake of consistency.
1124 The XDB compatibility characters are only allowed when using the right
1125 mode because they clash with other GDB commands - specifically '/' is
1126 used as a suffix for print, examine and display.
1127 Note that this is larger than the character set allowed when creating
1128 user-defined commands. */
1129 while (isalnum (*p
) || *p
== '-' || *p
== '_' ||
1130 /* Characters used by TUI specific commands. */
1131 *p
== '+' || *p
== '<' || *p
== '>' || *p
== '$' ||
1132 /* Characters used for XDB compatibility. */
1133 (xdb_commands
&& (*p
== '!' || *p
== '/' || *p
== '?')))
1139 /* This routine takes a line of TEXT and a CLIST in which to start the
1140 lookup. When it returns it will have incremented the text pointer past
1141 the section of text it matched, set *RESULT_LIST to point to the list in
1142 which the last word was matched, and will return a pointer to the cmd
1143 list element which the text matches. It will return NULL if no match at
1144 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1145 matches are possible; in this case *RESULT_LIST will be set to point to
1146 the list in which there are ambiguous choices (and *TEXT will be set to
1147 the ambiguous text string).
1149 If the located command was an abbreviation, this routine returns the base
1150 command of the abbreviation.
1152 It does no error reporting whatsoever; control will always return
1153 to the superior routine.
1155 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1156 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1157 if no prefix command was ever found. For example, in the case of "info a",
1158 "info" matches without ambiguity, but "a" could be "args" or "address", so
1159 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1160 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1161 list; it simply points to a specific command. In the case of an ambiguous
1162 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1163 "info t" can be "info types" or "info target"; upon return *TEXT has been
1164 advanced past "info ").
1166 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1167 affect the operation).
1169 This routine does *not* modify the text pointed to by TEXT.
1171 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1172 are actually help classes rather than commands (i.e. the function field of
1173 the struct cmd_list_element is NULL). */
1175 struct cmd_list_element
*
1176 lookup_cmd_1 (char **text
, struct cmd_list_element
*clist
,
1177 struct cmd_list_element
**result_list
, int ignore_help_classes
)
1180 int len
, tmp
, nfound
;
1181 struct cmd_list_element
*found
, *c
;
1184 while (**text
== ' ' || **text
== '\t')
1187 /* Identify the name of the command. */
1188 len
= find_command_name_length (*text
);
1190 /* If nothing but whitespace, return 0. */
1194 /* *text and p now bracket the first command word to lookup (and
1195 it's length is len). We copy this into a local temporary */
1198 command
= (char *) alloca (len
+ 1);
1199 memcpy (command
, *text
, len
);
1200 command
[len
] = '\0';
1205 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1208 ** We didn't find the command in the entered case, so lower case it
1209 ** and search again.
1211 if (!found
|| nfound
== 0)
1213 for (tmp
= 0; tmp
< len
; tmp
++)
1215 char x
= command
[tmp
];
1216 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1218 found
= find_cmd (command
, len
, clist
, ignore_help_classes
, &nfound
);
1221 /* If nothing matches, we have a simple failure. */
1227 if (result_list
!= NULL
)
1228 /* Will be modified in calling routine
1229 if we know what the prefix command is. */
1231 return (struct cmd_list_element
*) -1; /* Ambiguous. */
1234 /* We've matched something on this list. Move text pointer forward. */
1238 if (found
->cmd_pointer
)
1240 /* We drop the alias (abbreviation) in favor of the command it is
1241 pointing to. If the alias is deprecated, though, we need to
1242 warn the user about it before we drop it. Note that while we
1243 are warning about the alias, we may also warn about the command
1244 itself and we will adjust the appropriate DEPRECATED_WARN_USER
1247 if (found
->flags
& DEPRECATED_WARN_USER
)
1248 deprecated_cmd_warning (&line
);
1249 found
= found
->cmd_pointer
;
1251 /* If we found a prefix command, keep looking. */
1253 if (found
->prefixlist
)
1255 c
= lookup_cmd_1 (text
, *found
->prefixlist
, result_list
,
1256 ignore_help_classes
);
1259 /* Didn't find anything; this is as far as we got. */
1260 if (result_list
!= NULL
)
1261 *result_list
= clist
;
1264 else if (c
== (struct cmd_list_element
*) -1)
1266 /* We've gotten this far properly, but the next step
1267 is ambiguous. We need to set the result list to the best
1268 we've found (if an inferior hasn't already set it). */
1269 if (result_list
!= NULL
)
1271 /* This used to say *result_list = *found->prefixlist
1272 If that was correct, need to modify the documentation
1273 at the top of this function to clarify what is supposed
1275 *result_list
= found
;
1286 if (result_list
!= NULL
)
1287 *result_list
= clist
;
1292 /* All this hair to move the space to the front of cmdtype */
1295 undef_cmd_error (char *cmdtype
, char *q
)
1297 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1300 *cmdtype
? " " : "",
1301 (int) strlen (cmdtype
) - 1,
1305 /* Look up the contents of *LINE as a command in the command list LIST.
1306 LIST is a chain of struct cmd_list_element's.
1307 If it is found, return the struct cmd_list_element for that command
1308 and update *LINE to point after the command name, at the first argument.
1309 If not found, call error if ALLOW_UNKNOWN is zero
1310 otherwise (or if error returns) return zero.
1311 Call error if specified command is ambiguous,
1312 unless ALLOW_UNKNOWN is negative.
1313 CMDTYPE precedes the word "command" in the error message.
1315 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1316 elements which are actually help classes rather than commands (i.e.
1317 the function field of the struct cmd_list_element is 0). */
1319 struct cmd_list_element
*
1320 lookup_cmd (char **line
, struct cmd_list_element
*list
, char *cmdtype
,
1321 int allow_unknown
, int ignore_help_classes
)
1323 struct cmd_list_element
*last_list
= 0;
1324 struct cmd_list_element
*c
;
1326 /* Note: Do not remove trailing whitespace here because this
1327 would be wrong for complete_command. Jim Kingdon */
1330 error (_("Lack of needed %scommand"), cmdtype
);
1332 c
= lookup_cmd_1 (line
, list
, &last_list
, ignore_help_classes
);
1339 int len
= find_command_name_length (*line
);
1341 q
= (char *) alloca (len
+ 1);
1342 strncpy (q
, *line
, len
);
1344 undef_cmd_error (cmdtype
, q
);
1349 else if (c
== (struct cmd_list_element
*) -1)
1351 /* Ambigous. Local values should be off prefixlist or called
1353 int local_allow_unknown
= (last_list
? last_list
->allow_unknown
:
1355 char *local_cmdtype
= last_list
? last_list
->prefixname
: cmdtype
;
1356 struct cmd_list_element
*local_list
=
1357 (last_list
? *(last_list
->prefixlist
) : list
);
1359 if (local_allow_unknown
< 0)
1362 return last_list
; /* Found something. */
1364 return 0; /* Found nothing. */
1368 /* Report as error. */
1373 ((*line
)[amb_len
] && (*line
)[amb_len
] != ' '
1374 && (*line
)[amb_len
] != '\t');
1379 for (c
= local_list
; c
; c
= c
->next
)
1380 if (!strncmp (*line
, c
->name
, amb_len
))
1382 if (strlen (ambbuf
) + strlen (c
->name
) + 6 < (int) sizeof ambbuf
)
1384 if (strlen (ambbuf
))
1385 strcat (ambbuf
, ", ");
1386 strcat (ambbuf
, c
->name
);
1390 strcat (ambbuf
, "..");
1394 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype
,
1396 return 0; /* lint */
1401 /* We've got something. It may still not be what the caller
1402 wants (if this command *needs* a subcommand). */
1403 while (**line
== ' ' || **line
== '\t')
1406 if (c
->prefixlist
&& **line
&& !c
->allow_unknown
)
1407 undef_cmd_error (c
->prefixname
, *line
);
1409 /* Seems to be what he wants. Return it. */
1415 /* We are here presumably because an alias or command in *TEXT is
1416 deprecated and a warning message should be generated. This function
1417 decodes *TEXT and potentially generates a warning message as outlined
1420 Example for 'set endian big' which has a fictitious alias 'seb'.
1422 If alias wasn't used in *TEXT, and the command is deprecated:
1423 "warning: 'set endian big' is deprecated."
1425 If alias was used, and only the alias is deprecated:
1426 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1428 If alias was used and command is deprecated (regardless of whether the
1429 alias itself is deprecated:
1431 "warning: 'set endian big' (seb) is deprecated."
1433 After the message has been sent, clear the appropriate flags in the
1434 command and/or the alias so the user is no longer bothered.
1438 deprecated_cmd_warning (char **text
)
1440 struct cmd_list_element
*alias
= NULL
;
1441 struct cmd_list_element
*prefix_cmd
= NULL
;
1442 struct cmd_list_element
*cmd
= NULL
;
1443 struct cmd_list_element
*c
;
1446 if (!lookup_cmd_composition (*text
, &alias
, &prefix_cmd
, &cmd
))
1447 /* return if text doesn't evaluate to a command */
1450 if (!((alias
? (alias
->flags
& DEPRECATED_WARN_USER
) : 0)
1451 || (cmd
->flags
& DEPRECATED_WARN_USER
) ) )
1452 /* return if nothing is deprecated */
1455 printf_filtered ("Warning:");
1457 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1458 printf_filtered (" '%s', an alias for the", alias
->name
);
1460 printf_filtered (" command '");
1463 printf_filtered ("%s", prefix_cmd
->prefixname
);
1465 printf_filtered ("%s", cmd
->name
);
1467 if (alias
&& (cmd
->flags
& CMD_DEPRECATED
))
1468 printf_filtered ("' (%s) is deprecated.\n", alias
->name
);
1470 printf_filtered ("' is deprecated.\n");
1473 /* if it is only the alias that is deprecated, we want to indicate the
1474 new alias, otherwise we'll indicate the new command */
1476 if (alias
&& !(cmd
->flags
& CMD_DEPRECATED
))
1478 if (alias
->replacement
)
1479 printf_filtered ("Use '%s'.\n\n", alias
->replacement
);
1481 printf_filtered ("No alternative known.\n\n");
1485 if (cmd
->replacement
)
1486 printf_filtered ("Use '%s'.\n\n", cmd
->replacement
);
1488 printf_filtered ("No alternative known.\n\n");
1491 /* We've warned you, now we'll keep quiet */
1493 alias
->flags
&= ~DEPRECATED_WARN_USER
;
1495 cmd
->flags
&= ~DEPRECATED_WARN_USER
;
1500 /* Look up the contents of LINE as a command in the command list 'cmdlist'.
1501 Return 1 on success, 0 on failure.
1503 If LINE refers to an alias, *alias will point to that alias.
1505 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1506 command) set *prefix_cmd.
1508 Set *cmd to point to the command LINE indicates.
1510 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1511 exist, they are NULL when we return.
1515 lookup_cmd_composition (char *text
,
1516 struct cmd_list_element
**alias
,
1517 struct cmd_list_element
**prefix_cmd
,
1518 struct cmd_list_element
**cmd
)
1521 int len
, tmp
, nfound
;
1522 struct cmd_list_element
*cur_list
;
1523 struct cmd_list_element
*prev_cmd
;
1532 /* Go through as many command lists as we need to
1533 to find the command TEXT refers to. */
1537 while (*text
== ' ' || *text
== '\t')
1540 /* Identify the name of the command. */
1541 len
= find_command_name_length (text
);
1543 /* If nothing but whitespace, return. */
1547 /* text is the start of the first command word to lookup (and
1548 it's length is len). We copy this into a local temporary */
1550 command
= (char *) alloca (len
+ 1);
1551 memcpy (command
, text
, len
);
1552 command
[len
] = '\0';
1557 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1559 /* We didn't find the command in the entered case, so lower case it
1562 if (!*cmd
|| nfound
== 0)
1564 for (tmp
= 0; tmp
< len
; tmp
++)
1566 char x
= command
[tmp
];
1567 command
[tmp
] = isupper (x
) ? tolower (x
) : x
;
1569 *cmd
= find_cmd (command
, len
, cur_list
, 1, &nfound
);
1572 if (*cmd
== (struct cmd_list_element
*) -1)
1574 return 0; /* ambiguous */
1578 return 0; /* nothing found */
1581 if ((*cmd
)->cmd_pointer
)
1583 /* cmd was actually an alias, we note that an alias was used
1584 (by assigning *alais) and we set *cmd.
1587 *cmd
= (*cmd
)->cmd_pointer
;
1589 *prefix_cmd
= prev_cmd
;
1591 if ((*cmd
)->prefixlist
)
1592 cur_list
= *(*cmd
)->prefixlist
;
1600 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1602 /* Return a vector of char pointers which point to the different
1603 possible completions in LIST of TEXT.
1605 WORD points in the same buffer as TEXT, and completions should be
1606 returned relative to this position. For example, suppose TEXT is "foo"
1607 and we want to complete to "foobar". If WORD is "oo", return
1608 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1611 complete_on_cmdlist (struct cmd_list_element
*list
, char *text
, char *word
)
1613 struct cmd_list_element
*ptr
;
1615 int sizeof_matchlist
;
1617 int textlen
= strlen (text
);
1619 sizeof_matchlist
= 10;
1620 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1623 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
1624 if (!strncmp (ptr
->name
, text
, textlen
)
1625 && !ptr
->abbrev_flag
1627 || ptr
->prefixlist
))
1629 if (matches
== sizeof_matchlist
)
1631 sizeof_matchlist
*= 2;
1632 matchlist
= (char **) xrealloc ((char *) matchlist
,
1634 * sizeof (char *)));
1637 matchlist
[matches
] = (char *)
1638 xmalloc (strlen (word
) + strlen (ptr
->name
) + 1);
1640 strcpy (matchlist
[matches
], ptr
->name
);
1641 else if (word
> text
)
1643 /* Return some portion of ptr->name. */
1644 strcpy (matchlist
[matches
], ptr
->name
+ (word
- text
));
1648 /* Return some of text plus ptr->name. */
1649 strncpy (matchlist
[matches
], word
, text
- word
);
1650 matchlist
[matches
][text
- word
] = '\0';
1651 strcat (matchlist
[matches
], ptr
->name
);
1663 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1664 * sizeof (char *)));
1665 matchlist
[matches
] = (char *) 0;
1671 /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1673 /* Return a vector of char pointers which point to the different
1674 possible completions in CMD of TEXT.
1676 WORD points in the same buffer as TEXT, and completions should be
1677 returned relative to this position. For example, suppose TEXT is "foo"
1678 and we want to complete to "foobar". If WORD is "oo", return
1679 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1682 complete_on_enum (const char *enumlist
[],
1687 int sizeof_matchlist
;
1689 int textlen
= strlen (text
);
1693 sizeof_matchlist
= 10;
1694 matchlist
= (char **) xmalloc (sizeof_matchlist
* sizeof (char *));
1697 for (i
= 0; (name
= enumlist
[i
]) != NULL
; i
++)
1698 if (strncmp (name
, text
, textlen
) == 0)
1700 if (matches
== sizeof_matchlist
)
1702 sizeof_matchlist
*= 2;
1703 matchlist
= (char **) xrealloc ((char *) matchlist
,
1705 * sizeof (char *)));
1708 matchlist
[matches
] = (char *)
1709 xmalloc (strlen (word
) + strlen (name
) + 1);
1711 strcpy (matchlist
[matches
], name
);
1712 else if (word
> text
)
1714 /* Return some portion of name. */
1715 strcpy (matchlist
[matches
], name
+ (word
- text
));
1719 /* Return some of text plus name. */
1720 strncpy (matchlist
[matches
], word
, text
- word
);
1721 matchlist
[matches
][text
- word
] = '\0';
1722 strcat (matchlist
[matches
], name
);
1734 matchlist
= (char **) xrealloc ((char *) matchlist
, ((matches
+ 1)
1735 * sizeof (char *)));
1736 matchlist
[matches
] = (char *) 0;
1743 /* check function pointer */
1745 cmd_func_p (struct cmd_list_element
*cmd
)
1747 return (cmd
->func
!= NULL
);
1751 /* call the command function */
1753 cmd_func (struct cmd_list_element
*cmd
, char *args
, int from_tty
)
1755 if (cmd_func_p (cmd
))
1756 (*cmd
->func
) (cmd
, args
, from_tty
);
1758 error (_("Invalid command"));