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