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