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