3 Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "cli/cli-utils.h"
26 /* See documentation in cli-utils.h. */
29 get_number_trailer (const char **pp
, int trailer
)
31 int retval
= 0; /* default */
33 bool negative
= false;
43 struct value
*val
= value_from_history_ref (p
, &p
);
45 if (val
) /* Value history reference */
47 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_INT
)
48 retval
= value_as_long (val
);
51 printf_filtered (_("History value must have integer type.\n"));
55 else /* Convenience variable */
57 /* Internal variable. Make a copy of the name, so we can
58 null-terminate it to pass to lookup_internalvar(). */
60 const char *start
= ++p
;
63 while (isalnum (*p
) || *p
== '_')
65 varname
= (char *) alloca (p
- start
+ 1);
66 strncpy (varname
, start
, p
- start
);
67 varname
[p
- start
] = '\0';
68 if (get_internalvar_integer (lookup_internalvar (varname
), &val
))
72 printf_filtered (_("Convenience variable must "
73 "have integer value.\n"));
81 while (*p
>= '0' && *p
<= '9')
84 /* There is no number here. (e.g. "cond a == b"). */
86 /* Skip non-numeric token. */
87 while (*p
&& !isspace((int) *p
))
89 /* Return zero, which caller must interpret as error. */
95 if (!(isspace (*p
) || *p
== '\0' || *p
== trailer
))
97 /* Trailing junk: return 0 and let caller print error msg. */
98 while (!(isspace (*p
) || *p
== '\0' || *p
== trailer
))
104 return negative
? -retval
: retval
;
107 /* See documentation in cli-utils.h. */
110 get_number (const char **pp
)
112 return get_number_trailer (pp
, '\0');
115 /* See documentation in cli-utils.h. */
118 get_number (char **pp
)
123 result
= get_number_trailer (&p
, '\0');
128 /* See documentation in cli-utils.h. */
130 number_or_range_parser::number_or_range_parser (const char *string
)
135 /* See documentation in cli-utils.h. */
138 number_or_range_parser::init (const char *string
)
148 /* See documentation in cli-utils.h. */
151 number_or_range_parser::get_number ()
155 /* All number-parsing has already been done. Return the next
156 integer value (one greater than the saved previous value).
157 Do not advance the token pointer until the end of range is
160 if (++m_last_retval
== m_end_value
)
162 /* End of range reached; advance token pointer. */
163 m_cur_tok
= m_end_ptr
;
167 else if (*m_cur_tok
!= '-')
169 /* Default case: state->m_cur_tok is pointing either to a solo
170 number, or to the first number of a range. */
171 m_last_retval
= get_number_trailer (&m_cur_tok
, '-');
172 if (*m_cur_tok
== '-')
176 /* This is the start of a range (<number1> - <number2>).
177 Skip the '-', parse and remember the second number,
178 and also remember the end of the final token. */
181 m_end_ptr
= skip_spaces (m_cur_tok
+ 1);
182 m_end_value
= ::get_number (temp
);
183 if (m_end_value
< m_last_retval
)
185 error (_("inverted range"));
187 else if (m_end_value
== m_last_retval
)
189 /* Degenerate range (number1 == number2). Advance the
190 token pointer so that the range will be treated as a
192 m_cur_tok
= m_end_ptr
;
199 error (_("negative value"));
200 m_finished
= *m_cur_tok
== '\0';
201 return m_last_retval
;
204 /* See documentation in cli-utils.h. */
207 number_or_range_parser::setup_range (int start_value
, int end_value
,
210 gdb_assert (start_value
> 0);
214 m_last_retval
= start_value
- 1;
215 m_end_value
= end_value
;
218 /* Accept a number and a string-form list of numbers such as is
219 accepted by get_number_or_range. Return TRUE if the number is
222 By definition, an empty list includes all numbers. This is to
223 be interpreted as typing a command such as "delete break" with
227 number_is_in_list (const char *list
, int number
)
229 if (list
== NULL
|| *list
== '\0')
232 number_or_range_parser
parser (list
);
233 while (!parser
.finished ())
235 int gotnum
= parser
.get_number ();
238 error (_("Args must be numbers or '$' variables."));
239 if (gotnum
== number
)
245 /* See documentation in cli-utils.h. */
248 remove_trailing_whitespace (const char *start
, const char *s
)
250 while (s
> start
&& isspace (*(s
- 1)))
256 /* See documentation in cli-utils.h. */
259 extract_arg (const char **arg
)
264 return std::string ();
266 /* Find the start of the argument. */
267 *arg
= skip_spaces (*arg
);
269 return std::string ();
272 /* Find the end of the argument. */
273 *arg
= skip_to_space (*arg
+ 1);
276 return std::string ();
278 return std::string (result
, *arg
- result
);
281 /* See documentation in cli-utils.h. */
284 extract_arg (char **arg
)
286 const char *arg_const
= *arg
;
289 result
= extract_arg (&arg_const
);
290 *arg
+= arg_const
- *arg
;
294 /* See documentation in cli-utils.h. */
297 check_for_argument (const char **str
, const char *arg
, int arg_len
)
299 if (strncmp (*str
, arg
, arg_len
) == 0
300 && ((*str
)[arg_len
] == '\0' || isspace ((*str
)[arg_len
])))
This page took 0.037129 seconds and 5 git commands to generate.