1 /* CLI utilities. 2 3 Copyright (C) 2011-2016 Free Software Foundation, Inc. 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 20 #ifndef CLI_UTILS_H 21 #define CLI_UTILS_H 22 23 /* *PP is a string denoting a number. Get the number. Advance *PP 24 after the string and any trailing whitespace. 25 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 32 extern int get_number_trailer (const char **pp, int trailer); 33 34 /* Convenience. Like get_number_trailer, but with no TRAILER. */ 35 36 extern int get_number_const (const char **); 37 38 /* Like get_number_const, but takes a non-const "char **". */ 39 40 extern int get_number (char **); 41 42 /* An object of this type is passed to get_number_or_range. It must 43 be initialized by calling init_number_or_range. This type is 44 defined here so that it can be stack-allocated, but all members 45 other than `finished' and `string' should be treated as opaque. */ 46 47 struct get_number_or_range_state 48 { 49 /* Non-zero if parsing has completed. */ 50 int finished; 51 52 /* The string being parsed. When parsing has finished, this points 53 past the last parsed token. */ 54 const char *string; 55 56 /* Last value returned. */ 57 int last_retval; 58 59 /* When parsing a range, the final value in the range. */ 60 int end_value; 61 62 /* When parsing a range, a pointer past the final token in the 63 range. */ 64 const char *end_ptr; 65 66 /* Non-zero when parsing a range. */ 67 int in_range; 68 }; 69 70 /* Initialize a get_number_or_range_state for use with 71 get_number_or_range_state. STRING is the string to be parsed. */ 72 73 extern void init_number_or_range (struct get_number_or_range_state *state, 74 const char *string); 75 76 /* Parse a number or a range. 77 A number will be of the form handled by get_number. 78 A range will be of the form <number1> - <number2>, and 79 will represent all the integers between number1 and number2, 80 inclusive. 81 82 While processing a range, this fuction is called iteratively; 83 At each call it will return the next value in the range. 84 85 At the beginning of parsing a range, the char pointer STATE->string will 86 be advanced past <number1> and left pointing at the '-' token. 87 Subsequent calls will not advance the pointer until the range 88 is completed. The call that completes the range will advance 89 the pointer past <number2>. */ 90 91 extern int get_number_or_range (struct get_number_or_range_state *state); 92 93 /* Setups STATE such that get_number_or_range returns numbers in range 94 START_VALUE to END_VALUE. When get_number_or_range returns 95 END_VALUE, the STATE string is advanced to END_PTR. */ 96 97 extern void number_range_setup_range (struct get_number_or_range_state *state, 98 int start_value, int end_value, 99 const char *end_ptr); 100 101 /* Accept a number and a string-form list of numbers such as is 102 accepted by get_number_or_range. Return TRUE if the number is 103 in the list. 104 105 By definition, an empty list includes all numbers. This is to 106 be interpreted as typing a command such as "delete break" with 107 no arguments. */ 108 109 extern int number_is_in_list (const char *list, int number); 110 111 /* Reverse S to the last non-whitespace character without skipping past 112 START. */ 113 114 extern char *remove_trailing_whitespace (const char *start, char *s); 115 116 /* A helper function to extract an argument from *ARG. An argument is 117 delimited by whitespace. The return value is either NULL if no 118 argument was found, or an xmalloc'd string. */ 119 120 extern char *extract_arg (char **arg); 121 122 /* A const-correct version of "extract_arg". 123 124 Since the returned value is xmalloc'd, it eventually needs to be 125 xfree'ed, which prevents us from making it const as well. */ 126 127 extern char *extract_arg_const (const char **arg); 128 129 /* A helper function that looks for an argument at the start of a 130 string. The argument must also either be at the end of the string, 131 or be followed by whitespace. Returns 1 if it finds the argument, 132 0 otherwise. If the argument is found, it updates *STR. */ 133 extern int check_for_argument (char **str, char *arg, int arg_len); 134 135 #endif /* CLI_UTILS_H */ 136