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