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