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