3 Copyright (C) 2011-2019 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_ulongest (const char **pp, int trailer)
31 LONGEST retval = 0; /* default */
36 value *val = value_from_history_ref (p, &p);
38 if (val != NULL) /* Value history reference */
40 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
41 retval = value_as_long (val);
43 error (_("History value must have integer type."));
45 else /* Convenience variable */
47 /* Internal variable. Make a copy of the name, so we can
48 null-terminate it to pass to lookup_internalvar(). */
49 const char *start = ++p;
50 while (isalnum (*p) || *p == '_')
52 std::string varname (start, p - start);
53 if (!get_internalvar_integer (lookup_internalvar (varname.c_str ()),
55 error (_("Convenience variable $%s does not have integer value."),
62 retval = strtoulst (p, &end, 0);
65 /* There is no number here. (e.g. "cond a == b"). */
66 error (_("Expected integer at: %s"), p);
71 if (!(isspace (*p) || *p == '\0' || *p == trailer))
72 error (_("Trailing junk at: %s"), p);
78 /* See documentation in cli-utils.h. */
81 get_number_trailer (const char **pp, int trailer)
83 int retval = 0; /* default */
85 bool negative = false;
95 struct value *val = value_from_history_ref (p, &p);
97 if (val) /* Value history reference */
99 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
100 retval = value_as_long (val);
103 printf_filtered (_("History value must have integer type.\n"));
107 else /* Convenience variable */
109 /* Internal variable. Make a copy of the name, so we can
110 null-terminate it to pass to lookup_internalvar(). */
112 const char *start = ++p;
115 while (isalnum (*p) || *p == '_')
117 varname = (char *) alloca (p - start + 1);
118 strncpy (varname, start, p - start);
119 varname[p - start] = '\0';
120 if (get_internalvar_integer (lookup_internalvar (varname),
122 retval = (int) longest_val;
125 printf_filtered (_("Convenience variable must "
126 "have integer value.\n"));
134 while (*p >= '0' && *p <= '9')
137 /* There is no number here. (e.g. "cond a == b"). */
139 /* Skip non-numeric token. */
140 while (*p && !isspace((int) *p))
142 /* Return zero, which caller must interpret as error. */
148 if (!(isspace (*p) || *p == '\0' || *p == trailer))
150 /* Trailing junk: return 0 and let caller print error msg. */
151 while (!(isspace (*p) || *p == '\0' || *p == trailer))
157 return negative ? -retval : retval;
160 /* See documentation in cli-utils.h. */
163 get_number (const char **pp)
165 return get_number_trailer (pp, '\0');
168 /* See documentation in cli-utils.h. */
171 get_number (char **pp)
176 result = get_number_trailer (&p, '\0');
181 /* See documentation in cli-utils.h. */
184 report_unrecognized_option_error (const char *command, const char *args)
186 std::string option = extract_arg (&args);
188 error (_("Unrecognized option '%s' to %s command. "
189 "Try \"help %s\"."), option.c_str (),
193 /* See documentation in cli-utils.h. */
196 info_print_args_help (const char *prefix,
197 const char *entity_kind)
199 return xstrprintf (_("\
200 %sIf NAMEREGEXP is provided, only prints the %s whose name\n\
201 matches NAMEREGEXP.\n\
202 If -t TYPEREGEXP is provided, only prints the %s whose type\n\
203 matches TYPEREGEXP. Note that the matching is done with the type\n\
204 printed by the 'whatis' command.\n\
205 By default, the command might produce headers and/or messages indicating\n\
206 why no %s can be printed.\n\
207 The flag -q disables the production of these headers and messages."),
208 prefix, entity_kind, entity_kind, entity_kind);
211 /* See documentation in cli-utils.h. */
213 number_or_range_parser::number_or_range_parser (const char *string)
218 /* See documentation in cli-utils.h. */
221 number_or_range_parser::init (const char *string)
230 /* See documentation in cli-utils.h. */
233 number_or_range_parser::get_number ()
237 /* All number-parsing has already been done. Return the next
238 integer value (one greater than the saved previous value).
239 Do not advance the token pointer until the end of range is
242 if (++m_last_retval == m_end_value)
244 /* End of range reached; advance token pointer. */
245 m_cur_tok = m_end_ptr;
249 else if (*m_cur_tok != '-')
251 /* Default case: state->m_cur_tok is pointing either to a solo
252 number, or to the first number of a range. */
253 m_last_retval = get_number_trailer (&m_cur_tok, '-');
254 /* If get_number_trailer has found a '-' preceded by a space, it
255 might be the start of a command option. So, do not parse a
256 range if the '-' is followed by an alpha or another '-'. We
257 might also be completing something like
258 "frame apply level 0 -" and we prefer treating that "-" as an
259 option rather than an incomplete range, so check for end of
261 if (m_cur_tok[0] == '-'
262 && !(isspace (m_cur_tok[-1])
263 && (isalpha (m_cur_tok[1])
264 || m_cur_tok[1] == '-'
265 || m_cur_tok[1] == '\0')))
269 /* This is the start of a range (<number1> - <number2>).
270 Skip the '-', parse and remember the second number,
271 and also remember the end of the final token. */
274 m_end_ptr = skip_spaces (m_cur_tok + 1);
275 m_end_value = ::get_number (temp);
276 if (m_end_value < m_last_retval)
278 error (_("inverted range"));
280 else if (m_end_value == m_last_retval)
282 /* Degenerate range (number1 == number2). Advance the
283 token pointer so that the range will be treated as a
285 m_cur_tok = m_end_ptr;
293 if (isdigit (*(m_cur_tok + 1)))
294 error (_("negative value"));
295 if (*(m_cur_tok + 1) == '$')
297 /* Convenience variable. */
298 m_last_retval = ::get_number (&m_cur_tok);
299 if (m_last_retval < 0)
300 error (_("negative value"));
303 return m_last_retval;
306 /* See documentation in cli-utils.h. */
309 number_or_range_parser::setup_range (int start_value, int end_value,
312 gdb_assert (start_value > 0);
316 m_last_retval = start_value - 1;
317 m_end_value = end_value;
320 /* See documentation in cli-utils.h. */
323 number_or_range_parser::finished () const
325 /* Parsing is finished when at end of string or null string,
326 or we are not in a range and not in front of an integer, negative
327 integer, convenience var or negative convenience var. */
328 return (m_cur_tok == NULL || *m_cur_tok == '\0'
330 && !(isdigit (*m_cur_tok) || *m_cur_tok == '$')
331 && !(*m_cur_tok == '-'
332 && (isdigit (m_cur_tok[1]) || m_cur_tok[1] == '$'))));
335 /* Accept a number and a string-form list of numbers such as is
336 accepted by get_number_or_range. Return TRUE if the number is
339 By definition, an empty list includes all numbers. This is to
340 be interpreted as typing a command such as "delete break" with
344 number_is_in_list (const char *list, int number)
346 if (list == NULL || *list == '\0')
349 number_or_range_parser parser (list);
351 if (parser.finished ())
352 error (_("Arguments must be numbers or '$' variables."));
353 while (!parser.finished ())
355 int gotnum = parser.get_number ();
358 error (_("Arguments must be numbers or '$' variables."));
359 if (gotnum == number)
365 /* See documentation in cli-utils.h. */
368 remove_trailing_whitespace (const char *start, const char *s)
370 while (s > start && isspace (*(s - 1)))
376 /* See documentation in cli-utils.h. */
379 extract_arg (const char **arg)
384 return std::string ();
386 /* Find the start of the argument. */
387 *arg = skip_spaces (*arg);
389 return std::string ();
392 /* Find the end of the argument. */
393 *arg = skip_to_space (*arg + 1);
396 return std::string ();
398 return std::string (result, *arg - result);
401 /* See documentation in cli-utils.h. */
404 extract_arg (char **arg)
406 const char *arg_const = *arg;
409 result = extract_arg (&arg_const);
410 *arg += arg_const - *arg;
414 /* See documentation in cli-utils.h. */
417 check_for_argument (const char **str, const char *arg, int arg_len)
419 if (strncmp (*str, arg, arg_len) == 0
420 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
423 *str = skip_spaces (*str);
429 /* See documentation in cli-utils.h. */
432 validate_flags_qcs (const char *which_command, qcs_flags *flags)
434 if (flags->cont && flags->silent)
435 error (_("%s: -c and -s are mutually exclusive"), which_command);
438 /* The options used by the 'info variables' commands and similar. */
440 static const gdb::option::option_def info_print_options_defs[] = {
441 gdb::option::boolean_option_def<info_print_options> {
443 [] (info_print_options *opt) { return &opt->quiet; },
444 nullptr, /* show_cmd_cb */
445 nullptr /* set_doc */
448 gdb::option::string_option_def<info_print_options> {
450 [] (info_print_options *opt) { return &opt->type_regexp; },
451 nullptr, /* show_cmd_cb */
452 nullptr /* set_doc */
456 /* Returns the option group used by 'info variables' and similar. */
458 static gdb::option::option_def_group
459 make_info_print_options_def_group (info_print_options *opts)
461 return {{info_print_options_defs}, opts};
464 /* See documentation in cli-utils.h. */
467 extract_info_print_options (info_print_options *opts,
470 auto grp = make_info_print_options_def_group (opts);
471 gdb::option::process_options
472 (args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
473 if (*args != nullptr && **args == '\0')