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