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