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