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