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