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