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