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