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