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