| 1 | /* CLI utilities. |
| 2 | |
| 3 | Copyright (C) 2011-2017 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 | /* Parse a number or a range. |
| 43 | A number will be of the form handled by get_number. |
| 44 | A range will be of the form <number1> - <number2>, and |
| 45 | will represent all the integers between number1 and number2, |
| 46 | inclusive. */ |
| 47 | |
| 48 | class number_or_range_parser |
| 49 | { |
| 50 | public: |
| 51 | /* Default construction. Must call init before calling |
| 52 | get_next. */ |
| 53 | number_or_range_parser () {} |
| 54 | |
| 55 | /* Calls init automatically. */ |
| 56 | number_or_range_parser (const char *string); |
| 57 | |
| 58 | /* STRING is the string to be parsed. */ |
| 59 | void init (const char *string); |
| 60 | |
| 61 | /* While processing a range, this fuction is called iteratively; At |
| 62 | each call it will return the next value in the range. |
| 63 | |
| 64 | At the beginning of parsing a range, the char pointer |
| 65 | STATE->m_cur_tok will be advanced past <number1> and left |
| 66 | pointing at the '-' token. Subsequent calls will not advance the |
| 67 | pointer until the range is completed. The call that completes |
| 68 | the range will advance the pointer past <number2>. */ |
| 69 | int get_number (); |
| 70 | |
| 71 | /* Setup internal state such that get_next() returns numbers in the |
| 72 | START_VALUE to END_VALUE range. END_PTR is where the string is |
| 73 | advanced to when get_next() returns END_VALUE. */ |
| 74 | void setup_range (int start_value, int end_value, |
| 75 | const char *end_ptr); |
| 76 | |
| 77 | /* Returns true if parsing has completed. */ |
| 78 | bool finished () const |
| 79 | { return m_finished; } |
| 80 | |
| 81 | /* Return the string being parsed. When parsing has finished, this |
| 82 | points past the last parsed token. */ |
| 83 | const char *cur_tok () const |
| 84 | { return m_cur_tok; } |
| 85 | |
| 86 | /* True when parsing a range. */ |
| 87 | bool in_range () const |
| 88 | { return m_in_range; } |
| 89 | |
| 90 | /* When parsing a range, the final value in the range. */ |
| 91 | int end_value () const |
| 92 | { return m_end_value; } |
| 93 | |
| 94 | /* When parsing a range, skip past the final token in the range. */ |
| 95 | void skip_range () |
| 96 | { |
| 97 | gdb_assert (m_in_range); |
| 98 | m_cur_tok = m_end_ptr; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | /* No need for these. They are intentionally not defined anywhere. */ |
| 103 | number_or_range_parser (const number_or_range_parser &); |
| 104 | number_or_range_parser &operator= (const number_or_range_parser &); |
| 105 | |
| 106 | /* True if parsing has completed. */ |
| 107 | bool m_finished; |
| 108 | |
| 109 | /* The string being parsed. When parsing has finished, this points |
| 110 | past the last parsed token. */ |
| 111 | const char *m_cur_tok; |
| 112 | |
| 113 | /* Last value returned. */ |
| 114 | int m_last_retval; |
| 115 | |
| 116 | /* When parsing a range, the final value in the range. */ |
| 117 | int m_end_value; |
| 118 | |
| 119 | /* When parsing a range, a pointer past the final token in the |
| 120 | range. */ |
| 121 | const char *m_end_ptr; |
| 122 | |
| 123 | /* True when parsing a range. */ |
| 124 | bool m_in_range; |
| 125 | }; |
| 126 | |
| 127 | /* Accept a number and a string-form list of numbers such as is |
| 128 | accepted by get_number_or_range. Return TRUE if the number is |
| 129 | in the list. |
| 130 | |
| 131 | By definition, an empty list includes all numbers. This is to |
| 132 | be interpreted as typing a command such as "delete break" with |
| 133 | no arguments. */ |
| 134 | |
| 135 | extern int number_is_in_list (const char *list, int number); |
| 136 | |
| 137 | /* Reverse S to the last non-whitespace character without skipping past |
| 138 | START. */ |
| 139 | |
| 140 | extern const char *remove_trailing_whitespace (const char *start, |
| 141 | const char *s); |
| 142 | |
| 143 | /* Same, for non-const S. */ |
| 144 | |
| 145 | static inline char * |
| 146 | remove_trailing_whitespace (const char *start, char *s) |
| 147 | { |
| 148 | return (char *) remove_trailing_whitespace (start, (const char *) s); |
| 149 | } |
| 150 | |
| 151 | /* A helper function to extract an argument from *ARG. An argument is |
| 152 | delimited by whitespace. The return value is either NULL if no |
| 153 | argument was found, or an xmalloc'd string. */ |
| 154 | |
| 155 | extern char *extract_arg (char **arg); |
| 156 | |
| 157 | /* A const-correct version of "extract_arg". |
| 158 | |
| 159 | Since the returned value is xmalloc'd, it eventually needs to be |
| 160 | xfree'ed, which prevents us from making it const as well. */ |
| 161 | |
| 162 | extern char *extract_arg_const (const char **arg); |
| 163 | |
| 164 | /* A helper function that looks for an argument at the start of a |
| 165 | string. The argument must also either be at the end of the string, |
| 166 | or be followed by whitespace. Returns 1 if it finds the argument, |
| 167 | 0 otherwise. If the argument is found, it updates *STR. */ |
| 168 | extern int check_for_argument (const char **str, const char *arg, int arg_len); |
| 169 | |
| 170 | /* Same, for non-const STR. */ |
| 171 | |
| 172 | static inline int |
| 173 | check_for_argument (char **str, const char *arg, int arg_len) |
| 174 | { |
| 175 | return check_for_argument (const_cast<const char **> (str), |
| 176 | arg, arg_len); |
| 177 | } |
| 178 | |
| 179 | #endif /* CLI_UTILS_H */ |