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