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