Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / cli / cli-decode.c
CommitLineData
5796c8dc
SS
1/* Handle lists of commands, their decoding and documentation, for GDB.
2
ef5ccd6c 3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5796c8dc
SS
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
c50c785c 30#include "tui/tui.h" /* For tui_active et al. */
5796c8dc
SS
31#endif
32
33#include "gdb_assert.h"
34
c50c785c 35/* Prototypes for local functions. */
5796c8dc
SS
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
ef5ccd6c
JM
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
5796c8dc
SS
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
ef5ccd6c 168set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
5796c8dc
SS
169{
170 cmd->completer = completer; /* Ok. */
171}
172
5796c8dc 173/* Add element named NAME.
a45ae5f8 174 Space for NAME and DOC must be allocated by the caller.
5796c8dc
SS
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
c50c785c 186 Add this command to command list *LIST.
5796c8dc
SS
187
188 Returns a pointer to the added command (not necessarily the head
c50c785c 189 of *LIST). */
5796c8dc
SS
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
cf7f2e2d 196 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
5796c8dc
SS
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;
ef5ccd6c 242 c->prefix = NULL;
5796c8dc
SS
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.
c50c785c
JM
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.
5796c8dc
SS
260
261 This function does not check to see if command REPLACEMENT exists
c50c785c
JM
262 since gdb may not have gotten around to adding REPLACEMENT when
263 this function is called.
5796c8dc
SS
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{
c50c785c
JM
284 /* Must do this since lookup_cmd tries to side-effect its first
285 arg. */
5796c8dc
SS
286 char *copied_name;
287 struct cmd_list_element *old;
288 struct cmd_list_element *c;
cf7f2e2d 289
5796c8dc
SS
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);
cf7f2e2d 300
5796c8dc
SS
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);
ef5ccd6c
JM
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 }
5796c8dc
SS
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;
ef5ccd6c
JM
325
326 set_cmd_prefix (c, list);
5796c8dc
SS
327 return c;
328}
329
c50c785c
JM
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. */
5796c8dc
SS
334
335struct cmd_list_element *
c50c785c
JM
336add_prefix_cmd (char *name, enum command_class class,
337 void (*fun) (char *, int),
5796c8dc
SS
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);
ef5ccd6c 343 struct cmd_list_element *p;
cf7f2e2d 344
5796c8dc
SS
345 c->prefixlist = prefixlist;
346 c->prefixname = prefixname;
347 c->allow_unknown = allow_unknown;
ef5ccd6c
JM
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
5796c8dc
SS
358 return c;
359}
360
c50c785c 361/* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
5796c8dc
SS
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);
cf7f2e2d 370
5796c8dc
SS
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);
cf7f2e2d 410
5796c8dc
SS
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);
ef5ccd6c
JM
460 set->flags |= DOC_ALLOCATED;
461
5796c8dc
SS
462 if (set_func != NULL)
463 set_cmd_sfunc (set, set_func);
ef5ccd6c
JM
464
465 set_cmd_prefix (set, set_list);
466
5796c8dc
SS
467 show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
468 full_show_doc, show_list);
ef5ccd6c 469 show->flags |= DOC_ALLOCATED;
5796c8dc
SS
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,
ef5ccd6c 486 const char *const *enumlist,
5796c8dc
SS
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;
cf7f2e2d 497
5796c8dc
SS
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
ef5ccd6c
JM
506const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
507
5796c8dc
SS
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{
5796c8dc 523 struct cmd_list_element *c;
cf7f2e2d 524
5796c8dc
SS
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;
cf7f2e2d 548
5796c8dc
SS
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;
cf7f2e2d 570
5796c8dc
SS
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). */
ef5ccd6c 600struct cmd_list_element *
5796c8dc
SS
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{
ef5ccd6c
JM
610 struct cmd_list_element *set_cmd;
611
5796c8dc
SS
612 add_setshow_cmd_full (name, class, var_string_noescape, var,
613 set_doc, show_doc, help_doc,
614 set_func, show_func,
615 set_list, show_list,
ef5ccd6c
JM
616 &set_cmd, NULL);
617 return set_cmd;
5796c8dc
SS
618}
619
620/* Add element named NAME to both the set and show command LISTs (the
621 list for set/show or some sublist thereof). */
622void
623add_setshow_optional_filename_cmd (char *name, enum command_class class,
624 char **var,
625 const char *set_doc, const char *show_doc,
626 const char *help_doc,
627 cmd_sfunc_ftype *set_func,
628 show_value_ftype *show_func,
629 struct cmd_list_element **set_list,
630 struct cmd_list_element **show_list)
631{
632 struct cmd_list_element *set_result;
633
634 add_setshow_cmd_full (name, class, var_optional_filename, var,
635 set_doc, show_doc, help_doc,
636 set_func, show_func,
637 set_list, show_list,
638 &set_result, NULL);
639
640 set_cmd_completer (set_result, filename_completer);
641
642}
643
644/* Add element named NAME to both the set and show command LISTs (the
645 list for set/show or some sublist thereof). CLASS is as in
646 add_cmd. VAR is address of the variable which will contain the
ef5ccd6c
JM
647 value. SET_DOC and SHOW_DOC are the documentation strings. This
648 function is only used in Python API. Please don't use it elsewhere. */
5796c8dc
SS
649void
650add_setshow_integer_cmd (char *name, enum command_class class,
651 int *var,
652 const char *set_doc, const char *show_doc,
653 const char *help_doc,
654 cmd_sfunc_ftype *set_func,
655 show_value_ftype *show_func,
656 struct cmd_list_element **set_list,
657 struct cmd_list_element **show_list)
658{
659 add_setshow_cmd_full (name, class, var_integer, var,
660 set_doc, show_doc, help_doc,
661 set_func, show_func,
662 set_list, show_list,
663 NULL, NULL);
664}
665
666/* Add element named NAME to both the set and show command LISTs (the
667 list for set/show or some sublist thereof). CLASS is as in
668 add_cmd. VAR is address of the variable which will contain the
669 value. SET_DOC and SHOW_DOC are the documentation strings. */
670void
671add_setshow_uinteger_cmd (char *name, enum command_class class,
672 unsigned int *var,
673 const char *set_doc, const char *show_doc,
674 const char *help_doc,
675 cmd_sfunc_ftype *set_func,
676 show_value_ftype *show_func,
677 struct cmd_list_element **set_list,
678 struct cmd_list_element **show_list)
679{
680 add_setshow_cmd_full (name, class, var_uinteger, var,
681 set_doc, show_doc, help_doc,
682 set_func, show_func,
683 set_list, show_list,
684 NULL, NULL);
685}
686
687/* Add element named NAME to both the set and show command LISTs (the
688 list for set/show or some sublist thereof). CLASS is as in
689 add_cmd. VAR is address of the variable which will contain the
690 value. SET_DOC and SHOW_DOC are the documentation strings. */
691void
692add_setshow_zinteger_cmd (char *name, enum command_class class,
693 int *var,
694 const char *set_doc, const char *show_doc,
695 const char *help_doc,
696 cmd_sfunc_ftype *set_func,
697 show_value_ftype *show_func,
698 struct cmd_list_element **set_list,
699 struct cmd_list_element **show_list)
700{
701 add_setshow_cmd_full (name, class, var_zinteger, var,
702 set_doc, show_doc, help_doc,
703 set_func, show_func,
704 set_list, show_list,
705 NULL, NULL);
706}
707
ef5ccd6c
JM
708void
709add_setshow_zuinteger_unlimited_cmd (char *name,
710 enum command_class class,
711 int *var,
712 const char *set_doc,
713 const char *show_doc,
714 const char *help_doc,
715 cmd_sfunc_ftype *set_func,
716 show_value_ftype *show_func,
717 struct cmd_list_element **set_list,
718 struct cmd_list_element **show_list)
719{
720 add_setshow_cmd_full (name, class, var_zuinteger_unlimited, var,
721 set_doc, show_doc, help_doc,
722 set_func, show_func,
723 set_list, show_list,
724 NULL, NULL);
725}
726
5796c8dc
SS
727/* Add element named NAME to both the set and show command LISTs (the
728 list for set/show or some sublist thereof). CLASS is as in
729 add_cmd. VAR is address of the variable which will contain the
730 value. SET_DOC and SHOW_DOC are the documentation strings. */
731void
732add_setshow_zuinteger_cmd (char *name, enum command_class class,
733 unsigned int *var,
734 const char *set_doc, const char *show_doc,
735 const char *help_doc,
736 cmd_sfunc_ftype *set_func,
737 show_value_ftype *show_func,
738 struct cmd_list_element **set_list,
739 struct cmd_list_element **show_list)
740{
741 add_setshow_cmd_full (name, class, var_zuinteger, var,
742 set_doc, show_doc, help_doc,
743 set_func, show_func,
744 set_list, show_list,
745 NULL, NULL);
746}
747
748/* Remove the command named NAME from the command list. Return the
749 list commands which were aliased to the deleted command. If the
750 command had no aliases, return NULL. The various *HOOKs are set to
751 the pre- and post-hook commands for the deleted command. If the
752 command does not have a hook, the corresponding out parameter is
753 set to NULL. */
754
755static struct cmd_list_element *
756delete_cmd (char *name, struct cmd_list_element **list,
757 struct cmd_list_element **prehook,
758 struct cmd_list_element **prehookee,
759 struct cmd_list_element **posthook,
760 struct cmd_list_element **posthookee)
761{
762 struct cmd_list_element *iter;
763 struct cmd_list_element **previous_chain_ptr;
764 struct cmd_list_element *aliases = NULL;
765
766 *prehook = NULL;
767 *prehookee = NULL;
768 *posthook = NULL;
769 *posthookee = NULL;
770 previous_chain_ptr = list;
771
772 for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
773 {
774 if (strcmp (iter->name, name) == 0)
775 {
776 if (iter->destroyer)
777 iter->destroyer (iter, iter->context);
778 if (iter->hookee_pre)
779 iter->hookee_pre->hook_pre = 0;
780 *prehook = iter->hook_pre;
781 *prehookee = iter->hookee_pre;
782 if (iter->hookee_post)
783 iter->hookee_post->hook_post = 0;
ef5ccd6c
JM
784 if (iter->doc && (iter->flags & DOC_ALLOCATED) != 0)
785 xfree (iter->doc);
5796c8dc
SS
786 *posthook = iter->hook_post;
787 *posthookee = iter->hookee_post;
788
789 /* Update the link. */
790 *previous_chain_ptr = iter->next;
791
792 aliases = iter->aliases;
793
794 /* If this command was an alias, remove it from the list of
795 aliases. */
796 if (iter->cmd_pointer)
797 {
798 struct cmd_list_element **prevp = &iter->cmd_pointer->aliases;
799 struct cmd_list_element *a = *prevp;
800
801 while (a != iter)
802 {
803 prevp = &a->alias_chain;
804 a = *prevp;
805 }
806 *prevp = iter->alias_chain;
807 }
808
809 xfree (iter);
810
811 /* We won't see another command with the same name. */
812 break;
813 }
814 else
815 previous_chain_ptr = &iter->next;
816 }
817
818 return aliases;
819}
820\f
c50c785c 821/* Shorthands to the commands above. */
5796c8dc
SS
822
823/* Add an element to the list of info subcommands. */
824
825struct cmd_list_element *
826add_info (char *name, void (*fun) (char *, int), char *doc)
827{
828 return add_cmd (name, no_class, fun, doc, &infolist);
829}
830
831/* Add an alias to the list of info subcommands. */
832
833struct cmd_list_element *
834add_info_alias (char *name, char *oldname, int abbrev_flag)
835{
836 return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
837}
838
839/* Add an element to the list of commands. */
840
841struct cmd_list_element *
842add_com (char *name, enum command_class class, void (*fun) (char *, int),
843 char *doc)
844{
845 return add_cmd (name, class, fun, doc, &cmdlist);
846}
847
848/* Add an alias or abbreviation command to the list of commands. */
849
850struct cmd_list_element *
851add_com_alias (char *name, char *oldname, enum command_class class,
852 int abbrev_flag)
853{
854 return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
855}
856\f
857/* Recursively walk the commandlist structures, and print out the
858 documentation of commands that match our regex in either their
859 name, or their documentation.
860*/
861void
c50c785c
JM
862apropos_cmd (struct ui_file *stream,
863 struct cmd_list_element *commandlist,
cf7f2e2d 864 struct re_pattern_buffer *regex, char *prefix)
5796c8dc
SS
865{
866 struct cmd_list_element *c;
867 int returnvalue;
cf7f2e2d 868
c50c785c 869 /* Walk through the commands. */
5796c8dc
SS
870 for (c=commandlist;c;c=c->next)
871 {
c50c785c 872 returnvalue = -1; /* Needed to avoid double printing. */
5796c8dc
SS
873 if (c->name != NULL)
874 {
c50c785c 875 /* Try to match against the name. */
cf7f2e2d
JM
876 returnvalue = re_search (regex, c->name, strlen(c->name),
877 0, strlen (c->name), NULL);
5796c8dc
SS
878 if (returnvalue >= 0)
879 {
880 print_help_for_command (c, prefix,
881 0 /* don't recurse */, stream);
882 }
883 }
884 if (c->doc != NULL && returnvalue < 0)
885 {
c50c785c 886 /* Try to match against documentation. */
5796c8dc
SS
887 if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
888 {
889 print_help_for_command (c, prefix,
890 0 /* don't recurse */, stream);
891 }
892 }
c50c785c
JM
893 /* Check if this command has subcommands and is not an
894 abbreviation. We skip listing subcommands of abbreviations
895 in order to avoid duplicates in the output. */
5796c8dc
SS
896 if (c->prefixlist != NULL && !c->abbrev_flag)
897 {
898 /* Recursively call ourselves on the subcommand list,
c50c785c 899 passing the right prefix in. */
5796c8dc
SS
900 apropos_cmd (stream,*c->prefixlist,regex,c->prefixname);
901 }
902 }
903}
904
905/* This command really has to deal with two things:
c50c785c
JM
906 1) I want documentation on *this string* (usually called by
907 "help commandname").
908
909 2) I want documentation on *this list* (usually called by giving a
910 command that requires subcommands. Also called by saying just
911 "help".)
912
913 I am going to split this into two seperate comamnds, help_cmd and
914 help_list. */
5796c8dc
SS
915
916void
917help_cmd (char *command, struct ui_file *stream)
918{
919 struct cmd_list_element *c;
5796c8dc
SS
920
921 if (!command)
922 {
923 help_list (cmdlist, "", all_classes, stream);
924 return;
925 }
926
927 if (strcmp (command, "all") == 0)
928 {
929 help_all (stream);
930 return;
931 }
932
933 c = lookup_cmd (&command, cmdlist, "", 0, 0);
934
935 if (c == 0)
936 return;
937
938 /* There are three cases here.
939 If c->prefixlist is nonzero, we have a prefix command.
940 Print its documentation, then list its subcommands.
941
942 If c->func is non NULL, we really have a command. Print its
943 documentation and return.
944
945 If c->func is NULL, we have a class name. Print its
946 documentation (as if it were a command) and then set class to the
947 number of this class so that the commands in the class will be
948 listed. */
949
950 fputs_filtered (c->doc, stream);
951 fputs_filtered ("\n", stream);
952
953 if (c->prefixlist == 0 && c->func != NULL)
954 return;
955 fprintf_filtered (stream, "\n");
956
c50c785c 957 /* If this is a prefix command, print it's subcommands. */
5796c8dc
SS
958 if (c->prefixlist)
959 help_list (*c->prefixlist, c->prefixname, all_commands, stream);
960
c50c785c 961 /* If this is a class name, print all of the commands in the class. */
5796c8dc
SS
962 if (c->func == NULL)
963 help_list (cmdlist, "", c->class, stream);
964
965 if (c->hook_pre || c->hook_post)
966 fprintf_filtered (stream,
967 "\nThis command has a hook (or hooks) defined:\n");
968
969 if (c->hook_pre)
970 fprintf_filtered (stream,
971 "\tThis command is run after : %s (pre hook)\n",
972 c->hook_pre->name);
973 if (c->hook_post)
974 fprintf_filtered (stream,
975 "\tThis command is run before : %s (post hook)\n",
976 c->hook_post->name);
977}
978
979/*
980 * Get a specific kind of help on a command list.
981 *
982 * LIST is the list.
983 * CMDTYPE is the prefix to use in the title string.
984 * CLASS is the class with which to list the nodes of this list (see
985 * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
986 * everything, ALL_CLASSES for just classes, and non-negative for only things
987 * in a specific class.
988 * and STREAM is the output stream on which to print things.
989 * If you call this routine with a class >= 0, it recurses.
990 */
991void
992help_list (struct cmd_list_element *list, char *cmdtype,
993 enum command_class class, struct ui_file *stream)
994{
995 int len;
996 char *cmdtype1, *cmdtype2;
997
c50c785c
JM
998 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
999 */
5796c8dc
SS
1000 len = strlen (cmdtype);
1001 cmdtype1 = (char *) alloca (len + 1);
1002 cmdtype1[0] = 0;
1003 cmdtype2 = (char *) alloca (len + 4);
1004 cmdtype2[0] = 0;
1005 if (len)
1006 {
1007 cmdtype1[0] = ' ';
1008 strncpy (cmdtype1 + 1, cmdtype, len - 1);
1009 cmdtype1[len] = 0;
1010 strncpy (cmdtype2, cmdtype, len - 1);
1011 strcpy (cmdtype2 + len - 1, " sub");
1012 }
1013
1014 if (class == all_classes)
1015 fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
1016 else
1017 fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
1018
1019 help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
1020
1021 if (class == all_classes)
1022 {
1023 fprintf_filtered (stream, "\n\
1024Type \"help%s\" followed by a class name for a list of commands in ",
1025 cmdtype1);
1026 wrap_here ("");
1027 fprintf_filtered (stream, "that class.");
1028
1029 fprintf_filtered (stream, "\n\
1030Type \"help all\" for the list of all commands.");
1031 }
1032
1033 fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ",
1034 cmdtype1, cmdtype2);
1035 wrap_here ("");
1036 fputs_filtered ("for ", stream);
1037 wrap_here ("");
1038 fputs_filtered ("full ", stream);
1039 wrap_here ("");
1040 fputs_filtered ("documentation.\n", stream);
1041 fputs_filtered ("Type \"apropos word\" to search "
c50c785c 1042 "for commands related to \"word\".\n", stream);
5796c8dc
SS
1043 fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n",
1044 stream);
1045}
1046
1047static void
1048help_all (struct ui_file *stream)
1049{
1050 struct cmd_list_element *c;
5796c8dc
SS
1051 int seen_unclassified = 0;
1052
1053 for (c = cmdlist; c; c = c->next)
1054 {
1055 if (c->abbrev_flag)
1056 continue;
c50c785c
JM
1057 /* If this is a class name, print all of the commands in the
1058 class. */
5796c8dc
SS
1059
1060 if (c->func == NULL)
1061 {
1062 fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name);
1063 help_cmd_list (cmdlist, c->class, "", 1, stream);
1064 }
1065 }
1066
1067 /* While it's expected that all commands are in some class,
1068 as a safety measure, we'll print commands outside of any
1069 class at the end. */
1070
1071 for (c = cmdlist; c; c = c->next)
1072 {
1073 if (c->abbrev_flag)
1074 continue;
1075
1076 if (c->class == no_class)
1077 {
1078 if (!seen_unclassified)
1079 {
1080 fprintf_filtered (stream, "\nUnclassified commands\n\n");
1081 seen_unclassified = 1;
1082 }
1083 print_help_for_command (c, "", 1, stream);
1084 }
1085 }
1086
1087}
1088
1089/* Print only the first line of STR on STREAM. */
1090void
1091print_doc_line (struct ui_file *stream, char *str)
1092{
1093 static char *line_buffer = 0;
1094 static int line_size;
1095 char *p;
1096
1097 if (!line_buffer)
1098 {
1099 line_size = 80;
1100 line_buffer = (char *) xmalloc (line_size);
1101 }
1102
ef5ccd6c
JM
1103 /* Keep printing '.' or ',' not followed by a whitespace for embedded strings
1104 like '.gdbinit'. */
5796c8dc 1105 p = str;
ef5ccd6c
JM
1106 while (*p && *p != '\n'
1107 && ((*p != '.' && *p != ',') || (p[1] && !isspace (p[1]))))
5796c8dc
SS
1108 p++;
1109 if (p - str > line_size - 1)
1110 {
1111 line_size = p - str + 1;
1112 xfree (line_buffer);
1113 line_buffer = (char *) xmalloc (line_size);
1114 }
1115 strncpy (line_buffer, str, p - str);
1116 line_buffer[p - str] = '\0';
1117 if (islower (line_buffer[0]))
1118 line_buffer[0] = toupper (line_buffer[0]);
ef5ccd6c 1119 fputs_filtered (line_buffer, stream);
5796c8dc
SS
1120}
1121
1122/* Print one-line help for command C.
1123 If RECURSE is non-zero, also print one-line descriptions
c50c785c 1124 of all prefixed subcommands. */
5796c8dc
SS
1125static void
1126print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse,
1127 struct ui_file *stream)
1128{
1129 fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
1130 print_doc_line (stream, c->doc);
1131 fputs_filtered ("\n", stream);
1132
1133 if (recurse
1134 && c->prefixlist != 0
1135 && c->abbrev_flag == 0)
1136 /* Subcommands of a prefix command typically have 'all_commands'
1137 as class. If we pass CLASS to recursive invocation,
c50c785c 1138 most often we won't see anything. */
5796c8dc
SS
1139 help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream);
1140}
1141
1142/*
1143 * Implement a help command on command list LIST.
1144 * RECURSE should be non-zero if this should be done recursively on
1145 * all sublists of LIST.
1146 * PREFIX is the prefix to print before each command name.
1147 * STREAM is the stream upon which the output should be written.
1148 * CLASS should be:
1149 * A non-negative class number to list only commands in that
1150 * class.
1151 * ALL_COMMANDS to list all commands in list.
1152 * ALL_CLASSES to list all classes in list.
1153 *
1154 * Note that RECURSE will be active on *all* sublists, not just the
1155 * ones selected by the criteria above (ie. the selection mechanism
1156 * is at the low level, not the high-level).
1157 */
1158void
1159help_cmd_list (struct cmd_list_element *list, enum command_class class,
1160 char *prefix, int recurse, struct ui_file *stream)
1161{
1162 struct cmd_list_element *c;
1163
1164 for (c = list; c; c = c->next)
1165 {
cf7f2e2d
JM
1166 if (c->abbrev_flag == 0
1167 && (class == all_commands
1168 || (class == all_classes && c->func == NULL)
1169 || (class == c->class && c->func != NULL)))
5796c8dc
SS
1170 {
1171 print_help_for_command (c, prefix, recurse, stream);
1172 }
1173 else if (c->abbrev_flag == 0 && recurse
1174 && class == class_user && c->prefixlist != NULL)
1175 /* User-defined commands may be subcommands. */
c50c785c
JM
1176 help_cmd_list (*c->prefixlist, class, c->prefixname,
1177 recurse, stream);
5796c8dc
SS
1178 }
1179}
1180\f
1181
1182/* Search the input clist for 'command'. Return the command if
1183 found (or NULL if not), and return the number of commands
c50c785c 1184 found in nfound. */
5796c8dc
SS
1185
1186static struct cmd_list_element *
1187find_cmd (char *command, int len, struct cmd_list_element *clist,
1188 int ignore_help_classes, int *nfound)
1189{
1190 struct cmd_list_element *found, *c;
1191
1192 found = (struct cmd_list_element *) NULL;
1193 *nfound = 0;
1194 for (c = clist; c; c = c->next)
1195 if (!strncmp (command, c->name, len)
1196 && (!ignore_help_classes || c->func))
1197 {
1198 found = c;
1199 (*nfound)++;
1200 if (c->name[len] == '\0')
1201 {
1202 *nfound = 1;
1203 break;
1204 }
1205 }
1206 return found;
1207}
1208
1209static int
1210find_command_name_length (const char *text)
1211{
1212 const char *p = text;
1213
1214 /* Treating underscores as part of command words is important
1215 so that "set args_foo()" doesn't get interpreted as
1216 "set args _foo()". */
c50c785c
JM
1217 /* Some characters are only used for TUI specific commands.
1218 However, they are always allowed for the sake of consistency.
1219
1220 The XDB compatibility characters are only allowed when using the
1221 right mode because they clash with other GDB commands -
1222 specifically '/' is used as a suffix for print, examine and
1223 display.
1224
1225 Note that this is larger than the character set allowed when
1226 creating user-defined commands. */
1227
a45ae5f8
JM
1228 /* Recognize '!' as a single character command so that, e.g., "!ls"
1229 works as expected. */
1230 if (*p == '!')
1231 return 1;
1232
cf7f2e2d 1233 while (isalnum (*p) || *p == '-' || *p == '_'
5796c8dc 1234 /* Characters used by TUI specific commands. */
cf7f2e2d 1235 || *p == '+' || *p == '<' || *p == '>' || *p == '$'
5796c8dc 1236 /* Characters used for XDB compatibility. */
a45ae5f8 1237 || (xdb_commands && (*p == '/' || *p == '?')))
5796c8dc
SS
1238 p++;
1239
1240 return p - text;
1241}
1242
a45ae5f8
JM
1243/* Return TRUE if NAME is a valid user-defined command name.
1244 This is a stricter subset of all gdb commands,
1245 see find_command_name_length. */
1246
1247int
1248valid_user_defined_cmd_name_p (const char *name)
1249{
1250 const char *p;
1251
1252 if (*name == '\0')
1253 return FALSE;
1254
1255 /* Alas "42" is a legitimate user-defined command.
1256 In the interests of not breaking anything we preserve that. */
1257
1258 for (p = name; *p != '\0'; ++p)
1259 {
1260 if (isalnum (*p)
1261 || *p == '-'
1262 || *p == '_')
1263 ; /* Ok. */
1264 else
1265 return FALSE;
1266 }
1267
1268 return TRUE;
1269}
1270
5796c8dc
SS
1271/* This routine takes a line of TEXT and a CLIST in which to start the
1272 lookup. When it returns it will have incremented the text pointer past
1273 the section of text it matched, set *RESULT_LIST to point to the list in
1274 which the last word was matched, and will return a pointer to the cmd
1275 list element which the text matches. It will return NULL if no match at
1276 all was possible. It will return -1 (cast appropriately, ick) if ambigous
1277 matches are possible; in this case *RESULT_LIST will be set to point to
1278 the list in which there are ambiguous choices (and *TEXT will be set to
1279 the ambiguous text string).
1280
1281 If the located command was an abbreviation, this routine returns the base
1282 command of the abbreviation.
1283
1284 It does no error reporting whatsoever; control will always return
1285 to the superior routine.
1286
1287 In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
1288 at the prefix_command (ie. the best match) *or* (special case) will be NULL
1289 if no prefix command was ever found. For example, in the case of "info a",
1290 "info" matches without ambiguity, but "a" could be "args" or "address", so
1291 *RESULT_LIST is set to the cmd_list_element for "info". So in this case
1292 RESULT_LIST should not be interpeted as a pointer to the beginning of a
1293 list; it simply points to a specific command. In the case of an ambiguous
1294 return *TEXT is advanced past the last non-ambiguous prefix (e.g.
1295 "info t" can be "info types" or "info target"; upon return *TEXT has been
1296 advanced past "info ").
1297
1298 If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
1299 affect the operation).
1300
1301 This routine does *not* modify the text pointed to by TEXT.
1302
1303 If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
1304 are actually help classes rather than commands (i.e. the function field of
1305 the struct cmd_list_element is NULL). */
1306
1307struct cmd_list_element *
1308lookup_cmd_1 (char **text, struct cmd_list_element *clist,
1309 struct cmd_list_element **result_list, int ignore_help_classes)
1310{
1311 char *command;
1312 int len, tmp, nfound;
1313 struct cmd_list_element *found, *c;
1314 char *line = *text;
1315
1316 while (**text == ' ' || **text == '\t')
1317 (*text)++;
1318
1319 /* Identify the name of the command. */
1320 len = find_command_name_length (*text);
1321
1322 /* If nothing but whitespace, return 0. */
1323 if (len == 0)
1324 return 0;
1325
1326 /* *text and p now bracket the first command word to lookup (and
c50c785c 1327 it's length is len). We copy this into a local temporary. */
5796c8dc
SS
1328
1329
1330 command = (char *) alloca (len + 1);
1331 memcpy (command, *text, len);
1332 command[len] = '\0';
1333
1334 /* Look it up. */
1335 found = 0;
1336 nfound = 0;
1337 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1338
c50c785c
JM
1339 /* We didn't find the command in the entered case, so lower case it
1340 and search again. */
5796c8dc
SS
1341 if (!found || nfound == 0)
1342 {
1343 for (tmp = 0; tmp < len; tmp++)
1344 {
1345 char x = command[tmp];
cf7f2e2d 1346
5796c8dc
SS
1347 command[tmp] = isupper (x) ? tolower (x) : x;
1348 }
1349 found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
1350 }
1351
1352 /* If nothing matches, we have a simple failure. */
1353 if (nfound == 0)
1354 return 0;
1355
1356 if (nfound > 1)
1357 {
1358 if (result_list != NULL)
1359 /* Will be modified in calling routine
1360 if we know what the prefix command is. */
1361 *result_list = 0;
c50c785c 1362 return CMD_LIST_AMBIGUOUS; /* Ambiguous. */
5796c8dc
SS
1363 }
1364
c50c785c 1365 /* We've matched something on this list. Move text pointer forward. */
5796c8dc
SS
1366
1367 *text += len;
1368
1369 if (found->cmd_pointer)
1370 {
c50c785c
JM
1371 /* We drop the alias (abbreviation) in favor of the command it
1372 is pointing to. If the alias is deprecated, though, we need to
5796c8dc
SS
1373 warn the user about it before we drop it. Note that while we
1374 are warning about the alias, we may also warn about the command
1375 itself and we will adjust the appropriate DEPRECATED_WARN_USER
c50c785c 1376 flags. */
5796c8dc
SS
1377
1378 if (found->flags & DEPRECATED_WARN_USER)
1379 deprecated_cmd_warning (&line);
1380 found = found->cmd_pointer;
1381 }
1382 /* If we found a prefix command, keep looking. */
1383
1384 if (found->prefixlist)
1385 {
1386 c = lookup_cmd_1 (text, *found->prefixlist, result_list,
1387 ignore_help_classes);
1388 if (!c)
1389 {
1390 /* Didn't find anything; this is as far as we got. */
1391 if (result_list != NULL)
1392 *result_list = clist;
1393 return found;
1394 }
c50c785c 1395 else if (c == CMD_LIST_AMBIGUOUS)
5796c8dc 1396 {
c50c785c
JM
1397 /* We've gotten this far properly, but the next step is
1398 ambiguous. We need to set the result list to the best
5796c8dc
SS
1399 we've found (if an inferior hasn't already set it). */
1400 if (result_list != NULL)
1401 if (!*result_list)
c50c785c 1402 /* This used to say *result_list = *found->prefixlist.
5796c8dc 1403 If that was correct, need to modify the documentation
c50c785c
JM
1404 at the top of this function to clarify what is
1405 supposed to be going on. */
5796c8dc
SS
1406 *result_list = found;
1407 return c;
1408 }
1409 else
1410 {
1411 /* We matched! */
1412 return c;
1413 }
1414 }
1415 else
1416 {
1417 if (result_list != NULL)
1418 *result_list = clist;
1419 return found;
1420 }
1421}
1422
1423/* All this hair to move the space to the front of cmdtype */
1424
1425static void
1426undef_cmd_error (char *cmdtype, char *q)
1427{
1428 error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
1429 cmdtype,
1430 q,
1431 *cmdtype ? " " : "",
1432 (int) strlen (cmdtype) - 1,
1433 cmdtype);
1434}
1435
1436/* Look up the contents of *LINE as a command in the command list LIST.
1437 LIST is a chain of struct cmd_list_element's.
1438 If it is found, return the struct cmd_list_element for that command
1439 and update *LINE to point after the command name, at the first argument.
1440 If not found, call error if ALLOW_UNKNOWN is zero
1441 otherwise (or if error returns) return zero.
1442 Call error if specified command is ambiguous,
1443 unless ALLOW_UNKNOWN is negative.
1444 CMDTYPE precedes the word "command" in the error message.
1445
1446 If INGNORE_HELP_CLASSES is nonzero, ignore any command list
1447 elements which are actually help classes rather than commands (i.e.
1448 the function field of the struct cmd_list_element is 0). */
1449
1450struct cmd_list_element *
1451lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
1452 int allow_unknown, int ignore_help_classes)
1453{
1454 struct cmd_list_element *last_list = 0;
1455 struct cmd_list_element *c;
1456
1457 /* Note: Do not remove trailing whitespace here because this
1458 would be wrong for complete_command. Jim Kingdon */
1459
1460 if (!*line)
1461 error (_("Lack of needed %scommand"), cmdtype);
1462
1463 c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
1464
1465 if (!c)
1466 {
1467 if (!allow_unknown)
1468 {
1469 char *q;
1470 int len = find_command_name_length (*line);
1471
1472 q = (char *) alloca (len + 1);
1473 strncpy (q, *line, len);
1474 q[len] = '\0';
1475 undef_cmd_error (cmdtype, q);
1476 }
1477 else
1478 return 0;
1479 }
c50c785c 1480 else if (c == CMD_LIST_AMBIGUOUS)
5796c8dc
SS
1481 {
1482 /* Ambigous. Local values should be off prefixlist or called
1483 values. */
1484 int local_allow_unknown = (last_list ? last_list->allow_unknown :
1485 allow_unknown);
1486 char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
1487 struct cmd_list_element *local_list =
cf7f2e2d 1488 (last_list ? *(last_list->prefixlist) : list);
5796c8dc
SS
1489
1490 if (local_allow_unknown < 0)
1491 {
1492 if (last_list)
1493 return last_list; /* Found something. */
1494 else
1495 return 0; /* Found nothing. */
1496 }
1497 else
1498 {
1499 /* Report as error. */
1500 int amb_len;
1501 char ambbuf[100];
1502
1503 for (amb_len = 0;
1504 ((*line)[amb_len] && (*line)[amb_len] != ' '
1505 && (*line)[amb_len] != '\t');
1506 amb_len++)
1507 ;
1508
1509 ambbuf[0] = 0;
1510 for (c = local_list; c; c = c->next)
1511 if (!strncmp (*line, c->name, amb_len))
1512 {
c50c785c
JM
1513 if (strlen (ambbuf) + strlen (c->name) + 6
1514 < (int) sizeof ambbuf)
5796c8dc
SS
1515 {
1516 if (strlen (ambbuf))
1517 strcat (ambbuf, ", ");
1518 strcat (ambbuf, c->name);
1519 }
1520 else
1521 {
1522 strcat (ambbuf, "..");
1523 break;
1524 }
1525 }
1526 error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype,
1527 *line, ambbuf);
1528 return 0; /* lint */
1529 }
1530 }
1531 else
1532 {
1533 /* We've got something. It may still not be what the caller
1534 wants (if this command *needs* a subcommand). */
1535 while (**line == ' ' || **line == '\t')
1536 (*line)++;
1537
1538 if (c->prefixlist && **line && !c->allow_unknown)
1539 undef_cmd_error (c->prefixname, *line);
1540
1541 /* Seems to be what he wants. Return it. */
1542 return c;
1543 }
1544 return 0;
1545}
1546
c50c785c
JM
1547/* We are here presumably because an alias or command in *TEXT is
1548 deprecated and a warning message should be generated. This
1549 function decodes *TEXT and potentially generates a warning message
1550 as outlined below.
5796c8dc
SS
1551
1552 Example for 'set endian big' which has a fictitious alias 'seb'.
1553
1554 If alias wasn't used in *TEXT, and the command is deprecated:
1555 "warning: 'set endian big' is deprecated."
1556
1557 If alias was used, and only the alias is deprecated:
1558 "warning: 'seb' an alias for the command 'set endian big' is deprecated."
1559
c50c785c
JM
1560 If alias was used and command is deprecated (regardless of whether
1561 the alias itself is deprecated:
5796c8dc
SS
1562
1563 "warning: 'set endian big' (seb) is deprecated."
1564
1565 After the message has been sent, clear the appropriate flags in the
1566 command and/or the alias so the user is no longer bothered.
1567
1568*/
1569void
1570deprecated_cmd_warning (char **text)
1571{
1572 struct cmd_list_element *alias = NULL;
1573 struct cmd_list_element *prefix_cmd = NULL;
1574 struct cmd_list_element *cmd = NULL;
5796c8dc
SS
1575
1576 if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
c50c785c 1577 /* Return if text doesn't evaluate to a command. */
5796c8dc
SS
1578 return;
1579
1580 if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
1581 || (cmd->flags & DEPRECATED_WARN_USER) ) )
c50c785c 1582 /* Return if nothing is deprecated. */
5796c8dc
SS
1583 return;
1584
1585 printf_filtered ("Warning:");
1586
1587 if (alias && !(cmd->flags & CMD_DEPRECATED))
1588 printf_filtered (" '%s', an alias for the", alias->name);
1589
1590 printf_filtered (" command '");
1591
1592 if (prefix_cmd)
1593 printf_filtered ("%s", prefix_cmd->prefixname);
1594
1595 printf_filtered ("%s", cmd->name);
1596
1597 if (alias && (cmd->flags & CMD_DEPRECATED))
1598 printf_filtered ("' (%s) is deprecated.\n", alias->name);
1599 else
1600 printf_filtered ("' is deprecated.\n");
1601
1602
c50c785c
JM
1603 /* If it is only the alias that is deprecated, we want to indicate
1604 the new alias, otherwise we'll indicate the new command. */
5796c8dc
SS
1605
1606 if (alias && !(cmd->flags & CMD_DEPRECATED))
1607 {
1608 if (alias->replacement)
cf7f2e2d 1609 printf_filtered ("Use '%s'.\n\n", alias->replacement);
5796c8dc 1610 else
cf7f2e2d 1611 printf_filtered ("No alternative known.\n\n");
5796c8dc
SS
1612 }
1613 else
1614 {
1615 if (cmd->replacement)
cf7f2e2d 1616 printf_filtered ("Use '%s'.\n\n", cmd->replacement);
5796c8dc 1617 else
cf7f2e2d 1618 printf_filtered ("No alternative known.\n\n");
5796c8dc
SS
1619 }
1620
c50c785c 1621 /* We've warned you, now we'll keep quiet. */
5796c8dc
SS
1622 if (alias)
1623 alias->flags &= ~DEPRECATED_WARN_USER;
1624
1625 cmd->flags &= ~DEPRECATED_WARN_USER;
1626}
1627
1628
c50c785c 1629/* Look up the contents of LINE as a command in the command list 'cmdlist'.
5796c8dc
SS
1630 Return 1 on success, 0 on failure.
1631
1632 If LINE refers to an alias, *alias will point to that alias.
1633
a45ae5f8 1634 If LINE is a postfix command (i.e. one that is preceded by a prefix
5796c8dc
SS
1635 command) set *prefix_cmd.
1636
1637 Set *cmd to point to the command LINE indicates.
1638
1639 If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not
1640 exist, they are NULL when we return.
1641
1642*/
1643int
1644lookup_cmd_composition (char *text,
1645 struct cmd_list_element **alias,
1646 struct cmd_list_element **prefix_cmd,
1647 struct cmd_list_element **cmd)
1648{
1649 char *command;
1650 int len, tmp, nfound;
1651 struct cmd_list_element *cur_list;
1652 struct cmd_list_element *prev_cmd;
cf7f2e2d 1653
5796c8dc
SS
1654 *alias = NULL;
1655 *prefix_cmd = NULL;
1656 *cmd = NULL;
1657
1658 cur_list = cmdlist;
1659
1660 while (1)
1661 {
a45ae5f8 1662 /* Go through as many command lists as we need to,
c50c785c 1663 to find the command TEXT refers to. */
5796c8dc
SS
1664
1665 prev_cmd = *cmd;
1666
1667 while (*text == ' ' || *text == '\t')
cf7f2e2d 1668 (text)++;
5796c8dc
SS
1669
1670 /* Identify the name of the command. */
1671 len = find_command_name_length (text);
1672
1673 /* If nothing but whitespace, return. */
1674 if (len == 0)
1675 return 0;
1676
cf7f2e2d
JM
1677 /* Text is the start of the first command word to lookup (and
1678 it's length is len). We copy this into a local temporary. */
5796c8dc
SS
1679
1680 command = (char *) alloca (len + 1);
1681 memcpy (command, text, len);
1682 command[len] = '\0';
1683
1684 /* Look it up. */
1685 *cmd = 0;
1686 nfound = 0;
1687 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1688
c50c785c
JM
1689 /* We didn't find the command in the entered case, so lower case
1690 it and search again.
5796c8dc
SS
1691 */
1692 if (!*cmd || nfound == 0)
cf7f2e2d
JM
1693 {
1694 for (tmp = 0; tmp < len; tmp++)
1695 {
1696 char x = command[tmp];
1697
1698 command[tmp] = isupper (x) ? tolower (x) : x;
1699 }
1700 *cmd = find_cmd (command, len, cur_list, 1, &nfound);
1701 }
5796c8dc 1702
c50c785c 1703 if (*cmd == CMD_LIST_AMBIGUOUS)
cf7f2e2d
JM
1704 {
1705 return 0; /* ambiguous */
1706 }
5796c8dc
SS
1707
1708 if (*cmd == NULL)
cf7f2e2d 1709 return 0; /* nothing found */
5796c8dc 1710 else
cf7f2e2d
JM
1711 {
1712 if ((*cmd)->cmd_pointer)
1713 {
c50c785c
JM
1714 /* cmd was actually an alias, we note that an alias was
1715 used (by assigning *alais) and we set *cmd. */
cf7f2e2d
JM
1716 *alias = *cmd;
1717 *cmd = (*cmd)->cmd_pointer;
1718 }
1719 *prefix_cmd = prev_cmd;
1720 }
5796c8dc 1721 if ((*cmd)->prefixlist)
cf7f2e2d 1722 cur_list = *(*cmd)->prefixlist;
5796c8dc 1723 else
cf7f2e2d 1724 return 1;
5796c8dc
SS
1725
1726 text += len;
1727 }
1728}
1729
1730/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1731
1732/* Return a vector of char pointers which point to the different
c50c785c 1733 possible completions in LIST of TEXT.
5796c8dc
SS
1734
1735 WORD points in the same buffer as TEXT, and completions should be
c50c785c
JM
1736 returned relative to this position. For example, suppose TEXT is
1737 "foo" and we want to complete to "foobar". If WORD is "oo", return
5796c8dc
SS
1738 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1739
ef5ccd6c
JM
1740VEC (char_ptr) *
1741complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word,
1742 int ignore_help_classes)
5796c8dc
SS
1743{
1744 struct cmd_list_element *ptr;
ef5ccd6c 1745 VEC (char_ptr) *matchlist = NULL;
5796c8dc 1746 int textlen = strlen (text);
cf7f2e2d
JM
1747 int pass;
1748 int saw_deprecated_match = 0;
5796c8dc 1749
cf7f2e2d
JM
1750 /* We do one or two passes. In the first pass, we skip deprecated
1751 commands. If we see no matching commands in the first pass, and
1752 if we did happen to see a matching deprecated command, we do
1753 another loop to collect those. */
ef5ccd6c 1754 for (pass = 0; matchlist == 0 && pass < 2; ++pass)
cf7f2e2d
JM
1755 {
1756 for (ptr = list; ptr; ptr = ptr->next)
1757 if (!strncmp (ptr->name, text, textlen)
1758 && !ptr->abbrev_flag
ef5ccd6c 1759 && (!ignore_help_classes || ptr->func
cf7f2e2d 1760 || ptr->prefixlist))
5796c8dc 1761 {
ef5ccd6c
JM
1762 char *match;
1763
cf7f2e2d
JM
1764 if (pass == 0)
1765 {
1766 if ((ptr->flags & CMD_DEPRECATED) != 0)
1767 {
1768 saw_deprecated_match = 1;
1769 continue;
1770 }
1771 }
5796c8dc 1772
ef5ccd6c 1773 match = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1);
cf7f2e2d 1774 if (word == text)
ef5ccd6c 1775 strcpy (match, ptr->name);
cf7f2e2d
JM
1776 else if (word > text)
1777 {
1778 /* Return some portion of ptr->name. */
ef5ccd6c 1779 strcpy (match, ptr->name + (word - text));
cf7f2e2d
JM
1780 }
1781 else
1782 {
1783 /* Return some of text plus ptr->name. */
ef5ccd6c
JM
1784 strncpy (match, word, text - word);
1785 match[text - word] = '\0';
1786 strcat (match, ptr->name);
cf7f2e2d 1787 }
ef5ccd6c 1788 VEC_safe_push (char_ptr, matchlist, match);
5796c8dc 1789 }
cf7f2e2d
JM
1790 /* If we saw no matching deprecated commands in the first pass,
1791 just bail out. */
1792 if (!saw_deprecated_match)
1793 break;
1794 }
5796c8dc 1795
5796c8dc
SS
1796 return matchlist;
1797}
1798
1799/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
1800
1801/* Return a vector of char pointers which point to the different
c50c785c 1802 possible completions in CMD of TEXT.
5796c8dc
SS
1803
1804 WORD points in the same buffer as TEXT, and completions should be
1805 returned relative to this position. For example, suppose TEXT is "foo"
1806 and we want to complete to "foobar". If WORD is "oo", return
1807 "oobar"; if WORD is "baz/foo", return "baz/foobar". */
1808
ef5ccd6c
JM
1809VEC (char_ptr) *
1810complete_on_enum (const char *const *enumlist,
5796c8dc
SS
1811 char *text,
1812 char *word)
1813{
ef5ccd6c 1814 VEC (char_ptr) *matchlist = NULL;
5796c8dc
SS
1815 int textlen = strlen (text);
1816 int i;
1817 const char *name;
1818
5796c8dc
SS
1819 for (i = 0; (name = enumlist[i]) != NULL; i++)
1820 if (strncmp (name, text, textlen) == 0)
1821 {
ef5ccd6c 1822 char *match;
5796c8dc 1823
ef5ccd6c 1824 match = (char *) xmalloc (strlen (word) + strlen (name) + 1);
5796c8dc 1825 if (word == text)
ef5ccd6c 1826 strcpy (match, name);
5796c8dc
SS
1827 else if (word > text)
1828 {
1829 /* Return some portion of name. */
ef5ccd6c 1830 strcpy (match, name + (word - text));
5796c8dc
SS
1831 }
1832 else
1833 {
1834 /* Return some of text plus name. */
ef5ccd6c
JM
1835 strncpy (match, word, text - word);
1836 match[text - word] = '\0';
1837 strcat (match, name);
5796c8dc 1838 }
ef5ccd6c 1839 VEC_safe_push (char_ptr, matchlist, match);
5796c8dc
SS
1840 }
1841
5796c8dc
SS
1842 return matchlist;
1843}
1844
1845
c50c785c 1846/* Check function pointer. */
5796c8dc
SS
1847int
1848cmd_func_p (struct cmd_list_element *cmd)
1849{
1850 return (cmd->func != NULL);
1851}
1852
1853
c50c785c 1854/* Call the command function. */
5796c8dc
SS
1855void
1856cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
1857{
1858 if (cmd_func_p (cmd))
1859 (*cmd->func) (cmd, args, from_tty);
1860 else
1861 error (_("Invalid command"));
1862}