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