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