3 Copyright (C) 2011-2016 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 /* *PP is a string denoting a number. Get the number of the. Advance
27 *PP after the string and any trailing whitespace.
29 Currently the string can either be a number, or "$" followed by the
30 name of a convenience variable, or ("$" or "$$") followed by digits.
32 TRAILER is a character which can be found after the number; most
33 commonly this is `-'. If you don't want a trailer, use \0. */
36 get_number_trailer (const char **pp, int trailer)
38 int retval = 0; /* default */
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"));
82 while (*p >= '0' && *p <= '9')
85 /* There is no number here. (e.g. "cond a == b"). */
87 /* Skip non-numeric token. */
88 while (*p && !isspace((int) *p))
90 /* Return zero, which caller must interpret as error. */
96 if (!(isspace (*p) || *p == '\0' || *p == trailer))
98 /* Trailing junk: return 0 and let caller print error msg. */
99 while (!(isspace (*p) || *p == '\0' || *p == trailer))
103 p = skip_spaces_const (p);
108 /* See documentation in cli-utils.h. */
111 get_number_const (const char **pp)
113 return get_number_trailer (pp, '\0');
116 /* See documentation in cli-utils.h. */
119 get_number (char **pp)
124 result = get_number_trailer (&p, '\0');
129 /* See documentation in cli-utils.h. */
132 init_number_or_range (struct get_number_or_range_state *state,
135 memset (state, 0, sizeof (*state));
136 state->string = string;
139 /* See documentation in cli-utils.h. */
142 get_number_or_range (struct get_number_or_range_state *state)
144 if (*state->string != '-')
146 /* Default case: state->string is pointing either to a solo
147 number, or to the first number of a range. */
148 state->last_retval = get_number_trailer (&state->string, '-');
149 if (*state->string == '-')
153 /* This is the start of a range (<number1> - <number2>).
154 Skip the '-', parse and remember the second number,
155 and also remember the end of the final token. */
157 temp = &state->end_ptr;
158 state->end_ptr = skip_spaces_const (state->string + 1);
159 state->end_value = get_number_const (temp);
160 if (state->end_value < state->last_retval)
162 error (_("inverted range"));
164 else if (state->end_value == state->last_retval)
166 /* Degenerate range (number1 == number2). Advance the
167 token pointer so that the range will be treated as a
169 state->string = state->end_ptr;
175 else if (! state->in_range)
176 error (_("negative value"));
179 /* state->string points to the '-' that betokens a range. All
180 number-parsing has already been done. Return the next
181 integer value (one greater than the saved previous value).
182 Do not advance the token pointer until the end of range
185 if (++state->last_retval == state->end_value)
187 /* End of range reached; advance token pointer. */
188 state->string = state->end_ptr;
192 state->finished = *state->string == '\0';
193 return state->last_retval;
196 /* Accept a number and a string-form list of numbers such as is
197 accepted by get_number_or_range. Return TRUE if the number is
200 By definition, an empty list includes all numbers. This is to
201 be interpreted as typing a command such as "delete break" with
205 number_is_in_list (const char *list, int number)
207 struct get_number_or_range_state state;
209 if (list == NULL || *list == '\0')
212 init_number_or_range (&state, list);
213 while (!state.finished)
215 int gotnum = get_number_or_range (&state);
218 error (_("Args must be numbers or '$' variables."));
219 if (gotnum == number)
225 /* See documentation in cli-utils.h. */
228 remove_trailing_whitespace (const char *start, char *s)
230 while (s > start && isspace (*(s - 1)))
236 /* See documentation in cli-utils.h. */
239 extract_arg_const (const char **arg)
246 /* Find the start of the argument. */
247 *arg = skip_spaces_const (*arg);
252 /* Find the end of the argument. */
253 *arg = skip_to_space_const (*arg + 1);
258 return savestring (result, *arg - result);
261 /* See documentation in cli-utils.h. */
264 extract_arg (char **arg)
266 const char *arg_const = *arg;
269 result = extract_arg_const (&arg_const);
270 *arg += arg_const - *arg;
274 /* See documentation in cli-utils.h. */
277 check_for_argument (char **str, char *arg, int arg_len)
279 if (strncmp (*str, arg, arg_len) == 0
280 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))