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