* configure.tgt (i[3456]86-*-unixware*, i[3456]86-*-unixware2*):
[deliverable/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
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)
de74f71f
MS
676 {
677 fprintf_filtered (stream, "\n\
678Type \"help%s\" followed by a class name for a list of commands in ",
679 cmdtype1);
680 wrap_here ("");
681 fprintf_filtered (stream, "that class.");
682 }
c906108c 683
de74f71f 684 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
c5aa993b 685 cmdtype1, cmdtype2);
de74f71f
MS
686 wrap_here ("");
687 fputs_filtered ("for ", stream);
688 wrap_here ("");
689 fputs_filtered ("full ", stream);
690 wrap_here ("");
691 fputs_filtered ("documentation.\n", stream);
692 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
693 stream);
c906108c 694}
c5aa993b 695
49a5a3a3 696static void
c85871a3 697help_all (struct ui_file *stream)
49a5a3a3
GM
698{
699 struct cmd_list_element *c;
700 extern struct cmd_list_element *cmdlist;
701
702 for (c = cmdlist; c; c = c->next)
703 {
704 if (c->abbrev_flag)
705 continue;
706 /* If this is a prefix command, print it's subcommands */
707 if (c->prefixlist)
708 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
709
710 /* If this is a class name, print all of the commands in the class */
9f60d481 711 else if (c->func == NULL)
49a5a3a3
GM
712 help_cmd_list (cmdlist, c->class, "", 0, stream);
713 }
714}
715
c906108c 716/* Print only the first line of STR on STREAM. */
d318976c 717void
fba45db2 718print_doc_line (struct ui_file *stream, char *str)
c906108c
SS
719{
720 static char *line_buffer = 0;
721 static int line_size;
722 register char *p;
723
724 if (!line_buffer)
725 {
726 line_size = 80;
727 line_buffer = (char *) xmalloc (line_size);
728 }
729
730 p = str;
731 while (*p && *p != '\n' && *p != '.' && *p != ',')
732 p++;
733 if (p - str > line_size - 1)
734 {
735 line_size = p - str + 1;
b8c9b27d 736 xfree (line_buffer);
c906108c
SS
737 line_buffer = (char *) xmalloc (line_size);
738 }
739 strncpy (line_buffer, str, p - str);
740 line_buffer[p - str] = '\0';
741 if (islower (line_buffer[0]))
742 line_buffer[0] = toupper (line_buffer[0]);
8b93c638 743 ui_out_text (uiout, line_buffer);
c906108c
SS
744}
745
746/*
747 * Implement a help command on command list LIST.
748 * RECURSE should be non-zero if this should be done recursively on
749 * all sublists of LIST.
750 * PREFIX is the prefix to print before each command name.
751 * STREAM is the stream upon which the output should be written.
752 * CLASS should be:
c5aa993b 753 * A non-negative class number to list only commands in that
c906108c 754 * class.
c5aa993b
JM
755 * ALL_COMMANDS to list all commands in list.
756 * ALL_CLASSES to list all classes in list.
c906108c
SS
757 *
758 * Note that RECURSE will be active on *all* sublists, not just the
759 * ones selected by the criteria above (ie. the selection mechanism
760 * is at the low level, not the high-level).
761 */
762void
fba45db2
KB
763help_cmd_list (struct cmd_list_element *list, enum command_class class,
764 char *prefix, int recurse, struct ui_file *stream)
c906108c
SS
765{
766 register struct cmd_list_element *c;
767
768 for (c = list; c; c = c->next)
769 {
770 if (c->abbrev_flag == 0 &&
771 (class == all_commands
9f60d481
AC
772 || (class == all_classes && c->func == NULL)
773 || (class == c->class && c->func != NULL)))
c906108c
SS
774 {
775 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
776 print_doc_line (stream, c->doc);
777 fputs_filtered ("\n", stream);
778 }
779 if (recurse
780 && c->prefixlist != 0
781 && c->abbrev_flag == 0)
782 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
783 }
784}
c906108c 785\f
c5aa993b 786
c906108c
SS
787/* Search the input clist for 'command'. Return the command if
788 found (or NULL if not), and return the number of commands
789 found in nfound */
790
791static struct cmd_list_element *
fba45db2
KB
792find_cmd (char *command, int len, struct cmd_list_element *clist,
793 int ignore_help_classes, int *nfound)
c906108c
SS
794{
795 struct cmd_list_element *found, *c;
796
c5aa993b 797 found = (struct cmd_list_element *) NULL;
c906108c
SS
798 *nfound = 0;
799 for (c = clist; c; c = c->next)
800 if (!strncmp (command, c->name, len)
9f60d481 801 && (!ignore_help_classes || c->func))
c906108c 802 {
c5aa993b
JM
803 found = c;
804 (*nfound)++;
805 if (c->name[len] == '\0')
806 {
807 *nfound = 1;
808 break;
809 }
c906108c
SS
810 }
811 return found;
812}
813
814/* This routine takes a line of TEXT and a CLIST in which to start the
815 lookup. When it returns it will have incremented the text pointer past
816 the section of text it matched, set *RESULT_LIST to point to the list in
817 which the last word was matched, and will return a pointer to the cmd
818 list element which the text matches. It will return NULL if no match at
819 all was possible. It will return -1 (cast appropriately, ick) if ambigous
820 matches are possible; in this case *RESULT_LIST will be set to point to
821 the list in which there are ambiguous choices (and *TEXT will be set to
822 the ambiguous text string).
823
824 If the located command was an abbreviation, this routine returns the base
825 command of the abbreviation.
826
827 It does no error reporting whatsoever; control will always return
828 to the superior routine.
829
830 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
831 at the prefix_command (ie. the best match) *or* (special case) will be NULL
832 if no prefix command was ever found. For example, in the case of "info a",
833 "info" matches without ambiguity, but "a" could be "args" or "address", so
834 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
835 RESULT_LIST should not be interpeted as a pointer to the beginning of a
836 list; it simply points to a specific command. In the case of an ambiguous
837 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
838 "info t" can be "info types" or "info target"; upon return *TEXT has been
839 advanced past "info ").
840
841 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
842 affect the operation).
843
844 This routine does *not* modify the text pointed to by TEXT.
c5aa993b 845
c906108c
SS
846 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
847 are actually help classes rather than commands (i.e. the function field of
848 the struct cmd_list_element is NULL). */
849
850struct cmd_list_element *
fba45db2
KB
851lookup_cmd_1 (char **text, struct cmd_list_element *clist,
852 struct cmd_list_element **result_list, int ignore_help_classes)
c906108c
SS
853{
854 char *p, *command;
855 int len, tmp, nfound;
856 struct cmd_list_element *found, *c;
56382845 857 char *line = *text;
c906108c
SS
858
859 while (**text == ' ' || **text == '\t')
860 (*text)++;
861
862 /* Treating underscores as part of command words is important
863 so that "set args_foo()" doesn't get interpreted as
864 "set args _foo()". */
865 for (p = *text;
c5aa993b 866 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
c906108c
SS
867 (tui_version &&
868 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
869 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
870 p++)
871 ;
872
873 /* If nothing but whitespace, return 0. */
874 if (p == *text)
875 return 0;
c5aa993b 876
c906108c
SS
877 len = p - *text;
878
879 /* *text and p now bracket the first command word to lookup (and
880 it's length is len). We copy this into a local temporary */
881
882
883 command = (char *) alloca (len + 1);
884 for (tmp = 0; tmp < len; tmp++)
885 {
886 char x = (*text)[tmp];
887 command[tmp] = x;
888 }
889 command[len] = '\0';
890
891 /* Look it up. */
892 found = 0;
893 nfound = 0;
c5aa993b 894 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c
SS
895
896 /*
c5aa993b
JM
897 ** We didn't find the command in the entered case, so lower case it
898 ** and search again.
899 */
c906108c
SS
900 if (!found || nfound == 0)
901 {
902 for (tmp = 0; tmp < len; tmp++)
c5aa993b
JM
903 {
904 char x = command[tmp];
905 command[tmp] = isupper (x) ? tolower (x) : x;
906 }
907 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
c906108c
SS
908 }
909
910 /* If nothing matches, we have a simple failure. */
911 if (nfound == 0)
912 return 0;
913
914 if (nfound > 1)
915 {
916 if (result_list != NULL)
917 /* Will be modified in calling routine
918 if we know what the prefix command is. */
c5aa993b
JM
919 *result_list = 0;
920 return (struct cmd_list_element *) -1; /* Ambiguous. */
c906108c
SS
921 }
922
923 /* We've matched something on this list. Move text pointer forward. */
924
925 *text = p;
926
c906108c 927 if (found->cmd_pointer)
56382845
FN
928 {
929 /* We drop the alias (abbreviation) in favor of the command it is
930 pointing to. If the alias is deprecated, though, we need to
931 warn the user about it before we drop it. Note that while we
932 are warning about the alias, we may also warn about the command
933 itself and we will adjust the appropriate DEPRECATED_WARN_USER
934 flags */
935
936 if (found->flags & DEPRECATED_WARN_USER)
937 deprecated_cmd_warning (&line);
938 found = found->cmd_pointer;
939 }
c906108c
SS
940 /* If we found a prefix command, keep looking. */
941
942 if (found->prefixlist)
943 {
944 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
945 ignore_help_classes);
946 if (!c)
947 {
948 /* Didn't find anything; this is as far as we got. */
949 if (result_list != NULL)
950 *result_list = clist;
951 return found;
952 }
953 else if (c == (struct cmd_list_element *) -1)
954 {
955 /* We've gotten this far properly, but the next step
956 is ambiguous. We need to set the result list to the best
957 we've found (if an inferior hasn't already set it). */
958 if (result_list != NULL)
959 if (!*result_list)
960 /* This used to say *result_list = *found->prefixlist
c5aa993b
JM
961 If that was correct, need to modify the documentation
962 at the top of this function to clarify what is supposed
963 to be going on. */
c906108c
SS
964 *result_list = found;
965 return c;
966 }
967 else
968 {
969 /* We matched! */
970 return c;
971 }
972 }
973 else
974 {
975 if (result_list != NULL)
976 *result_list = clist;
977 return found;
978 }
979}
980
981/* All this hair to move the space to the front of cmdtype */
982
983static void
fba45db2 984undef_cmd_error (char *cmdtype, char *q)
c906108c
SS
985{
986 error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
c5aa993b
JM
987 cmdtype,
988 q,
989 *cmdtype ? " " : "",
823ca731 990 (int) strlen (cmdtype) - 1,
c5aa993b 991 cmdtype);
c906108c
SS
992}
993
994/* Look up the contents of *LINE as a command in the command list LIST.
995 LIST is a chain of struct cmd_list_element's.
996 If it is found, return the struct cmd_list_element for that command
997 and update *LINE to point after the command name, at the first argument.
998 If not found, call error if ALLOW_UNKNOWN is zero
999 otherwise (or if error returns) return zero.
1000 Call error if specified command is ambiguous,
1001 unless ALLOW_UNKNOWN is negative.
1002 CMDTYPE precedes the word "command" in the error message.
1003
1004 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1005 elements which are actually help classes rather than commands (i.e.
1006 the function field of the struct cmd_list_element is 0). */
1007
1008struct cmd_list_element *
fba45db2
KB
1009lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1010 int allow_unknown, int ignore_help_classes)
c906108c
SS
1011{
1012 struct cmd_list_element *last_list = 0;
1013 struct cmd_list_element *c =
c5aa993b 1014 lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
c64601c7
FN
1015
1016 /* Note: Do not remove trailing whitespace here because this
1017 would be wrong for complete_command. Jim Kingdon */
c5aa993b 1018
c906108c
SS
1019 if (!c)
1020 {
1021 if (!allow_unknown)
1022 {
1023 if (!*line)
1024 error ("Lack of needed %scommand", cmdtype);
1025 else
1026 {
1027 char *p = *line, *q;
1028
c5aa993b 1029 while (isalnum (*p) || *p == '-')
c906108c
SS
1030 p++;
1031
1032 q = (char *) alloca (p - *line + 1);
1033 strncpy (q, *line, p - *line);
1034 q[p - *line] = '\0';
1035 undef_cmd_error (cmdtype, q);
1036 }
1037 }
1038 else
1039 return 0;
1040 }
1041 else if (c == (struct cmd_list_element *) -1)
1042 {
1043 /* Ambigous. Local values should be off prefixlist or called
c5aa993b 1044 values. */
c906108c
SS
1045 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1046 allow_unknown);
1047 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1048 struct cmd_list_element *local_list =
c5aa993b
JM
1049 (last_list ? *(last_list->prefixlist) : list);
1050
c906108c
SS
1051 if (local_allow_unknown < 0)
1052 {
1053 if (last_list)
1054 return last_list; /* Found something. */
1055 else
1056 return 0; /* Found nothing. */
1057 }
1058 else
1059 {
1060 /* Report as error. */
1061 int amb_len;
1062 char ambbuf[100];
1063
1064 for (amb_len = 0;
1065 ((*line)[amb_len] && (*line)[amb_len] != ' '
1066 && (*line)[amb_len] != '\t');
1067 amb_len++)
1068 ;
c5aa993b 1069
c906108c
SS
1070 ambbuf[0] = 0;
1071 for (c = local_list; c; c = c->next)
1072 if (!strncmp (*line, c->name, amb_len))
1073 {
c5aa993b 1074 if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
c906108c
SS
1075 {
1076 if (strlen (ambbuf))
1077 strcat (ambbuf, ", ");
1078 strcat (ambbuf, c->name);
1079 }
1080 else
1081 {
1082 strcat (ambbuf, "..");
1083 break;
1084 }
1085 }
1086 error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
1087 *line, ambbuf);
1088 return 0; /* lint */
1089 }
1090 }
1091 else
1092 {
1093 /* We've got something. It may still not be what the caller
1094 wants (if this command *needs* a subcommand). */
1095 while (**line == ' ' || **line == '\t')
1096 (*line)++;
1097
1098 if (c->prefixlist && **line && !c->allow_unknown)
1099 undef_cmd_error (c->prefixname, *line);
1100
1101 /* Seems to be what he wants. Return it. */
1102 return c;
1103 }
1104 return 0;
1105}
c5aa993b 1106
56382845
FN
1107/* We are here presumably because an alias or command in *TEXT is
1108 deprecated and a warning message should be generated. This function
1109 decodes *TEXT and potentially generates a warning message as outlined
1110 below.
1111
1112 Example for 'set endian big' which has a fictitious alias 'seb'.
1113
1114 If alias wasn't used in *TEXT, and the command is deprecated:
1115 "warning: 'set endian big' is deprecated."
1116
1117 If alias was used, and only the alias is deprecated:
1118 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1119
1120 If alias was used and command is deprecated (regardless of whether the
1121 alias itself is deprecated:
1122
1123 "warning: 'set endian big' (seb) is deprecated."
1124
1125 After the message has been sent, clear the appropriate flags in the
1126 command and/or the alias so the user is no longer bothered.
1127
1128*/
1129void
1130deprecated_cmd_warning (char **text)
1131{
1132 struct cmd_list_element *alias = NULL;
1133 struct cmd_list_element *prefix_cmd = NULL;
1134 struct cmd_list_element *cmd = NULL;
1135 struct cmd_list_element *c;
1136 char *type;
1137
1138 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
1139 /* return if text doesn't evaluate to a command */
1140 return;
1141
1142 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1143 || (cmd->flags & DEPRECATED_WARN_USER) ) )
1144 /* return if nothing is deprecated */
1145 return;
1146
1147 printf_filtered ("Warning:");
1148
1149 if (alias && !(cmd->flags & CMD_DEPRECATED))
1150 printf_filtered (" '%s', an alias for the", alias->name);
1151
1152 printf_filtered (" command '");
1153
1154 if (prefix_cmd)
1155 printf_filtered ("%s", prefix_cmd->prefixname);
1156
1157 printf_filtered ("%s", cmd->name);
1158
1159 if (alias && (cmd->flags & CMD_DEPRECATED))
1160 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1161 else
1162 printf_filtered ("' is deprecated.\n");
1163
1164
1165 /* if it is only the alias that is deprecated, we want to indicate the
1166 new alias, otherwise we'll indicate the new command */
1167
1168 if (alias && !(cmd->flags & CMD_DEPRECATED))
1169 {
1170 if (alias->replacement)
1171 printf_filtered ("Use '%s'.\n\n", alias->replacement);
1172 else
1173 printf_filtered ("No alternative known.\n\n");
1174 }
1175 else
1176 {
1177 if (cmd->replacement)
1178 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
1179 else
1180 printf_filtered ("No alternative known.\n\n");
1181 }
1182
1183 /* We've warned you, now we'll keep quiet */
1184 if (alias)
1185 alias->flags &= ~DEPRECATED_WARN_USER;
1186
1187 cmd->flags &= ~DEPRECATED_WARN_USER;
1188}
1189
1190
1191
1192/* Look up the contents of LINE as a command in the command list 'cmdlist'.
1193 Return 1 on success, 0 on failure.
1194
1195 If LINE refers to an alias, *alias will point to that alias.
1196
1197 If LINE is a postfix command (i.e. one that is preceeded by a prefix
1198 command) set *prefix_cmd.
1199
1200 Set *cmd to point to the command LINE indicates.
1201
1202 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1203 exist, they are NULL when we return.
1204
1205*/
1206int
1207lookup_cmd_composition (char *text,
1208 struct cmd_list_element **alias,
1209 struct cmd_list_element **prefix_cmd,
1210 struct cmd_list_element **cmd)
1211{
1212 char *p, *command;
1213 int len, tmp, nfound;
1214 struct cmd_list_element *cur_list;
1215 struct cmd_list_element *prev_cmd;
1216 *alias = NULL;
1217 *prefix_cmd = NULL;
1218 *cmd = NULL;
1219
1220 cur_list = cmdlist;
1221
1222 while (1)
1223 {
1224 /* Go through as many command lists as we need to
1225 to find the command TEXT refers to. */
1226
1227 prev_cmd = *cmd;
1228
1229 while (*text == ' ' || *text == '\t')
1230 (text)++;
1231
1232 /* Treating underscores as part of command words is important
1233 so that "set args_foo()" doesn't get interpreted as
1234 "set args _foo()". */
1235 for (p = text;
1236 *p && (isalnum (*p) || *p == '-' || *p == '_' ||
1237 (tui_version &&
1238 (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
1239 (xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
1240 p++)
1241 ;
1242
1243 /* If nothing but whitespace, return. */
1244 if (p == text)
1245 return 0;
1246
1247 len = p - text;
1248
1249 /* text and p now bracket the first command word to lookup (and
1250 it's length is len). We copy this into a local temporary */
1251
1252 command = (char *) alloca (len + 1);
1253 for (tmp = 0; tmp < len; tmp++)
1254 {
1255 char x = text[tmp];
1256 command[tmp] = x;
1257 }
1258 command[len] = '\0';
1259
1260 /* Look it up. */
1261 *cmd = 0;
1262 nfound = 0;
1263 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1264
1265 /* We didn't find the command in the entered case, so lower case it
1266 and search again.
1267 */
1268 if (!*cmd || nfound == 0)
1269 {
1270 for (tmp = 0; tmp < len; tmp++)
1271 {
1272 char x = command[tmp];
1273 command[tmp] = isupper (x) ? tolower (x) : x;
1274 }
1275 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1276 }
1277
1278 if (*cmd == (struct cmd_list_element *) -1)
1279 {
1280 return 0; /* ambiguous */
1281 }
1282
1283 if (*cmd == NULL)
1284 return 0; /* nothing found */
1285 else
1286 {
1287 if ((*cmd)->cmd_pointer)
1288 {
1289 /* cmd was actually an alias, we note that an alias was used
1290 (by assigning *alais) and we set *cmd.
1291 */
1292 *alias = *cmd;
1293 *cmd = (*cmd)->cmd_pointer;
1294 }
1295 *prefix_cmd = prev_cmd;
1296 }
1297 if ((*cmd)->prefixlist)
1298 cur_list = *(*cmd)->prefixlist;
1299 else
1300 return 1;
1301
1302 text = p;
1303 }
1304}
1305
c906108c
SS
1306/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1307
1308/* Return a vector of char pointers which point to the different
1309 possible completions in LIST of TEXT.
1310
1311 WORD points in the same buffer as TEXT, and completions should be
1312 returned relative to this position. For example, suppose TEXT is "foo"
1313 and we want to complete to "foobar". If WORD is "oo", return
1314 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1315
1316char **
fba45db2 1317complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
c906108c
SS
1318{
1319 struct cmd_list_element *ptr;
1320 char **matchlist;
1321 int sizeof_matchlist;
1322 int matches;
1323 int textlen = strlen (text);
1324
1325 sizeof_matchlist = 10;
1326 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1327 matches = 0;
1328
1329 for (ptr = list; ptr; ptr = ptr->next)
1330 if (!strncmp (ptr->name, text, textlen)
1331 && !ptr->abbrev_flag
9f60d481 1332 && (ptr->func
c906108c
SS
1333 || ptr->prefixlist))
1334 {
1335 if (matches == sizeof_matchlist)
1336 {
1337 sizeof_matchlist *= 2;
c5aa993b 1338 matchlist = (char **) xrealloc ((char *) matchlist,
c906108c
SS
1339 (sizeof_matchlist
1340 * sizeof (char *)));
1341 }
1342
c5aa993b 1343 matchlist[matches] = (char *)
c906108c
SS
1344 xmalloc (strlen (word) + strlen (ptr->name) + 1);
1345 if (word == text)
1346 strcpy (matchlist[matches], ptr->name);
1347 else if (word > text)
1348 {
1349 /* Return some portion of ptr->name. */
1350 strcpy (matchlist[matches], ptr->name + (word - text));
1351 }
1352 else
1353 {
1354 /* Return some of text plus ptr->name. */
1355 strncpy (matchlist[matches], word, text - word);
1356 matchlist[matches][text - word] = '\0';
1357 strcat (matchlist[matches], ptr->name);
1358 }
1359 ++matches;
1360 }
1361
1362 if (matches == 0)
1363 {
b8c9b27d 1364 xfree (matchlist);
c906108c
SS
1365 matchlist = 0;
1366 }
1367 else
1368 {
c5aa993b
JM
1369 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1370 * sizeof (char *)));
c906108c
SS
1371 matchlist[matches] = (char *) 0;
1372 }
1373
1374 return matchlist;
1375}
1376
1377/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1378
1379/* Return a vector of char pointers which point to the different
1380 possible completions in CMD of TEXT.
1381
1382 WORD points in the same buffer as TEXT, and completions should be
1383 returned relative to this position. For example, suppose TEXT is "foo"
1384 and we want to complete to "foobar". If WORD is "oo", return
1385 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1386
1387char **
53904c9e
AC
1388complete_on_enum (const char *enumlist[],
1389 char *text,
1390 char *word)
c906108c
SS
1391{
1392 char **matchlist;
1393 int sizeof_matchlist;
1394 int matches;
1395 int textlen = strlen (text);
1396 int i;
53904c9e 1397 const char *name;
c906108c
SS
1398
1399 sizeof_matchlist = 10;
1400 matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
1401 matches = 0;
1402
1403 for (i = 0; (name = enumlist[i]) != NULL; i++)
1404 if (strncmp (name, text, textlen) == 0)
1405 {
1406 if (matches == sizeof_matchlist)
1407 {
1408 sizeof_matchlist *= 2;
c5aa993b 1409 matchlist = (char **) xrealloc ((char *) matchlist,
c906108c
SS
1410 (sizeof_matchlist
1411 * sizeof (char *)));
1412 }
1413
c5aa993b 1414 matchlist[matches] = (char *)
c906108c
SS
1415 xmalloc (strlen (word) + strlen (name) + 1);
1416 if (word == text)
1417 strcpy (matchlist[matches], name);
1418 else if (word > text)
1419 {
1420 /* Return some portion of name. */
1421 strcpy (matchlist[matches], name + (word - text));
1422 }
1423 else
1424 {
1425 /* Return some of text plus name. */
1426 strncpy (matchlist[matches], word, text - word);
1427 matchlist[matches][text - word] = '\0';
1428 strcat (matchlist[matches], name);
1429 }
1430 ++matches;
1431 }
1432
1433 if (matches == 0)
1434 {
b8c9b27d 1435 xfree (matchlist);
c906108c
SS
1436 matchlist = 0;
1437 }
1438 else
1439 {
c5aa993b
JM
1440 matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
1441 * sizeof (char *)));
c906108c
SS
1442 matchlist[matches] = (char *) 0;
1443 }
1444
1445 return matchlist;
1446}
1447
This page took 0.217867 seconds and 4 git commands to generate.