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