cli-utils.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* CLI utilities.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef CLI_CLI_UTILS_H
  15. #define CLI_CLI_UTILS_H
  16. #include "completer.h"
  17. struct cmd_list_element;
  18. /* *PP is a string denoting a number. Get the number. Advance *PP
  19. after the string and any trailing whitespace.
  20. The string can either be a number, or "$" followed by the name of a
  21. convenience variable, or ("$" or "$$") followed by digits.
  22. TRAILER is a character which can be found after the number; most
  23. commonly this is `-'. If you don't want a trailer, use \0. */
  24. extern int get_number_trailer (const char **pp, int trailer);
  25. /* Convenience. Like get_number_trailer, but with no TRAILER. */
  26. extern int get_number (const char **);
  27. /* Like the above, but takes a non-const "char **". */
  28. extern int get_number (char **);
  29. /* Like get_number_trailer, but works with ULONGEST, and throws on
  30. error instead of returning 0. */
  31. extern ULONGEST get_ulongest (const char **pp, int trailer = '\0');
  32. /* Throws an error telling the user that ARGS starts with an option
  33. unrecognized by COMMAND. */
  34. extern void report_unrecognized_option_error (const char *command,
  35. const char *args);
  36. /* Builds the help string for a command documented by PREFIX,
  37. followed by the extract_info_print_args help for ENTITY_KIND. If
  38. DOCUMENT_N_FLAG is true then help text describing the -n flag is also
  39. included. */
  40. const char *info_print_args_help (const char *prefix,
  41. const char *entity_kind,
  42. bool document_n_flag);
  43. /* Parse a number or a range.
  44. A number will be of the form handled by get_number.
  45. A range will be of the form <number1> - <number2>, and
  46. will represent all the integers between number1 and number2,
  47. inclusive. */
  48. class number_or_range_parser
  49. {
  50. public:
  51. /* Default construction. Must call init before calling
  52. get_next. */
  53. number_or_range_parser () {}
  54. /* Calls init automatically. */
  55. number_or_range_parser (const char *string);
  56. /* STRING is the string to be parsed. */
  57. void init (const char *string);
  58. /* While processing a range, this fuction is called iteratively; At
  59. each call it will return the next value in the range.
  60. At the beginning of parsing a range, the char pointer
  61. STATE->m_cur_tok will be advanced past <number1> and left
  62. pointing at the '-' token. Subsequent calls will not advance the
  63. pointer until the range is completed. The call that completes
  64. the range will advance the pointer past <number2>. */
  65. int get_number ();
  66. /* Setup internal state such that get_next() returns numbers in the
  67. START_VALUE to END_VALUE range. END_PTR is where the string is
  68. advanced to when get_next() returns END_VALUE. */
  69. void setup_range (int start_value, int end_value,
  70. const char *end_ptr);
  71. /* Returns true if parsing has completed. */
  72. bool finished () const;
  73. /* Return the string being parsed. When parsing has finished, this
  74. points past the last parsed token. */
  75. const char *cur_tok () const
  76. { return m_cur_tok; }
  77. /* True when parsing a range. */
  78. bool in_range () const
  79. { return m_in_range; }
  80. /* When parsing a range, the final value in the range. */
  81. int end_value () const
  82. { return m_end_value; }
  83. /* When parsing a range, skip past the final token in the range. */
  84. void skip_range ()
  85. {
  86. gdb_assert (m_in_range);
  87. m_cur_tok = m_end_ptr;
  88. m_in_range = false;
  89. }
  90. private:
  91. /* No need for these. They are intentionally not defined anywhere. */
  92. number_or_range_parser (const number_or_range_parser &);
  93. number_or_range_parser &operator= (const number_or_range_parser &);
  94. /* The string being parsed. When parsing has finished, this points
  95. past the last parsed token. */
  96. const char *m_cur_tok;
  97. /* Last value returned. */
  98. int m_last_retval;
  99. /* When parsing a range, the final value in the range. */
  100. int m_end_value;
  101. /* When parsing a range, a pointer past the final token in the
  102. range. */
  103. const char *m_end_ptr;
  104. /* True when parsing a range. */
  105. bool m_in_range;
  106. };
  107. /* Accept a number and a string-form list of numbers such as is
  108. accepted by get_number_or_range. Return TRUE if the number is
  109. in the list.
  110. By definition, an empty list includes all numbers. This is to
  111. be interpreted as typing a command such as "delete break" with
  112. no arguments. */
  113. extern int number_is_in_list (const char *list, int number);
  114. /* Reverse S to the last non-whitespace character without skipping past
  115. START. */
  116. extern const char *remove_trailing_whitespace (const char *start,
  117. const char *s);
  118. /* Same, for non-const S. */
  119. static inline char *
  120. remove_trailing_whitespace (const char *start, char *s)
  121. {
  122. return (char *) remove_trailing_whitespace (start, (const char *) s);
  123. }
  124. /* A helper function to extract an argument from *ARG. An argument is
  125. delimited by whitespace. The return value is empty if no argument
  126. was found. */
  127. extern std::string extract_arg (char **arg);
  128. /* A const-correct version of the above. */
  129. extern std::string extract_arg (const char **arg);
  130. /* A helper function that looks for an argument at the start of a
  131. string. The argument must also either be at the end of the string,
  132. or be followed by whitespace. Returns 1 if it finds the argument,
  133. 0 otherwise. If the argument is found, it updates *STR to point
  134. past the argument and past any whitespace following the
  135. argument. */
  136. extern int check_for_argument (const char **str, const char *arg, int arg_len);
  137. /* Same as above, but ARG's length is computed. */
  138. static inline int
  139. check_for_argument (const char **str, const char *arg)
  140. {
  141. return check_for_argument (str, arg, strlen (arg));
  142. }
  143. /* Same, for non-const STR. */
  144. static inline int
  145. check_for_argument (char **str, const char *arg, int arg_len)
  146. {
  147. return check_for_argument (const_cast<const char **> (str),
  148. arg, arg_len);
  149. }
  150. static inline int
  151. check_for_argument (char **str, const char *arg)
  152. {
  153. return check_for_argument (str, arg, strlen (arg));
  154. }
  155. /* qcs_flags struct groups the -q, -c, and -s flags parsed by "thread
  156. apply" and "frame apply" commands */
  157. struct qcs_flags
  158. {
  159. bool quiet = false;
  160. bool cont = false;
  161. bool silent = false;
  162. };
  163. /* Validate FLAGS. Throws an error if both FLAGS->CONT and
  164. FLAGS->SILENT are true. WHICH_COMMAND is included in the error
  165. message. */
  166. extern void validate_flags_qcs (const char *which_command, qcs_flags *flags);
  167. #endif /* CLI_CLI_UTILS_H */