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