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