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