1 //===-- Args.h --------------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UTILITY_ARGS_H
10 #define LLDB_UTILITY_ARGS_H
12 #include "lldb/Utility/Environment.h"
13 #include "lldb/lldb-private-types.h"
14 #include "lldb/lldb-types.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/StringRef.h"
21 namespace lldb_private {
23 /// \class Args Args.h "lldb/Utility/Args.h"
24 /// A command line argument class.
26 /// The Args class is designed to be fed a command line. The command line is
27 /// copied into an internal buffer and then split up into arguments. Arguments
28 /// are space delimited if there are no quotes (single, double, or backtick
29 /// quotes) surrounding the argument. Spaces can be escaped using a \
30 /// character to avoid having to surround an argument that contains a space
37 std::unique_ptr<char[]> ptr;
39 char *data() { return ptr.get(); }
43 ArgEntry(llvm::StringRef str, char quote);
47 const char *c_str() const { return ptr.get(); }
49 /// Returns true if this argument was quoted in any way.
50 bool IsQuoted() const { return quote != '\0'; }
53 /// Construct with an option command string.
55 /// \param[in] command
56 /// A NULL terminated command that will be copied and split up
59 /// \see Args::SetCommandString(llvm::StringRef)
60 Args(llvm::StringRef command = llvm::StringRef());
62 Args(const Args &rhs);
63 explicit Args(const StringList &list);
65 Args &operator=(const Args &rhs);
70 explicit Args(const Environment &env) : Args() {
71 SetArguments(const_cast<const char **>(env.getEnvp().get()));
74 explicit operator Environment() const { return GetConstArgumentVector(); }
76 /// Dump all entries to the stream \a s using label \a label_name.
78 /// If label_name is nullptr, the dump operation is skipped.
81 /// The stream to which to dump all arguments in the argument
83 /// \param[in] label_name
84 /// The label_name to use as the label printed for each
85 /// entry of the args like so:
86 /// {label_name}[{index}]={value}
87 void Dump(Stream &s, const char *label_name = "argv") const;
89 /// Sets the command string contained by this object.
91 /// The command string will be copied and split up into arguments that can
92 /// be accessed via the accessor functions.
94 /// \param[in] command
95 /// A command StringRef that will be copied and split up
98 /// \see Args::GetArgumentCount() const
99 /// \see Args::GetArgumentAtIndex (size_t) const @see
100 /// Args::GetArgumentVector () \see Args::Shift () \see Args::Unshift (const
102 void SetCommandString(llvm::StringRef command);
104 bool GetCommandString(std::string &command) const;
106 bool GetQuotedCommandString(std::string &command) const;
108 /// Gets the number of arguments left in this command object.
111 /// The number or arguments in this object.
112 size_t GetArgumentCount() const;
113 bool empty() const { return GetArgumentCount() == 0; }
115 /// Gets the NULL terminated C string argument pointer for the argument at
119 /// The NULL terminated C string argument pointer if \a idx is a
120 /// valid argument index, NULL otherwise.
121 const char *GetArgumentAtIndex(size_t idx) const;
123 llvm::ArrayRef<ArgEntry> entries() const { return m_entries; }
124 char GetArgumentQuoteCharAtIndex(size_t idx) const;
126 using const_iterator = std::vector<ArgEntry>::const_iterator;
128 const_iterator begin() const { return m_entries.begin(); }
129 const_iterator end() const { return m_entries.end(); }
131 size_t size() const { return GetArgumentCount(); }
132 const ArgEntry &operator[](size_t n) const { return m_entries[n]; }
134 /// Gets the argument vector.
136 /// The value returned by this function can be used by any function that
137 /// takes and vector. The return value is just like \a argv in the standard
138 /// C entry point function:
140 /// int main (int argc, const char **argv);
144 /// An array of NULL terminated C string argument pointers that
145 /// also has a terminating NULL C string pointer
146 char **GetArgumentVector();
148 /// Gets the argument vector.
150 /// The value returned by this function can be used by any function that
151 /// takes and vector. The return value is just like \a argv in the standard
152 /// C entry point function:
154 /// int main (int argc, const char **argv);
158 /// An array of NULL terminate C string argument pointers that
159 /// also has a terminating NULL C string pointer
160 const char **GetConstArgumentVector() const;
162 /// Gets the argument as an ArrayRef. Note that the return value does *not*
163 /// have a nullptr const char * at the end, as the size of the list is
164 /// embedded in the ArrayRef object.
165 llvm::ArrayRef<const char *> GetArgumentArrayRef() const {
166 return llvm::makeArrayRef(m_argv).drop_back();
169 /// Appends a new argument to the end of the list argument list.
171 /// \param[in] arg_cstr
172 /// The new argument as a NULL terminated C string.
174 /// \param[in] quote_char
175 /// If the argument was originally quoted, put in the quote char here.
176 void AppendArgument(llvm::StringRef arg_str, char quote_char = '\0');
178 void AppendArguments(const Args &rhs);
180 void AppendArguments(const char **argv);
182 /// Insert the argument value at index \a idx to \a arg_cstr.
185 /// The index of where to insert the argument.
187 /// \param[in] arg_cstr
188 /// The new argument as a NULL terminated C string.
190 /// \param[in] quote_char
191 /// If the argument was originally quoted, put in the quote char here.
194 /// The NULL terminated C string of the copy of \a arg_cstr.
195 void InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
196 char quote_char = '\0');
198 /// Replaces the argument value at index \a idx to \a arg_cstr if \a idx is
199 /// a valid argument index.
202 /// The index of the argument that will have its value replaced.
204 /// \param[in] arg_cstr
205 /// The new argument as a NULL terminated C string.
207 /// \param[in] quote_char
208 /// If the argument was originally quoted, put in the quote char here.
209 void ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
210 char quote_char = '\0');
212 /// Deletes the argument value at index
213 /// if \a idx is a valid argument index.
216 /// The index of the argument that will have its value replaced.
218 void DeleteArgumentAtIndex(size_t idx);
220 /// Sets the argument vector value, optionally copying all arguments into an
223 /// Sets the arguments to match those found in \a argv. All argument strings
224 /// will be copied into an internal buffers.
226 // FIXME: Handle the quote character somehow.
227 void SetArguments(size_t argc, const char **argv);
229 void SetArguments(const char **argv);
231 /// Shifts the first argument C string value of the array off the argument
234 /// The string value will be freed, so a copy of the string should be made
235 /// by calling Args::GetArgumentAtIndex (size_t) const first and copying the
236 /// returned value before calling Args::Shift().
238 /// \see Args::GetArgumentAtIndex (size_t) const
241 /// Inserts a class owned copy of \a arg_cstr at the beginning of the
244 /// A copy \a arg_cstr will be made.
246 /// \param[in] arg_cstr
247 /// The argument to push on the front of the argument stack.
249 /// \param[in] quote_char
250 /// If the argument was originally quoted, put in the quote char here.
251 void Unshift(llvm::StringRef arg_str, char quote_char = '\0');
253 // Clear the arguments.
255 // For re-setting or blanking out the list of arguments.
258 static const char *StripSpaces(std::string &s, bool leading = true,
259 bool trailing = true,
260 bool return_null_if_empty = true);
262 static bool UInt64ValueIsValidForByteSize(uint64_t uval64,
263 size_t total_byte_size) {
264 if (total_byte_size > 8)
267 if (total_byte_size == 8)
270 const uint64_t max = (static_cast<uint64_t>(1)
271 << static_cast<uint64_t>(total_byte_size * 8)) -
273 return uval64 <= max;
276 static bool SInt64ValueIsValidForByteSize(int64_t sval64,
277 size_t total_byte_size) {
278 if (total_byte_size > 8)
281 if (total_byte_size == 8)
284 const int64_t max = (static_cast<int64_t>(1)
285 << static_cast<uint64_t>(total_byte_size * 8 - 1)) -
287 const int64_t min = ~(max);
288 return min <= sval64 && sval64 <= max;
291 static lldb::Encoding
292 StringToEncoding(llvm::StringRef s,
293 lldb::Encoding fail_value = lldb::eEncodingInvalid);
295 static uint32_t StringToGenericRegister(llvm::StringRef s);
297 static const char *GetShellSafeArgument(const FileSpec &shell,
298 const char *unsafe_arg,
299 std::string &safe_arg);
301 // EncodeEscapeSequences will change the textual representation of common
302 // escape sequences like "\n" (two characters) into a single '\n'. It does
303 // this for all of the supported escaped sequences and for the \0ooo (octal)
304 // and \xXX (hex). The resulting "dst" string will contain the character
305 // versions of all supported escape sequences. The common supported escape
306 // sequences are: "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\'", "\"", "\\".
308 static void EncodeEscapeSequences(const char *src, std::string &dst);
310 // ExpandEscapeSequences will change a string of possibly non-printable
311 // characters and expand them into text. So '\n' will turn into two
312 // characters like "\n" which is suitable for human reading. When a character
313 // is not printable and isn't one of the common in escape sequences listed in
314 // the help for EncodeEscapeSequences, then it will be encoded as octal.
315 // Printable characters are left alone.
316 static void ExpandEscapedCharacters(const char *src, std::string &dst);
318 static std::string EscapeLLDBCommandArgument(const std::string &arg,
322 std::vector<ArgEntry> m_entries;
323 std::vector<char *> m_argv;
326 /// \class OptionsWithRaw Args.h "lldb/Utility/Args.h"
327 /// A pair of an option list with a 'raw' string as a suffix.
329 /// This class works similar to Args, but handles the case where we have a
330 /// trailing string that shouldn't be interpreted as a list of arguments but
331 /// preserved as is. It is also only useful for handling command line options
332 /// (e.g. '-foo bar -i0') that start with a dash.
334 /// The leading option list is optional. If the first non-space character
335 /// in the string starts with a dash, and the string contains an argument
336 /// that is an unquoted double dash (' -- '), then everything up to the double
337 /// dash is parsed as a list of arguments. Everything after the double dash
338 /// is interpreted as the raw suffix string. Note that the space behind the
339 /// double dash is not part of the raw suffix.
341 /// All strings not matching the above format as considered to be just a raw
342 /// string without any options.
345 class OptionsWithRaw {
347 /// Parse the given string as a list of optional arguments with a raw suffix.
349 /// See the class description for a description of the input format.
351 /// \param[in] argument_string
352 /// The string that should be parsed.
353 explicit OptionsWithRaw(llvm::StringRef argument_string);
355 /// Returns true if there are any arguments before the raw suffix.
356 bool HasArgs() const { return m_has_args; }
358 /// Returns the list of arguments.
360 /// You can only call this method if HasArgs returns true.
366 /// Returns the list of arguments.
368 /// You can only call this method if HasArgs returns true.
369 const Args &GetArgs() const {
374 /// Returns the part of the input string that was used for parsing the
375 /// argument list. This string also includes the double dash that is used
376 /// for separating the argument list from the suffix.
378 /// You can only call this method if HasArgs returns true.
379 llvm::StringRef GetArgStringWithDelimiter() const {
381 return m_arg_string_with_delimiter;
384 /// Returns the part of the input string that was used for parsing the
387 /// You can only call this method if HasArgs returns true.
388 llvm::StringRef GetArgString() const {
393 /// Returns the raw suffix part of the parsed string.
394 const std::string &GetRawPart() const { return m_suffix; }
397 void SetFromString(llvm::StringRef arg_string);
399 /// Keeps track if we have parsed and stored any arguments.
400 bool m_has_args = false;
402 llvm::StringRef m_arg_string;
403 llvm::StringRef m_arg_string_with_delimiter;
405 // FIXME: This should be a StringRef, but some of the calling code expect a
406 // C string here so only a real std::string is possible.
407 std::string m_suffix;
410 } // namespace lldb_private
412 #endif // LLDB_UTILITY_ARGS_H