Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame^] | 1 | //===-- Args.h --------------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef liblldb_Command_h_ |
| 11 | #define liblldb_Command_h_ |
| 12 | |
| 13 | // C Includes |
| 14 | #include <getopt.h> |
| 15 | |
| 16 | // C++ Includes |
| 17 | #include <list> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | #include <utility> |
| 21 | |
| 22 | // Other libraries and framework includes |
| 23 | // Project includes |
| 24 | #include "lldb/lldb-private.h" |
| 25 | #include "lldb/Core/Error.h" |
| 26 | #include "lldb/lldb-types.h" |
| 27 | |
| 28 | namespace lldb_private { |
| 29 | |
| 30 | typedef std::pair<std::string, std::string> OptionArgPair; |
| 31 | typedef std::vector<OptionArgPair> OptionArgVector; |
| 32 | typedef lldb::SharedPtr<OptionArgVector>::Type OptionArgVectorSP; |
| 33 | |
| 34 | struct OptionArgElement |
| 35 | { |
| 36 | OptionArgElement (int defs_index, int pos, int arg_pos) : |
| 37 | opt_defs_index(defs_index), |
| 38 | opt_pos (pos), |
| 39 | opt_arg_pos (arg_pos) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | int opt_defs_index; |
| 44 | int opt_pos; |
| 45 | int opt_arg_pos; |
| 46 | }; |
| 47 | |
| 48 | typedef std::vector<OptionArgElement> OptionElementVector; |
| 49 | |
| 50 | //---------------------------------------------------------------------- |
| 51 | /// @class Args Args.h "lldb/Core/Args.h" |
| 52 | /// @brief A command line argument class. |
| 53 | /// |
| 54 | /// The Args class is designed to be fed a command line. The |
| 55 | /// command line is copied into an internal buffer and then split up |
| 56 | /// into arguments. Arguments are space delimited if there are no quotes |
| 57 | /// (single, double, or backtick quotes) surrounding the argument. Spaces |
| 58 | /// can be escaped using a \ character to avoid having to surround an |
| 59 | /// argument that contains a space with quotes. |
| 60 | //---------------------------------------------------------------------- |
| 61 | class Args |
| 62 | { |
| 63 | public: |
| 64 | |
| 65 | //------------------------------------------------------------------ |
| 66 | /// Construct with an option command string. |
| 67 | /// |
| 68 | /// @param[in] command |
| 69 | /// A NULL terminated command that will be copied and split up |
| 70 | /// into arguments. |
| 71 | /// |
| 72 | /// @see Args::SetCommandString(const char *) |
| 73 | //------------------------------------------------------------------ |
| 74 | Args (const char *command = NULL); |
| 75 | |
| 76 | Args (const char *command, size_t len); |
| 77 | |
| 78 | //------------------------------------------------------------------ |
| 79 | /// Destructor. |
| 80 | //------------------------------------------------------------------ |
| 81 | ~Args(); |
| 82 | |
| 83 | //------------------------------------------------------------------ |
| 84 | /// Dump all arguments to the stream \a s. |
| 85 | /// |
| 86 | /// @param[in] s |
| 87 | /// The stream to which to dump all arguments in the argument |
| 88 | /// vector. |
| 89 | //------------------------------------------------------------------ |
| 90 | void |
| 91 | Dump (Stream *s); |
| 92 | |
| 93 | //------------------------------------------------------------------ |
| 94 | /// Sets the command string contained by this object. |
| 95 | /// |
| 96 | /// The command string will be copied and split up into arguments |
| 97 | /// that can be accessed via the accessor functions. |
| 98 | /// |
| 99 | /// @param[in] command |
| 100 | /// A NULL terminated command that will be copied and split up |
| 101 | /// into arguments. |
| 102 | /// |
| 103 | /// @see Args::GetArgumentCount() const |
| 104 | /// @see Args::GetArgumentAtIndex (size_t) const |
| 105 | /// @see Args::GetArgumentVector () |
| 106 | /// @see Args::Shift () |
| 107 | /// @see Args::Unshift (const char *) |
| 108 | //------------------------------------------------------------------ |
| 109 | void |
| 110 | SetCommandString (const char *command); |
| 111 | |
| 112 | void |
| 113 | SetCommandString (const char *command, size_t len); |
| 114 | |
| 115 | bool |
| 116 | GetCommandString (std::string &command); |
| 117 | |
| 118 | //------------------------------------------------------------------ |
| 119 | /// Gets the number of arguments left in this command object. |
| 120 | /// |
| 121 | /// @return |
| 122 | /// The number or arguments in this object. |
| 123 | //------------------------------------------------------------------ |
| 124 | size_t |
| 125 | GetArgumentCount () const; |
| 126 | |
| 127 | //------------------------------------------------------------------ |
| 128 | /// Gets the NULL terminated C string argument pointer for the |
| 129 | /// argument at index \a idx. |
| 130 | /// |
| 131 | /// @return |
| 132 | /// The NULL terminated C string argument pointer if \a idx is a |
| 133 | /// valid argument index, NULL otherwise. |
| 134 | //------------------------------------------------------------------ |
| 135 | const char * |
| 136 | GetArgumentAtIndex (size_t idx) const; |
| 137 | |
| 138 | char |
| 139 | GetArgumentQuoteCharAtIndex (size_t idx) const; |
| 140 | |
| 141 | //------------------------------------------------------------------ |
| 142 | /// Gets the argument vector. |
| 143 | /// |
| 144 | /// The value returned by this function can be used by any function |
| 145 | /// that takes and vector. The return value is just like \a argv |
| 146 | /// in the standard C entry point function: |
| 147 | /// \code |
| 148 | /// int main (int argc, const char **argv); |
| 149 | /// \endcode |
| 150 | /// |
| 151 | /// @return |
| 152 | /// An array of NULL terminated C string argument pointers that |
| 153 | /// also has a terminating NULL C string pointer |
| 154 | //------------------------------------------------------------------ |
| 155 | char ** |
| 156 | GetArgumentVector (); |
| 157 | |
| 158 | //------------------------------------------------------------------ |
| 159 | /// Gets the argument vector. |
| 160 | /// |
| 161 | /// The value returned by this function can be used by any function |
| 162 | /// that takes and vector. The return value is just like \a argv |
| 163 | /// in the standard C entry point function: |
| 164 | /// \code |
| 165 | /// int main (int argc, const char **argv); |
| 166 | /// \endcode |
| 167 | /// |
| 168 | /// @return |
| 169 | /// An array of NULL terminate C string argument pointers that |
| 170 | /// also has a terminating NULL C string pointer |
| 171 | //------------------------------------------------------------------ |
| 172 | const char ** |
| 173 | GetConstArgumentVector () const; |
| 174 | |
| 175 | |
| 176 | //------------------------------------------------------------------ |
| 177 | /// Appends a new argument to the end of the list argument list. |
| 178 | /// |
| 179 | /// @param[in] arg_cstr |
| 180 | /// The new argument as a NULL terminated C string. |
| 181 | /// |
| 182 | /// @param[in] quote_char |
| 183 | /// If the argument was originally quoted, put in the quote char here. |
| 184 | /// |
| 185 | /// @return |
| 186 | /// The NULL terminated C string of the copy of \a arg_cstr. |
| 187 | //------------------------------------------------------------------ |
| 188 | const char * |
| 189 | AppendArgument (const char *arg_cstr, char quote_char = '\0'); |
| 190 | |
| 191 | void |
| 192 | AppendArguments (const Args &rhs); |
| 193 | //------------------------------------------------------------------ |
| 194 | /// Insert the argument value at index \a idx to \a arg_cstr. |
| 195 | /// |
| 196 | /// @param[in] idx |
| 197 | /// The index of where to insert the argument. |
| 198 | /// |
| 199 | /// @param[in] arg_cstr |
| 200 | /// The new argument as a NULL terminated C string. |
| 201 | /// |
| 202 | /// @param[in] quote_char |
| 203 | /// If the argument was originally quoted, put in the quote char here. |
| 204 | /// |
| 205 | /// @return |
| 206 | /// The NULL terminated C string of the copy of \a arg_cstr. |
| 207 | //------------------------------------------------------------------ |
| 208 | const char * |
| 209 | InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char = '\0'); |
| 210 | |
| 211 | //------------------------------------------------------------------ |
| 212 | /// Replaces the argument value at index \a idx to \a arg_cstr |
| 213 | /// if \a idx is a valid argument index. |
| 214 | /// |
| 215 | /// @param[in] idx |
| 216 | /// The index of the argument that will have its value replaced. |
| 217 | /// |
| 218 | /// @param[in] arg_cstr |
| 219 | /// The new argument as a NULL terminated C string. |
| 220 | /// |
| 221 | /// @param[in] quote_char |
| 222 | /// If the argument was originally quoted, put in the quote char here. |
| 223 | /// |
| 224 | /// @return |
| 225 | /// The NULL terminated C string of the copy of \a arg_cstr if |
| 226 | /// \a idx was a valid index, NULL otherwise. |
| 227 | //------------------------------------------------------------------ |
| 228 | const char * |
| 229 | ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char = '\0'); |
| 230 | |
| 231 | //------------------------------------------------------------------ |
| 232 | /// Deletes the argument value at index |
| 233 | /// if \a idx is a valid argument index. |
| 234 | /// |
| 235 | /// @param[in] idx |
| 236 | /// The index of the argument that will have its value replaced. |
| 237 | /// |
| 238 | //------------------------------------------------------------------ |
| 239 | void |
| 240 | DeleteArgumentAtIndex (size_t idx); |
| 241 | |
| 242 | //------------------------------------------------------------------ |
| 243 | /// Sets the argument vector value, optionally copying all |
| 244 | /// arguments into an internal buffer. |
| 245 | /// |
| 246 | /// Sets the arguments to match those found in \a argv. All argument |
| 247 | /// strings will be copied into an internal buffers. |
| 248 | // |
| 249 | // FIXME: Handle the quote character somehow. |
| 250 | //------------------------------------------------------------------ |
| 251 | void |
| 252 | SetArguments (int argc, const char **argv); |
| 253 | |
| 254 | //------------------------------------------------------------------ |
| 255 | /// Shifts the first argument C string value of the array off the |
| 256 | /// argument array. |
| 257 | /// |
| 258 | /// The string value will be freed, so a copy of the string should |
| 259 | /// be made by calling Args::GetArgumentAtIndex (size_t) const |
| 260 | /// first and copying the returned value before calling |
| 261 | /// Args::Shift(). |
| 262 | /// |
| 263 | /// @see Args::GetArgumentAtIndex (size_t) const |
| 264 | //------------------------------------------------------------------ |
| 265 | void |
| 266 | Shift (); |
| 267 | |
| 268 | //------------------------------------------------------------------ |
| 269 | /// Inserts a class owned copy of \a arg_cstr at the beginning of |
| 270 | /// the argument vector. |
| 271 | /// |
| 272 | /// A copy \a arg_cstr will be made. |
| 273 | /// |
| 274 | /// @param[in] arg_cstr |
| 275 | /// The argument to push on the front the the argument stack. |
| 276 | /// |
| 277 | /// @param[in] quote_char |
| 278 | /// If the argument was originally quoted, put in the quote char here. |
| 279 | /// |
| 280 | /// @return |
| 281 | /// A pointer to the copy of \a arg_cstr that was made. |
| 282 | //------------------------------------------------------------------ |
| 283 | const char * |
| 284 | Unshift (const char *arg_cstr, char quote_char = '\0'); |
| 285 | |
| 286 | //------------------------------------------------------------------ |
| 287 | /// Parse the arguments in the contained arguments. |
| 288 | /// |
| 289 | /// The arguments that are consumed by the argument parsing process |
| 290 | /// will be removed from the argument vector. The arguements that |
| 291 | /// get processed start at the second argument. The first argument |
| 292 | /// is assumed to be the command and will not be touched. |
| 293 | /// |
| 294 | /// @see class Options |
| 295 | //------------------------------------------------------------------ |
| 296 | Error |
| 297 | ParseOptions (Options &options); |
| 298 | |
| 299 | // The following works almost identically to ParseOptions, except that no option is required to have arguments, |
| 300 | // and it builds up the option_arg_vector as it parses the options. |
| 301 | |
| 302 | void |
| 303 | ParseAliasOptions (Options &options, CommandReturnObject &result, OptionArgVector *option_arg_vector); |
| 304 | |
| 305 | void |
| 306 | ParseArgsForCompletion (Options &options, OptionElementVector &option_element_vector); |
| 307 | |
| 308 | //------------------------------------------------------------------ |
| 309 | // Clear the arguments. |
| 310 | // |
| 311 | // For re-setting or blanking out the list of arguments. |
| 312 | //------------------------------------------------------------------ |
| 313 | void |
| 314 | Clear (); |
| 315 | |
| 316 | static int32_t |
| 317 | StringToSInt32 (const char *s, int32_t fail_value = 0, int base = 0, bool *success_ptr = NULL); |
| 318 | |
| 319 | static uint32_t |
| 320 | StringToUInt32 (const char *s, uint32_t fail_value = 0, int base = 0, bool *success_ptr = NULL); |
| 321 | |
| 322 | static int64_t |
| 323 | StringToSInt64 (const char *s, int64_t fail_value = 0, int base = 0, bool *success_ptr = NULL); |
| 324 | |
| 325 | static uint64_t |
| 326 | StringToUInt64 (const char *s, uint64_t fail_value = 0, int base = 0, bool *success_ptr = NULL); |
| 327 | |
| 328 | static lldb::addr_t |
| 329 | StringToAddress (const char *s, lldb::addr_t fail_value = LLDB_INVALID_ADDRESS, bool *success_ptr = NULL); |
| 330 | |
| 331 | static bool |
| 332 | StringToBoolean (const char *s, bool fail_value, bool *success_ptr); |
| 333 | |
| 334 | static int32_t |
| 335 | StringToOptionEnum (const char *s, lldb::OptionEnumValueElement *enum_values, int32_t fail_value, bool *success_ptr); |
| 336 | |
| 337 | static lldb::ScriptLanguage |
| 338 | StringToScriptLanguage (const char *s, lldb::ScriptLanguage fail_value, bool *success_ptr); |
| 339 | |
| 340 | static Error |
| 341 | StringToFormat (const char *s, lldb::Format &format); |
| 342 | |
| 343 | // This one isn't really relevant to Arguments per se, but we're using the Args as a |
| 344 | // general strings container, so... |
| 345 | void |
| 346 | LongestCommonPrefix (std::string &common_prefix); |
| 347 | |
| 348 | protected: |
| 349 | //------------------------------------------------------------------ |
| 350 | // Classes that inherit from Args can see and modify these |
| 351 | //------------------------------------------------------------------ |
| 352 | typedef std::list<std::string> arg_sstr_collection; |
| 353 | typedef std::vector<const char *> arg_cstr_collection; |
| 354 | typedef std::vector<char> arg_quote_char_collection; |
| 355 | arg_sstr_collection m_args; |
| 356 | arg_cstr_collection m_argv; ///< The current argument vector. |
| 357 | arg_quote_char_collection m_args_quote_char; |
| 358 | |
| 359 | void |
| 360 | UpdateArgsAfterOptionParsing (); |
| 361 | |
| 362 | void |
| 363 | UpdateArgvFromArgs (); |
| 364 | }; |
| 365 | |
| 366 | } // namespace lldb_private |
| 367 | |
| 368 | #endif // liblldb_Command_h_ |