|
|
|||
File indexing completed on 2026-05-10 08:42:56
0001 //===-- Args.h --------------------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLDB_UTILITY_ARGS_H 0010 #define LLDB_UTILITY_ARGS_H 0011 0012 #include "lldb/Utility/Environment.h" 0013 #include "lldb/lldb-private-types.h" 0014 #include "lldb/lldb-types.h" 0015 #include "llvm/ADT/ArrayRef.h" 0016 #include "llvm/ADT/StringExtras.h" 0017 #include "llvm/ADT/StringRef.h" 0018 #include <string> 0019 #include <utility> 0020 #include <vector> 0021 0022 namespace lldb_private { 0023 0024 /// \class Args Args.h "lldb/Utility/Args.h" 0025 /// A command line argument class. 0026 /// 0027 /// The Args class is designed to be fed a command line. The command line is 0028 /// copied into an internal buffer and then split up into arguments. Arguments 0029 /// are space delimited if there are no quotes (single, double, or backtick 0030 /// quotes) surrounding the argument. Spaces can be escaped using a \ 0031 /// character to avoid having to surround an argument that contains a space 0032 /// with quotes. 0033 class Args { 0034 public: 0035 struct ArgEntry { 0036 private: 0037 friend class Args; 0038 0039 std::unique_ptr<char[]> ptr; 0040 char quote = '\0'; 0041 /// The position of the argument in the original argument string. 0042 std::optional<uint16_t> column; 0043 0044 char *data() { return ptr.get(); } 0045 0046 public: 0047 ArgEntry() = default; 0048 ArgEntry(llvm::StringRef str, char quote, std::optional<uint16_t> column); 0049 0050 llvm::StringRef ref() const { return c_str(); } 0051 const char *c_str() const { return ptr.get(); } 0052 0053 /// Returns true if this argument was quoted in any way. 0054 bool IsQuoted() const { return quote != '\0'; } 0055 char GetQuoteChar() const { return quote; } 0056 std::optional<uint16_t> GetPos() const { return column; } 0057 size_t GetLength() const { return ref().size(); } 0058 }; 0059 0060 /// Construct with an option command string. 0061 /// 0062 /// \param[in] command 0063 /// A NULL terminated command that will be copied and split up 0064 /// into arguments. 0065 /// 0066 /// \see Args::SetCommandString(llvm::StringRef) 0067 Args(llvm::StringRef command = llvm::StringRef()); 0068 0069 Args(const Args &rhs); 0070 explicit Args(const StringList &list); 0071 explicit Args(llvm::ArrayRef<llvm::StringRef> args); 0072 0073 Args &operator=(const Args &rhs); 0074 0075 /// Destructor. 0076 ~Args(); 0077 0078 explicit Args(const Environment &env) : Args() { 0079 SetArguments(const_cast<const char **>(env.getEnvp().get())); 0080 } 0081 0082 explicit operator Environment() const { return GetConstArgumentVector(); } 0083 0084 /// Dump all entries to the stream \a s using label \a label_name. 0085 /// 0086 /// If label_name is nullptr, the dump operation is skipped. 0087 /// 0088 /// \param[in] s 0089 /// The stream to which to dump all arguments in the argument 0090 /// vector. 0091 /// \param[in] label_name 0092 /// The label_name to use as the label printed for each 0093 /// entry of the args like so: 0094 /// {label_name}[{index}]={value} 0095 void Dump(Stream &s, const char *label_name = "argv") const; 0096 0097 /// Sets the command string contained by this object. 0098 /// 0099 /// The command string will be copied and split up into arguments that can 0100 /// be accessed via the accessor functions. 0101 /// 0102 /// \param[in] command 0103 /// A command StringRef that will be copied and split up 0104 /// into arguments. 0105 /// 0106 /// \see Args::GetArgumentCount() const 0107 /// \see Args::GetArgumentAtIndex (size_t) const @see 0108 /// Args::GetArgumentVector () \see Args::Shift () \see Args::Unshift (const 0109 /// char *) 0110 void SetCommandString(llvm::StringRef command); 0111 0112 bool GetCommandString(std::string &command) const; 0113 0114 bool GetQuotedCommandString(std::string &command) const; 0115 0116 /// Gets the number of arguments left in this command object. 0117 /// 0118 /// \return 0119 /// The number or arguments in this object. 0120 size_t GetArgumentCount() const { return m_entries.size(); } 0121 0122 bool empty() const { return GetArgumentCount() == 0; } 0123 0124 /// Gets the NULL terminated C string argument pointer for the argument at 0125 /// index \a idx. 0126 /// 0127 /// \return 0128 /// The NULL terminated C string argument pointer if \a idx is a 0129 /// valid argument index, NULL otherwise. 0130 const char *GetArgumentAtIndex(size_t idx) const; 0131 0132 llvm::ArrayRef<ArgEntry> entries() const { return m_entries; } 0133 0134 using const_iterator = std::vector<ArgEntry>::const_iterator; 0135 0136 const_iterator begin() const { return m_entries.begin(); } 0137 const_iterator end() const { return m_entries.end(); } 0138 0139 size_t size() const { return GetArgumentCount(); } 0140 const ArgEntry &operator[](size_t n) const { return m_entries[n]; } 0141 0142 /// Gets the argument vector. 0143 /// 0144 /// The value returned by this function can be used by any function that 0145 /// takes and vector. The return value is just like \a argv in the standard 0146 /// C entry point function: 0147 /// \code 0148 /// int main (int argc, const char **argv); 0149 /// \endcode 0150 /// 0151 /// \return 0152 /// An array of NULL terminated C string argument pointers that 0153 /// also has a terminating NULL C string pointer 0154 char **GetArgumentVector(); 0155 0156 /// Gets the argument vector. 0157 /// 0158 /// The value returned by this function can be used by any function that 0159 /// takes and vector. The return value is just like \a argv in the standard 0160 /// C entry point function: 0161 /// \code 0162 /// int main (int argc, const char **argv); 0163 /// \endcode 0164 /// 0165 /// \return 0166 /// An array of NULL terminate C string argument pointers that 0167 /// also has a terminating NULL C string pointer 0168 const char **GetConstArgumentVector() const; 0169 0170 /// Gets the argument as an ArrayRef. Note that the return value does *not* 0171 /// have a nullptr const char * at the end, as the size of the list is 0172 /// embedded in the ArrayRef object. 0173 llvm::ArrayRef<const char *> GetArgumentArrayRef() const { 0174 return llvm::ArrayRef(m_argv).drop_back(); 0175 } 0176 0177 /// Appends a new argument to the end of the list argument list. 0178 /// 0179 /// \param[in] arg_str 0180 /// The new argument. 0181 /// 0182 /// \param[in] quote_char 0183 /// If the argument was originally quoted, put in the quote char here. 0184 void AppendArgument(llvm::StringRef arg_str, char quote_char = '\0'); 0185 0186 void AppendArguments(const Args &rhs); 0187 0188 void AppendArguments(const char **argv); 0189 0190 /// Insert the argument value at index \a idx to \a arg_str. 0191 /// 0192 /// \param[in] idx 0193 /// The index of where to insert the argument. 0194 /// 0195 /// \param[in] arg_str 0196 /// The new argument. 0197 /// 0198 /// \param[in] quote_char 0199 /// If the argument was originally quoted, put in the quote char here. 0200 void InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str, 0201 char quote_char = '\0'); 0202 0203 /// Replaces the argument value at index \a idx to \a arg_str if \a idx is 0204 /// a valid argument index. 0205 /// 0206 /// \param[in] idx 0207 /// The index of the argument that will have its value replaced. 0208 /// 0209 /// \param[in] arg_str 0210 /// The new argument. 0211 /// 0212 /// \param[in] quote_char 0213 /// If the argument was originally quoted, put in the quote char here. 0214 void ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str, 0215 char quote_char = '\0'); 0216 0217 /// Deletes the argument value at index 0218 /// if \a idx is a valid argument index. 0219 /// 0220 /// \param[in] idx 0221 /// The index of the argument that will have its value replaced. 0222 /// 0223 void DeleteArgumentAtIndex(size_t idx); 0224 0225 /// Sets the argument vector value, optionally copying all arguments into an 0226 /// internal buffer. 0227 /// 0228 /// Sets the arguments to match those found in \a argv. All argument strings 0229 /// will be copied into an internal buffers. 0230 // 0231 // FIXME: Handle the quote character somehow. 0232 void SetArguments(size_t argc, const char **argv); 0233 0234 void SetArguments(const char **argv); 0235 0236 /// Shifts the first argument C string value of the array off the argument 0237 /// array. 0238 /// 0239 /// The string value will be freed, so a copy of the string should be made 0240 /// by calling Args::GetArgumentAtIndex (size_t) const first and copying the 0241 /// returned value before calling Args::Shift(). 0242 /// 0243 /// \see Args::GetArgumentAtIndex (size_t) const 0244 void Shift(); 0245 0246 /// Inserts a class owned copy of \a arg_str at the beginning of the 0247 /// argument vector. 0248 /// 0249 /// A copy \a arg_str will be made. 0250 /// 0251 /// \param[in] arg_str 0252 /// The argument to push on the front of the argument stack. 0253 /// 0254 /// \param[in] quote_char 0255 /// If the argument was originally quoted, put in the quote char here. 0256 void Unshift(llvm::StringRef arg_str, char quote_char = '\0'); 0257 0258 /// Clear the arguments. 0259 /// 0260 /// For re-setting or blanking out the list of arguments. 0261 void Clear(); 0262 0263 static lldb::Encoding 0264 StringToEncoding(llvm::StringRef s, 0265 lldb::Encoding fail_value = lldb::eEncodingInvalid); 0266 0267 static uint32_t StringToGenericRegister(llvm::StringRef s); 0268 0269 static std::string GetShellSafeArgument(const FileSpec &shell, 0270 llvm::StringRef unsafe_arg); 0271 0272 /// EncodeEscapeSequences will change the textual representation of common 0273 /// escape sequences like "\n" (two characters) into a single '\n'. It does 0274 /// this for all of the supported escaped sequences and for the \0ooo (octal) 0275 /// and \xXX (hex). The resulting "dst" string will contain the character 0276 /// versions of all supported escape sequences. The common supported escape 0277 /// sequences are: "\a", "\b", "\f", "\n", "\r", "\t", "\v", "\'", "\"", "\\". 0278 static void EncodeEscapeSequences(const char *src, std::string &dst); 0279 0280 /// ExpandEscapeSequences will change a string of possibly non-printable 0281 /// characters and expand them into text. So '\n' will turn into two 0282 /// characters like "\n" which is suitable for human reading. When a character 0283 /// is not printable and isn't one of the common in escape sequences listed in 0284 /// the help for EncodeEscapeSequences, then it will be encoded as octal. 0285 /// Printable characters are left alone. 0286 static void ExpandEscapedCharacters(const char *src, std::string &dst); 0287 0288 static std::string EscapeLLDBCommandArgument(const std::string &arg, 0289 char quote_char); 0290 0291 private: 0292 std::vector<ArgEntry> m_entries; 0293 /// The arguments as C strings with a trailing nullptr element. 0294 /// 0295 /// These strings are owned by the ArgEntry object in m_entries with the 0296 /// same index. 0297 std::vector<char *> m_argv; 0298 }; 0299 0300 /// \class OptionsWithRaw Args.h "lldb/Utility/Args.h" 0301 /// A pair of an option list with a 'raw' string as a suffix. 0302 /// 0303 /// This class works similar to Args, but handles the case where we have a 0304 /// trailing string that shouldn't be interpreted as a list of arguments but 0305 /// preserved as is. It is also only useful for handling command line options 0306 /// (e.g. '-foo bar -i0') that start with a dash. 0307 /// 0308 /// The leading option list is optional. If the first non-space character 0309 /// in the string starts with a dash, and the string contains an argument 0310 /// that is an unquoted double dash (' -- '), then everything up to the double 0311 /// dash is parsed as a list of arguments. Everything after the double dash 0312 /// is interpreted as the raw suffix string. Note that the space behind the 0313 /// double dash is not part of the raw suffix. 0314 /// 0315 /// All strings not matching the above format as considered to be just a raw 0316 /// string without any options. 0317 /// 0318 /// \see Args 0319 class OptionsWithRaw { 0320 public: 0321 /// Parse the given string as a list of optional arguments with a raw suffix. 0322 /// 0323 /// See the class description for a description of the input format. 0324 /// 0325 /// \param[in] argument_string 0326 /// The string that should be parsed. 0327 explicit OptionsWithRaw(llvm::StringRef argument_string); 0328 0329 /// Returns true if there are any arguments before the raw suffix. 0330 bool HasArgs() const { return m_has_args; } 0331 0332 /// Returns the list of arguments. 0333 /// 0334 /// You can only call this method if HasArgs returns true. 0335 Args &GetArgs() { 0336 assert(m_has_args); 0337 return m_args; 0338 } 0339 0340 /// Returns the list of arguments. 0341 /// 0342 /// You can only call this method if HasArgs returns true. 0343 const Args &GetArgs() const { 0344 assert(m_has_args); 0345 return m_args; 0346 } 0347 0348 /// Returns the part of the input string that was used for parsing the 0349 /// argument list. This string also includes the double dash that is used 0350 /// for separating the argument list from the suffix. 0351 /// 0352 /// You can only call this method if HasArgs returns true. 0353 llvm::StringRef GetArgStringWithDelimiter() const { 0354 assert(m_has_args); 0355 return m_arg_string_with_delimiter; 0356 } 0357 0358 /// Returns the part of the input string that was used for parsing the 0359 /// argument list. 0360 /// 0361 /// You can only call this method if HasArgs returns true. 0362 llvm::StringRef GetArgString() const { 0363 assert(m_has_args); 0364 return m_arg_string; 0365 } 0366 0367 /// Returns the raw suffix part of the parsed string. 0368 const std::string &GetRawPart() const { return m_suffix; } 0369 0370 private: 0371 void SetFromString(llvm::StringRef arg_string); 0372 0373 /// Keeps track if we have parsed and stored any arguments. 0374 bool m_has_args = false; 0375 Args m_args; 0376 llvm::StringRef m_arg_string; 0377 llvm::StringRef m_arg_string_with_delimiter; 0378 0379 // FIXME: This should be a StringRef, but some of the calling code expect a 0380 // C string here so only a real std::string is possible. 0381 std::string m_suffix; 0382 }; 0383 0384 } // namespace lldb_private 0385 0386 #endif // LLDB_UTILITY_ARGS_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|