cli-decode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Header file for GDB command decoding library.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef CLI_CLI_DECODE_H
  14. #define CLI_CLI_DECODE_H
  15. /* This file defines the private interfaces for any code implementing
  16. command internals. */
  17. /* Include the public interfaces. */
  18. #include "command.h"
  19. #include "gdbsupport/gdb_regex.h"
  20. #include "cli-script.h"
  21. #include "completer.h"
  22. #include "gdbsupport/intrusive_list.h"
  23. #include "gdbsupport/buildargv.h"
  24. /* Not a set/show command. Note that some commands which begin with
  25. "set" or "show" might be in this category, if their syntax does
  26. not fall into one of the following categories. */
  27. enum cmd_types
  28. {
  29. not_set_cmd,
  30. set_cmd,
  31. show_cmd
  32. };
  33. /* This structure records one command'd definition. */
  34. struct cmd_list_element
  35. {
  36. cmd_list_element (const char *name_, enum command_class theclass_,
  37. const char *doc_)
  38. : name (name_),
  39. theclass (theclass_),
  40. cmd_deprecated (0),
  41. deprecated_warn_user (0),
  42. malloced_replacement (0),
  43. doc_allocated (0),
  44. name_allocated (0),
  45. hook_in (0),
  46. allow_unknown (0),
  47. abbrev_flag (0),
  48. type (not_set_cmd),
  49. doc (doc_)
  50. {
  51. memset (&function, 0, sizeof (function));
  52. }
  53. ~cmd_list_element ()
  54. {
  55. if (doc && doc_allocated)
  56. xfree ((char *) doc);
  57. if (name_allocated)
  58. xfree ((char *) name);
  59. }
  60. DISABLE_COPY_AND_ASSIGN (cmd_list_element);
  61. /* For prefix commands, return a string containing prefix commands to
  62. get here: this one plus any others needed to get to it. Ends in a
  63. space. It is used before the word "command" in describing the
  64. commands reached through this prefix.
  65. For non-prefix commands, return an empty string. */
  66. std::string prefixname () const;
  67. /* Return a vector of strings describing the components of the full name
  68. of this command. For example, if this command is 'set AA BB CC',
  69. then the vector will contain 4 elements 'set', 'AA', 'BB', and 'CC'
  70. in that order. */
  71. std::vector<std::string> command_components () const;
  72. /* Return true if this command is an alias of another command. */
  73. bool is_alias () const
  74. { return this->alias_target != nullptr; }
  75. /* Return true if this command is a prefix command. */
  76. bool is_prefix () const
  77. { return this->subcommands != nullptr; }
  78. /* Return true if this command is a "command class help" command. For
  79. instance, a "stack" dummy command is registered so that one can do
  80. "help stack" and show help for all commands of the "stack" class. */
  81. bool is_command_class_help () const
  82. { return this->func == nullptr; }
  83. void set_context (void *context)
  84. {
  85. gdb_assert (m_context == nullptr);
  86. m_context = context;
  87. }
  88. void *context () const
  89. { return m_context; }
  90. /* Points to next command in this list. */
  91. struct cmd_list_element *next = nullptr;
  92. /* Name of this command. */
  93. const char *name;
  94. /* Command class; class values are chosen by application program. */
  95. enum command_class theclass;
  96. /* When 1 indicated that this command is deprecated. It may be
  97. removed from gdb's command set in the future. */
  98. unsigned int cmd_deprecated : 1;
  99. /* The user needs to be warned that this is a deprecated command.
  100. The user should only be warned the first time a command is
  101. used. */
  102. unsigned int deprecated_warn_user : 1;
  103. /* When functions are deprecated at compile time (this is the way
  104. it should, in general, be done) the memory containing the
  105. replacement string is statically allocated. In some cases it
  106. makes sense to deprecate commands at runtime (the testsuite is
  107. one example). In this case the memory for replacement is
  108. malloc'ed. When a command is undeprecated or re-deprecated at
  109. runtime we don't want to risk calling free on statically
  110. allocated memory, so we check this flag. */
  111. unsigned int malloced_replacement : 1;
  112. /* Set if the doc field should be xfree'd. */
  113. unsigned int doc_allocated : 1;
  114. /* Set if the name field should be xfree'd. */
  115. unsigned int name_allocated : 1;
  116. /* Flag that specifies if this command is already running its hook. */
  117. /* Prevents the possibility of hook recursion. */
  118. unsigned int hook_in : 1;
  119. /* For prefix commands only:
  120. nonzero means do not get an error if subcommand is not
  121. recognized; call the prefix's own function in that case. */
  122. unsigned int allow_unknown : 1;
  123. /* Nonzero says this is an abbreviation, and should not
  124. be mentioned in lists of commands.
  125. This allows "br<tab>" to complete to "break", which it
  126. otherwise wouldn't. */
  127. unsigned int abbrev_flag : 1;
  128. /* Type of "set" or "show" command (or SET_NOT_SET if not "set"
  129. or "show"). */
  130. ENUM_BITFIELD (cmd_types) type : 2;
  131. /* Function definition of this command. NULL for command class
  132. names and for help topics that are not really commands. NOTE:
  133. cagney/2002-02-02: This function signature is evolving. For
  134. the moment suggest sticking with either set_cmd_cfunc() or
  135. set_cmd_sfunc(). */
  136. cmd_func_ftype *func;
  137. /* The command's real callback. At present func() bounces through
  138. to one of the below. */
  139. union
  140. {
  141. /* Most commands don't need the cmd_list_element parameter passed to FUNC.
  142. They therefore register a command of this type, which doesn't have the
  143. cmd_list_element parameter. do_simple_func is installed as FUNC, and
  144. acts as a shim between the two. */
  145. cmd_simple_func_ftype *simple_func;
  146. }
  147. function;
  148. /* Documentation of this command (or help topic).
  149. First line is brief documentation; remaining lines form, with it,
  150. the full documentation. First line should end with a period.
  151. Entire string should also end with a period, not a newline. */
  152. const char *doc;
  153. /* For set/show commands. A method for printing the output to the
  154. specified stream. */
  155. show_value_ftype *show_value_func = nullptr;
  156. /* If this command is deprecated, this is the replacement name. */
  157. const char *replacement = nullptr;
  158. /* Hook for another command to be executed before this command. */
  159. struct cmd_list_element *hook_pre = nullptr;
  160. /* Hook for another command to be executed after this command. */
  161. struct cmd_list_element *hook_post = nullptr;
  162. /* Default arguments to automatically prepend to the user
  163. provided arguments when running this command or alias. */
  164. std::string default_args;
  165. /* Nonzero identifies a prefix command. For them, the address
  166. of the variable containing the list of subcommands. */
  167. struct cmd_list_element **subcommands = nullptr;
  168. /* The prefix command of this command. */
  169. struct cmd_list_element *prefix = nullptr;
  170. /* Completion routine for this command. */
  171. completer_ftype *completer = symbol_completer;
  172. /* Handle the word break characters for this completer. Usually
  173. this function need not be defined, but for some types of
  174. completers (e.g., Python completers declared as methods inside
  175. a class) the word break chars may need to be redefined
  176. depending on the completer type (e.g., for filename
  177. completers). */
  178. completer_handle_brkchars_ftype *completer_handle_brkchars = nullptr;
  179. /* Destruction routine for this command. If non-NULL, this is
  180. called when this command instance is destroyed. This may be
  181. used to finalize the CONTEXT field, if needed. */
  182. void (*destroyer) (struct cmd_list_element *self, void *context) = nullptr;
  183. /* Setting affected by "set" and "show". Not used if type is not_set_cmd. */
  184. gdb::optional<setting> var;
  185. /* Pointer to NULL terminated list of enumerated values (like
  186. argv). */
  187. const char *const *enums = nullptr;
  188. /* Pointer to command strings of user-defined commands */
  189. counted_command_line user_commands;
  190. /* Pointer to command that is hooked by this one, (by hook_pre)
  191. so the hook can be removed when this one is deleted. */
  192. struct cmd_list_element *hookee_pre = nullptr;
  193. /* Pointer to command that is hooked by this one, (by hook_post)
  194. so the hook can be removed when this one is deleted. */
  195. struct cmd_list_element *hookee_post = nullptr;
  196. /* Pointer to command that is aliased by this one, so the
  197. aliased command can be located in case it has been hooked. */
  198. struct cmd_list_element *alias_target = nullptr;
  199. /* Node to link aliases on an alias list. */
  200. using aliases_list_node_type
  201. = intrusive_list_node<cmd_list_element>;
  202. aliases_list_node_type aliases_list_node;
  203. /* Linked list of all aliases of this command. */
  204. using aliases_list_member_node_type
  205. = intrusive_member_node<cmd_list_element,
  206. &cmd_list_element::aliases_list_node>;
  207. using aliases_list_type
  208. = intrusive_list<cmd_list_element, aliases_list_member_node_type>;
  209. aliases_list_type aliases;
  210. /* If non-null, the pointer to a field in 'struct
  211. cli_suppress_notification', which will be set to true in cmd_func
  212. when this command is being executed. It will be set back to false
  213. when the command has been executed. */
  214. bool *suppress_notification = nullptr;
  215. private:
  216. /* Local state (context) for this command. This can be anything. */
  217. void *m_context = nullptr;
  218. };
  219. /* Functions that implement commands about CLI commands. */
  220. extern void help_cmd (const char *, struct ui_file *);
  221. extern void apropos_cmd (struct ui_file *, struct cmd_list_element *,
  222. bool verbose, compiled_regex &, const char *);
  223. /* Used to mark commands that don't do anything. If we just leave the
  224. function field NULL, the command is interpreted as a help topic, or
  225. as a class of commands. */
  226. extern void not_just_help_class_command (const char *arg, int from_tty);
  227. /* Print only the first line of STR on STREAM.
  228. FOR_VALUE_PREFIX true indicates that the first line is output
  229. to be a prefix to show a value (see deprecated_show_value_hack):
  230. the first character is printed in uppercase, and the trailing
  231. dot character is not printed. */
  232. extern void print_doc_line (struct ui_file *stream, const char *str,
  233. bool for_value_prefix);
  234. /* The enums of boolean commands. */
  235. extern const char * const boolean_enums[];
  236. /* The enums of auto-boolean commands. */
  237. extern const char * const auto_boolean_enums[];
  238. /* Verify whether a given cmd_list_element is a user-defined command.
  239. Return 1 if it is user-defined. Return 0 otherwise. */
  240. extern int cli_user_command_p (struct cmd_list_element *);
  241. extern int find_command_name_length (const char *);
  242. #endif /* CLI_CLI_DECODE_H */