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