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