GDB (xrefs)
Loading...
Searching...
No Matches
cli-utils.h
Go to the documentation of this file.
1/* CLI utilities.
2
3 Copyright (C) 2011-2023 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#include "completer.h"
24
25struct cmd_list_element;
26
27/* *PP is a string denoting a number. Get the number. Advance *PP
28 after the string and any trailing whitespace.
29
30 The string can either be a number, or "$" followed by the name of a
31 convenience variable, or ("$" or "$$") followed by digits.
32
33 TRAILER is a character which can be found after the number; most
34 commonly this is `-'. If you don't want a trailer, use \0. */
35
36extern int get_number_trailer (const char **pp, int trailer);
37
38/* Convenience. Like get_number_trailer, but with no TRAILER. */
39
40extern int get_number (const char **);
41
42/* Like the above, but takes a non-const "char **". */
43
44extern int get_number (char **);
45
46/* Like get_number_trailer, but works with ULONGEST, and throws on
47 error instead of returning 0. */
48extern ULONGEST get_ulongest (const char **pp, int trailer = '\0');
49
50/* Throws an error telling the user that ARGS starts with an option
51 unrecognized by COMMAND. */
52
53extern void report_unrecognized_option_error (const char *command,
54 const char *args);
55
56
57/* Builds the help string for a command documented by PREFIX,
58 followed by the extract_info_print_args help for ENTITY_KIND. If
59 DOCUMENT_N_FLAG is true then help text describing the -n flag is also
60 included. */
61
62const char *info_print_args_help (const char *prefix,
63 const char *entity_kind,
64 bool document_n_flag);
65
66/* Parse a number or a range.
67 A number will be of the form handled by get_number.
68 A range will be of the form <number1> - <number2>, and
69 will represent all the integers between number1 and number2,
70 inclusive. */
71
73{
74public:
75 /* Default construction. Must call init before calling
76 get_next. */
78
79 /* Calls init automatically. */
80 number_or_range_parser (const char *string);
81
82 /* STRING is the string to be parsed. */
83 void init (const char *string);
84
85 /* While processing a range, this fuction is called iteratively; At
86 each call it will return the next value in the range.
87
88 At the beginning of parsing a range, the char pointer
89 STATE->m_cur_tok will be advanced past <number1> and left
90 pointing at the '-' token. Subsequent calls will not advance the
91 pointer until the range is completed. The call that completes
92 the range will advance the pointer past <number2>. */
93 int get_number ();
94
95 /* Setup internal state such that get_next() returns numbers in the
96 START_VALUE to END_VALUE range. END_PTR is where the string is
97 advanced to when get_next() returns END_VALUE. */
98 void setup_range (int start_value, int end_value,
99 const char *end_ptr);
100
101 /* Returns true if parsing has completed. */
102 bool finished () const;
103
104 /* Return the string being parsed. When parsing has finished, this
105 points past the last parsed token. */
106 const char *cur_tok () const
107 { return m_cur_tok; }
108
109 /* True when parsing a range. */
110 bool in_range () const
111 { return m_in_range; }
112
113 /* When parsing a range, the final value in the range. */
114 int end_value () const
115 { return m_end_value; }
116
117 /* When parsing a range, skip past the final token in the range. */
119 {
120 gdb_assert (m_in_range);
122 m_in_range = false;
123 }
124
125private:
126 /* No need for these. They are intentionally not defined anywhere. */
129
130 /* The string being parsed. When parsing has finished, this points
131 past the last parsed token. */
132 const char *m_cur_tok;
133
134 /* Last value returned. */
136
137 /* When parsing a range, the final value in the range. */
139
140 /* When parsing a range, a pointer past the final token in the
141 range. */
142 const char *m_end_ptr;
143
144 /* True when parsing a range. */
146};
147
148/* Accept a number and a string-form list of numbers such as is
149 accepted by get_number_or_range. Return TRUE if the number is
150 in the list.
151
152 By definition, an empty list includes all numbers. This is to
153 be interpreted as typing a command such as "delete break" with
154 no arguments. */
155
156extern int number_is_in_list (const char *list, int number);
157
158/* Reverse S to the last non-whitespace character without skipping past
159 START. */
160
161extern const char *remove_trailing_whitespace (const char *start,
162 const char *s);
163
164/* Same, for non-const S. */
165
166static inline char *
167remove_trailing_whitespace (const char *start, char *s)
168{
169 return (char *) remove_trailing_whitespace (start, (const char *) s);
170}
171
172/* A helper function to extract an argument from *ARG. An argument is
173 delimited by whitespace. The return value is empty if no argument
174 was found. */
175
176extern std::string extract_arg (char **arg);
177
178/* A const-correct version of the above. */
179
180extern std::string extract_arg (const char **arg);
181
182/* A helper function that looks for an argument at the start of a
183 string. The argument must also either be at the end of the string,
184 or be followed by whitespace. Returns 1 if it finds the argument,
185 0 otherwise. If the argument is found, it updates *STR to point
186 past the argument and past any whitespace following the
187 argument. */
188extern int check_for_argument (const char **str, const char *arg, int arg_len);
189
190/* Same as above, but ARG's length is computed. */
191
192static inline int
193check_for_argument (const char **str, const char *arg)
194{
195 return check_for_argument (str, arg, strlen (arg));
196}
197
198/* Same, for non-const STR. */
199
200static inline int
201check_for_argument (char **str, const char *arg, int arg_len)
202{
203 return check_for_argument (const_cast<const char **> (str),
204 arg, arg_len);
205}
206
207static inline int
208check_for_argument (char **str, const char *arg)
209{
210 return check_for_argument (str, arg, strlen (arg));
211}
212
213/* qcs_flags struct groups the -q, -c, and -s flags parsed by "thread
214 apply" and "frame apply" commands */
215
217{
218 bool quiet = false;
219 bool cont = false;
220 bool silent = false;
221};
222
223/* Validate FLAGS. Throws an error if both FLAGS->CONT and
224 FLAGS->SILENT are true. WHICH_COMMAND is included in the error
225 message. */
226extern void validate_flags_qcs (const char *which_command, qcs_flags *flags);
227
228#endif /* CLI_CLI_UTILS_H */
int end_value() const
Definition cli-utils.h:114
bool finished() const
Definition cli-utils.c:327
const char * cur_tok() const
Definition cli-utils.h:106
const char * m_cur_tok
Definition cli-utils.h:132
void init(const char *string)
Definition cli-utils.c:225
bool in_range() const
Definition cli-utils.h:110
number_or_range_parser(const number_or_range_parser &)
number_or_range_parser & operator=(const number_or_range_parser &)
void setup_range(int start_value, int end_value, const char *end_ptr)
Definition cli-utils.c:313
const char * m_end_ptr
Definition cli-utils.h:142
const char * info_print_args_help(const char *prefix, const char *entity_kind, bool document_n_flag)
Definition cli-utils.c:196
void report_unrecognized_option_error(const char *command, const char *args)
Definition cli-utils.c:184
ULONGEST get_ulongest(const char **pp, int trailer='\0')
Definition cli-utils.c:29
int number_is_in_list(const char *list, int number)
Definition cli-utils.c:348
std::string extract_arg(char **arg)
Definition cli-utils.c:408
int get_number(const char **)
Definition cli-utils.c:163
void validate_flags_qcs(const char *which_command, qcs_flags *flags)
Definition cli-utils.c:436
int check_for_argument(const char **str, const char *arg, int arg_len)
Definition cli-utils.c:421
const char * remove_trailing_whitespace(const char *start, const char *s)
Definition cli-utils.c:372
int get_number_trailer(const char **pp, int trailer)
Definition cli-utils.c:81
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
#define prefix(a, b, R, do)
Definition ppc64-tdep.c:52
bool silent
Definition cli-utils.h:220
bool cont
Definition cli-utils.h:219
bool quiet
Definition cli-utils.h:218