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