]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cli/cli-utils.h
gdb: Allow quoting around string options in the gdb::option framework
[thirdparty/binutils-gdb.git] / gdb / cli / cli-utils.h
CommitLineData
e9cafbcc
TT
1/* CLI utilities.
2
42a4f53d 3 Copyright (C) 2011-2019 Free Software Foundation, Inc.
e9cafbcc
TT
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
1a5c2598
TT
20#ifndef CLI_CLI_UTILS_H
21#define CLI_CLI_UTILS_H
e9cafbcc 22
5d5658a1
PA
23/* *PP is a string denoting a number. Get the number. Advance *PP
24 after the string and any trailing whitespace.
e9cafbcc 25
5d5658a1
PA
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
32extern int get_number_trailer (const char **pp, int trailer);
33
34/* Convenience. Like get_number_trailer, but with no TRAILER. */
e9cafbcc 35
f1735a53 36extern int get_number (const char **);
e799154c 37
f1735a53 38/* Like the above, but takes a non-const "char **". */
e799154c 39
e9cafbcc
TT
40extern int get_number (char **);
41
9d0faba9
PA
42/* Like get_number_trailer, but works with ULONGEST, and throws on
43 error instead of returning 0. */
44extern ULONGEST get_ulongest (const char **pp, int trailer = '\0');
45
0d4cad90
PW
46/* Extract from ARGS the arguments [-q] [-t TYPEREGEXP] [--] NAMEREGEXP.
47
48 The caller is responsible to initialize *QUIET to false, *REGEXP
49 and *T_REGEXP to "".
50 extract_info_print_args can then be called iteratively to search
51 for valid arguments, as part of a 'main parsing loop' searching for
52 -q/-t/-- arguments together with other flags and options.
53
54 Returns true and updates *ARGS + one of *QUIET, *REGEXP, *T_REGEXP if
55 it finds a valid argument.
56 Returns false if no valid argument is found at the beginning of ARGS. */
57
58extern bool extract_info_print_args (const char **args,
59 bool *quiet,
60 std::string *regexp,
61 std::string *t_regexp);
62
63/* Throws an error telling the user that ARGS starts with an option
64 unrecognized by COMMAND. */
65
66extern void report_unrecognized_option_error (const char *command,
67 const char *args);
68
69
70/* Builds the help string for a command documented by PREFIX,
71 followed by the extract_info_print_args help for ENTITY_KIND. */
72
73const char *info_print_args_help (const char *prefix,
74 const char *entity_kind);
75
bfd28288
PA
76/* Parse a number or a range.
77 A number will be of the form handled by get_number.
78 A range will be of the form <number1> - <number2>, and
79 will represent all the integers between number1 and number2,
80 inclusive. */
197f0a60 81
bfd28288 82class number_or_range_parser
197f0a60 83{
bfd28288
PA
84public:
85 /* Default construction. Must call init before calling
86 get_next. */
87 number_or_range_parser () {}
88
89 /* Calls init automatically. */
90 number_or_range_parser (const char *string);
91
92 /* STRING is the string to be parsed. */
93 void init (const char *string);
94
95 /* While processing a range, this fuction is called iteratively; At
96 each call it will return the next value in the range.
97
98 At the beginning of parsing a range, the char pointer
99 STATE->m_cur_tok will be advanced past <number1> and left
100 pointing at the '-' token. Subsequent calls will not advance the
101 pointer until the range is completed. The call that completes
102 the range will advance the pointer past <number2>. */
103 int get_number ();
104
105 /* Setup internal state such that get_next() returns numbers in the
106 START_VALUE to END_VALUE range. END_PTR is where the string is
107 advanced to when get_next() returns END_VALUE. */
108 void setup_range (int start_value, int end_value,
109 const char *end_ptr);
110
111 /* Returns true if parsing has completed. */
529c08b2 112 bool finished () const;
bfd28288
PA
113
114 /* Return the string being parsed. When parsing has finished, this
115 points past the last parsed token. */
116 const char *cur_tok () const
117 { return m_cur_tok; }
118
119 /* True when parsing a range. */
120 bool in_range () const
121 { return m_in_range; }
122
123 /* When parsing a range, the final value in the range. */
124 int end_value () const
125 { return m_end_value; }
126
127 /* When parsing a range, skip past the final token in the range. */
128 void skip_range ()
129 {
130 gdb_assert (m_in_range);
131 m_cur_tok = m_end_ptr;
529c08b2 132 m_in_range = false;
bfd28288
PA
133 }
134
135private:
136 /* No need for these. They are intentionally not defined anywhere. */
137 number_or_range_parser (const number_or_range_parser &);
138 number_or_range_parser &operator= (const number_or_range_parser &);
139
197f0a60
TT
140 /* The string being parsed. When parsing has finished, this points
141 past the last parsed token. */
bfd28288 142 const char *m_cur_tok;
197f0a60
TT
143
144 /* Last value returned. */
bfd28288 145 int m_last_retval;
197f0a60
TT
146
147 /* When parsing a range, the final value in the range. */
bfd28288 148 int m_end_value;
197f0a60
TT
149
150 /* When parsing a range, a pointer past the final token in the
151 range. */
bfd28288 152 const char *m_end_ptr;
197f0a60 153
bfd28288
PA
154 /* True when parsing a range. */
155 bool m_in_range;
197f0a60
TT
156};
157
aea5b279
MS
158/* Accept a number and a string-form list of numbers such as is
159 accepted by get_number_or_range. Return TRUE if the number is
160 in the list.
161
162 By definition, an empty list includes all numbers. This is to
163 be interpreted as typing a command such as "delete break" with
164 no arguments. */
165
e799154c 166extern int number_is_in_list (const char *list, int number);
aea5b279 167
c00f8484
KS
168/* Reverse S to the last non-whitespace character without skipping past
169 START. */
170
63160a43
PA
171extern const char *remove_trailing_whitespace (const char *start,
172 const char *s);
173
174/* Same, for non-const S. */
175
176static inline char *
177remove_trailing_whitespace (const char *start, char *s)
178{
179 return (char *) remove_trailing_whitespace (start, (const char *) s);
180}
55aa24fb
SDJ
181
182/* A helper function to extract an argument from *ARG. An argument is
cb791d59
TT
183 delimited by whitespace. The return value is empty if no argument
184 was found. */
55aa24fb 185
cb791d59 186extern std::string extract_arg (char **arg);
55aa24fb 187
cb791d59 188/* A const-correct version of the above. */
b5be8ce0 189
cb791d59 190extern std::string extract_arg (const char **arg);
b5be8ce0 191
e6f0bce7
HZ
192/* A helper function that looks for an argument at the start of a
193 string. The argument must also either be at the end of the string,
194 or be followed by whitespace. Returns 1 if it finds the argument,
cbba3ecd
PA
195 0 otherwise. If the argument is found, it updates *STR to point
196 past the argument and past any whitespace following the
197 argument. */
63160a43
PA
198extern int check_for_argument (const char **str, const char *arg, int arg_len);
199
9d0faba9
PA
200/* Same as above, but ARG's length is computed. */
201
202static inline int
203check_for_argument (const char **str, const char *arg)
204{
205 return check_for_argument (str, arg, strlen (arg));
206}
207
63160a43
PA
208/* Same, for non-const STR. */
209
210static inline int
211check_for_argument (char **str, const char *arg, int arg_len)
212{
213 return check_for_argument (const_cast<const char **> (str),
214 arg, arg_len);
215}
e6f0bce7 216
9d0faba9
PA
217static inline int
218check_for_argument (char **str, const char *arg)
219{
220 return check_for_argument (str, arg, strlen (arg));
221}
222
5d707134
PA
223/* qcs_flags struct groups the -q, -c, and -s flags parsed by "thread
224 apply" and "frame apply" commands */
529c08b2
PW
225
226struct qcs_flags
227{
5d707134
PA
228 int quiet = false;
229 int cont = false;
230 int silent = false;
529c08b2
PW
231};
232
5d707134
PA
233/* Validate FLAGS. Throws an error if both FLAGS->CONT and
234 FLAGS->SILENT are true. WHICH_COMMAND is included in the error
235 message. */
236extern void validate_flags_qcs (const char *which_command, qcs_flags *flags);
237
1a5c2598 238#endif /* CLI_CLI_UTILS_H */