1 //===-- Args.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Command_h_
11 #define liblldb_Command_h_
20 // Other libraries and framework includes
21 #include "llvm/ADT/StringRef.h"
23 #include "lldb/lldb-private-types.h"
24 #include "lldb/lldb-types.h"
25 #include "lldb/Core/Error.h"
26 #include "lldb/Host/OptionParser.h"
28 namespace lldb_private {
30 typedef std::pair<int, std::string> OptionArgValue;
31 typedef std::pair<std::string, OptionArgValue> OptionArgPair;
32 typedef std::vector<OptionArgPair> OptionArgVector;
33 typedef std::shared_ptr<OptionArgVector> OptionArgVectorSP;
35 struct OptionArgElement
38 eUnrecognizedArg = -1,
43 OptionArgElement (int defs_index, int pos, int arg_pos) :
44 opt_defs_index(defs_index),
55 typedef std::vector<OptionArgElement> OptionElementVector;
57 //----------------------------------------------------------------------
58 /// @class Args Args.h "lldb/Interpreter/Args.h"
59 /// @brief A command line argument class.
61 /// The Args class is designed to be fed a command line. The
62 /// command line is copied into an internal buffer and then split up
63 /// into arguments. Arguments are space delimited if there are no quotes
64 /// (single, double, or backtick quotes) surrounding the argument. Spaces
65 /// can be escaped using a \ character to avoid having to surround an
66 /// argument that contains a space with quotes.
67 //----------------------------------------------------------------------
72 //------------------------------------------------------------------
73 /// Construct with an option command string.
75 /// @param[in] command
76 /// A NULL terminated command that will be copied and split up
79 /// @see Args::SetCommandString(llvm::StringRef)
80 //------------------------------------------------------------------
81 Args (llvm::StringRef command = llvm::StringRef());
83 Args (const Args &rhs);
86 operator= (const Args &rhs);
88 //------------------------------------------------------------------
90 //------------------------------------------------------------------
93 //------------------------------------------------------------------
94 /// Dump all arguments to the stream \a s.
97 /// The stream to which to dump all arguments in the argument
99 //------------------------------------------------------------------
103 //------------------------------------------------------------------
104 /// Sets the command string contained by this object.
106 /// The command string will be copied and split up into arguments
107 /// that can be accessed via the accessor functions.
109 /// @param[in] command
110 /// A command StringRef that will be copied and split up
113 /// @see Args::GetArgumentCount() const
114 /// @see Args::GetArgumentAtIndex (size_t) const
115 /// @see Args::GetArgumentVector ()
116 /// @see Args::Shift ()
117 /// @see Args::Unshift (const char *)
118 //------------------------------------------------------------------
120 SetCommandString (llvm::StringRef command);
123 GetCommandString (std::string &command) const;
126 GetQuotedCommandString (std::string &command) const;
128 //------------------------------------------------------------------
129 /// Gets the number of arguments left in this command object.
132 /// The number or arguments in this object.
133 //------------------------------------------------------------------
135 GetArgumentCount () const;
137 //------------------------------------------------------------------
138 /// Gets the NULL terminated C string argument pointer for the
139 /// argument at index \a idx.
142 /// The NULL terminated C string argument pointer if \a idx is a
143 /// valid argument index, NULL otherwise.
144 //------------------------------------------------------------------
146 GetArgumentAtIndex (size_t idx) const;
149 GetArgumentQuoteCharAtIndex (size_t idx) const;
151 //------------------------------------------------------------------
152 /// Gets the argument vector.
154 /// The value returned by this function can be used by any function
155 /// that takes and vector. The return value is just like \a argv
156 /// in the standard C entry point function:
158 /// int main (int argc, const char **argv);
162 /// An array of NULL terminated C string argument pointers that
163 /// also has a terminating NULL C string pointer
164 //------------------------------------------------------------------
166 GetArgumentVector ();
168 //------------------------------------------------------------------
169 /// Gets the argument vector.
171 /// The value returned by this function can be used by any function
172 /// that takes and vector. The return value is just like \a argv
173 /// in the standard C entry point function:
175 /// int main (int argc, const char **argv);
179 /// An array of NULL terminate C string argument pointers that
180 /// also has a terminating NULL C string pointer
181 //------------------------------------------------------------------
183 GetConstArgumentVector () const;
186 //------------------------------------------------------------------
187 /// Appends a new argument to the end of the list argument list.
189 /// @param[in] arg_cstr
190 /// The new argument as a NULL terminated C string.
192 /// @param[in] quote_char
193 /// If the argument was originally quoted, put in the quote char here.
196 /// The NULL terminated C string of the copy of \a arg_cstr.
197 //------------------------------------------------------------------
199 AppendArgument (const char *arg_cstr, char quote_char = '\0');
202 AppendArguments (const Args &rhs);
205 AppendArguments (const char **argv);
207 //------------------------------------------------------------------
208 /// Insert the argument value at index \a idx to \a arg_cstr.
211 /// The index of where to insert the argument.
213 /// @param[in] arg_cstr
214 /// The new argument as a NULL terminated C string.
216 /// @param[in] quote_char
217 /// If the argument was originally quoted, put in the quote char here.
220 /// The NULL terminated C string of the copy of \a arg_cstr.
221 //------------------------------------------------------------------
223 InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char = '\0');
225 //------------------------------------------------------------------
226 /// Replaces the argument value at index \a idx to \a arg_cstr
227 /// if \a idx is a valid argument index.
230 /// The index of the argument that will have its value replaced.
232 /// @param[in] arg_cstr
233 /// The new argument as a NULL terminated C string.
235 /// @param[in] quote_char
236 /// If the argument was originally quoted, put in the quote char here.
239 /// The NULL terminated C string of the copy of \a arg_cstr if
240 /// \a idx was a valid index, NULL otherwise.
241 //------------------------------------------------------------------
243 ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char = '\0');
245 //------------------------------------------------------------------
246 /// Deletes the argument value at index
247 /// if \a idx is a valid argument index.
250 /// The index of the argument that will have its value replaced.
252 //------------------------------------------------------------------
254 DeleteArgumentAtIndex (size_t idx);
256 //------------------------------------------------------------------
257 /// Sets the argument vector value, optionally copying all
258 /// arguments into an internal buffer.
260 /// Sets the arguments to match those found in \a argv. All argument
261 /// strings will be copied into an internal buffers.
263 // FIXME: Handle the quote character somehow.
264 //------------------------------------------------------------------
266 SetArguments (size_t argc, const char **argv);
269 SetArguments (const char **argv);
271 //------------------------------------------------------------------
272 /// Shifts the first argument C string value of the array off the
275 /// The string value will be freed, so a copy of the string should
276 /// be made by calling Args::GetArgumentAtIndex (size_t) const
277 /// first and copying the returned value before calling
280 /// @see Args::GetArgumentAtIndex (size_t) const
281 //------------------------------------------------------------------
285 //------------------------------------------------------------------
286 /// Inserts a class owned copy of \a arg_cstr at the beginning of
287 /// the argument vector.
289 /// A copy \a arg_cstr will be made.
291 /// @param[in] arg_cstr
292 /// The argument to push on the front of the argument stack.
294 /// @param[in] quote_char
295 /// If the argument was originally quoted, put in the quote char here.
298 /// A pointer to the copy of \a arg_cstr that was made.
299 //------------------------------------------------------------------
301 Unshift (const char *arg_cstr, char quote_char = '\0');
303 //------------------------------------------------------------------
304 /// Parse the arguments in the contained arguments.
306 /// The arguments that are consumed by the argument parsing process
307 /// will be removed from the argument vector. The arguments that
308 /// get processed start at the second argument. The first argument
309 /// is assumed to be the command and will not be touched.
311 /// @see class Options
312 //------------------------------------------------------------------
314 ParseOptions (Options &options);
317 FindArgumentIndexForOption (Option *long_options, int long_options_index);
320 IsPositionalArgument (const char *arg);
322 // The following works almost identically to ParseOptions, except that no option is required to have arguments,
323 // and it builds up the option_arg_vector as it parses the options.
326 ParseAliasOptions (Options &options, CommandReturnObject &result, OptionArgVector *option_arg_vector,
327 std::string &raw_input_line);
330 ParseArgsForCompletion (Options &options, OptionElementVector &option_element_vector, uint32_t cursor_index);
332 //------------------------------------------------------------------
333 // Clear the arguments.
335 // For re-setting or blanking out the list of arguments.
336 //------------------------------------------------------------------
341 StripSpaces (std::string &s,
343 bool trailing = true,
344 bool return_null_if_empty = true);
347 UInt64ValueIsValidForByteSize (uint64_t uval64, size_t total_byte_size)
349 if (total_byte_size > 8)
352 if (total_byte_size == 8)
355 const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
356 return uval64 <= max;
360 SInt64ValueIsValidForByteSize (int64_t sval64, size_t total_byte_size)
362 if (total_byte_size > 8)
365 if (total_byte_size == 8)
368 const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
369 const int64_t min = ~(max);
370 return min <= sval64 && sval64 <= max;
374 StringToAddress (const ExecutionContext *exe_ctx,
376 lldb::addr_t fail_value,
380 StringToBoolean (const char *s, bool fail_value, bool *success_ptr);
382 static char StringToChar(const char *s, char fail_value, bool *success_ptr);
385 StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, Error &error);
387 static lldb::ScriptLanguage
388 StringToScriptLanguage (const char *s, lldb::ScriptLanguage fail_value, bool *success_ptr);
391 StringToFormat (const char *s,
392 lldb::Format &format,
393 size_t *byte_size_ptr); // If non-NULL, then a byte size can precede the format character
395 static lldb::Encoding
396 StringToEncoding (const char *s,
397 lldb::Encoding fail_value = lldb::eEncodingInvalid);
400 StringToGenericRegister (const char *s);
403 StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update);
406 GetShellSafeArgument (const char *unsafe_arg, std::string &safe_arg);
408 // EncodeEscapeSequences will change the textual representation of common
409 // escape sequences like "\n" (two characters) into a single '\n'. It does
410 // this for all of the supported escaped sequences and for the \0ooo (octal)
411 // and \xXX (hex). The resulting "dst" string will contain the character
412 // versions of all supported escape sequences. The common supported escape
413 // sequences are: "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\'", "\"", "\\".
416 EncodeEscapeSequences (const char *src, std::string &dst);
418 // ExpandEscapeSequences will change a string of possibly non-printable
419 // characters and expand them into text. So '\n' will turn into two characters
420 // like "\n" which is suitable for human reading. When a character is not
421 // printable and isn't one of the common in escape sequences listed in the
422 // help for EncodeEscapeSequences, then it will be encoded as octal. Printable
423 // characters are left alone.
425 ExpandEscapedCharacters (const char *src, std::string &dst);
427 // This one isn't really relevant to Arguments per se, but we're using the Args as a
428 // general strings container, so...
430 LongestCommonPrefix (std::string &common_prefix);
433 //------------------------------------------------------------------
434 // Classes that inherit from Args can see and modify these
435 //------------------------------------------------------------------
436 typedef std::list<std::string> arg_sstr_collection;
437 typedef std::vector<const char *> arg_cstr_collection;
438 typedef std::vector<char> arg_quote_char_collection;
439 arg_sstr_collection m_args;
440 arg_cstr_collection m_argv; ///< The current argument vector.
441 arg_quote_char_collection m_args_quote_char;
444 UpdateArgsAfterOptionParsing ();
447 UpdateArgvFromArgs ();
450 ParseSingleArgument (llvm::StringRef command);
453 } // namespace lldb_private
455 #endif // liblldb_Command_h_