3 Copyright (c) 2011 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"
22 #include "gdb_string.h"
24 #include "gdb_assert.h"
28 /* *PP is a string denoting a number. Get the number of the. Advance
29 *PP after the string and any trailing whitespace.
31 Currently the string can either be a number, or "$" followed by the
32 name of a convenience variable, or ("$" or "$$") followed by digits.
34 TRAILER is a character which can be found after the number; most
35 commonly this is `-'. If you don't want a trailer, use \0. */
38 get_number_trailer (char **pp, int trailer)
40 int retval = 0; /* default */
45 struct value *val = value_from_history_ref (p, &p);
47 if (val) /* Value history reference */
49 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
50 retval = value_as_long (val);
53 printf_filtered (_("History value must have integer type."));
57 else /* Convenience variable */
59 /* Internal variable. Make a copy of the name, so we can
60 null-terminate it to pass to lookup_internalvar(). */
65 while (isalnum (*p) || *p == '_')
67 varname = (char *) alloca (p - start + 1);
68 strncpy (varname, start, p - start);
69 varname[p - start] = '\0';
70 if (get_internalvar_integer (lookup_internalvar (varname), &val))
74 printf_filtered (_("Convenience variable must "
75 "have integer value.\n"));
84 while (*p >= '0' && *p <= '9')
87 /* There is no number here. (e.g. "cond a == b"). */
89 /* Skip non-numeric token. */
90 while (*p && !isspace((int) *p))
92 /* Return zero, which caller must interpret as error. */
98 if (!(isspace (*p) || *p == '\0' || *p == trailer))
100 /* Trailing junk: return 0 and let caller print error msg. */
101 while (!(isspace (*p) || *p == '\0' || *p == trailer))
110 /* See documentation in cli-utils.h. */
113 get_number (char **pp)
115 return get_number_trailer (pp, '\0');
118 /* See documentation in cli-utils.h. */
121 get_number_or_range (char **pp)
123 static int last_retval, end_value;
124 static char *end_ptr;
125 static int in_range = 0;
129 /* Default case: pp is pointing either to a solo number,
130 or to the first number of a range. */
131 last_retval = get_number_trailer (pp, '-');
136 /* This is the start of a range (<number1> - <number2>).
137 Skip the '-', parse and remember the second number,
138 and also remember the end of the final token. */
142 while (isspace ((int) *end_ptr))
143 end_ptr++; /* skip white space */
144 end_value = get_number (temp);
145 if (end_value < last_retval)
147 error (_("inverted range"));
149 else if (end_value == last_retval)
151 /* Degenerate range (number1 == number2). Advance the
152 token pointer so that the range will be treated as a
161 error (_("negative value"));
164 /* pp points to the '-' that betokens a range. All
165 number-parsing has already been done. Return the next
166 integer value (one greater than the saved previous value).
167 Do not advance the token pointer 'pp' until the end of range
170 if (++last_retval == end_value)
172 /* End of range reached; advance token pointer. */
180 /* Accept a number and a string-form list of numbers such as is
181 accepted by get_number_or_range. Return TRUE if the number is
184 By definition, an empty list includes all numbers. This is to
185 be interpreted as typing a command such as "delete break" with
189 number_is_in_list (char *list, int number)
191 if (list == NULL || *list == '\0')
194 while (*list != '\0')
196 int gotnum = get_number_or_range (&list);
199 error (_("Args must be numbers or '$' variables."));
200 if (gotnum == number)
206 /* See documentation in cli-utils.h. */
209 skip_spaces (char *chp)
213 while (*chp && isspace (*chp))
218 /* See documentation in cli-utils.h. */
221 skip_to_space (char *chp)
225 while (*chp && !isspace (*chp))