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