1 1.1 christos /* Handle lists of commands, their decoding and documentation, for GDB. 2 1.1 christos 3 1.11 christos Copyright (C) 1986-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This program is free software; you can redistribute it and/or modify 6 1.1 christos it under the terms of the GNU General Public License as published by 7 1.1 christos the Free Software Foundation; either version 3 of the License, or 8 1.1 christos (at your option) any later version. 9 1.1 christos 10 1.1 christos This program is distributed in the hope that it will be useful, 11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 1.1 christos GNU General Public License for more details. 14 1.1 christos 15 1.1 christos You should have received a copy of the GNU General Public License 16 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 1.1 christos 18 1.1 christos #include "symtab.h" 19 1.1 christos #include <ctype.h> 20 1.10 christos #include "gdbsupport/gdb_regex.h" 21 1.1 christos #include "completer.h" 22 1.1 christos #include "ui-out.h" 23 1.1 christos #include "cli/cli-cmds.h" 24 1.1 christos #include "cli/cli-decode.h" 25 1.9 christos #include "cli/cli-style.h" 26 1.11 christos #include <optional> 27 1.1 christos 28 1.1 christos /* Prototypes for local functions. */ 29 1.1 christos 30 1.1 christos static void undef_cmd_error (const char *, const char *); 31 1.1 christos 32 1.10 christos static cmd_list_element::aliases_list_type delete_cmd 33 1.10 christos (const char *name, cmd_list_element **list, cmd_list_element **prehook, 34 1.10 christos cmd_list_element **prehookee, cmd_list_element **posthook, 35 1.10 christos cmd_list_element **posthookee); 36 1.1 christos 37 1.1 christos static struct cmd_list_element *find_cmd (const char *command, 38 1.1 christos int len, 39 1.1 christos struct cmd_list_element *clist, 40 1.1 christos int ignore_help_classes, 41 1.1 christos int *nfound); 42 1.1 christos 43 1.9 christos static void help_cmd_list (struct cmd_list_element *list, 44 1.9 christos enum command_class theclass, 45 1.9 christos bool recurse, 46 1.9 christos struct ui_file *stream); 47 1.9 christos 48 1.1 christos static void help_all (struct ui_file *stream); 49 1.1 christos 50 1.10 christos static int lookup_cmd_composition_1 (const char *text, 51 1.10 christos struct cmd_list_element **alias, 52 1.10 christos struct cmd_list_element **prefix_cmd, 53 1.10 christos struct cmd_list_element **cmd, 54 1.10 christos struct cmd_list_element *cur_list); 55 1.10 christos 56 1.10 christos /* Look up a command whose 'subcommands' field is SUBCOMMANDS. Return the 57 1.10 christos command if found, otherwise return NULL. */ 58 1.1 christos 59 1.1 christos static struct cmd_list_element * 60 1.10 christos lookup_cmd_with_subcommands (cmd_list_element **subcommands, 61 1.10 christos cmd_list_element *list) 62 1.1 christos { 63 1.1 christos struct cmd_list_element *p = NULL; 64 1.1 christos 65 1.1 christos for (p = list; p != NULL; p = p->next) 66 1.1 christos { 67 1.1 christos struct cmd_list_element *q; 68 1.1 christos 69 1.10 christos if (!p->is_prefix ()) 70 1.1 christos continue; 71 1.10 christos 72 1.10 christos else if (p->subcommands == subcommands) 73 1.9 christos { 74 1.9 christos /* If we found an alias, we must return the aliased 75 1.9 christos command. */ 76 1.10 christos return p->is_alias () ? p->alias_target : p; 77 1.9 christos } 78 1.1 christos 79 1.10 christos q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands)); 80 1.1 christos if (q != NULL) 81 1.1 christos return q; 82 1.1 christos } 83 1.1 christos 84 1.1 christos return NULL; 85 1.1 christos } 86 1.1 christos 87 1.1 christos static void 88 1.10 christos print_help_for_command (const cmd_list_element &c, 89 1.9 christos bool recurse, struct ui_file *stream); 90 1.1 christos 91 1.1 christos static void 92 1.10 christos do_simple_func (const char *args, int from_tty, cmd_list_element *c) 93 1.1 christos { 94 1.10 christos c->function.simple_func (args, from_tty); 95 1.1 christos } 96 1.1 christos 97 1.8 christos static void 98 1.10 christos set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func) 99 1.1 christos { 100 1.10 christos if (simple_func == NULL) 101 1.1 christos cmd->func = NULL; 102 1.1 christos else 103 1.10 christos cmd->func = do_simple_func; 104 1.10 christos 105 1.10 christos cmd->function.simple_func = simple_func; 106 1.1 christos } 107 1.1 christos 108 1.10 christos int 109 1.10 christos cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func) 110 1.1 christos { 111 1.10 christos return (cmd->func == do_simple_func 112 1.10 christos && cmd->function.simple_func == simple_func); 113 1.1 christos } 114 1.1 christos 115 1.1 christos void 116 1.10 christos set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer) 117 1.1 christos { 118 1.10 christos cmd->completer = completer; /* Ok. */ 119 1.1 christos } 120 1.1 christos 121 1.10 christos /* See definition in commands.h. */ 122 1.1 christos 123 1.1 christos void 124 1.10 christos set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd, 125 1.10 christos completer_handle_brkchars_ftype *func) 126 1.1 christos { 127 1.10 christos cmd->completer_handle_brkchars = func; 128 1.1 christos } 129 1.1 christos 130 1.10 christos std::string 131 1.10 christos cmd_list_element::prefixname () const 132 1.1 christos { 133 1.10 christos if (!this->is_prefix ()) 134 1.10 christos /* Not a prefix command. */ 135 1.10 christos return ""; 136 1.10 christos 137 1.10 christos std::string prefixname; 138 1.10 christos if (this->prefix != nullptr) 139 1.10 christos prefixname = this->prefix->prefixname (); 140 1.10 christos 141 1.10 christos prefixname += this->name; 142 1.10 christos prefixname += " "; 143 1.10 christos 144 1.10 christos return prefixname; 145 1.1 christos } 146 1.1 christos 147 1.10 christos /* See cli/cli-decode.h. */ 148 1.10 christos 149 1.10 christos std::vector<std::string> 150 1.10 christos cmd_list_element::command_components () const 151 1.1 christos { 152 1.10 christos std::vector<std::string> result; 153 1.1 christos 154 1.10 christos if (this->prefix != nullptr) 155 1.10 christos result = this->prefix->command_components (); 156 1.3 christos 157 1.11 christos result.emplace_back (this->name); 158 1.10 christos return result; 159 1.3 christos } 160 1.3 christos 161 1.1 christos /* Add element named NAME. 162 1.1 christos Space for NAME and DOC must be allocated by the caller. 163 1.11 christos THECLASS is the top level category into which commands are broken down 164 1.1 christos for "help" purposes. 165 1.1 christos FUN should be the function to execute the command; 166 1.1 christos it will get a character string as argument, with leading 167 1.1 christos and trailing blanks already eliminated. 168 1.1 christos 169 1.1 christos DOC is a documentation string for the command. 170 1.1 christos Its first line should be a complete sentence. 171 1.1 christos It should start with ? for a command that is an abbreviation 172 1.1 christos or with * for a command that most users don't need to know about. 173 1.1 christos 174 1.1 christos Add this command to command list *LIST. 175 1.1 christos 176 1.1 christos Returns a pointer to the added command (not necessarily the head 177 1.1 christos of *LIST). */ 178 1.1 christos 179 1.8 christos static struct cmd_list_element * 180 1.8 christos do_add_cmd (const char *name, enum command_class theclass, 181 1.8 christos const char *doc, struct cmd_list_element **list) 182 1.1 christos { 183 1.8 christos struct cmd_list_element *c = new struct cmd_list_element (name, theclass, 184 1.8 christos doc); 185 1.1 christos 186 1.1 christos /* Turn each alias of the old command into an alias of the new 187 1.1 christos command. */ 188 1.1 christos c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre, 189 1.1 christos &c->hook_post, &c->hookee_post); 190 1.10 christos 191 1.10 christos for (cmd_list_element &alias : c->aliases) 192 1.10 christos alias.alias_target = c; 193 1.10 christos 194 1.1 christos if (c->hook_pre) 195 1.1 christos c->hook_pre->hookee_pre = c; 196 1.10 christos 197 1.1 christos if (c->hookee_pre) 198 1.1 christos c->hookee_pre->hook_pre = c; 199 1.10 christos 200 1.1 christos if (c->hook_post) 201 1.1 christos c->hook_post->hookee_post = c; 202 1.10 christos 203 1.1 christos if (c->hookee_post) 204 1.1 christos c->hookee_post->hook_post = c; 205 1.1 christos 206 1.1 christos if (*list == NULL || strcmp ((*list)->name, name) >= 0) 207 1.1 christos { 208 1.1 christos c->next = *list; 209 1.1 christos *list = c; 210 1.1 christos } 211 1.1 christos else 212 1.1 christos { 213 1.10 christos cmd_list_element *p = *list; 214 1.1 christos while (p->next && strcmp (p->next->name, name) <= 0) 215 1.1 christos { 216 1.1 christos p = p->next; 217 1.1 christos } 218 1.1 christos c->next = p->next; 219 1.1 christos p->next = c; 220 1.1 christos } 221 1.1 christos 222 1.9 christos /* Search the prefix cmd of C, and assigns it to C->prefix. 223 1.9 christos See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */ 224 1.10 christos cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist); 225 1.9 christos c->prefix = prefixcmd; 226 1.9 christos 227 1.9 christos 228 1.8 christos return c; 229 1.8 christos } 230 1.8 christos 231 1.8 christos struct cmd_list_element * 232 1.8 christos add_cmd (const char *name, enum command_class theclass, 233 1.8 christos const char *doc, struct cmd_list_element **list) 234 1.8 christos { 235 1.8 christos cmd_list_element *result = do_add_cmd (name, theclass, doc, list); 236 1.8 christos result->func = NULL; 237 1.10 christos result->function.simple_func = NULL; 238 1.8 christos return result; 239 1.8 christos } 240 1.8 christos 241 1.8 christos struct cmd_list_element * 242 1.8 christos add_cmd (const char *name, enum command_class theclass, 243 1.10 christos cmd_simple_func_ftype *fun, 244 1.8 christos const char *doc, struct cmd_list_element **list) 245 1.8 christos { 246 1.8 christos cmd_list_element *result = do_add_cmd (name, theclass, doc, list); 247 1.10 christos set_cmd_simple_func (result, fun); 248 1.8 christos return result; 249 1.8 christos } 250 1.8 christos 251 1.8 christos /* Add an element with a suppress notification to the LIST of commands. */ 252 1.1 christos 253 1.8 christos struct cmd_list_element * 254 1.8 christos add_cmd_suppress_notification (const char *name, enum command_class theclass, 255 1.10 christos cmd_simple_func_ftype *fun, const char *doc, 256 1.8 christos struct cmd_list_element **list, 257 1.10 christos bool *suppress_notification) 258 1.8 christos { 259 1.8 christos struct cmd_list_element *element; 260 1.8 christos 261 1.8 christos element = add_cmd (name, theclass, fun, doc, list); 262 1.8 christos element->suppress_notification = suppress_notification; 263 1.8 christos 264 1.8 christos return element; 265 1.1 christos } 266 1.1 christos 267 1.8 christos 268 1.1 christos /* Deprecates a command CMD. 269 1.1 christos REPLACEMENT is the name of the command which should be used in 270 1.1 christos place of this command, or NULL if no such command exists. 271 1.1 christos 272 1.1 christos This function does not check to see if command REPLACEMENT exists 273 1.1 christos since gdb may not have gotten around to adding REPLACEMENT when 274 1.1 christos this function is called. 275 1.1 christos 276 1.1 christos Returns a pointer to the deprecated command. */ 277 1.1 christos 278 1.1 christos struct cmd_list_element * 279 1.3 christos deprecate_cmd (struct cmd_list_element *cmd, const char *replacement) 280 1.1 christos { 281 1.3 christos cmd->cmd_deprecated = 1; 282 1.3 christos cmd->deprecated_warn_user = 1; 283 1.1 christos 284 1.1 christos if (replacement != NULL) 285 1.1 christos cmd->replacement = replacement; 286 1.1 christos else 287 1.1 christos cmd->replacement = NULL; 288 1.1 christos 289 1.1 christos return cmd; 290 1.1 christos } 291 1.1 christos 292 1.1 christos struct cmd_list_element * 293 1.10 christos add_alias_cmd (const char *name, cmd_list_element *target, 294 1.7 christos enum command_class theclass, int abbrev_flag, 295 1.7 christos struct cmd_list_element **list) 296 1.1 christos { 297 1.10 christos gdb_assert (target != nullptr); 298 1.1 christos 299 1.10 christos struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list); 300 1.1 christos 301 1.10 christos /* If TARGET->DOC can be freed, we should make another copy. */ 302 1.10 christos if (target->doc_allocated) 303 1.1 christos { 304 1.10 christos c->doc = xstrdup (target->doc); 305 1.3 christos c->doc_allocated = 1; 306 1.1 christos } 307 1.1 christos /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */ 308 1.10 christos c->func = target->func; 309 1.10 christos c->function = target->function; 310 1.10 christos c->subcommands = target->subcommands; 311 1.10 christos c->allow_unknown = target->allow_unknown; 312 1.1 christos c->abbrev_flag = abbrev_flag; 313 1.10 christos c->alias_target = target; 314 1.10 christos target->aliases.push_front (*c); 315 1.1 christos 316 1.1 christos return c; 317 1.1 christos } 318 1.1 christos 319 1.9 christos /* Update the prefix field of all sub-commands of the prefix command C. 320 1.9 christos We must do this when a prefix command is defined as the GDB init sequence 321 1.9 christos does not guarantee that a prefix command is created before its sub-commands. 322 1.9 christos For example, break-catch-sig.c initialization runs before breakpoint.c 323 1.9 christos initialization, but it is breakpoint.c that creates the "catch" command used 324 1.9 christos by the "catch signal" command created by break-catch-sig.c. */ 325 1.9 christos 326 1.9 christos static void 327 1.9 christos update_prefix_field_of_prefixed_commands (struct cmd_list_element *c) 328 1.9 christos { 329 1.10 christos for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next) 330 1.9 christos { 331 1.9 christos p->prefix = c; 332 1.9 christos 333 1.9 christos /* We must recursively update the prefix field to cover 334 1.9 christos e.g. 'info auto-load libthread-db' where the creation 335 1.9 christos order was: 336 1.10 christos libthread-db 337 1.10 christos auto-load 338 1.10 christos info 339 1.9 christos In such a case, when 'auto-load' was created by do_add_cmd, 340 1.10 christos the 'libthread-db' prefix field could not be updated, as the 341 1.9 christos 'auto-load' command was not yet reachable by 342 1.10 christos lookup_cmd_for_subcommands (list, cmdlist) 343 1.9 christos that searches from the top level 'cmdlist'. */ 344 1.10 christos if (p->is_prefix ()) 345 1.9 christos update_prefix_field_of_prefixed_commands (p); 346 1.9 christos } 347 1.9 christos } 348 1.9 christos 349 1.9 christos 350 1.1 christos /* Like add_cmd but adds an element for a command prefix: a name that 351 1.1 christos should be followed by a subcommand to be looked up in another 352 1.10 christos command list. SUBCOMMANDS should be the address of the variable 353 1.1 christos containing that list. */ 354 1.1 christos 355 1.1 christos struct cmd_list_element * 356 1.5 christos add_prefix_cmd (const char *name, enum command_class theclass, 357 1.10 christos cmd_simple_func_ftype *fun, 358 1.10 christos const char *doc, struct cmd_list_element **subcommands, 359 1.10 christos int allow_unknown, struct cmd_list_element **list) 360 1.1 christos { 361 1.5 christos struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list); 362 1.1 christos 363 1.10 christos c->subcommands = subcommands; 364 1.1 christos c->allow_unknown = allow_unknown; 365 1.1 christos 366 1.9 christos /* Now that prefix command C is defined, we need to set the prefix field 367 1.9 christos of all prefixed commands that were defined before C itself was defined. */ 368 1.9 christos update_prefix_field_of_prefixed_commands (c); 369 1.9 christos 370 1.9 christos return c; 371 1.9 christos } 372 1.9 christos 373 1.9 christos /* A helper function for add_basic_prefix_cmd. This is a command 374 1.9 christos function that just forwards to help_list. */ 375 1.9 christos 376 1.9 christos static void 377 1.9 christos do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c) 378 1.9 christos { 379 1.9 christos /* Look past all aliases. */ 380 1.10 christos while (c->is_alias ()) 381 1.10 christos c = c->alias_target; 382 1.9 christos 383 1.10 christos help_list (*c->subcommands, c->prefixname ().c_str (), 384 1.10 christos all_commands, gdb_stdout); 385 1.9 christos } 386 1.9 christos 387 1.9 christos /* See command.h. */ 388 1.9 christos 389 1.9 christos struct cmd_list_element * 390 1.9 christos add_basic_prefix_cmd (const char *name, enum command_class theclass, 391 1.10 christos const char *doc, struct cmd_list_element **subcommands, 392 1.10 christos int allow_unknown, struct cmd_list_element **list) 393 1.9 christos { 394 1.9 christos struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr, 395 1.10 christos doc, subcommands, 396 1.9 christos allow_unknown, list); 397 1.10 christos cmd->func = do_prefix_cmd; 398 1.9 christos return cmd; 399 1.9 christos } 400 1.9 christos 401 1.9 christos /* A helper function for add_show_prefix_cmd. This is a command 402 1.9 christos function that just forwards to cmd_show_list. */ 403 1.9 christos 404 1.9 christos static void 405 1.9 christos do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c) 406 1.9 christos { 407 1.10 christos cmd_show_list (*c->subcommands, from_tty); 408 1.9 christos } 409 1.1 christos 410 1.9 christos /* See command.h. */ 411 1.1 christos 412 1.9 christos struct cmd_list_element * 413 1.9 christos add_show_prefix_cmd (const char *name, enum command_class theclass, 414 1.10 christos const char *doc, struct cmd_list_element **subcommands, 415 1.10 christos int allow_unknown, struct cmd_list_element **list) 416 1.9 christos { 417 1.9 christos struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr, 418 1.10 christos doc, subcommands, 419 1.9 christos allow_unknown, list); 420 1.10 christos cmd->func = do_show_prefix_cmd; 421 1.9 christos return cmd; 422 1.1 christos } 423 1.1 christos 424 1.10 christos /* See command.h. */ 425 1.10 christos 426 1.10 christos set_show_commands 427 1.10 christos add_setshow_prefix_cmd (const char *name, command_class theclass, 428 1.10 christos const char *set_doc, const char *show_doc, 429 1.10 christos cmd_list_element **set_subcommands_list, 430 1.10 christos cmd_list_element **show_subcommands_list, 431 1.10 christos cmd_list_element **set_list, 432 1.10 christos cmd_list_element **show_list) 433 1.10 christos { 434 1.10 christos set_show_commands cmds; 435 1.10 christos 436 1.10 christos cmds.set = add_basic_prefix_cmd (name, theclass, set_doc, 437 1.10 christos set_subcommands_list, 0, 438 1.10 christos set_list); 439 1.10 christos cmds.show = add_show_prefix_cmd (name, theclass, show_doc, 440 1.10 christos show_subcommands_list, 0, 441 1.10 christos show_list); 442 1.10 christos 443 1.10 christos return cmds; 444 1.10 christos } 445 1.10 christos 446 1.8 christos /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the 447 1.8 christos new command list element. */ 448 1.8 christos 449 1.8 christos struct cmd_list_element * 450 1.8 christos add_prefix_cmd_suppress_notification 451 1.10 christos (const char *name, enum command_class theclass, 452 1.10 christos cmd_simple_func_ftype *fun, 453 1.10 christos const char *doc, struct cmd_list_element **subcommands, 454 1.10 christos int allow_unknown, struct cmd_list_element **list, 455 1.10 christos bool *suppress_notification) 456 1.8 christos { 457 1.8 christos struct cmd_list_element *element 458 1.10 christos = add_prefix_cmd (name, theclass, fun, doc, subcommands, 459 1.10 christos allow_unknown, list); 460 1.8 christos element->suppress_notification = suppress_notification; 461 1.8 christos return element; 462 1.8 christos } 463 1.8 christos 464 1.1 christos /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */ 465 1.1 christos 466 1.1 christos struct cmd_list_element * 467 1.5 christos add_abbrev_prefix_cmd (const char *name, enum command_class theclass, 468 1.10 christos cmd_simple_func_ftype *fun, const char *doc, 469 1.10 christos struct cmd_list_element **subcommands, 470 1.1 christos int allow_unknown, struct cmd_list_element **list) 471 1.1 christos { 472 1.5 christos struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list); 473 1.1 christos 474 1.10 christos c->subcommands = subcommands; 475 1.1 christos c->allow_unknown = allow_unknown; 476 1.1 christos c->abbrev_flag = 1; 477 1.1 christos return c; 478 1.1 christos } 479 1.1 christos 480 1.10 christos /* This is an empty "simple func". */ 481 1.1 christos void 482 1.8 christos not_just_help_class_command (const char *args, int from_tty) 483 1.1 christos { 484 1.1 christos } 485 1.1 christos 486 1.10 christos /* This is an empty cmd func. */ 487 1.1 christos 488 1.1 christos static void 489 1.10 christos empty_func (const char *args, int from_tty, cmd_list_element *c) 490 1.1 christos { 491 1.1 christos } 492 1.1 christos 493 1.1 christos /* Add element named NAME to command list LIST (the list for set/show 494 1.1 christos or some sublist thereof). 495 1.1 christos TYPE is set_cmd or show_cmd. 496 1.11 christos THECLASS is as in add_cmd. 497 1.1 christos VAR_TYPE is the kind of thing we are setting. 498 1.11 christos EXTRA_LITERALS if non-NULL define extra literals to be accepted in lieu of 499 1.11 christos a number for integer variables. 500 1.11 christos ARGS is a pre-validated type-erased reference to the variable being 501 1.11 christos controlled by this command. 502 1.1 christos DOC is the documentation string. */ 503 1.1 christos 504 1.1 christos static struct cmd_list_element * 505 1.1 christos add_set_or_show_cmd (const char *name, 506 1.1 christos enum cmd_types type, 507 1.5 christos enum command_class theclass, 508 1.1 christos var_types var_type, 509 1.11 christos const literal_def *extra_literals, 510 1.10 christos const setting::erased_args &arg, 511 1.3 christos const char *doc, 512 1.1 christos struct cmd_list_element **list) 513 1.1 christos { 514 1.8 christos struct cmd_list_element *c = add_cmd (name, theclass, doc, list); 515 1.1 christos 516 1.1 christos gdb_assert (type == set_cmd || type == show_cmd); 517 1.1 christos c->type = type; 518 1.11 christos c->var.emplace (var_type, extra_literals, arg); 519 1.10 christos 520 1.1 christos /* This needs to be something besides NULL so that this isn't 521 1.1 christos treated as a help class. */ 522 1.10 christos c->func = empty_func; 523 1.1 christos return c; 524 1.1 christos } 525 1.1 christos 526 1.11 christos /* Add element named NAME to both command lists SET_LIST and SHOW_LIST. 527 1.11 christos THECLASS is as in add_cmd. VAR_TYPE is the kind of thing we are 528 1.11 christos setting. EXTRA_LITERALS if non-NULL define extra literals to be 529 1.11 christos accepted in lieu of a number for integer variables. ARGS is a 530 1.11 christos pre-validated type-erased reference to the variable being controlled 531 1.11 christos by this command. SET_FUNC and SHOW_FUNC are the callback functions 532 1.11 christos (if non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation 533 1.11 christos strings. 534 1.10 christos 535 1.10 christos Return the newly created set and show commands. */ 536 1.10 christos 537 1.10 christos static set_show_commands 538 1.10 christos add_setshow_cmd_full_erased (const char *name, 539 1.10 christos enum command_class theclass, 540 1.10 christos var_types var_type, 541 1.11 christos const literal_def *extra_literals, 542 1.10 christos const setting::erased_args &args, 543 1.10 christos const char *set_doc, const char *show_doc, 544 1.10 christos const char *help_doc, 545 1.10 christos cmd_func_ftype *set_func, 546 1.10 christos show_value_ftype *show_func, 547 1.10 christos struct cmd_list_element **set_list, 548 1.10 christos struct cmd_list_element **show_list) 549 1.1 christos { 550 1.1 christos struct cmd_list_element *set; 551 1.1 christos struct cmd_list_element *show; 552 1.10 christos gdb::unique_xmalloc_ptr<char> full_set_doc; 553 1.10 christos gdb::unique_xmalloc_ptr<char> full_show_doc; 554 1.1 christos 555 1.1 christos if (help_doc != NULL) 556 1.1 christos { 557 1.1 christos full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc); 558 1.1 christos full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc); 559 1.1 christos } 560 1.1 christos else 561 1.1 christos { 562 1.10 christos full_set_doc = make_unique_xstrdup (set_doc); 563 1.10 christos full_show_doc = make_unique_xstrdup (show_doc); 564 1.1 christos } 565 1.11 christos set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, 566 1.11 christos extra_literals, args, 567 1.10 christos full_set_doc.release (), set_list); 568 1.3 christos set->doc_allocated = 1; 569 1.1 christos 570 1.1 christos if (set_func != NULL) 571 1.10 christos set->func = set_func; 572 1.1 christos 573 1.11 christos show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, 574 1.11 christos extra_literals, args, 575 1.10 christos full_show_doc.release (), show_list); 576 1.3 christos show->doc_allocated = 1; 577 1.1 christos show->show_value_func = show_func; 578 1.9 christos /* Disable the default symbol completer. Doesn't make much sense 579 1.9 christos for the "show" command to complete on anything. */ 580 1.9 christos set_cmd_completer (show, nullptr); 581 1.1 christos 582 1.10 christos return {set, show}; 583 1.10 christos } 584 1.10 christos 585 1.11 christos /* Completes on integer commands that support extra literals. */ 586 1.11 christos 587 1.11 christos static void 588 1.11 christos integer_literals_completer (struct cmd_list_element *c, 589 1.11 christos completion_tracker &tracker, 590 1.11 christos const char *text, const char *word) 591 1.11 christos { 592 1.11 christos const literal_def *extra_literals = c->var->extra_literals (); 593 1.11 christos 594 1.11 christos if (*text == '\0') 595 1.11 christos { 596 1.11 christos tracker.add_completion (make_unique_xstrdup ("NUMBER")); 597 1.11 christos for (const literal_def *l = extra_literals; 598 1.11 christos l->literal != nullptr; 599 1.11 christos l++) 600 1.11 christos tracker.add_completion (make_unique_xstrdup (l->literal)); 601 1.11 christos } 602 1.11 christos else 603 1.11 christos for (const literal_def *l = extra_literals; 604 1.11 christos l->literal != nullptr; 605 1.11 christos l++) 606 1.11 christos if (startswith (l->literal, text)) 607 1.11 christos tracker.add_completion (make_unique_xstrdup (l->literal)); 608 1.11 christos } 609 1.11 christos 610 1.11 christos /* Add element named NAME to both command lists SET_LIST and SHOW_LIST. 611 1.11 christos THECLASS is as in add_cmd. VAR_TYPE is the kind of thing we are 612 1.11 christos setting. VAR is address of the variable being controlled by this 613 1.11 christos command. EXTRA_LITERALS if non-NULL define extra literals to be 614 1.11 christos accepted in lieu of a number for integer variables. If nullptr is 615 1.11 christos given as VAR, then both SET_SETTING_FUNC and GET_SETTING_FUNC must 616 1.11 christos be provided. SET_SETTING_FUNC and GET_SETTING_FUNC are callbacks 617 1.11 christos used to access and modify the underlying property, whatever its 618 1.11 christos storage is. SET_FUNC and SHOW_FUNC are the callback functions 619 1.11 christos (if non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the 620 1.11 christos documentation strings. 621 1.11 christos 622 1.11 christos Return the newly created set and show commands. */ 623 1.11 christos 624 1.10 christos template<typename T> 625 1.10 christos static set_show_commands 626 1.10 christos add_setshow_cmd_full (const char *name, 627 1.10 christos enum command_class theclass, 628 1.10 christos var_types var_type, T *var, 629 1.11 christos const literal_def *extra_literals, 630 1.10 christos const char *set_doc, const char *show_doc, 631 1.10 christos const char *help_doc, 632 1.10 christos typename setting_func_types<T>::set set_setting_func, 633 1.10 christos typename setting_func_types<T>::get get_setting_func, 634 1.10 christos cmd_func_ftype *set_func, 635 1.10 christos show_value_ftype *show_func, 636 1.10 christos struct cmd_list_element **set_list, 637 1.10 christos struct cmd_list_element **show_list) 638 1.10 christos { 639 1.10 christos auto erased_args 640 1.10 christos = setting::erase_args (var_type, var, 641 1.10 christos set_setting_func, get_setting_func); 642 1.11 christos auto cmds = add_setshow_cmd_full_erased (name, 643 1.11 christos theclass, 644 1.11 christos var_type, extra_literals, 645 1.11 christos erased_args, 646 1.11 christos set_doc, show_doc, 647 1.11 christos help_doc, 648 1.11 christos set_func, 649 1.11 christos show_func, 650 1.11 christos set_list, 651 1.11 christos show_list); 652 1.11 christos 653 1.11 christos if (extra_literals != nullptr) 654 1.11 christos set_cmd_completer (cmds.set, integer_literals_completer); 655 1.10 christos 656 1.11 christos return cmds; 657 1.11 christos } 658 1.11 christos 659 1.11 christos /* Same as above but omitting EXTRA_LITERALS. */ 660 1.11 christos 661 1.11 christos template<typename T> 662 1.11 christos static set_show_commands 663 1.11 christos add_setshow_cmd_full (const char *name, 664 1.11 christos enum command_class theclass, 665 1.11 christos var_types var_type, T *var, 666 1.11 christos const char *set_doc, const char *show_doc, 667 1.11 christos const char *help_doc, 668 1.11 christos typename setting_func_types<T>::set set_setting_func, 669 1.11 christos typename setting_func_types<T>::get get_setting_func, 670 1.11 christos cmd_func_ftype *set_func, 671 1.11 christos show_value_ftype *show_func, 672 1.11 christos struct cmd_list_element **set_list, 673 1.11 christos struct cmd_list_element **show_list) 674 1.11 christos { 675 1.11 christos return add_setshow_cmd_full (name, theclass, var_type, var, nullptr, 676 1.11 christos set_doc, show_doc, help_doc, 677 1.11 christos set_setting_func, get_setting_func, 678 1.11 christos set_func, show_func, set_list, show_list); 679 1.1 christos } 680 1.1 christos 681 1.1 christos /* Add element named NAME to command list LIST (the list for set or 682 1.11 christos some sublist thereof). THECLASS is as in add_cmd. ENUMLIST is a list 683 1.1 christos of strings which may follow NAME. VAR is address of the variable 684 1.1 christos which will contain the matching string (from ENUMLIST). */ 685 1.1 christos 686 1.10 christos set_show_commands 687 1.1 christos add_setshow_enum_cmd (const char *name, 688 1.5 christos enum command_class theclass, 689 1.1 christos const char *const *enumlist, 690 1.1 christos const char **var, 691 1.1 christos const char *set_doc, 692 1.1 christos const char *show_doc, 693 1.1 christos const char *help_doc, 694 1.10 christos cmd_func_ftype *set_func, 695 1.1 christos show_value_ftype *show_func, 696 1.1 christos struct cmd_list_element **set_list, 697 1.10 christos struct cmd_list_element **show_list) 698 1.10 christos { 699 1.10 christos /* We require *VAR to be initialized before this call, and 700 1.10 christos furthermore it must be == to one of the values in ENUMLIST. */ 701 1.10 christos gdb_assert (var != nullptr && *var != nullptr); 702 1.10 christos for (int i = 0; ; ++i) 703 1.10 christos { 704 1.10 christos gdb_assert (enumlist[i] != nullptr); 705 1.10 christos if (*var == enumlist[i]) 706 1.10 christos break; 707 1.10 christos } 708 1.10 christos 709 1.10 christos set_show_commands commands 710 1.10 christos = add_setshow_cmd_full<const char *> (name, theclass, var_enum, var, 711 1.10 christos set_doc, show_doc, help_doc, 712 1.10 christos nullptr, nullptr, set_func, 713 1.10 christos show_func, set_list, show_list); 714 1.10 christos commands.set->enums = enumlist; 715 1.10 christos return commands; 716 1.10 christos } 717 1.10 christos 718 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 719 1.10 christos to a global storage buffer. */ 720 1.10 christos 721 1.10 christos set_show_commands 722 1.10 christos add_setshow_enum_cmd (const char *name, command_class theclass, 723 1.10 christos const char *const *enumlist, const char *set_doc, 724 1.10 christos const char *show_doc, const char *help_doc, 725 1.10 christos setting_func_types<const char *>::set set_func, 726 1.10 christos setting_func_types<const char *>::get get_func, 727 1.10 christos show_value_ftype *show_func, 728 1.10 christos cmd_list_element **set_list, 729 1.10 christos cmd_list_element **show_list) 730 1.1 christos { 731 1.10 christos auto cmds = add_setshow_cmd_full<const char *> (name, theclass, var_enum, 732 1.10 christos nullptr, set_doc, show_doc, 733 1.10 christos help_doc, set_func, get_func, 734 1.10 christos nullptr, show_func, set_list, 735 1.10 christos show_list); 736 1.1 christos 737 1.10 christos cmds.set->enums = enumlist; 738 1.8 christos 739 1.10 christos return cmds; 740 1.1 christos } 741 1.1 christos 742 1.9 christos /* See cli-decode.h. */ 743 1.1 christos const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL }; 744 1.1 christos 745 1.1 christos /* Add an auto-boolean command named NAME to both the set and show 746 1.11 christos command list lists. THECLASS is as in add_cmd. VAR is address of the 747 1.1 christos variable which will contain the value. DOC is the documentation 748 1.1 christos string. FUNC is the corresponding callback. */ 749 1.10 christos 750 1.10 christos set_show_commands 751 1.1 christos add_setshow_auto_boolean_cmd (const char *name, 752 1.5 christos enum command_class theclass, 753 1.1 christos enum auto_boolean *var, 754 1.1 christos const char *set_doc, const char *show_doc, 755 1.1 christos const char *help_doc, 756 1.10 christos cmd_func_ftype *set_func, 757 1.1 christos show_value_ftype *show_func, 758 1.1 christos struct cmd_list_element **set_list, 759 1.1 christos struct cmd_list_element **show_list) 760 1.1 christos { 761 1.10 christos set_show_commands commands 762 1.10 christos = add_setshow_cmd_full<enum auto_boolean> (name, theclass, var_auto_boolean, 763 1.10 christos var, set_doc, show_doc, help_doc, 764 1.10 christos nullptr, nullptr, set_func, 765 1.10 christos show_func, set_list, show_list); 766 1.10 christos 767 1.10 christos commands.set->enums = auto_boolean_enums; 768 1.10 christos 769 1.10 christos return commands; 770 1.10 christos } 771 1.10 christos 772 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 773 1.10 christos to a global storage buffer. */ 774 1.10 christos 775 1.10 christos set_show_commands 776 1.10 christos add_setshow_auto_boolean_cmd (const char *name, command_class theclass, 777 1.10 christos const char *set_doc, const char *show_doc, 778 1.10 christos const char *help_doc, 779 1.10 christos setting_func_types<enum auto_boolean>::set set_func, 780 1.10 christos setting_func_types<enum auto_boolean>::get get_func, 781 1.10 christos show_value_ftype *show_func, 782 1.10 christos cmd_list_element **set_list, 783 1.10 christos cmd_list_element **show_list) 784 1.10 christos { 785 1.10 christos auto cmds = add_setshow_cmd_full<enum auto_boolean> (name, theclass, 786 1.10 christos var_auto_boolean, 787 1.10 christos nullptr, set_doc, 788 1.10 christos show_doc, help_doc, 789 1.10 christos set_func, get_func, 790 1.10 christos nullptr, show_func, 791 1.10 christos set_list, show_list); 792 1.10 christos 793 1.10 christos cmds.set->enums = auto_boolean_enums; 794 1.1 christos 795 1.10 christos return cmds; 796 1.1 christos } 797 1.1 christos 798 1.9 christos /* See cli-decode.h. */ 799 1.9 christos const char * const boolean_enums[] = { "on", "off", NULL }; 800 1.9 christos 801 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 802 1.11 christos list for set/show or some sublist thereof). THECLASS is as in 803 1.1 christos add_cmd. VAR is address of the variable which will contain the 804 1.9 christos value. SET_DOC and SHOW_DOC are the documentation strings. 805 1.9 christos Returns the new command element. */ 806 1.9 christos 807 1.10 christos set_show_commands 808 1.9 christos add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var, 809 1.1 christos const char *set_doc, const char *show_doc, 810 1.1 christos const char *help_doc, 811 1.10 christos cmd_func_ftype *set_func, 812 1.1 christos show_value_ftype *show_func, 813 1.1 christos struct cmd_list_element **set_list, 814 1.1 christos struct cmd_list_element **show_list) 815 1.1 christos { 816 1.10 christos set_show_commands commands 817 1.10 christos = add_setshow_cmd_full<bool> (name, theclass, var_boolean, var, 818 1.10 christos set_doc, show_doc, help_doc, 819 1.10 christos nullptr, nullptr, set_func, show_func, 820 1.10 christos set_list, show_list); 821 1.10 christos 822 1.10 christos commands.set->enums = boolean_enums; 823 1.10 christos 824 1.10 christos return commands; 825 1.10 christos } 826 1.10 christos 827 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 828 1.10 christos to a global storage buffer. */ 829 1.10 christos 830 1.10 christos set_show_commands 831 1.10 christos add_setshow_boolean_cmd (const char *name, command_class theclass, 832 1.10 christos const char *set_doc, const char *show_doc, 833 1.10 christos const char *help_doc, 834 1.10 christos setting_func_types<bool>::set set_func, 835 1.10 christos setting_func_types<bool>::get get_func, 836 1.10 christos show_value_ftype *show_func, 837 1.10 christos cmd_list_element **set_list, 838 1.10 christos cmd_list_element **show_list) 839 1.10 christos { 840 1.10 christos auto cmds = add_setshow_cmd_full<bool> (name, theclass, var_boolean, nullptr, 841 1.10 christos set_doc, show_doc, help_doc, 842 1.10 christos set_func, get_func, nullptr, 843 1.10 christos show_func, set_list, show_list); 844 1.1 christos 845 1.10 christos cmds.set->enums = boolean_enums; 846 1.9 christos 847 1.10 christos return cmds; 848 1.1 christos } 849 1.1 christos 850 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 851 1.1 christos list for set/show or some sublist thereof). */ 852 1.10 christos 853 1.10 christos set_show_commands 854 1.5 christos add_setshow_filename_cmd (const char *name, enum command_class theclass, 855 1.10 christos std::string *var, 856 1.1 christos const char *set_doc, const char *show_doc, 857 1.1 christos const char *help_doc, 858 1.10 christos cmd_func_ftype *set_func, 859 1.1 christos show_value_ftype *show_func, 860 1.1 christos struct cmd_list_element **set_list, 861 1.1 christos struct cmd_list_element **show_list) 862 1.1 christos { 863 1.10 christos set_show_commands commands 864 1.10 christos = add_setshow_cmd_full<std::string> (name, theclass, var_filename, var, 865 1.10 christos set_doc, show_doc, help_doc, 866 1.10 christos nullptr, nullptr, set_func, 867 1.10 christos show_func, set_list, show_list); 868 1.10 christos 869 1.12 christos set_cmd_completer (commands.set, deprecated_filename_completer); 870 1.10 christos 871 1.10 christos return commands; 872 1.10 christos } 873 1.10 christos 874 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 875 1.10 christos to a global storage buffer. */ 876 1.10 christos 877 1.10 christos set_show_commands 878 1.10 christos add_setshow_filename_cmd (const char *name, command_class theclass, 879 1.10 christos const char *set_doc, const char *show_doc, 880 1.10 christos const char *help_doc, 881 1.10 christos setting_func_types<std::string>::set set_func, 882 1.10 christos setting_func_types<std::string>::get get_func, 883 1.10 christos show_value_ftype *show_func, 884 1.10 christos cmd_list_element **set_list, 885 1.10 christos cmd_list_element **show_list) 886 1.10 christos { 887 1.10 christos auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_filename, 888 1.10 christos nullptr, set_doc, show_doc, 889 1.10 christos help_doc, set_func, get_func, 890 1.10 christos nullptr, show_func, set_list, 891 1.10 christos show_list); 892 1.1 christos 893 1.12 christos set_cmd_completer (cmds.set, deprecated_filename_completer); 894 1.10 christos 895 1.10 christos return cmds; 896 1.1 christos } 897 1.1 christos 898 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 899 1.1 christos list for set/show or some sublist thereof). */ 900 1.10 christos 901 1.10 christos set_show_commands 902 1.5 christos add_setshow_string_cmd (const char *name, enum command_class theclass, 903 1.10 christos std::string *var, 904 1.1 christos const char *set_doc, const char *show_doc, 905 1.1 christos const char *help_doc, 906 1.10 christos cmd_func_ftype *set_func, 907 1.1 christos show_value_ftype *show_func, 908 1.1 christos struct cmd_list_element **set_list, 909 1.1 christos struct cmd_list_element **show_list) 910 1.1 christos { 911 1.10 christos set_show_commands commands 912 1.10 christos = add_setshow_cmd_full<std::string> (name, theclass, var_string, var, 913 1.10 christos set_doc, show_doc, help_doc, 914 1.10 christos nullptr, nullptr, set_func, 915 1.10 christos show_func, set_list, show_list); 916 1.10 christos 917 1.10 christos /* Disable the default symbol completer. */ 918 1.10 christos set_cmd_completer (commands.set, nullptr); 919 1.10 christos 920 1.10 christos return commands; 921 1.10 christos } 922 1.9 christos 923 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 924 1.10 christos to a global storage buffer. */ 925 1.10 christos 926 1.10 christos set_show_commands 927 1.10 christos add_setshow_string_cmd (const char *name, command_class theclass, 928 1.10 christos const char *set_doc, const char *show_doc, 929 1.10 christos const char *help_doc, 930 1.10 christos setting_func_types<std::string>::set set_func, 931 1.10 christos setting_func_types<std::string>::get get_func, 932 1.10 christos show_value_ftype *show_func, 933 1.10 christos cmd_list_element **set_list, 934 1.10 christos cmd_list_element **show_list) 935 1.10 christos { 936 1.10 christos auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_string, 937 1.10 christos nullptr, set_doc, show_doc, 938 1.10 christos help_doc, set_func, get_func, 939 1.10 christos nullptr, show_func, set_list, 940 1.10 christos show_list); 941 1.9 christos 942 1.9 christos /* Disable the default symbol completer. */ 943 1.10 christos set_cmd_completer (cmds.set, nullptr); 944 1.10 christos 945 1.10 christos return cmds; 946 1.1 christos } 947 1.1 christos 948 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 949 1.1 christos list for set/show or some sublist thereof). */ 950 1.10 christos 951 1.10 christos set_show_commands 952 1.5 christos add_setshow_string_noescape_cmd (const char *name, enum command_class theclass, 953 1.10 christos std::string *var, 954 1.1 christos const char *set_doc, const char *show_doc, 955 1.1 christos const char *help_doc, 956 1.10 christos cmd_func_ftype *set_func, 957 1.1 christos show_value_ftype *show_func, 958 1.1 christos struct cmd_list_element **set_list, 959 1.1 christos struct cmd_list_element **show_list) 960 1.1 christos { 961 1.10 christos set_show_commands commands 962 1.10 christos = add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape, 963 1.10 christos var, set_doc, show_doc, help_doc, 964 1.10 christos nullptr, nullptr, set_func, show_func, 965 1.10 christos set_list, show_list); 966 1.10 christos 967 1.10 christos /* Disable the default symbol completer. */ 968 1.10 christos set_cmd_completer (commands.set, nullptr); 969 1.1 christos 970 1.10 christos return commands; 971 1.10 christos } 972 1.10 christos 973 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 974 1.10 christos to a global storage buffer. */ 975 1.10 christos 976 1.10 christos set_show_commands 977 1.10 christos add_setshow_string_noescape_cmd (const char *name, command_class theclass, 978 1.10 christos const char *set_doc, const char *show_doc, 979 1.10 christos const char *help_doc, 980 1.10 christos setting_func_types<std::string>::set set_func, 981 1.10 christos setting_func_types<std::string>::get get_func, 982 1.10 christos show_value_ftype *show_func, 983 1.10 christos cmd_list_element **set_list, 984 1.10 christos cmd_list_element **show_list) 985 1.10 christos { 986 1.10 christos auto cmds = add_setshow_cmd_full<std::string> (name, theclass, 987 1.10 christos var_string_noescape, nullptr, 988 1.10 christos set_doc, show_doc, help_doc, 989 1.10 christos set_func, get_func, 990 1.10 christos nullptr, show_func, set_list, 991 1.10 christos show_list); 992 1.9 christos 993 1.9 christos /* Disable the default symbol completer. */ 994 1.10 christos set_cmd_completer (cmds.set, nullptr); 995 1.9 christos 996 1.10 christos return cmds; 997 1.1 christos } 998 1.1 christos 999 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 1000 1.1 christos list for set/show or some sublist thereof). */ 1001 1.10 christos 1002 1.10 christos set_show_commands 1003 1.5 christos add_setshow_optional_filename_cmd (const char *name, enum command_class theclass, 1004 1.10 christos std::string *var, 1005 1.1 christos const char *set_doc, const char *show_doc, 1006 1.1 christos const char *help_doc, 1007 1.10 christos cmd_func_ftype *set_func, 1008 1.1 christos show_value_ftype *show_func, 1009 1.1 christos struct cmd_list_element **set_list, 1010 1.1 christos struct cmd_list_element **show_list) 1011 1.1 christos { 1012 1.10 christos set_show_commands commands 1013 1.10 christos = add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename, 1014 1.10 christos var, set_doc, show_doc, help_doc, 1015 1.10 christos nullptr, nullptr, set_func, show_func, 1016 1.10 christos set_list, show_list); 1017 1.10 christos 1018 1.12 christos set_cmd_completer (commands.set, deprecated_filename_completer); 1019 1.10 christos 1020 1.10 christos return commands; 1021 1.10 christos } 1022 1.10 christos 1023 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 1024 1.10 christos to a global storage buffer. */ 1025 1.10 christos 1026 1.10 christos set_show_commands 1027 1.10 christos add_setshow_optional_filename_cmd (const char *name, command_class theclass, 1028 1.10 christos const char *set_doc, const char *show_doc, 1029 1.10 christos const char *help_doc, 1030 1.10 christos setting_func_types<std::string>::set set_func, 1031 1.10 christos setting_func_types<std::string>::get get_func, 1032 1.10 christos show_value_ftype *show_func, 1033 1.10 christos cmd_list_element **set_list, 1034 1.10 christos cmd_list_element **show_list) 1035 1.10 christos { 1036 1.10 christos auto cmds = 1037 1.10 christos add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename, 1038 1.10 christos nullptr, set_doc, show_doc, help_doc, 1039 1.10 christos set_func, get_func, nullptr, show_func, 1040 1.10 christos set_list,show_list); 1041 1.1 christos 1042 1.12 christos set_cmd_completer (cmds.set, deprecated_filename_completer); 1043 1.10 christos 1044 1.10 christos return cmds; 1045 1.1 christos } 1046 1.1 christos 1047 1.11 christos /* Add element named NAME to both the set and show command LISTs (the 1048 1.11 christos list for set/show or some sublist thereof). THECLASS is as in 1049 1.11 christos add_cmd. VAR is address of the variable which will contain the 1050 1.11 christos value. SET_DOC and SHOW_DOC are the documentation strings. This 1051 1.11 christos function is only used in Python API. Please don't use it elsewhere. */ 1052 1.1 christos 1053 1.11 christos set_show_commands 1054 1.11 christos add_setshow_integer_cmd (const char *name, enum command_class theclass, 1055 1.11 christos int *var, const literal_def *extra_literals, 1056 1.11 christos const char *set_doc, const char *show_doc, 1057 1.11 christos const char *help_doc, 1058 1.11 christos cmd_func_ftype *set_func, 1059 1.11 christos show_value_ftype *show_func, 1060 1.11 christos struct cmd_list_element **set_list, 1061 1.11 christos struct cmd_list_element **show_list) 1062 1.1 christos { 1063 1.11 christos set_show_commands commands 1064 1.11 christos = add_setshow_cmd_full<int> (name, theclass, var_integer, var, 1065 1.11 christos extra_literals, set_doc, show_doc, 1066 1.11 christos help_doc, nullptr, nullptr, set_func, 1067 1.11 christos show_func, set_list, show_list); 1068 1.11 christos return commands; 1069 1.11 christos } 1070 1.11 christos 1071 1.11 christos /* Same as above but using a getter and a setter function instead of a pointer 1072 1.11 christos to a global storage buffer. */ 1073 1.1 christos 1074 1.11 christos set_show_commands 1075 1.11 christos add_setshow_integer_cmd (const char *name, command_class theclass, 1076 1.11 christos const literal_def *extra_literals, 1077 1.11 christos const char *set_doc, const char *show_doc, 1078 1.11 christos const char *help_doc, 1079 1.11 christos setting_func_types<int>::set set_func, 1080 1.11 christos setting_func_types<int>::get get_func, 1081 1.11 christos show_value_ftype *show_func, 1082 1.11 christos cmd_list_element **set_list, 1083 1.11 christos cmd_list_element **show_list) 1084 1.11 christos { 1085 1.11 christos auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr, 1086 1.11 christos extra_literals, set_doc, show_doc, 1087 1.11 christos help_doc, set_func, get_func, nullptr, 1088 1.11 christos show_func, set_list, show_list); 1089 1.11 christos return cmds; 1090 1.1 christos } 1091 1.1 christos 1092 1.11 christos /* Accept `unlimited' or 0, translated internally to INT_MAX. */ 1093 1.11 christos const literal_def integer_unlimited_literals[] = 1094 1.11 christos { 1095 1.11 christos { "unlimited", INT_MAX, 0 }, 1096 1.11 christos { nullptr } 1097 1.11 christos }; 1098 1.11 christos 1099 1.11 christos /* Same as above but using `integer_unlimited_literals', with a pointer 1100 1.11 christos to a global storage buffer. */ 1101 1.10 christos 1102 1.10 christos set_show_commands 1103 1.5 christos add_setshow_integer_cmd (const char *name, enum command_class theclass, 1104 1.1 christos int *var, 1105 1.1 christos const char *set_doc, const char *show_doc, 1106 1.1 christos const char *help_doc, 1107 1.10 christos cmd_func_ftype *set_func, 1108 1.1 christos show_value_ftype *show_func, 1109 1.1 christos struct cmd_list_element **set_list, 1110 1.1 christos struct cmd_list_element **show_list) 1111 1.1 christos { 1112 1.10 christos set_show_commands commands 1113 1.10 christos = add_setshow_cmd_full<int> (name, theclass, var_integer, var, 1114 1.11 christos integer_unlimited_literals, 1115 1.10 christos set_doc, show_doc, help_doc, 1116 1.10 christos nullptr, nullptr, set_func, 1117 1.10 christos show_func, set_list, show_list); 1118 1.10 christos return commands; 1119 1.10 christos } 1120 1.10 christos 1121 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 1122 1.10 christos to a global storage buffer. */ 1123 1.10 christos 1124 1.10 christos set_show_commands 1125 1.10 christos add_setshow_integer_cmd (const char *name, command_class theclass, 1126 1.10 christos const char *set_doc, const char *show_doc, 1127 1.10 christos const char *help_doc, 1128 1.10 christos setting_func_types<int>::set set_func, 1129 1.10 christos setting_func_types<int>::get get_func, 1130 1.10 christos show_value_ftype *show_func, 1131 1.10 christos cmd_list_element **set_list, 1132 1.10 christos cmd_list_element **show_list) 1133 1.10 christos { 1134 1.10 christos auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr, 1135 1.11 christos integer_unlimited_literals, 1136 1.10 christos set_doc, show_doc, help_doc, set_func, 1137 1.10 christos get_func, nullptr, show_func, set_list, 1138 1.10 christos show_list); 1139 1.11 christos return cmds; 1140 1.11 christos } 1141 1.1 christos 1142 1.11 christos /* Add element named NAME to both the set and show command LISTs (the 1143 1.11 christos list for set/show or some sublist thereof). CLASS is as in 1144 1.11 christos add_cmd. VAR is address of the variable which will contain the 1145 1.11 christos value. SET_DOC and SHOW_DOC are the documentation strings. */ 1146 1.1 christos 1147 1.11 christos set_show_commands 1148 1.11 christos add_setshow_pinteger_cmd (const char *name, enum command_class theclass, 1149 1.11 christos int *var, const literal_def *extra_literals, 1150 1.11 christos const char *set_doc, const char *show_doc, 1151 1.11 christos const char *help_doc, 1152 1.11 christos cmd_func_ftype *set_func, 1153 1.11 christos show_value_ftype *show_func, 1154 1.11 christos struct cmd_list_element **set_list, 1155 1.11 christos struct cmd_list_element **show_list) 1156 1.11 christos { 1157 1.11 christos set_show_commands commands 1158 1.11 christos = add_setshow_cmd_full<int> (name, theclass, var_pinteger, var, 1159 1.11 christos extra_literals, set_doc, show_doc, 1160 1.11 christos help_doc, nullptr, nullptr, set_func, 1161 1.11 christos show_func, set_list, show_list); 1162 1.11 christos return commands; 1163 1.11 christos } 1164 1.11 christos 1165 1.11 christos /* Same as above but using a getter and a setter function instead of a pointer 1166 1.11 christos to a global storage buffer. */ 1167 1.11 christos 1168 1.11 christos set_show_commands 1169 1.11 christos add_setshow_pinteger_cmd (const char *name, command_class theclass, 1170 1.11 christos const literal_def *extra_literals, 1171 1.11 christos const char *set_doc, const char *show_doc, 1172 1.11 christos const char *help_doc, 1173 1.11 christos setting_func_types<int>::set set_func, 1174 1.11 christos setting_func_types<int>::get get_func, 1175 1.11 christos show_value_ftype *show_func, 1176 1.11 christos cmd_list_element **set_list, 1177 1.11 christos cmd_list_element **show_list) 1178 1.11 christos { 1179 1.11 christos auto cmds = add_setshow_cmd_full<int> (name, theclass, var_pinteger, nullptr, 1180 1.11 christos extra_literals, set_doc, show_doc, 1181 1.11 christos help_doc, set_func, get_func, nullptr, 1182 1.11 christos show_func, set_list, show_list); 1183 1.10 christos return cmds; 1184 1.1 christos } 1185 1.1 christos 1186 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 1187 1.11 christos list for set/show or some sublist thereof). THECLASS is as in 1188 1.1 christos add_cmd. VAR is address of the variable which will contain the 1189 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. */ 1190 1.10 christos 1191 1.10 christos set_show_commands 1192 1.5 christos add_setshow_uinteger_cmd (const char *name, enum command_class theclass, 1193 1.11 christos unsigned int *var, const literal_def *extra_literals, 1194 1.1 christos const char *set_doc, const char *show_doc, 1195 1.1 christos const char *help_doc, 1196 1.10 christos cmd_func_ftype *set_func, 1197 1.1 christos show_value_ftype *show_func, 1198 1.1 christos struct cmd_list_element **set_list, 1199 1.1 christos struct cmd_list_element **show_list) 1200 1.1 christos { 1201 1.10 christos set_show_commands commands 1202 1.10 christos = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var, 1203 1.11 christos extra_literals, set_doc, show_doc, 1204 1.11 christos help_doc, nullptr, nullptr, set_func, 1205 1.10 christos show_func, set_list, show_list); 1206 1.11 christos return commands; 1207 1.11 christos } 1208 1.11 christos 1209 1.11 christos /* Same as above but using a getter and a setter function instead of a pointer 1210 1.11 christos to a global storage buffer. */ 1211 1.11 christos 1212 1.11 christos set_show_commands 1213 1.11 christos add_setshow_uinteger_cmd (const char *name, command_class theclass, 1214 1.11 christos const literal_def *extra_literals, 1215 1.11 christos const char *set_doc, const char *show_doc, 1216 1.11 christos const char *help_doc, 1217 1.11 christos setting_func_types<unsigned int>::set set_func, 1218 1.11 christos setting_func_types<unsigned int>::get get_func, 1219 1.11 christos show_value_ftype *show_func, 1220 1.11 christos cmd_list_element **set_list, 1221 1.11 christos cmd_list_element **show_list) 1222 1.11 christos { 1223 1.11 christos auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, 1224 1.11 christos nullptr, extra_literals, 1225 1.11 christos set_doc, show_doc, help_doc, 1226 1.11 christos set_func, get_func, nullptr, 1227 1.11 christos show_func, set_list, 1228 1.11 christos show_list); 1229 1.11 christos return cmds; 1230 1.11 christos } 1231 1.10 christos 1232 1.11 christos /* Accept `unlimited' or 0, translated internally to UINT_MAX. */ 1233 1.11 christos const literal_def uinteger_unlimited_literals[] = 1234 1.11 christos { 1235 1.11 christos { "unlimited", UINT_MAX, 0 }, 1236 1.11 christos { nullptr } 1237 1.11 christos }; 1238 1.10 christos 1239 1.11 christos /* Same as above but using `uinteger_unlimited_literals', with a pointer 1240 1.11 christos to a global storage buffer. */ 1241 1.11 christos 1242 1.11 christos set_show_commands 1243 1.11 christos add_setshow_uinteger_cmd (const char *name, enum command_class theclass, 1244 1.11 christos unsigned int *var, 1245 1.11 christos const char *set_doc, const char *show_doc, 1246 1.11 christos const char *help_doc, 1247 1.11 christos cmd_func_ftype *set_func, 1248 1.11 christos show_value_ftype *show_func, 1249 1.11 christos struct cmd_list_element **set_list, 1250 1.11 christos struct cmd_list_element **show_list) 1251 1.11 christos { 1252 1.11 christos set_show_commands commands 1253 1.11 christos = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var, 1254 1.11 christos uinteger_unlimited_literals, 1255 1.11 christos set_doc, show_doc, help_doc, nullptr, 1256 1.11 christos nullptr, set_func, show_func, 1257 1.11 christos set_list, show_list); 1258 1.10 christos return commands; 1259 1.10 christos } 1260 1.10 christos 1261 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 1262 1.10 christos to a global storage buffer. */ 1263 1.10 christos 1264 1.10 christos set_show_commands 1265 1.10 christos add_setshow_uinteger_cmd (const char *name, command_class theclass, 1266 1.10 christos const char *set_doc, const char *show_doc, 1267 1.10 christos const char *help_doc, 1268 1.10 christos setting_func_types<unsigned int>::set set_func, 1269 1.10 christos setting_func_types<unsigned int>::get get_func, 1270 1.10 christos show_value_ftype *show_func, 1271 1.10 christos cmd_list_element **set_list, 1272 1.10 christos cmd_list_element **show_list) 1273 1.10 christos { 1274 1.10 christos auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, 1275 1.11 christos nullptr, 1276 1.11 christos uinteger_unlimited_literals, 1277 1.11 christos set_doc, show_doc, help_doc, 1278 1.11 christos set_func, get_func, nullptr, 1279 1.11 christos show_func, set_list, 1280 1.10 christos show_list); 1281 1.10 christos return cmds; 1282 1.1 christos } 1283 1.1 christos 1284 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 1285 1.11 christos list for set/show or some sublist thereof). THECLASS is as in 1286 1.1 christos add_cmd. VAR is address of the variable which will contain the 1287 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. */ 1288 1.10 christos 1289 1.10 christos set_show_commands 1290 1.5 christos add_setshow_zinteger_cmd (const char *name, enum command_class theclass, 1291 1.1 christos int *var, 1292 1.1 christos const char *set_doc, const char *show_doc, 1293 1.1 christos const char *help_doc, 1294 1.10 christos cmd_func_ftype *set_func, 1295 1.1 christos show_value_ftype *show_func, 1296 1.1 christos struct cmd_list_element **set_list, 1297 1.1 christos struct cmd_list_element **show_list) 1298 1.1 christos { 1299 1.11 christos return add_setshow_cmd_full<int> (name, theclass, var_integer, var, 1300 1.10 christos set_doc, show_doc, help_doc, 1301 1.10 christos nullptr, nullptr, set_func, 1302 1.10 christos show_func, set_list, show_list); 1303 1.1 christos } 1304 1.1 christos 1305 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 1306 1.10 christos to a global storage buffer. */ 1307 1.10 christos 1308 1.10 christos set_show_commands 1309 1.10 christos add_setshow_zinteger_cmd (const char *name, command_class theclass, 1310 1.10 christos const char *set_doc, const char *show_doc, 1311 1.10 christos const char *help_doc, 1312 1.10 christos setting_func_types<int>::set set_func, 1313 1.10 christos setting_func_types<int>::get get_func, 1314 1.10 christos show_value_ftype *show_func, 1315 1.10 christos cmd_list_element **set_list, 1316 1.10 christos cmd_list_element **show_list) 1317 1.10 christos { 1318 1.11 christos return add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr, 1319 1.10 christos set_doc, show_doc, help_doc, set_func, 1320 1.10 christos get_func, nullptr, show_func, set_list, 1321 1.10 christos show_list); 1322 1.10 christos } 1323 1.10 christos 1324 1.11 christos /* Accept `unlimited' or -1, using -1 internally. */ 1325 1.11 christos const literal_def pinteger_unlimited_literals[] = 1326 1.11 christos { 1327 1.11 christos { "unlimited", -1, -1 }, 1328 1.11 christos { nullptr } 1329 1.11 christos }; 1330 1.11 christos 1331 1.11 christos /* Same as above but using `pinteger_unlimited_literals', with a pointer 1332 1.11 christos to a global storage buffer. */ 1333 1.11 christos 1334 1.10 christos set_show_commands 1335 1.1 christos add_setshow_zuinteger_unlimited_cmd (const char *name, 1336 1.5 christos enum command_class theclass, 1337 1.1 christos int *var, 1338 1.1 christos const char *set_doc, 1339 1.1 christos const char *show_doc, 1340 1.1 christos const char *help_doc, 1341 1.10 christos cmd_func_ftype *set_func, 1342 1.1 christos show_value_ftype *show_func, 1343 1.1 christos struct cmd_list_element **set_list, 1344 1.1 christos struct cmd_list_element **show_list) 1345 1.1 christos { 1346 1.10 christos set_show_commands commands 1347 1.11 christos = add_setshow_cmd_full<int> (name, theclass, var_pinteger, var, 1348 1.11 christos pinteger_unlimited_literals, 1349 1.10 christos set_doc, show_doc, help_doc, nullptr, 1350 1.10 christos nullptr, set_func, show_func, set_list, 1351 1.10 christos show_list); 1352 1.10 christos return commands; 1353 1.10 christos } 1354 1.10 christos 1355 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 1356 1.10 christos to a global storage buffer. */ 1357 1.10 christos 1358 1.10 christos set_show_commands 1359 1.10 christos add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass, 1360 1.10 christos const char *set_doc, const char *show_doc, 1361 1.10 christos const char *help_doc, 1362 1.10 christos setting_func_types<int>::set set_func, 1363 1.10 christos setting_func_types<int>::get get_func, 1364 1.10 christos show_value_ftype *show_func, 1365 1.10 christos cmd_list_element **set_list, 1366 1.10 christos cmd_list_element **show_list) 1367 1.10 christos { 1368 1.10 christos auto cmds 1369 1.11 christos = add_setshow_cmd_full<int> (name, theclass, var_pinteger, nullptr, 1370 1.11 christos pinteger_unlimited_literals, 1371 1.11 christos set_doc, show_doc, help_doc, set_func, 1372 1.10 christos get_func, nullptr, show_func, set_list, 1373 1.10 christos show_list); 1374 1.10 christos return cmds; 1375 1.1 christos } 1376 1.1 christos 1377 1.1 christos /* Add element named NAME to both the set and show command LISTs (the 1378 1.11 christos list for set/show or some sublist thereof). THECLASS is as in 1379 1.1 christos add_cmd. VAR is address of the variable which will contain the 1380 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. */ 1381 1.10 christos 1382 1.10 christos set_show_commands 1383 1.5 christos add_setshow_zuinteger_cmd (const char *name, enum command_class theclass, 1384 1.1 christos unsigned int *var, 1385 1.1 christos const char *set_doc, const char *show_doc, 1386 1.1 christos const char *help_doc, 1387 1.10 christos cmd_func_ftype *set_func, 1388 1.1 christos show_value_ftype *show_func, 1389 1.1 christos struct cmd_list_element **set_list, 1390 1.1 christos struct cmd_list_element **show_list) 1391 1.1 christos { 1392 1.11 christos return add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, 1393 1.10 christos var, set_doc, show_doc, help_doc, 1394 1.10 christos nullptr, nullptr, set_func, 1395 1.10 christos show_func, set_list, show_list); 1396 1.1 christos } 1397 1.1 christos 1398 1.10 christos /* Same as above but using a getter and a setter function instead of a pointer 1399 1.10 christos to a global storage buffer. */ 1400 1.10 christos 1401 1.10 christos set_show_commands 1402 1.10 christos add_setshow_zuinteger_cmd (const char *name, command_class theclass, 1403 1.10 christos const char *set_doc, const char *show_doc, 1404 1.10 christos const char *help_doc, 1405 1.10 christos setting_func_types<unsigned int>::set set_func, 1406 1.10 christos setting_func_types<unsigned int>::get get_func, 1407 1.10 christos show_value_ftype *show_func, 1408 1.10 christos cmd_list_element **set_list, 1409 1.10 christos cmd_list_element **show_list) 1410 1.10 christos { 1411 1.11 christos return add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, 1412 1.10 christos nullptr, set_doc, show_doc, 1413 1.10 christos help_doc, set_func, get_func, 1414 1.10 christos nullptr, show_func, set_list, 1415 1.10 christos show_list); 1416 1.10 christos } 1417 1.10 christos 1418 1.10 christos /* Remove the command named NAME from the command list. Return the list 1419 1.10 christos commands which were aliased to the deleted command. The various *HOOKs are 1420 1.10 christos set to the pre- and post-hook commands for the deleted command. If the 1421 1.10 christos command does not have a hook, the corresponding out parameter is set to 1422 1.10 christos NULL. */ 1423 1.1 christos 1424 1.10 christos static cmd_list_element::aliases_list_type 1425 1.1 christos delete_cmd (const char *name, struct cmd_list_element **list, 1426 1.1 christos struct cmd_list_element **prehook, 1427 1.1 christos struct cmd_list_element **prehookee, 1428 1.1 christos struct cmd_list_element **posthook, 1429 1.1 christos struct cmd_list_element **posthookee) 1430 1.1 christos { 1431 1.1 christos struct cmd_list_element *iter; 1432 1.1 christos struct cmd_list_element **previous_chain_ptr; 1433 1.10 christos cmd_list_element::aliases_list_type aliases; 1434 1.1 christos 1435 1.1 christos *prehook = NULL; 1436 1.1 christos *prehookee = NULL; 1437 1.1 christos *posthook = NULL; 1438 1.1 christos *posthookee = NULL; 1439 1.1 christos previous_chain_ptr = list; 1440 1.1 christos 1441 1.1 christos for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr) 1442 1.1 christos { 1443 1.1 christos if (strcmp (iter->name, name) == 0) 1444 1.1 christos { 1445 1.1 christos if (iter->destroyer) 1446 1.10 christos iter->destroyer (iter, iter->context ()); 1447 1.10 christos 1448 1.1 christos if (iter->hookee_pre) 1449 1.1 christos iter->hookee_pre->hook_pre = 0; 1450 1.1 christos *prehook = iter->hook_pre; 1451 1.1 christos *prehookee = iter->hookee_pre; 1452 1.1 christos if (iter->hookee_post) 1453 1.1 christos iter->hookee_post->hook_post = 0; 1454 1.1 christos *posthook = iter->hook_post; 1455 1.1 christos *posthookee = iter->hookee_post; 1456 1.1 christos 1457 1.1 christos /* Update the link. */ 1458 1.1 christos *previous_chain_ptr = iter->next; 1459 1.1 christos 1460 1.10 christos aliases = std::move (iter->aliases); 1461 1.1 christos 1462 1.1 christos /* If this command was an alias, remove it from the list of 1463 1.1 christos aliases. */ 1464 1.10 christos if (iter->is_alias ()) 1465 1.1 christos { 1466 1.10 christos auto it = iter->alias_target->aliases.iterator_to (*iter); 1467 1.10 christos iter->alias_target->aliases.erase (it); 1468 1.1 christos } 1469 1.1 christos 1470 1.8 christos delete iter; 1471 1.1 christos 1472 1.1 christos /* We won't see another command with the same name. */ 1473 1.1 christos break; 1474 1.1 christos } 1475 1.1 christos else 1476 1.1 christos previous_chain_ptr = &iter->next; 1477 1.1 christos } 1478 1.1 christos 1479 1.1 christos return aliases; 1480 1.1 christos } 1481 1.1 christos 1482 1.1 christos /* Shorthands to the commands above. */ 1484 1.1 christos 1485 1.1 christos /* Add an element to the list of info subcommands. */ 1486 1.1 christos 1487 1.10 christos struct cmd_list_element * 1488 1.1 christos add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc) 1489 1.6 christos { 1490 1.1 christos return add_cmd (name, class_info, fun, doc, &infolist); 1491 1.1 christos } 1492 1.1 christos 1493 1.1 christos /* Add an alias to the list of info subcommands. */ 1494 1.10 christos 1495 1.10 christos cmd_list_element * 1496 1.1 christos add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag) 1497 1.10 christos { 1498 1.1 christos return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist); 1499 1.1 christos } 1500 1.1 christos 1501 1.1 christos /* Add an element to the list of commands. */ 1502 1.1 christos 1503 1.8 christos struct cmd_list_element * 1504 1.10 christos add_com (const char *name, enum command_class theclass, 1505 1.3 christos cmd_simple_func_ftype *fun, 1506 1.1 christos const char *doc) 1507 1.5 christos { 1508 1.1 christos return add_cmd (name, theclass, fun, doc, &cmdlist); 1509 1.1 christos } 1510 1.9 christos 1511 1.9 christos /* Add an alias or abbreviation command to the list of commands. 1512 1.9 christos For aliases predefined by GDB (such as bt), THECLASS must be 1513 1.9 christos different of class_alias, as class_alias is used to identify 1514 1.1 christos user defined aliases. */ 1515 1.10 christos 1516 1.10 christos cmd_list_element * 1517 1.10 christos add_com_alias (const char *name, cmd_list_element *target, 1518 1.1 christos command_class theclass, int abbrev_flag) 1519 1.10 christos { 1520 1.1 christos return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist); 1521 1.6 christos } 1522 1.6 christos 1523 1.6 christos /* Add an element with a suppress notification to the list of commands. */ 1524 1.6 christos 1525 1.6 christos struct cmd_list_element * 1526 1.10 christos add_com_suppress_notification (const char *name, enum command_class theclass, 1527 1.10 christos cmd_simple_func_ftype *fun, const char *doc, 1528 1.6 christos bool *suppress_notification) 1529 1.8 christos { 1530 1.8 christos return add_cmd_suppress_notification (name, theclass, fun, doc, 1531 1.6 christos &cmdlist, suppress_notification); 1532 1.6 christos } 1533 1.12 christos 1534 1.9 christos /* Print the prefix of C followed by name of C in command style. */ 1535 1.9 christos 1536 1.10 christos static void 1537 1.9 christos fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream) 1538 1.10 christos { 1539 1.10 christos std::string prefixname 1540 1.10 christos = c.prefix == nullptr ? "" : c.prefix->prefixname (); 1541 1.12 christos 1542 1.10 christos fprintf_styled (stream, command_style.style (), "%s%s", 1543 1.10 christos prefixname.c_str (), c.name); 1544 1.9 christos } 1545 1.10 christos 1546 1.10 christos /* True if ALIAS has a user-defined documentation. */ 1547 1.10 christos 1548 1.10 christos static bool 1549 1.10 christos user_documented_alias (const cmd_list_element &alias) 1550 1.10 christos { 1551 1.10 christos gdb_assert (alias.is_alias ()); 1552 1.10 christos /* Alias is user documented if it has an allocated documentation 1553 1.10 christos that differs from the aliased command. */ 1554 1.10 christos return (alias.doc_allocated 1555 1.9 christos && strcmp (alias.doc, alias.alias_target->doc) != 0); 1556 1.9 christos } 1557 1.9 christos 1558 1.9 christos /* Print the definition of alias C using title style for alias 1559 1.9 christos and aliased command. */ 1560 1.9 christos 1561 1.10 christos static void 1562 1.9 christos fput_alias_definition_styled (const cmd_list_element &c, 1563 1.9 christos struct ui_file *stream) 1564 1.10 christos { 1565 1.10 christos gdb_assert (c.is_alias ()); 1566 1.9 christos gdb_puts (" alias ", stream); 1567 1.10 christos fput_command_name_styled (c, stream); 1568 1.10 christos gdb_printf (stream, " = "); 1569 1.10 christos fput_command_name_styled (*c.alias_target, stream); 1570 1.9 christos gdb_printf (stream, " %s\n", c.default_args.c_str ()); 1571 1.9 christos } 1572 1.10 christos 1573 1.10 christos /* Print the definition of CMD aliases not deprecated and having default args 1574 1.9 christos and not specifically documented by the user. */ 1575 1.9 christos 1576 1.10 christos static void 1577 1.9 christos fput_aliases_definition_styled (const cmd_list_element &cmd, 1578 1.9 christos struct ui_file *stream) 1579 1.10 christos { 1580 1.10 christos for (const cmd_list_element &alias : cmd.aliases) 1581 1.10 christos if (!alias.cmd_deprecated 1582 1.10 christos && !user_documented_alias (alias) 1583 1.10 christos && !alias.default_args.empty ()) 1584 1.9 christos fput_alias_definition_styled (alias, stream); 1585 1.9 christos } 1586 1.10 christos 1587 1.10 christos /* If C has one or more aliases, style print the name of C and the name of its 1588 1.9 christos aliases not documented specifically by the user, separated by commas. 1589 1.9 christos If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases. 1590 1.9 christos If one or more names are printed, POSTFIX is printed after the last name. 1591 1.9 christos */ 1592 1.9 christos 1593 1.10 christos static void 1594 1.9 christos fput_command_names_styled (const cmd_list_element &c, 1595 1.9 christos bool always_fput_c_name, const char *postfix, 1596 1.9 christos struct ui_file *stream) 1597 1.10 christos { 1598 1.10 christos /* First, check if we are going to print something. That is, either if 1599 1.10 christos ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated 1600 1.10 christos alias not documented specifically by the user. */ 1601 1.10 christos 1602 1.10 christos auto print_alias = [] (const cmd_list_element &alias) 1603 1.10 christos { 1604 1.10 christos return !alias.cmd_deprecated && !user_documented_alias (alias); 1605 1.10 christos }; 1606 1.10 christos 1607 1.10 christos bool print_something = always_fput_c_name; 1608 1.10 christos if (!print_something) 1609 1.10 christos for (const cmd_list_element &alias : c.aliases) 1610 1.10 christos { 1611 1.10 christos if (!print_alias (alias)) 1612 1.10 christos continue; 1613 1.10 christos 1614 1.10 christos print_something = true; 1615 1.10 christos break; 1616 1.10 christos } 1617 1.10 christos 1618 1.9 christos if (print_something) 1619 1.10 christos fput_command_name_styled (c, stream); 1620 1.10 christos 1621 1.9 christos for (const cmd_list_element &alias : c.aliases) 1622 1.10 christos { 1623 1.10 christos if (!print_alias (alias)) 1624 1.10 christos continue; 1625 1.10 christos 1626 1.10 christos gdb_puts (", ", stream); 1627 1.10 christos stream->wrap_here (3); 1628 1.9 christos fput_command_name_styled (alias, stream); 1629 1.10 christos } 1630 1.10 christos 1631 1.10 christos if (print_something) 1632 1.9 christos gdb_puts (postfix, stream); 1633 1.9 christos } 1634 1.9 christos 1635 1.9 christos /* If VERBOSE, print the full help for command C and highlight the 1636 1.9 christos documentation parts matching HIGHLIGHT, 1637 1.9 christos otherwise print only one-line help for command C. */ 1638 1.9 christos 1639 1.11 christos static void 1640 1.11 christos print_doc_of_command (const cmd_list_element &c, bool verbose, 1641 1.9 christos compiled_regex &highlight, struct ui_file *stream) 1642 1.9 christos { 1643 1.9 christos /* When printing the full documentation, add a line to separate 1644 1.9 christos this documentation from the previous command help, in the likely 1645 1.9 christos case that apropos finds several commands. */ 1646 1.10 christos if (verbose) 1647 1.9 christos gdb_puts ("\n", stream); 1648 1.9 christos 1649 1.9 christos fput_command_names_styled (c, true, 1650 1.9 christos verbose ? "" : " -- ", stream); 1651 1.9 christos if (verbose) 1652 1.10 christos { 1653 1.9 christos gdb_puts ("\n", stream); 1654 1.10 christos fput_aliases_definition_styled (c, stream); 1655 1.10 christos fputs_highlighted (c.doc, highlight, stream); 1656 1.9 christos gdb_puts ("\n", stream); 1657 1.9 christos } 1658 1.9 christos else 1659 1.10 christos { 1660 1.10 christos print_doc_line (stream, c.doc, false); 1661 1.9 christos gdb_puts ("\n", stream); 1662 1.9 christos fput_aliases_definition_styled (c, stream); 1663 1.9 christos } 1664 1.9 christos } 1665 1.1 christos 1666 1.1 christos /* Recursively walk the commandlist structures, and print out the 1667 1.1 christos documentation of commands that match our regex in either their 1668 1.9 christos name, or their documentation. 1669 1.9 christos If VERBOSE, prints the complete documentation and highlight the 1670 1.9 christos documentation parts matching REGEX, otherwise prints only 1671 1.1 christos the first line. 1672 1.9 christos */ 1673 1.9 christos void 1674 1.1 christos apropos_cmd (struct ui_file *stream, 1675 1.11 christos struct cmd_list_element *commandlist, 1676 1.1 christos bool verbose, compiled_regex ®ex) 1677 1.1 christos { 1678 1.1 christos struct cmd_list_element *c; 1679 1.1 christos int returnvalue; 1680 1.1 christos 1681 1.1 christos /* Walk through the commands. */ 1682 1.1 christos for (c=commandlist;c;c=c->next) 1683 1.10 christos { 1684 1.9 christos if (c->is_alias () && !user_documented_alias (*c)) 1685 1.10 christos { 1686 1.10 christos /* Command aliases/abbreviations not specifically documented by the 1687 1.10 christos user are skipped to ensure we print the doc of a command only once, 1688 1.9 christos when encountering the aliased command. */ 1689 1.9 christos continue; 1690 1.9 christos } 1691 1.1 christos 1692 1.1 christos returnvalue = -1; /* Needed to avoid double printing. */ 1693 1.1 christos if (c->name != NULL) 1694 1.8 christos { 1695 1.8 christos size_t name_len = strlen (c->name); 1696 1.1 christos 1697 1.8 christos /* Try to match against the name. */ 1698 1.1 christos returnvalue = regex.search (c->name, name_len, 0, name_len, NULL); 1699 1.11 christos if (returnvalue >= 0) 1700 1.9 christos print_doc_of_command (*c, verbose, regex, stream); 1701 1.9 christos 1702 1.10 christos /* Try to match against the name of the aliases. */ 1703 1.1 christos for (const cmd_list_element &alias : c->aliases) 1704 1.10 christos { 1705 1.10 christos name_len = strlen (alias.name); 1706 1.9 christos returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL); 1707 1.10 christos if (returnvalue >= 0) 1708 1.11 christos { 1709 1.10 christos print_doc_of_command (*c, verbose, regex, stream); 1710 1.10 christos break; 1711 1.1 christos } 1712 1.1 christos } 1713 1.1 christos } 1714 1.1 christos if (c->doc != NULL && returnvalue < 0) 1715 1.8 christos { 1716 1.8 christos size_t doc_len = strlen (c->doc); 1717 1.1 christos 1718 1.8 christos /* Try to match against documentation. */ 1719 1.11 christos if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0) 1720 1.1 christos print_doc_of_command (*c, verbose, regex, stream); 1721 1.9 christos } 1722 1.10 christos /* Check if this command has subcommands. */ 1723 1.1 christos if (c->is_prefix ()) 1724 1.1 christos { 1725 1.1 christos /* Recursively call ourselves on the subcommand list, 1726 1.11 christos passing the right prefix in. */ 1727 1.1 christos apropos_cmd (stream, *c->subcommands, verbose, regex); 1728 1.1 christos } 1729 1.1 christos } 1730 1.1 christos } 1731 1.1 christos 1732 1.1 christos /* This command really has to deal with two things: 1733 1.1 christos 1) I want documentation on *this string* (usually called by 1734 1.1 christos "help commandname"). 1735 1.1 christos 1736 1.1 christos 2) I want documentation on *this list* (usually called by giving a 1737 1.1 christos command that requires subcommands. Also called by saying just 1738 1.1 christos "help".) 1739 1.9 christos 1740 1.1 christos I am going to split this into two separate commands, help_cmd and 1741 1.1 christos help_list. */ 1742 1.1 christos 1743 1.3 christos void 1744 1.1 christos help_cmd (const char *command, struct ui_file *stream) 1745 1.9 christos { 1746 1.1 christos struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd; 1747 1.1 christos 1748 1.1 christos if (!command) 1749 1.1 christos { 1750 1.1 christos help_list (cmdlist, "", all_classes, stream); 1751 1.1 christos return; 1752 1.1 christos } 1753 1.1 christos 1754 1.1 christos if (strcmp (command, "all") == 0) 1755 1.1 christos { 1756 1.1 christos help_all (stream); 1757 1.1 christos return; 1758 1.1 christos } 1759 1.9 christos 1760 1.9 christos const char *orig_command = command; 1761 1.1 christos c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0); 1762 1.1 christos 1763 1.1 christos if (c == 0) 1764 1.1 christos return; 1765 1.9 christos 1766 1.9 christos lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd); 1767 1.1 christos 1768 1.10 christos /* There are three cases here. 1769 1.1 christos If c->subcommands is nonzero, we have a prefix command. 1770 1.1 christos Print its documentation, then list its subcommands. 1771 1.1 christos 1772 1.1 christos If c->func is non NULL, we really have a command. Print its 1773 1.1 christos documentation and return. 1774 1.1 christos 1775 1.1 christos If c->func is NULL, we have a class name. Print its 1776 1.1 christos documentation (as if it were a command) and then set class to the 1777 1.1 christos number of this class so that the commands in the class will be 1778 1.1 christos listed. */ 1779 1.10 christos 1780 1.10 christos if (alias == nullptr || !user_documented_alias (*alias)) 1781 1.11 christos { 1782 1.10 christos /* Case of a normal command, or an alias not explicitly 1783 1.10 christos documented by the user. */ 1784 1.10 christos /* If the user asked 'help somecommand' and there is no alias, 1785 1.10 christos the false indicates to not output the (single) command name. */ 1786 1.10 christos fput_command_names_styled (*c, false, "\n", stream); 1787 1.10 christos fput_aliases_definition_styled (*c, stream); 1788 1.10 christos gdb_puts (c->doc, stream); 1789 1.10 christos } 1790 1.10 christos else 1791 1.11 christos { 1792 1.10 christos /* Case of an alias explicitly documented by the user. 1793 1.10 christos Only output the alias definition and its explicit documentation. */ 1794 1.10 christos fput_alias_definition_styled (*alias, stream); 1795 1.10 christos fput_command_names_styled (*alias, false, "\n", stream); 1796 1.10 christos gdb_puts (alias->doc, stream); 1797 1.10 christos } 1798 1.1 christos gdb_puts ("\n", stream); 1799 1.10 christos 1800 1.1 christos if (!c->is_prefix () && !c->is_command_class_help ()) 1801 1.10 christos return; 1802 1.10 christos 1803 1.1 christos gdb_printf (stream, "\n"); 1804 1.1 christos 1805 1.10 christos /* If this is a prefix command, print it's subcommands. */ 1806 1.10 christos if (c->is_prefix ()) 1807 1.10 christos help_list (*c->subcommands, c->prefixname ().c_str (), 1808 1.1 christos all_commands, stream); 1809 1.1 christos 1810 1.10 christos /* If this is a class name, print all of the commands in the class. */ 1811 1.5 christos if (c->is_command_class_help ()) 1812 1.1 christos help_list (cmdlist, "", c->theclass, stream); 1813 1.1 christos 1814 1.10 christos if (c->hook_pre || c->hook_post) 1815 1.10 christos gdb_printf (stream, 1816 1.1 christos "\nThis command has a hook (or hooks) defined:\n"); 1817 1.1 christos 1818 1.10 christos if (c->hook_pre) 1819 1.10 christos gdb_printf (stream, 1820 1.10 christos "\tThis command is run after : %s (pre hook)\n", 1821 1.1 christos c->hook_pre->name); 1822 1.10 christos if (c->hook_post) 1823 1.10 christos gdb_printf (stream, 1824 1.10 christos "\tThis command is run before : %s (post hook)\n", 1825 1.1 christos c->hook_post->name); 1826 1.1 christos } 1827 1.1 christos 1828 1.1 christos /* 1829 1.1 christos * Get a specific kind of help on a command list. 1830 1.1 christos * 1831 1.1 christos * LIST is the list. 1832 1.11 christos * CMDTYPE is the prefix to use in the title string. 1833 1.1 christos * THECLASS is the class with which to list the nodes of this list (see 1834 1.1 christos * documentation for help_cmd_list below), As usual, ALL_COMMANDS for 1835 1.1 christos * everything, ALL_CLASSES for just classes, and non-negative for only things 1836 1.1 christos * in a specific class. 1837 1.1 christos * and STREAM is the output stream on which to print things. 1838 1.1 christos * If you call this routine with a class >= 0, it recurses. 1839 1.1 christos */ 1840 1.3 christos void 1841 1.5 christos help_list (struct cmd_list_element *list, const char *cmdtype, 1842 1.1 christos enum command_class theclass, struct ui_file *stream) 1843 1.1 christos { 1844 1.1 christos int len; 1845 1.1 christos char *cmdtype1, *cmdtype2; 1846 1.1 christos 1847 1.1 christos /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub". 1848 1.1 christos */ 1849 1.1 christos len = strlen (cmdtype); 1850 1.1 christos cmdtype1 = (char *) alloca (len + 1); 1851 1.1 christos cmdtype1[0] = 0; 1852 1.1 christos cmdtype2 = (char *) alloca (len + 4); 1853 1.1 christos cmdtype2[0] = 0; 1854 1.1 christos if (len) 1855 1.1 christos { 1856 1.8 christos cmdtype1[0] = ' '; 1857 1.1 christos memcpy (cmdtype1 + 1, cmdtype, len - 1); 1858 1.8 christos cmdtype1[len] = 0; 1859 1.1 christos memcpy (cmdtype2, cmdtype, len - 1); 1860 1.1 christos strcpy (cmdtype2 + len - 1, " sub"); 1861 1.1 christos } 1862 1.5 christos 1863 1.10 christos if (theclass == all_classes) 1864 1.1 christos gdb_printf (stream, "List of classes of %scommands:\n\n", cmdtype2); 1865 1.10 christos else 1866 1.1 christos gdb_printf (stream, "List of %scommands:\n\n", cmdtype2); 1867 1.9 christos 1868 1.1 christos help_cmd_list (list, theclass, theclass >= 0, stream); 1869 1.5 christos 1870 1.1 christos if (theclass == all_classes) 1871 1.10 christos { 1872 1.12 christos gdb_printf (stream, "\n\ 1873 1.12 christos Type \"%p[help%s%p]\" followed by a class name for a list of commands in ", 1874 1.12 christos command_style.style ().ptr (), 1875 1.12 christos cmdtype1, 1876 1.10 christos nullptr); 1877 1.10 christos stream->wrap_here (0); 1878 1.1 christos gdb_printf (stream, "that class."); 1879 1.10 christos 1880 1.12 christos gdb_printf (stream, "\n\ 1881 1.12 christos Type \"%ps\" for the list of all commands.", 1882 1.1 christos styled_string (command_style.style (), "help all")); 1883 1.1 christos } 1884 1.12 christos 1885 1.12 christos gdb_printf (stream, "\nType \"%p[help%s%p]\" followed by %scommand name ", 1886 1.12 christos command_style.style ().ptr (), cmdtype1, nullptr, 1887 1.10 christos cmdtype2); 1888 1.10 christos stream->wrap_here (0); 1889 1.10 christos gdb_puts ("for ", stream); 1890 1.10 christos stream->wrap_here (0); 1891 1.10 christos gdb_puts ("full ", stream); 1892 1.10 christos stream->wrap_here (0); 1893 1.12 christos gdb_puts ("documentation.\n", stream); 1894 1.12 christos gdb_printf (stream, 1895 1.12 christos "Type \"%ps\" to search " 1896 1.12 christos "for commands related to \"word\".\n", 1897 1.12 christos styled_string (command_style.style (), "apropos word")); 1898 1.12 christos gdb_printf (stream, "Type \"%ps\" for full documentation", 1899 1.10 christos styled_string (command_style.style (), "apropos -v word")); 1900 1.10 christos stream->wrap_here (0); 1901 1.10 christos gdb_puts (" of commands related to \"word\".\n", stream); 1902 1.10 christos gdb_puts ("Command name abbreviations are allowed if unambiguous.\n", 1903 1.1 christos stream); 1904 1.1 christos } 1905 1.1 christos 1906 1.1 christos static void 1907 1.1 christos help_all (struct ui_file *stream) 1908 1.1 christos { 1909 1.1 christos struct cmd_list_element *c; 1910 1.1 christos int seen_unclassified = 0; 1911 1.1 christos 1912 1.1 christos for (c = cmdlist; c; c = c->next) 1913 1.1 christos { 1914 1.10 christos if (c->abbrev_flag) 1915 1.1 christos continue; 1916 1.1 christos /* If this is a class name, print all of the commands in the 1917 1.1 christos class. */ 1918 1.10 christos 1919 1.1 christos if (c->is_command_class_help ()) 1920 1.10 christos { 1921 1.9 christos gdb_printf (stream, "\nCommand class: %s\n\n", c->name); 1922 1.1 christos help_cmd_list (cmdlist, c->theclass, true, stream); 1923 1.1 christos } 1924 1.1 christos } 1925 1.1 christos 1926 1.1 christos /* While it's expected that all commands are in some class, 1927 1.1 christos as a safety measure, we'll print commands outside of any 1928 1.1 christos class at the end. */ 1929 1.1 christos 1930 1.1 christos for (c = cmdlist; c; c = c->next) 1931 1.1 christos { 1932 1.10 christos if (c->abbrev_flag) 1933 1.1 christos continue; 1934 1.5 christos 1935 1.1 christos if (c->theclass == no_class) 1936 1.1 christos { 1937 1.1 christos if (!seen_unclassified) 1938 1.10 christos { 1939 1.1 christos gdb_printf (stream, "\nUnclassified commands\n\n"); 1940 1.1 christos seen_unclassified = 1; 1941 1.10 christos } 1942 1.1 christos print_help_for_command (*c, true, stream); 1943 1.1 christos } 1944 1.1 christos } 1945 1.1 christos 1946 1.1 christos } 1947 1.9 christos 1948 1.9 christos /* See cli-decode.h. */ 1949 1.1 christos 1950 1.9 christos void 1951 1.9 christos print_doc_line (struct ui_file *stream, const char *str, 1952 1.1 christos bool for_value_prefix) 1953 1.1 christos { 1954 1.1 christos static char *line_buffer = 0; 1955 1.3 christos static int line_size; 1956 1.1 christos const char *p; 1957 1.1 christos 1958 1.1 christos if (!line_buffer) 1959 1.1 christos { 1960 1.1 christos line_size = 80; 1961 1.1 christos line_buffer = (char *) xmalloc (line_size); 1962 1.1 christos } 1963 1.9 christos 1964 1.1 christos /* Searches for the first end of line or the end of STR. */ 1965 1.9 christos p = str; 1966 1.1 christos while (*p && *p != '\n') 1967 1.1 christos p++; 1968 1.1 christos if (p - str > line_size - 1) 1969 1.1 christos { 1970 1.1 christos line_size = p - str + 1; 1971 1.1 christos xfree (line_buffer); 1972 1.1 christos line_buffer = (char *) xmalloc (line_size); 1973 1.1 christos } 1974 1.9 christos strncpy (line_buffer, str, p - str); 1975 1.9 christos if (for_value_prefix) 1976 1.9 christos { 1977 1.9 christos if (islower (line_buffer[0])) 1978 1.9 christos line_buffer[0] = toupper (line_buffer[0]); 1979 1.9 christos gdb_assert (p > str); 1980 1.9 christos if (line_buffer[p - str - 1] == '.') 1981 1.9 christos line_buffer[p - str - 1] = '\0'; 1982 1.9 christos else 1983 1.9 christos line_buffer[p - str] = '\0'; 1984 1.9 christos } 1985 1.9 christos else 1986 1.10 christos line_buffer[p - str] = '\0'; 1987 1.1 christos gdb_puts (line_buffer, stream); 1988 1.1 christos } 1989 1.1 christos 1990 1.1 christos /* Print one-line help for command C. 1991 1.1 christos If RECURSE is non-zero, also print one-line descriptions 1992 1.1 christos of all prefixed subcommands. */ 1993 1.10 christos static void 1994 1.9 christos print_help_for_command (const cmd_list_element &c, 1995 1.1 christos bool recurse, struct ui_file *stream) 1996 1.9 christos { 1997 1.10 christos fput_command_names_styled (c, true, " -- ", stream); 1998 1.10 christos print_doc_line (stream, c.doc, false); 1999 1.10 christos gdb_puts ("\n", stream); 2000 1.9 christos if (!c.default_args.empty ()) 2001 1.9 christos fput_alias_definition_styled (c, stream); 2002 1.9 christos fput_aliases_definition_styled (c, stream); 2003 1.1 christos 2004 1.10 christos if (recurse 2005 1.10 christos && c.is_prefix () 2006 1.1 christos && c.abbrev_flag == 0) 2007 1.1 christos /* Subcommands of a prefix command typically have 'all_commands' 2008 1.1 christos as class. If we pass CLASS to recursive invocation, 2009 1.10 christos most often we won't see anything. */ 2010 1.1 christos help_cmd_list (*c.subcommands, all_commands, true, stream); 2011 1.1 christos } 2012 1.1 christos 2013 1.1 christos /* 2014 1.1 christos * Implement a help command on command list LIST. 2015 1.1 christos * RECURSE should be non-zero if this should be done recursively on 2016 1.1 christos * all sublists of LIST. 2017 1.6 christos * STREAM is the stream upon which the output should be written. 2018 1.1 christos * THECLASS should be: 2019 1.1 christos * A non-negative class number to list only commands in that 2020 1.1 christos * ALL_COMMANDS to list all commands in list. 2021 1.1 christos * ALL_CLASSES to list all classes in list. 2022 1.9 christos * 2023 1.9 christos * Note that aliases are only shown when THECLASS is class_alias. 2024 1.9 christos * In the other cases, the aliases will be shown together with their 2025 1.9 christos * aliased command. 2026 1.1 christos * 2027 1.1 christos * Note that RECURSE will be active on *all* sublists, not just the 2028 1.1 christos * ones selected by the criteria above (ie. the selection mechanism 2029 1.1 christos * is at the low level, not the high-level). 2030 1.9 christos */ 2031 1.9 christos 2032 1.5 christos static void 2033 1.9 christos help_cmd_list (struct cmd_list_element *list, enum command_class theclass, 2034 1.1 christos bool recurse, struct ui_file *stream) 2035 1.1 christos { 2036 1.1 christos struct cmd_list_element *c; 2037 1.1 christos 2038 1.6 christos for (c = list; c; c = c->next) 2039 1.9 christos { 2040 1.9 christos if (c->abbrev_flag == 1 || c->cmd_deprecated) 2041 1.9 christos { 2042 1.9 christos /* Do not show abbreviations or deprecated commands. */ 2043 1.9 christos continue; 2044 1.9 christos } 2045 1.10 christos 2046 1.9 christos if (c->is_alias () && theclass != class_alias) 2047 1.9 christos { 2048 1.9 christos /* Do not show an alias, unless specifically showing the 2049 1.9 christos list of aliases: for all other classes, an alias is 2050 1.9 christos shown (if needed) together with its aliased command. */ 2051 1.9 christos continue; 2052 1.9 christos } 2053 1.9 christos 2054 1.10 christos if (theclass == all_commands 2055 1.10 christos || (theclass == all_classes && c->is_command_class_help ()) 2056 1.9 christos || (theclass == c->theclass && !c->is_command_class_help ())) 2057 1.9 christos { 2058 1.10 christos /* show C when 2059 1.9 christos - showing all commands 2060 1.9 christos - showing all classes and C is a help class 2061 1.9 christos - showing commands of THECLASS and C is not the help class */ 2062 1.9 christos 2063 1.9 christos /* If we show the class_alias and C is an alias, do not recurse, 2064 1.9 christos as this would show the (possibly very long) not very useful 2065 1.9 christos list of sub-commands of the aliased command. */ 2066 1.10 christos print_help_for_command 2067 1.10 christos (*c, 2068 1.9 christos recurse && (theclass != class_alias || !c->is_alias ()), 2069 1.9 christos stream); 2070 1.9 christos continue; 2071 1.9 christos } 2072 1.9 christos 2073 1.9 christos if (recurse 2074 1.10 christos && (theclass == class_user || theclass == class_alias) 2075 1.9 christos && c->is_prefix ()) 2076 1.9 christos { 2077 1.10 christos /* User-defined commands or aliases may be subcommands. */ 2078 1.9 christos help_cmd_list (*c->subcommands, theclass, recurse, stream); 2079 1.9 christos continue; 2080 1.9 christos } 2081 1.9 christos 2082 1.9 christos /* Do not show C or recurse on C, e.g. because C does not belong to 2083 1.1 christos THECLASS or because C is a help class. */ 2084 1.1 christos } 2085 1.1 christos } 2086 1.1 christos 2087 1.1 christos 2089 1.1 christos /* Search the input clist for 'command'. Return the command if 2090 1.1 christos found (or NULL if not), and return the number of commands 2091 1.1 christos found in nfound. */ 2092 1.1 christos 2093 1.1 christos static struct cmd_list_element * 2094 1.1 christos find_cmd (const char *command, int len, struct cmd_list_element *clist, 2095 1.1 christos int ignore_help_classes, int *nfound) 2096 1.1 christos { 2097 1.6 christos struct cmd_list_element *found, *c; 2098 1.1 christos 2099 1.1 christos found = NULL; 2100 1.1 christos *nfound = 0; 2101 1.10 christos for (c = clist; c; c = c->next) 2102 1.1 christos if (!strncmp (command, c->name, len) 2103 1.1 christos && (!ignore_help_classes || !c->is_command_class_help ())) 2104 1.1 christos { 2105 1.1 christos found = c; 2106 1.1 christos (*nfound)++; 2107 1.1 christos if (c->name[len] == '\0') 2108 1.1 christos { 2109 1.1 christos *nfound = 1; 2110 1.1 christos break; 2111 1.1 christos } 2112 1.1 christos } 2113 1.1 christos return found; 2114 1.7 christos } 2115 1.7 christos 2116 1.7 christos /* Return the length of command name in TEXT. */ 2117 1.1 christos 2118 1.1 christos int 2119 1.1 christos find_command_name_length (const char *text) 2120 1.1 christos { 2121 1.1 christos const char *p = text; 2122 1.1 christos 2123 1.1 christos /* Treating underscores as part of command words is important 2124 1.1 christos so that "set args_foo()" doesn't get interpreted as 2125 1.1 christos "set args _foo()". */ 2126 1.1 christos /* Some characters are only used for TUI specific commands. 2127 1.1 christos However, they are always allowed for the sake of consistency. 2128 1.1 christos 2129 1.1 christos Note that this is larger than the character set allowed when 2130 1.9 christos creating user-defined commands. */ 2131 1.1 christos 2132 1.9 christos /* Recognize the single character commands so that, e.g., "!ls" 2133 1.1 christos works as expected. */ 2134 1.1 christos if (*p == '!' || *p == '|') 2135 1.9 christos return 1; 2136 1.1 christos 2137 1.5 christos while (valid_cmd_char_p (*p) 2138 1.1 christos /* Characters used by TUI specific commands. */ 2139 1.1 christos || *p == '+' || *p == '<' || *p == '>' || *p == '$') 2140 1.1 christos p++; 2141 1.1 christos 2142 1.1 christos return p - text; 2143 1.9 christos } 2144 1.9 christos 2145 1.9 christos /* See command.h. */ 2146 1.9 christos 2147 1.9 christos bool 2148 1.9 christos valid_cmd_char_p (int c) 2149 1.9 christos { 2150 1.1 christos /* Alas "42" is a legitimate user-defined command. 2151 1.9 christos In the interests of not breaking anything we preserve that. */ 2152 1.9 christos 2153 1.9 christos return isalnum (c) || c == '-' || c == '_' || c == '.'; 2154 1.9 christos } 2155 1.9 christos 2156 1.9 christos /* See command.h. */ 2157 1.1 christos 2158 1.1 christos bool 2159 1.1 christos valid_user_defined_cmd_name_p (const char *name) 2160 1.1 christos { 2161 1.1 christos const char *p; 2162 1.9 christos 2163 1.1 christos if (*name == '\0') 2164 1.1 christos return false; 2165 1.1 christos 2166 1.9 christos for (p = name; *p != '\0'; ++p) 2167 1.1 christos { 2168 1.1 christos if (valid_cmd_char_p (*p)) 2169 1.9 christos ; /* Ok. */ 2170 1.1 christos else 2171 1.1 christos return false; 2172 1.9 christos } 2173 1.1 christos 2174 1.1 christos return true; 2175 1.10 christos } 2176 1.1 christos 2177 1.1 christos /* See command.h. */ 2178 1.1 christos 2179 1.9 christos struct cmd_list_element * 2180 1.10 christos lookup_cmd_1 (const char **text, struct cmd_list_element *clist, 2181 1.1 christos struct cmd_list_element **result_list, std::string *default_args, 2182 1.1 christos int ignore_help_classes, bool lookup_for_completion_p) 2183 1.8 christos { 2184 1.1 christos char *command; 2185 1.9 christos int len, nfound; 2186 1.1 christos struct cmd_list_element *found, *c; 2187 1.1 christos bool found_alias = false; 2188 1.1 christos const char *line = *text; 2189 1.1 christos 2190 1.1 christos while (**text == ' ' || **text == '\t') 2191 1.1 christos (*text)++; 2192 1.1 christos 2193 1.1 christos /* Identify the name of the command. */ 2194 1.1 christos len = find_command_name_length (*text); 2195 1.1 christos 2196 1.1 christos /* If nothing but whitespace, return 0. */ 2197 1.1 christos if (len == 0) 2198 1.1 christos return 0; 2199 1.1 christos 2200 1.1 christos /* *text and p now bracket the first command word to lookup (and 2201 1.1 christos it's length is len). We copy this into a local temporary. */ 2202 1.1 christos 2203 1.1 christos 2204 1.1 christos command = (char *) alloca (len + 1); 2205 1.1 christos memcpy (command, *text, len); 2206 1.1 christos command[len] = '\0'; 2207 1.1 christos 2208 1.1 christos /* Look it up. */ 2209 1.1 christos found = 0; 2210 1.1 christos nfound = 0; 2211 1.1 christos found = find_cmd (command, len, clist, ignore_help_classes, &nfound); 2212 1.1 christos 2213 1.1 christos /* If nothing matches, we have a simple failure. */ 2214 1.1 christos if (nfound == 0) 2215 1.1 christos return 0; 2216 1.1 christos 2217 1.9 christos if (nfound > 1) 2218 1.1 christos { 2219 1.1 christos if (result_list != nullptr) 2220 1.1 christos /* Will be modified in calling routine 2221 1.9 christos if we know what the prefix command is. */ 2222 1.9 christos *result_list = 0; 2223 1.1 christos if (default_args != nullptr) 2224 1.1 christos *default_args = std::string (); 2225 1.1 christos return CMD_LIST_AMBIGUOUS; /* Ambiguous. */ 2226 1.1 christos } 2227 1.1 christos 2228 1.1 christos /* We've matched something on this list. Move text pointer forward. */ 2229 1.1 christos 2230 1.10 christos *text += len; 2231 1.1 christos 2232 1.1 christos if (found->is_alias ()) 2233 1.1 christos { 2234 1.1 christos /* We drop the alias (abbreviation) in favor of the command it 2235 1.1 christos is pointing to. If the alias is deprecated, though, we need to 2236 1.1 christos warn the user about it before we drop it. Note that while we 2237 1.1 christos are warning about the alias, we may also warn about the command 2238 1.9 christos itself and we will adjust the appropriate DEPRECATED_WARN_USER 2239 1.10 christos flags. */ 2240 1.10 christos 2241 1.10 christos if (found->deprecated_warn_user && !lookup_for_completion_p) 2242 1.9 christos deprecated_cmd_warning (line, clist); 2243 1.9 christos 2244 1.9 christos 2245 1.9 christos /* Return the default_args of the alias, not the default_args 2246 1.9 christos of the command it is pointing to. */ 2247 1.10 christos if (default_args != nullptr) 2248 1.9 christos *default_args = found->default_args; 2249 1.1 christos found = found->alias_target; 2250 1.1 christos found_alias = true; 2251 1.1 christos } 2252 1.10 christos /* If we found a prefix command, keep looking. */ 2253 1.1 christos 2254 1.10 christos if (found->is_prefix ()) 2255 1.10 christos { 2256 1.1 christos c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args, 2257 1.1 christos ignore_help_classes, lookup_for_completion_p); 2258 1.1 christos if (!c) 2259 1.9 christos { 2260 1.1 christos /* Didn't find anything; this is as far as we got. */ 2261 1.9 christos if (result_list != nullptr) 2262 1.9 christos *result_list = clist; 2263 1.1 christos if (!found_alias && default_args != nullptr) 2264 1.1 christos *default_args = found->default_args; 2265 1.1 christos return found; 2266 1.1 christos } 2267 1.1 christos else if (c == CMD_LIST_AMBIGUOUS) 2268 1.1 christos { 2269 1.1 christos /* We've gotten this far properly, but the next step is 2270 1.9 christos ambiguous. We need to set the result list to the best 2271 1.1 christos we've found (if an inferior hasn't already set it). */ 2272 1.10 christos if (result_list != nullptr) 2273 1.10 christos if (!*result_list) 2274 1.10 christos /* This used to say *result_list = *found->subcommands. 2275 1.10 christos If that was correct, need to modify the documentation 2276 1.1 christos at the top of this function to clarify what is 2277 1.9 christos supposed to be going on. */ 2278 1.9 christos *result_list = found; 2279 1.9 christos /* For ambiguous commands, do not return any default_args args. */ 2280 1.1 christos if (default_args != nullptr) 2281 1.1 christos *default_args = std::string (); 2282 1.1 christos return c; 2283 1.1 christos } 2284 1.1 christos else 2285 1.1 christos { 2286 1.1 christos /* We matched! */ 2287 1.1 christos return c; 2288 1.1 christos } 2289 1.1 christos } 2290 1.9 christos else 2291 1.1 christos { 2292 1.9 christos if (result_list != nullptr) 2293 1.9 christos *result_list = clist; 2294 1.1 christos if (!found_alias && default_args != nullptr) 2295 1.1 christos *default_args = found->default_args; 2296 1.1 christos return found; 2297 1.1 christos } 2298 1.1 christos } 2299 1.1 christos 2300 1.1 christos /* All this hair to move the space to the front of cmdtype */ 2301 1.1 christos 2302 1.1 christos static void 2303 1.1 christos undef_cmd_error (const char *cmdtype, const char *q) 2304 1.1 christos { 2305 1.1 christos error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."), 2306 1.1 christos cmdtype, 2307 1.1 christos q, 2308 1.1 christos *cmdtype ? " " : "", 2309 1.1 christos (int) strlen (cmdtype) - 1, 2310 1.1 christos cmdtype); 2311 1.1 christos } 2312 1.1 christos 2313 1.9 christos /* Look up the contents of *LINE as a command in the command list LIST. 2314 1.9 christos LIST is a chain of struct cmd_list_element's. 2315 1.9 christos If it is found, return the struct cmd_list_element for that command, 2316 1.9 christos update *LINE to point after the command name, at the first argument 2317 1.9 christos and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default 2318 1.9 christos args to prepend to the user provided args when running the command. 2319 1.9 christos Note that if the found cmd_list_element is found via an alias, 2320 1.1 christos the default args of the alias are returned. 2321 1.1 christos 2322 1.1 christos If not found, call error if ALLOW_UNKNOWN is zero 2323 1.1 christos otherwise (or if error returns) return zero. 2324 1.1 christos Call error if specified command is ambiguous, 2325 1.1 christos unless ALLOW_UNKNOWN is negative. 2326 1.9 christos CMDTYPE precedes the word "command" in the error message. 2327 1.1 christos 2328 1.1 christos If IGNORE_HELP_CLASSES is nonzero, ignore any command list 2329 1.1 christos elements which are actually help classes rather than commands (i.e. 2330 1.1 christos the function field of the struct cmd_list_element is 0). */ 2331 1.7 christos 2332 1.7 christos struct cmd_list_element * 2333 1.9 christos lookup_cmd (const char **line, struct cmd_list_element *list, 2334 1.1 christos const char *cmdtype, 2335 1.1 christos std::string *default_args, 2336 1.1 christos int allow_unknown, int ignore_help_classes) 2337 1.1 christos { 2338 1.1 christos struct cmd_list_element *last_list = 0; 2339 1.1 christos struct cmd_list_element *c; 2340 1.1 christos 2341 1.1 christos /* Note: Do not remove trailing whitespace here because this 2342 1.1 christos would be wrong for complete_command. Jim Kingdon */ 2343 1.1 christos 2344 1.1 christos if (!*line) 2345 1.9 christos error (_("Lack of needed %scommand"), cmdtype); 2346 1.1 christos 2347 1.1 christos c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes); 2348 1.1 christos 2349 1.1 christos if (!c) 2350 1.1 christos { 2351 1.1 christos if (!allow_unknown) 2352 1.1 christos { 2353 1.1 christos char *q; 2354 1.1 christos int len = find_command_name_length (*line); 2355 1.1 christos 2356 1.1 christos q = (char *) alloca (len + 1); 2357 1.1 christos strncpy (q, *line, len); 2358 1.1 christos q[len] = '\0'; 2359 1.1 christos undef_cmd_error (cmdtype, q); 2360 1.1 christos } 2361 1.1 christos else 2362 1.1 christos return 0; 2363 1.1 christos } 2364 1.10 christos else if (c == CMD_LIST_AMBIGUOUS) 2365 1.10 christos { 2366 1.1 christos /* Ambigous. Local values should be off subcommands or called 2367 1.1 christos values. */ 2368 1.10 christos int local_allow_unknown = (last_list ? last_list->allow_unknown : 2369 1.10 christos allow_unknown); 2370 1.1 christos std::string local_cmdtype 2371 1.10 christos = last_list ? last_list->prefixname () : cmdtype; 2372 1.1 christos struct cmd_list_element *local_list = 2373 1.1 christos (last_list ? *(last_list->subcommands) : list); 2374 1.1 christos 2375 1.1 christos if (local_allow_unknown < 0) 2376 1.1 christos { 2377 1.1 christos if (last_list) 2378 1.1 christos return last_list; /* Found something. */ 2379 1.1 christos else 2380 1.1 christos return 0; /* Found nothing. */ 2381 1.1 christos } 2382 1.1 christos else 2383 1.1 christos { 2384 1.1 christos /* Report as error. */ 2385 1.1 christos int amb_len; 2386 1.1 christos char ambbuf[100]; 2387 1.1 christos 2388 1.1 christos for (amb_len = 0; 2389 1.1 christos ((*line)[amb_len] && (*line)[amb_len] != ' ' 2390 1.1 christos && (*line)[amb_len] != '\t'); 2391 1.1 christos amb_len++) 2392 1.1 christos ; 2393 1.1 christos 2394 1.1 christos ambbuf[0] = 0; 2395 1.1 christos for (c = local_list; c; c = c->next) 2396 1.1 christos if (!strncmp (*line, c->name, amb_len)) 2397 1.1 christos { 2398 1.1 christos if (strlen (ambbuf) + strlen (c->name) + 6 2399 1.1 christos < (int) sizeof ambbuf) 2400 1.1 christos { 2401 1.1 christos if (strlen (ambbuf)) 2402 1.1 christos strcat (ambbuf, ", "); 2403 1.1 christos strcat (ambbuf, c->name); 2404 1.1 christos } 2405 1.1 christos else 2406 1.1 christos { 2407 1.1 christos strcat (ambbuf, ".."); 2408 1.1 christos break; 2409 1.10 christos } 2410 1.10 christos } 2411 1.1 christos error (_("Ambiguous %scommand \"%s\": %s."), 2412 1.1 christos local_cmdtype.c_str (), *line, ambbuf); 2413 1.1 christos } 2414 1.1 christos } 2415 1.1 christos else 2416 1.10 christos { 2417 1.1 christos if (c->type == set_cmd && **line != '\0' && !isspace (**line)) 2418 1.1 christos error (_("Argument must be preceded by space.")); 2419 1.10 christos 2420 1.1 christos /* We've got something. It may still not be what the caller 2421 1.1 christos wants (if this command *needs* a subcommand). */ 2422 1.1 christos while (**line == ' ' || **line == '\t') 2423 1.10 christos (*line)++; 2424 1.10 christos 2425 1.1 christos if (c->is_prefix () && **line && !c->allow_unknown) 2426 1.1 christos undef_cmd_error (c->prefixname ().c_str (), *line); 2427 1.1 christos 2428 1.1 christos /* Seems to be what he wants. Return it. */ 2429 1.1 christos return c; 2430 1.1 christos } 2431 1.1 christos return 0; 2432 1.10 christos } 2433 1.10 christos 2434 1.10 christos /* See command.h. */ 2435 1.10 christos 2436 1.10 christos struct cmd_list_element * 2437 1.10 christos lookup_cmd_exact (const char *name, 2438 1.10 christos struct cmd_list_element *list, 2439 1.10 christos bool ignore_help_classes) 2440 1.10 christos { 2441 1.10 christos const char *tem = name; 2442 1.10 christos struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1, 2443 1.10 christos ignore_help_classes); 2444 1.10 christos if (cmd != nullptr && strcmp (name, cmd->name) != 0) 2445 1.10 christos cmd = nullptr; 2446 1.10 christos return cmd; 2447 1.1 christos } 2448 1.1 christos 2449 1.1 christos /* We are here presumably because an alias or command in TEXT is 2450 1.1 christos deprecated and a warning message should be generated. This 2451 1.1 christos function decodes TEXT and potentially generates a warning message 2452 1.1 christos as outlined below. 2453 1.1 christos 2454 1.1 christos Example for 'set endian big' which has a fictitious alias 'seb'. 2455 1.1 christos 2456 1.1 christos If alias wasn't used in TEXT, and the command is deprecated: 2457 1.1 christos "warning: 'set endian big' is deprecated." 2458 1.1 christos 2459 1.1 christos If alias was used, and only the alias is deprecated: 2460 1.1 christos "warning: 'seb' an alias for the command 'set endian big' is deprecated." 2461 1.1 christos 2462 1.1 christos If alias was used and command is deprecated (regardless of whether 2463 1.1 christos the alias itself is deprecated: 2464 1.1 christos 2465 1.1 christos "warning: 'set endian big' (seb) is deprecated." 2466 1.1 christos 2467 1.1 christos After the message has been sent, clear the appropriate flags in the 2468 1.1 christos command and/or the alias so the user is no longer bothered. 2469 1.1 christos 2470 1.10 christos */ 2471 1.1 christos void 2472 1.10 christos deprecated_cmd_warning (const char *text, struct cmd_list_element *list) 2473 1.10 christos { 2474 1.1 christos struct cmd_list_element *alias = nullptr; 2475 1.10 christos struct cmd_list_element *cmd = nullptr; 2476 1.10 christos 2477 1.10 christos /* Return if text doesn't evaluate to a command. We place this lookup 2478 1.10 christos within its own scope so that the PREFIX_CMD local is not visible 2479 1.10 christos later in this function. The value returned in PREFIX_CMD is based on 2480 1.10 christos the prefix found in TEXT, and is our case this prefix can be missing 2481 1.10 christos in some situations (when LIST is not the global CMDLIST). 2482 1.10 christos 2483 1.10 christos It is better for our purposes to use the prefix commands directly from 2484 1.10 christos the ALIAS and CMD results. */ 2485 1.10 christos { 2486 1.10 christos struct cmd_list_element *prefix_cmd = nullptr; 2487 1.10 christos if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list)) 2488 1.1 christos return; 2489 1.10 christos } 2490 1.10 christos 2491 1.10 christos /* Return if nothing is deprecated. */ 2492 1.1 christos if (!((alias != nullptr ? alias->deprecated_warn_user : 0) 2493 1.1 christos || cmd->deprecated_warn_user)) 2494 1.10 christos return; 2495 1.10 christos 2496 1.10 christos /* Join command prefix (if any) and the command name. */ 2497 1.10 christos std::string tmp_cmd_str; 2498 1.10 christos if (cmd->prefix != nullptr) 2499 1.10 christos tmp_cmd_str += cmd->prefix->prefixname (); 2500 1.10 christos tmp_cmd_str += std::string (cmd->name); 2501 1.10 christos 2502 1.10 christos /* Display the appropriate first line, this warns that the thing the user 2503 1.10 christos entered is deprecated. */ 2504 1.10 christos if (alias != nullptr) 2505 1.10 christos { 2506 1.10 christos /* Join the alias prefix (if any) and the alias name. */ 2507 1.10 christos std::string tmp_alias_str; 2508 1.10 christos if (alias->prefix != nullptr) 2509 1.10 christos tmp_alias_str += alias->prefix->prefixname (); 2510 1.10 christos tmp_alias_str += std::string (alias->name); 2511 1.10 christos 2512 1.12 christos if (cmd->cmd_deprecated) 2513 1.10 christos gdb_printf (_("Warning: command '%ps' (%ps) is deprecated.\n"), 2514 1.12 christos styled_string (command_style.style (), 2515 1.10 christos tmp_cmd_str.c_str ()), 2516 1.10 christos styled_string (command_style.style (), 2517 1.10 christos tmp_alias_str.c_str ())); 2518 1.10 christos else 2519 1.12 christos gdb_printf (_("Warning: '%ps', an alias for the command '%ps', " 2520 1.10 christos "is deprecated.\n"), 2521 1.12 christos styled_string (command_style.style (), 2522 1.10 christos tmp_alias_str.c_str ()), 2523 1.10 christos styled_string (command_style.style (), 2524 1.10 christos tmp_cmd_str.c_str ())); 2525 1.10 christos } 2526 1.12 christos else 2527 1.10 christos gdb_printf (_("Warning: command '%ps' is deprecated.\n"), 2528 1.10 christos styled_string (command_style.style (), 2529 1.10 christos tmp_cmd_str.c_str ())); 2530 1.10 christos 2531 1.10 christos /* Now display a second line indicating what the user should use instead. 2532 1.10 christos If it is only the alias that is deprecated, we want to indicate the 2533 1.10 christos new alias, otherwise we'll indicate the new command. */ 2534 1.10 christos const char *replacement; 2535 1.1 christos if (alias != nullptr && !cmd->cmd_deprecated) 2536 1.10 christos replacement = alias->replacement; 2537 1.10 christos else 2538 1.10 christos replacement = cmd->replacement; 2539 1.12 christos if (replacement != nullptr) 2540 1.10 christos gdb_printf (_("Use '%ps'.\n\n"), 2541 1.1 christos styled_string (command_style.style (), 2542 1.10 christos replacement)); 2543 1.1 christos else 2544 1.1 christos gdb_printf (_("No alternative known.\n\n")); 2545 1.10 christos 2546 1.3 christos /* We've warned you, now we'll keep quiet. */ 2547 1.3 christos if (alias != nullptr) 2548 1.1 christos alias->deprecated_warn_user = 0; 2549 1.1 christos cmd->deprecated_warn_user = 0; 2550 1.10 christos } 2551 1.1 christos 2552 1.9 christos /* Look up the contents of TEXT as a command in the command list CUR_LIST. 2553 1.9 christos Return 1 on success, 0 on failure. 2554 1.9 christos 2555 1.9 christos If TEXT refers to an alias, *ALIAS will point to that alias. 2556 1.9 christos 2557 1.9 christos If TEXT is a subcommand (i.e. one that is preceded by a prefix 2558 1.11 christos command) set *PREFIX_CMD. 2559 1.11 christos 2560 1.9 christos Set *CMD to point to the command TEXT indicates, or to 2561 1.9 christos CMD_LIST_AMBIGUOUS if there are multiple possible matches. 2562 1.1 christos 2563 1.9 christos If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not 2564 1.1 christos exist, they are NULL when we return. 2565 1.1 christos 2566 1.10 christos */ 2567 1.10 christos 2568 1.10 christos static int 2569 1.10 christos lookup_cmd_composition_1 (const char *text, 2570 1.10 christos struct cmd_list_element **alias, 2571 1.10 christos struct cmd_list_element **prefix_cmd, 2572 1.10 christos struct cmd_list_element **cmd, 2573 1.10 christos struct cmd_list_element *cur_list) 2574 1.10 christos { 2575 1.10 christos *alias = nullptr; 2576 1.9 christos *prefix_cmd = cur_list->prefix; 2577 1.9 christos *cmd = nullptr; 2578 1.9 christos 2579 1.10 christos text = skip_spaces (text); 2580 1.10 christos 2581 1.1 christos /* Go through as many command lists as we need to, to find the command 2582 1.9 christos TEXT refers to. */ 2583 1.1 christos while (1) 2584 1.10 christos { 2585 1.9 christos /* Identify the name of the command. */ 2586 1.1 christos int len = find_command_name_length (text); 2587 1.1 christos 2588 1.1 christos /* If nothing but whitespace, return. */ 2589 1.9 christos if (len == 0) 2590 1.9 christos return 0; 2591 1.9 christos 2592 1.10 christos /* TEXT is the start of the first command word to lookup (and 2593 1.9 christos it's length is LEN). We copy this into a local temporary. */ 2594 1.1 christos std::string command (text, len); 2595 1.10 christos 2596 1.10 christos /* Look it up. */ 2597 1.9 christos int nfound = 0; 2598 1.10 christos *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound); 2599 1.11 christos 2600 1.11 christos /* We only handle the case where a single command was found. */ 2601 1.11 christos if (nfound > 1) 2602 1.11 christos { 2603 1.11 christos *cmd = CMD_LIST_AMBIGUOUS; 2604 1.11 christos return 0; 2605 1.10 christos } 2606 1.1 christos else if (*cmd == nullptr) 2607 1.1 christos return 0; 2608 1.10 christos else 2609 1.1 christos { 2610 1.10 christos if ((*cmd)->is_alias ()) 2611 1.10 christos { 2612 1.1 christos /* If the command was actually an alias, we note that an 2613 1.10 christos alias was used (by assigning *ALIAS) and we set *CMD. */ 2614 1.1 christos *alias = *cmd; 2615 1.1 christos *cmd = (*cmd)->alias_target; 2616 1.9 christos } 2617 1.9 christos } 2618 1.9 christos 2619 1.9 christos text += len; 2620 1.10 christos text = skip_spaces (text); 2621 1.10 christos 2622 1.10 christos if ((*cmd)->is_prefix () && *text != '\0') 2623 1.10 christos { 2624 1.10 christos cur_list = *(*cmd)->subcommands; 2625 1.1 christos *prefix_cmd = *cmd; 2626 1.1 christos } 2627 1.1 christos else 2628 1.1 christos return 1; 2629 1.1 christos } 2630 1.10 christos } 2631 1.10 christos 2632 1.10 christos /* Look up the contents of TEXT as a command in the command list 'cmdlist'. 2633 1.10 christos Return 1 on success, 0 on failure. 2634 1.10 christos 2635 1.10 christos If TEXT refers to an alias, *ALIAS will point to that alias. 2636 1.10 christos 2637 1.10 christos If TEXT is a subcommand (i.e. one that is preceded by a prefix 2638 1.11 christos command) set *PREFIX_CMD. 2639 1.11 christos 2640 1.10 christos Set *CMD to point to the command TEXT indicates, or to 2641 1.10 christos CMD_LIST_AMBIGUOUS if there are multiple possible matches. 2642 1.10 christos 2643 1.10 christos If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not 2644 1.10 christos exist, they are NULL when we return. 2645 1.10 christos 2646 1.10 christos */ 2647 1.10 christos 2648 1.10 christos int 2649 1.10 christos lookup_cmd_composition (const char *text, 2650 1.10 christos struct cmd_list_element **alias, 2651 1.10 christos struct cmd_list_element **prefix_cmd, 2652 1.10 christos struct cmd_list_element **cmd) 2653 1.10 christos { 2654 1.10 christos return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist); 2655 1.1 christos } 2656 1.1 christos 2657 1.1 christos /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ 2658 1.1 christos 2659 1.1 christos /* Return a vector of char pointers which point to the different 2660 1.1 christos possible completions in LIST of TEXT. 2661 1.1 christos 2662 1.1 christos WORD points in the same buffer as TEXT, and completions should be 2663 1.1 christos returned relative to this position. For example, suppose TEXT is 2664 1.1 christos "foo" and we want to complete to "foobar". If WORD is "oo", return 2665 1.8 christos "oobar"; if WORD is "baz/foo", return "baz/foobar". */ 2666 1.1 christos 2667 1.8 christos void 2668 1.1 christos complete_on_cmdlist (struct cmd_list_element *list, 2669 1.1 christos completion_tracker &tracker, 2670 1.1 christos const char *text, const char *word, 2671 1.1 christos int ignore_help_classes) 2672 1.1 christos { 2673 1.1 christos struct cmd_list_element *ptr; 2674 1.1 christos int textlen = strlen (text); 2675 1.1 christos int pass; 2676 1.1 christos int saw_deprecated_match = 0; 2677 1.1 christos 2678 1.1 christos /* We do one or two passes. In the first pass, we skip deprecated 2679 1.1 christos commands. If we see no matching commands in the first pass, and 2680 1.8 christos if we did happen to see a matching deprecated command, we do 2681 1.1 christos another loop to collect those. */ 2682 1.8 christos for (pass = 0; pass < 2; ++pass) 2683 1.8 christos { 2684 1.1 christos bool got_matches = false; 2685 1.1 christos 2686 1.1 christos for (ptr = list; ptr; ptr = ptr->next) 2687 1.10 christos if (!strncmp (ptr->name, text, textlen) 2688 1.10 christos && !ptr->abbrev_flag 2689 1.1 christos && (!ignore_help_classes || !ptr->is_command_class_help () 2690 1.1 christos || ptr->is_prefix ())) 2691 1.1 christos { 2692 1.3 christos if (pass == 0) 2693 1.1 christos { 2694 1.1 christos if (ptr->cmd_deprecated) 2695 1.1 christos { 2696 1.1 christos saw_deprecated_match = 1; 2697 1.1 christos continue; 2698 1.1 christos } 2699 1.8 christos } 2700 1.8 christos 2701 1.8 christos tracker.add_completion 2702 1.1 christos (make_completion_match_str (ptr->name, text, word)); 2703 1.8 christos got_matches = true; 2704 1.8 christos } 2705 1.8 christos 2706 1.8 christos if (got_matches) 2707 1.1 christos break; 2708 1.1 christos 2709 1.1 christos /* If we saw no matching deprecated commands in the first pass, 2710 1.1 christos just bail out. */ 2711 1.1 christos if (!saw_deprecated_match) 2712 1.1 christos break; 2713 1.1 christos } 2714 1.1 christos } 2715 1.1 christos 2716 1.8 christos /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ 2717 1.1 christos 2718 1.1 christos /* Add the different possible completions in ENUMLIST of TEXT. 2719 1.1 christos 2720 1.1 christos WORD points in the same buffer as TEXT, and completions should be 2721 1.1 christos returned relative to this position. For example, suppose TEXT is "foo" 2722 1.1 christos and we want to complete to "foobar". If WORD is "oo", return 2723 1.8 christos "oobar"; if WORD is "baz/foo", return "baz/foobar". */ 2724 1.8 christos 2725 1.8 christos void 2726 1.1 christos complete_on_enum (completion_tracker &tracker, 2727 1.1 christos const char *const *enumlist, 2728 1.1 christos const char *text, const char *word) 2729 1.1 christos { 2730 1.1 christos int textlen = strlen (text); 2731 1.1 christos int i; 2732 1.1 christos const char *name; 2733 1.1 christos 2734 1.8 christos for (i = 0; (name = enumlist[i]) != NULL; i++) 2735 1.1 christos if (strncmp (name, text, textlen) == 0) 2736 1.1 christos tracker.add_completion (make_completion_match_str (name, text, word)); 2737 1.1 christos } 2738 1.1 christos 2739 1.8 christos /* Call the command function. */ 2740 1.1 christos void 2741 1.10 christos cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty) 2742 1.6 christos { 2743 1.11 christos if (!cmd->is_command_class_help ()) 2744 1.6 christos { 2745 1.6 christos std::optional<scoped_restore_tmpl<bool>> restore_suppress; 2746 1.10 christos 2747 1.6 christos if (cmd->suppress_notification != NULL) 2748 1.10 christos restore_suppress.emplace (cmd->suppress_notification, true); 2749 1.6 christos 2750 1.1 christos cmd->func (args, from_tty, cmd); 2751 1.1 christos } 2752 1.1 christos else 2753 1.3 christos error (_("Invalid command")); 2754 1.3 christos } 2755 1.3 christos 2756 1.3 christos int 2757 1.10 christos cli_user_command_p (struct cmd_list_element *cmd) 2758 1.3 christos { 2759 return cmd->theclass == class_user && cmd->func == do_simple_func; 2760 } 2761