gdb: Make use of gdb::option framework for some info commands
[deliverable/binutils-gdb.git] / gdb / cli / cli-utils.h
1 /* CLI utilities.
2
3 Copyright (C) 2011-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef CLI_CLI_UTILS_H
21 #define CLI_CLI_UTILS_H
22
23 /* *PP is a string denoting a number. Get the number. Advance *PP
24 after the string and any trailing whitespace.
25
26 The string can either be a number, or "$" followed by the name of a
27 convenience variable, or ("$" or "$$") followed by digits.
28
29 TRAILER is a character which can be found after the number; most
30 commonly this is `-'. If you don't want a trailer, use \0. */
31
32 extern int get_number_trailer (const char **pp, int trailer);
33
34 /* Convenience. Like get_number_trailer, but with no TRAILER. */
35
36 extern int get_number (const char **);
37
38 /* Like the above, but takes a non-const "char **". */
39
40 extern int get_number (char **);
41
42 /* Like get_number_trailer, but works with ULONGEST, and throws on
43 error instead of returning 0. */
44 extern ULONGEST get_ulongest (const char **pp, int trailer = '\0');
45
46 /* Structure to hold the values of the options used by the 'info
47 variables' command and other similar commands. These correspond to the
48 -q and -t options. */
49
50 struct info_print_options
51 {
52 int quiet = false;
53 char *type_regexp = nullptr;
54
55 ~info_print_options ()
56 {
57 xfree (type_regexp);
58 }
59 };
60
61 /* Extract options from ARGS for commands like 'info variables', placing
62 the options into OPTS. ARGS is updated to point to the first character
63 after the options, or, if there is nothing after the options, then ARGS
64 is set to nullptr. */
65
66 extern void extract_info_print_options (info_print_options *opts,
67 const char **args);
68
69 /* Throws an error telling the user that ARGS starts with an option
70 unrecognized by COMMAND. */
71
72 extern void report_unrecognized_option_error (const char *command,
73 const char *args);
74
75
76 /* Builds the help string for a command documented by PREFIX,
77 followed by the extract_info_print_args help for ENTITY_KIND. */
78
79 const char *info_print_args_help (const char *prefix,
80 const char *entity_kind);
81
82 /* Parse a number or a range.
83 A number will be of the form handled by get_number.
84 A range will be of the form <number1> - <number2>, and
85 will represent all the integers between number1 and number2,
86 inclusive. */
87
88 class number_or_range_parser
89 {
90 public:
91 /* Default construction. Must call init before calling
92 get_next. */
93 number_or_range_parser () {}
94
95 /* Calls init automatically. */
96 number_or_range_parser (const char *string);
97
98 /* STRING is the string to be parsed. */
99 void init (const char *string);
100
101 /* While processing a range, this fuction is called iteratively; At
102 each call it will return the next value in the range.
103
104 At the beginning of parsing a range, the char pointer
105 STATE->m_cur_tok will be advanced past <number1> and left
106 pointing at the '-' token. Subsequent calls will not advance the
107 pointer until the range is completed. The call that completes
108 the range will advance the pointer past <number2>. */
109 int get_number ();
110
111 /* Setup internal state such that get_next() returns numbers in the
112 START_VALUE to END_VALUE range. END_PTR is where the string is
113 advanced to when get_next() returns END_VALUE. */
114 void setup_range (int start_value, int end_value,
115 const char *end_ptr);
116
117 /* Returns true if parsing has completed. */
118 bool finished () const;
119
120 /* Return the string being parsed. When parsing has finished, this
121 points past the last parsed token. */
122 const char *cur_tok () const
123 { return m_cur_tok; }
124
125 /* True when parsing a range. */
126 bool in_range () const
127 { return m_in_range; }
128
129 /* When parsing a range, the final value in the range. */
130 int end_value () const
131 { return m_end_value; }
132
133 /* When parsing a range, skip past the final token in the range. */
134 void skip_range ()
135 {
136 gdb_assert (m_in_range);
137 m_cur_tok = m_end_ptr;
138 m_in_range = false;
139 }
140
141 private:
142 /* No need for these. They are intentionally not defined anywhere. */
143 number_or_range_parser (const number_or_range_parser &);
144 number_or_range_parser &operator= (const number_or_range_parser &);
145
146 /* The string being parsed. When parsing has finished, this points
147 past the last parsed token. */
148 const char *m_cur_tok;
149
150 /* Last value returned. */
151 int m_last_retval;
152
153 /* When parsing a range, the final value in the range. */
154 int m_end_value;
155
156 /* When parsing a range, a pointer past the final token in the
157 range. */
158 const char *m_end_ptr;
159
160 /* True when parsing a range. */
161 bool m_in_range;
162 };
163
164 /* Accept a number and a string-form list of numbers such as is
165 accepted by get_number_or_range. Return TRUE if the number is
166 in the list.
167
168 By definition, an empty list includes all numbers. This is to
169 be interpreted as typing a command such as "delete break" with
170 no arguments. */
171
172 extern int number_is_in_list (const char *list, int number);
173
174 /* Reverse S to the last non-whitespace character without skipping past
175 START. */
176
177 extern const char *remove_trailing_whitespace (const char *start,
178 const char *s);
179
180 /* Same, for non-const S. */
181
182 static inline char *
183 remove_trailing_whitespace (const char *start, char *s)
184 {
185 return (char *) remove_trailing_whitespace (start, (const char *) s);
186 }
187
188 /* A helper function to extract an argument from *ARG. An argument is
189 delimited by whitespace. The return value is empty if no argument
190 was found. */
191
192 extern std::string extract_arg (char **arg);
193
194 /* A const-correct version of the above. */
195
196 extern std::string extract_arg (const char **arg);
197
198 /* A helper function that looks for an argument at the start of a
199 string. The argument must also either be at the end of the string,
200 or be followed by whitespace. Returns 1 if it finds the argument,
201 0 otherwise. If the argument is found, it updates *STR to point
202 past the argument and past any whitespace following the
203 argument. */
204 extern int check_for_argument (const char **str, const char *arg, int arg_len);
205
206 /* Same as above, but ARG's length is computed. */
207
208 static inline int
209 check_for_argument (const char **str, const char *arg)
210 {
211 return check_for_argument (str, arg, strlen (arg));
212 }
213
214 /* Same, for non-const STR. */
215
216 static inline int
217 check_for_argument (char **str, const char *arg, int arg_len)
218 {
219 return check_for_argument (const_cast<const char **> (str),
220 arg, arg_len);
221 }
222
223 static inline int
224 check_for_argument (char **str, const char *arg)
225 {
226 return check_for_argument (str, arg, strlen (arg));
227 }
228
229 /* qcs_flags struct groups the -q, -c, and -s flags parsed by "thread
230 apply" and "frame apply" commands */
231
232 struct qcs_flags
233 {
234 int quiet = false;
235 int cont = false;
236 int silent = false;
237 };
238
239 /* Validate FLAGS. Throws an error if both FLAGS->CONT and
240 FLAGS->SILENT are true. WHICH_COMMAND is included in the error
241 message. */
242 extern void validate_flags_qcs (const char *which_command, qcs_flags *flags);
243
244 #endif /* CLI_CLI_UTILS_H */
This page took 0.03426 seconds and 4 git commands to generate.