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