Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Args.cpp ------------------------------------------------*- 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 | // C Includes |
Eli Friedman | 5661f92 | 2010-06-09 10:59:23 +0000 | [diff] [blame] | 11 | #include <cstdlib> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C++ Includes |
| 13 | // Other libraries and framework includes |
| 14 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Stream.h" |
| 16 | #include "lldb/Core/StreamFile.h" |
| 17 | #include "lldb/Core/StreamString.h" |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 18 | #include "lldb/DataFormatters/FormatManager.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 19 | #include "lldb/Host/StringConvert.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/Args.h" |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandReturnObject.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | #include "lldb/Interpreter/Options.h" |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Process.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 25 | #include "lldb/Target/StackFrame.h" |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Zachary Turner | 54695a3 | 2016-08-29 19:58:14 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" |
| 29 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | //---------------------------------------------------------------------- |
| 34 | // Args constructor |
| 35 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | Args::Args(llvm::StringRef command) : m_args(), m_argv(), m_args_quote_char() { |
| 37 | SetCommandString(command); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | //---------------------------------------------------------------------- |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 41 | // We have to be very careful on the copy constructor of this class |
| 42 | // to make sure we copy all of the string values, but we can't copy the |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | // rhs.m_argv into m_argv since it will point to the "const char *" c |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 44 | // strings in rhs.m_args. We need to copy the string list and update our |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | // own m_argv appropriately. |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 46 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | Args::Args(const Args &rhs) |
| 48 | : m_args(rhs.m_args), m_argv(), m_args_quote_char(rhs.m_args_quote_char) { |
| 49 | UpdateArgvFromArgs(); |
| 50 | } |
| 51 | |
| 52 | //---------------------------------------------------------------------- |
| 53 | // We have to be very careful on the copy constructor of this class |
| 54 | // to make sure we copy all of the string values, but we can't copy the |
| 55 | // rhs.m_argv into m_argv since it will point to the "const char *" c |
| 56 | // strings in rhs.m_args. We need to copy the string list and update our |
| 57 | // own m_argv appropriately. |
| 58 | //---------------------------------------------------------------------- |
| 59 | const Args &Args::operator=(const Args &rhs) { |
| 60 | // Make sure we aren't assigning to self |
| 61 | if (this != &rhs) { |
| 62 | m_args = rhs.m_args; |
| 63 | m_args_quote_char = rhs.m_args_quote_char; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 64 | UpdateArgvFromArgs(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | } |
| 66 | return *this; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | // Destructor |
| 71 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | Args::~Args() {} |
| 73 | |
| 74 | void Args::Dump(Stream &s, const char *label_name) const { |
| 75 | if (!label_name) |
| 76 | return; |
| 77 | |
| 78 | const size_t argc = m_argv.size(); |
| 79 | for (size_t i = 0; i < argc; ++i) { |
| 80 | s.Indent(); |
| 81 | const char *arg_cstr = m_argv[i]; |
| 82 | if (arg_cstr) |
| 83 | s.Printf("%s[%zi]=\"%s\"\n", label_name, i, arg_cstr); |
| 84 | else |
| 85 | s.Printf("%s[%zi]=NULL\n", label_name, i); |
| 86 | } |
| 87 | s.EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | bool Args::GetCommandString(std::string &command) const { |
| 91 | command.clear(); |
| 92 | const size_t argc = GetArgumentCount(); |
| 93 | for (size_t i = 0; i < argc; ++i) { |
| 94 | if (i > 0) |
| 95 | command += ' '; |
| 96 | command += m_argv[i]; |
| 97 | } |
| 98 | return argc > 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | bool Args::GetQuotedCommandString(std::string &command) const { |
| 102 | command.clear(); |
| 103 | const size_t argc = GetArgumentCount(); |
| 104 | for (size_t i = 0; i < argc; ++i) { |
| 105 | if (i > 0) |
| 106 | command.append(1, ' '); |
| 107 | char quote_char = GetArgumentQuoteCharAtIndex(i); |
| 108 | if (quote_char) { |
| 109 | command.append(1, quote_char); |
| 110 | command.append(m_argv[i]); |
| 111 | command.append(1, quote_char); |
| 112 | } else |
| 113 | command.append(m_argv[i]); |
| 114 | } |
| 115 | return argc > 0; |
Caroline Tice | 2d5289d | 2010-12-10 00:26:54 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 118 | // A helper function for argument parsing. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | // Parses the initial part of the first argument using normal double quote |
| 120 | // rules: |
| 121 | // backslash escapes the double quote and itself. The parsed string is appended |
| 122 | // to the second |
| 123 | // argument. The function returns the unparsed portion of the string, starting |
| 124 | // at the closing |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 125 | // quote. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | static llvm::StringRef ParseDoubleQuotes(llvm::StringRef quoted, |
| 127 | std::string &result) { |
| 128 | // Inside double quotes, '\' and '"' are special. |
| 129 | static const char *k_escapable_characters = "\"\\"; |
| 130 | while (true) { |
| 131 | // Skip over over regular characters and append them. |
| 132 | size_t regular = quoted.find_first_of(k_escapable_characters); |
| 133 | result += quoted.substr(0, regular); |
| 134 | quoted = quoted.substr(regular); |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | // If we have reached the end of string or the closing quote, we're done. |
| 137 | if (quoted.empty() || quoted.front() == '"') |
| 138 | break; |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | // We have found a backslash. |
| 141 | quoted = quoted.drop_front(); |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | if (quoted.empty()) { |
| 144 | // A lone backslash at the end of string, let's just append it. |
| 145 | result += '\\'; |
| 146 | break; |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | // If the character after the backslash is not a whitelisted escapable |
| 150 | // character, we |
| 151 | // leave the character sequence untouched. |
| 152 | if (strchr(k_escapable_characters, quoted.front()) == nullptr) |
| 153 | result += '\\'; |
| 154 | |
| 155 | result += quoted.front(); |
| 156 | quoted = quoted.drop_front(); |
| 157 | } |
| 158 | |
| 159 | return quoted; |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // A helper function for SetCommandString. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | // Parses a single argument from the command string, processing quotes and |
| 164 | // backslashes in a |
| 165 | // shell-like manner. The parsed argument is appended to the m_args array. The |
| 166 | // function returns |
| 167 | // the unparsed portion of the string, starting at the first unqouted, unescaped |
| 168 | // whitespace |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 169 | // character. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | llvm::StringRef Args::ParseSingleArgument(llvm::StringRef command) { |
| 171 | // Argument can be split into multiple discontiguous pieces, |
| 172 | // for example: |
| 173 | // "Hello ""World" |
| 174 | // this would result in a single argument "Hello World" (without/ |
| 175 | // the quotes) since the quotes would be removed and there is |
| 176 | // not space between the strings. |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | std::string arg; |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | // Since we can have multiple quotes that form a single command |
| 181 | // in a command like: "Hello "world'!' (which will make a single |
| 182 | // argument "Hello world!") we remember the first quote character |
| 183 | // we encounter and use that for the quote character. |
| 184 | char first_quote_char = '\0'; |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | bool arg_complete = false; |
| 187 | do { |
| 188 | // Skip over over regular characters and append them. |
| 189 | size_t regular = command.find_first_of(" \t\"'`\\"); |
| 190 | arg += command.substr(0, regular); |
| 191 | command = command.substr(regular); |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | if (command.empty()) |
| 194 | break; |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | char special = command.front(); |
| 197 | command = command.drop_front(); |
| 198 | switch (special) { |
| 199 | case '\\': |
| 200 | if (command.empty()) { |
| 201 | arg += '\\'; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | // If the character after the backslash is not a whitelisted escapable |
| 206 | // character, we |
| 207 | // leave the character sequence untouched. |
| 208 | if (strchr(" \t\\'\"`", command.front()) == nullptr) |
| 209 | arg += '\\'; |
| 210 | |
| 211 | arg += command.front(); |
| 212 | command = command.drop_front(); |
| 213 | |
| 214 | break; |
| 215 | |
| 216 | case ' ': |
| 217 | case '\t': |
| 218 | // We are not inside any quotes, we just found a space after an |
| 219 | // argument. We are done. |
| 220 | arg_complete = true; |
| 221 | break; |
| 222 | |
| 223 | case '"': |
| 224 | case '\'': |
| 225 | case '`': |
| 226 | // We found the start of a quote scope. |
| 227 | if (first_quote_char == '\0') |
| 228 | first_quote_char = special; |
| 229 | |
| 230 | if (special == '"') |
| 231 | command = ParseDoubleQuotes(command, arg); |
| 232 | else { |
| 233 | // For single quotes, we simply skip ahead to the matching quote |
| 234 | // character |
| 235 | // (or the end of the string). |
| 236 | size_t quoted = command.find(special); |
| 237 | arg += command.substr(0, quoted); |
| 238 | command = command.substr(quoted); |
| 239 | } |
| 240 | |
| 241 | // If we found a closing quote, skip it. |
| 242 | if (!command.empty()) |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 243 | command = command.drop_front(); |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 244 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | } while (!arg_complete); |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 248 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | m_args.push_back(arg); |
| 250 | m_args_quote_char.push_back(first_quote_char); |
| 251 | return command; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | void Args::SetCommandString(llvm::StringRef command) { |
| 255 | m_args.clear(); |
| 256 | m_argv.clear(); |
| 257 | m_args_quote_char.clear(); |
Greg Clayton | 6ad07dd | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | static const char *k_space_separators = " \t"; |
| 260 | command = command.ltrim(k_space_separators); |
| 261 | while (!command.empty()) { |
| 262 | command = ParseSingleArgument(command); |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 263 | command = command.ltrim(k_space_separators); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | UpdateArgvFromArgs(); |
| 267 | } |
| 268 | |
| 269 | void Args::UpdateArgsAfterOptionParsing() { |
| 270 | // Now m_argv might be out of date with m_args, so we need to fix that |
| 271 | arg_cstr_collection::const_iterator argv_pos, argv_end = m_argv.end(); |
| 272 | arg_sstr_collection::iterator args_pos; |
| 273 | arg_quote_char_collection::iterator quotes_pos; |
| 274 | |
| 275 | for (argv_pos = m_argv.begin(), args_pos = m_args.begin(), |
| 276 | quotes_pos = m_args_quote_char.begin(); |
| 277 | argv_pos != argv_end && args_pos != m_args.end(); ++argv_pos) { |
| 278 | const char *argv_cstr = *argv_pos; |
| 279 | if (argv_cstr == nullptr) |
| 280 | break; |
| 281 | |
| 282 | while (args_pos != m_args.end()) { |
| 283 | const char *args_cstr = args_pos->c_str(); |
| 284 | if (args_cstr == argv_cstr) { |
| 285 | // We found the argument that matches the C string in the |
| 286 | // vector, so we can now look for the next one |
| 287 | ++args_pos; |
| 288 | ++quotes_pos; |
| 289 | break; |
| 290 | } else { |
| 291 | quotes_pos = m_args_quote_char.erase(quotes_pos); |
| 292 | args_pos = m_args.erase(args_pos); |
| 293 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | } |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 297 | if (args_pos != m_args.end()) |
| 298 | m_args.erase(args_pos, m_args.end()); |
| 299 | |
| 300 | if (quotes_pos != m_args_quote_char.end()) |
| 301 | m_args_quote_char.erase(quotes_pos, m_args_quote_char.end()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 304 | void Args::UpdateArgvFromArgs() { |
| 305 | m_argv.clear(); |
| 306 | arg_sstr_collection::const_iterator pos, end = m_args.end(); |
| 307 | for (pos = m_args.begin(); pos != end; ++pos) |
| 308 | m_argv.push_back(pos->c_str()); |
| 309 | m_argv.push_back(nullptr); |
| 310 | // Make sure we have enough arg quote chars in the array |
| 311 | if (m_args_quote_char.size() < m_args.size()) |
| 312 | m_args_quote_char.resize(m_argv.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 315 | size_t Args::GetArgumentCount() const { |
| 316 | if (m_argv.empty()) |
| 317 | return 0; |
| 318 | return m_argv.size() - 1; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 321 | const char *Args::GetArgumentAtIndex(size_t idx) const { |
| 322 | if (idx < m_argv.size()) |
| 323 | return m_argv[idx]; |
| 324 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | char Args::GetArgumentQuoteCharAtIndex(size_t idx) const { |
| 328 | if (idx < m_args_quote_char.size()) |
| 329 | return m_args_quote_char[idx]; |
| 330 | return '\0'; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 333 | char **Args::GetArgumentVector() { |
| 334 | if (!m_argv.empty()) |
| 335 | return const_cast<char **>(&m_argv[0]); |
| 336 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 339 | const char **Args::GetConstArgumentVector() const { |
| 340 | if (!m_argv.empty()) |
| 341 | return const_cast<const char **>(&m_argv[0]); |
| 342 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | void Args::Shift() { |
| 346 | // Don't pop the last NULL terminator from the argv array |
| 347 | if (m_argv.size() > 1) { |
| 348 | m_argv.erase(m_argv.begin()); |
| 349 | m_args.pop_front(); |
| 350 | if (!m_args_quote_char.empty()) |
| 351 | m_args_quote_char.erase(m_args_quote_char.begin()); |
| 352 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 355 | llvm::StringRef Args::Unshift(llvm::StringRef arg_str, char quote_char) { |
| 356 | m_args.push_front(arg_str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | m_argv.insert(m_argv.begin(), m_args.front().c_str()); |
| 358 | m_args_quote_char.insert(m_args_quote_char.begin(), quote_char); |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 359 | return llvm::StringRef::withNullAsEmpty(GetArgumentAtIndex(0)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | void Args::AppendArguments(const Args &rhs) { |
| 363 | const size_t rhs_argc = rhs.GetArgumentCount(); |
| 364 | for (size_t i = 0; i < rhs_argc; ++i) |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 365 | AppendArgument(llvm::StringRef(rhs.GetArgumentAtIndex(i)), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | rhs.GetArgumentQuoteCharAtIndex(i)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | void Args::AppendArguments(const char **argv) { |
| 370 | if (argv) { |
| 371 | for (uint32_t i = 0; argv[i]; ++i) |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 372 | AppendArgument(llvm::StringRef::withNullAsEmpty(argv[i])); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 373 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 376 | llvm::StringRef Args::AppendArgument(llvm::StringRef arg_str, char quote_char) { |
| 377 | return InsertArgumentAtIndex(GetArgumentCount(), arg_str, quote_char); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 380 | llvm::StringRef Args::InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str, |
| 381 | char quote_char) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | // Since we are using a std::list to hold onto the copied C string and |
| 383 | // we don't have direct access to the elements, we have to iterate to |
| 384 | // find the value. |
| 385 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 386 | size_t i = idx; |
| 387 | for (pos = m_args.begin(); i > 0 && pos != end; ++pos) |
| 388 | --i; |
| 389 | |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 390 | pos = m_args.insert(pos, arg_str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 391 | |
| 392 | if (idx >= m_args_quote_char.size()) { |
| 393 | m_args_quote_char.resize(idx + 1); |
| 394 | m_args_quote_char[idx] = quote_char; |
| 395 | } else |
| 396 | m_args_quote_char.insert(m_args_quote_char.begin() + idx, quote_char); |
| 397 | |
| 398 | UpdateArgvFromArgs(); |
| 399 | return GetArgumentAtIndex(idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 402 | llvm::StringRef Args::ReplaceArgumentAtIndex(size_t idx, |
| 403 | llvm::StringRef arg_str, |
| 404 | char quote_char) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | // Since we are using a std::list to hold onto the copied C string and |
| 406 | // we don't have direct access to the elements, we have to iterate to |
| 407 | // find the value. |
| 408 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 409 | size_t i = idx; |
| 410 | for (pos = m_args.begin(); i > 0 && pos != end; ++pos) |
| 411 | --i; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 413 | if (pos != end) { |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 414 | pos->assign(arg_str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | assert(idx < m_argv.size() - 1); |
| 416 | m_argv[idx] = pos->c_str(); |
Greg Clayton | 6ad07dd | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 417 | if (idx >= m_args_quote_char.size()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | m_args_quote_char.resize(idx + 1); |
| 419 | m_args_quote_char[idx] = quote_char; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 420 | return GetArgumentAtIndex(idx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | } |
Zachary Turner | 4037780 | 2016-09-21 22:33:30 +0000 | [diff] [blame] | 422 | return llvm::StringRef(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | void Args::DeleteArgumentAtIndex(size_t idx) { |
| 426 | // Since we are using a std::list to hold onto the copied C string and |
| 427 | // we don't have direct access to the elements, we have to iterate to |
| 428 | // find the value. |
| 429 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 430 | size_t i = idx; |
| 431 | for (pos = m_args.begin(); i > 0 && pos != end; ++pos) |
| 432 | --i; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 433 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 434 | if (pos != end) { |
| 435 | m_args.erase(pos); |
| 436 | assert(idx < m_argv.size() - 1); |
| 437 | m_argv.erase(m_argv.begin() + idx); |
| 438 | if (idx < m_args_quote_char.size()) |
| 439 | m_args_quote_char.erase(m_args_quote_char.begin() + idx); |
| 440 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | void Args::SetArguments(size_t argc, const char **argv) { |
| 444 | // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is |
| 445 | // no need to clear it here. |
| 446 | m_args.clear(); |
| 447 | m_args_quote_char.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | // First copy each string |
| 450 | for (size_t i = 0; i < argc; ++i) { |
| 451 | m_args.push_back(argv[i]); |
| 452 | if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`')) |
| 453 | m_args_quote_char.push_back(argv[i][0]); |
| 454 | else |
| 455 | m_args_quote_char.push_back('\0'); |
| 456 | } |
| 457 | |
| 458 | UpdateArgvFromArgs(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | void Args::SetArguments(const char **argv) { |
| 462 | // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is |
| 463 | // no need to clear it here. |
| 464 | m_args.clear(); |
| 465 | m_args_quote_char.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | if (argv) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | // First copy each string |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | for (size_t i = 0; argv[i]; ++i) { |
| 470 | m_args.push_back(argv[i]); |
| 471 | if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`')) |
| 472 | m_args_quote_char.push_back(argv[i][0]); |
| 473 | else |
| 474 | m_args_quote_char.push_back('\0'); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 477 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 478 | UpdateArgvFromArgs(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | Error Args::ParseOptions(Options &options, ExecutionContext *execution_context, |
| 482 | PlatformSP platform_sp, bool require_validation) { |
| 483 | StreamString sstr; |
| 484 | Error error; |
| 485 | Option *long_options = options.GetLongOptions(); |
| 486 | if (long_options == nullptr) { |
| 487 | error.SetErrorStringWithFormat("invalid long options"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 491 | for (int i = 0; long_options[i].definition != nullptr; ++i) { |
| 492 | if (long_options[i].flag == nullptr) { |
| 493 | if (isprint8(long_options[i].val)) { |
| 494 | sstr << (char)long_options[i].val; |
| 495 | switch (long_options[i].definition->option_has_arg) { |
Tamas Berghammer | 89d3f09 | 2015-09-02 10:35:27 +0000 | [diff] [blame] | 496 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | case OptionParser::eNoArgument: |
| 498 | break; |
| 499 | case OptionParser::eRequiredArgument: |
| 500 | sstr << ':'; |
| 501 | break; |
| 502 | case OptionParser::eOptionalArgument: |
| 503 | sstr << "::"; |
| 504 | break; |
| 505 | } |
| 506 | } |
Tamas Berghammer | 89d3f09 | 2015-09-02 10:35:27 +0000 | [diff] [blame] | 507 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 508 | } |
| 509 | std::unique_lock<std::mutex> lock; |
| 510 | OptionParser::Prepare(lock); |
| 511 | int val; |
| 512 | while (1) { |
| 513 | int long_options_index = -1; |
| 514 | val = |
| 515 | OptionParser::Parse(GetArgumentCount(), GetArgumentVector(), |
| 516 | sstr.GetData(), long_options, &long_options_index); |
| 517 | if (val == -1) |
| 518 | break; |
Tamas Berghammer | 89d3f09 | 2015-09-02 10:35:27 +0000 | [diff] [blame] | 519 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 520 | // Did we get an error? |
| 521 | if (val == '?') { |
| 522 | error.SetErrorStringWithFormat("unknown or ambiguous option"); |
| 523 | break; |
Tamas Berghammer | 89d3f09 | 2015-09-02 10:35:27 +0000 | [diff] [blame] | 524 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | // The option auto-set itself |
| 526 | if (val == 0) |
| 527 | continue; |
| 528 | |
| 529 | ((Options *)&options)->OptionSeen(val); |
| 530 | |
| 531 | // Lookup the long option index |
| 532 | if (long_options_index == -1) { |
| 533 | for (int i = 0; long_options[i].definition || long_options[i].flag || |
| 534 | long_options[i].val; |
| 535 | ++i) { |
| 536 | if (long_options[i].val == val) { |
| 537 | long_options_index = i; |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | // Call the callback with the option |
| 543 | if (long_options_index >= 0 && |
| 544 | long_options[long_options_index].definition) { |
| 545 | const OptionDefinition *def = long_options[long_options_index].definition; |
| 546 | |
| 547 | if (!platform_sp) { |
| 548 | // User did not pass in an explicit platform. Try to grab |
| 549 | // from the execution context. |
| 550 | TargetSP target_sp = |
| 551 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
| 552 | platform_sp = target_sp ? target_sp->GetPlatform() : PlatformSP(); |
| 553 | } |
| 554 | OptionValidator *validator = def->validator; |
| 555 | |
| 556 | if (!platform_sp && require_validation) { |
| 557 | // Caller requires validation but we cannot validate as we |
| 558 | // don't have the mandatory platform against which to |
| 559 | // validate. |
| 560 | error.SetErrorString("cannot validate options: " |
| 561 | "no platform available"); |
| 562 | return error; |
| 563 | } |
| 564 | |
| 565 | bool validation_failed = false; |
| 566 | if (platform_sp) { |
| 567 | // Ensure we have an execution context, empty or not. |
| 568 | ExecutionContext dummy_context; |
| 569 | ExecutionContext *exe_ctx_p = |
| 570 | execution_context ? execution_context : &dummy_context; |
| 571 | if (validator && !validator->IsValid(*platform_sp, *exe_ctx_p)) { |
| 572 | validation_failed = true; |
| 573 | error.SetErrorStringWithFormat("Option \"%s\" invalid. %s", |
| 574 | def->long_option, |
| 575 | def->validator->LongConditionString()); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // As long as validation didn't fail, we set the option value. |
| 580 | if (!validation_failed) |
| 581 | error = options.SetOptionValue( |
| 582 | long_options_index, |
| 583 | (def->option_has_arg == OptionParser::eNoArgument) |
| 584 | ? nullptr |
| 585 | : OptionParser::GetOptionArgument(), |
| 586 | execution_context); |
| 587 | } else { |
| 588 | error.SetErrorStringWithFormat("invalid option with value '%i'", val); |
| 589 | } |
| 590 | if (error.Fail()) |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | // Update our ARGV now that get options has consumed all the options |
| 595 | m_argv.erase(m_argv.begin(), m_argv.begin() + OptionParser::GetOptionIndex()); |
| 596 | UpdateArgsAfterOptionParsing(); |
| 597 | return error; |
Tamas Berghammer | 89d3f09 | 2015-09-02 10:35:27 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 600 | void Args::Clear() { |
| 601 | m_args.clear(); |
| 602 | m_argv.clear(); |
| 603 | m_args_quote_char.clear(); |
| 604 | } |
| 605 | |
| 606 | lldb::addr_t Args::StringToAddress(const ExecutionContext *exe_ctx, |
| 607 | const char *s, lldb::addr_t fail_value, |
| 608 | Error *error_ptr) { |
| 609 | bool error_set = false; |
| 610 | if (s && s[0]) { |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 611 | llvm::StringRef sref = s; |
| 612 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 613 | char *end = nullptr; |
| 614 | lldb::addr_t addr = ::strtoull(s, &end, 0); |
| 615 | if (*end == '\0') { |
| 616 | if (error_ptr) |
| 617 | error_ptr->Clear(); |
| 618 | return addr; // All characters were used, return the result |
| 619 | } |
| 620 | // Try base 16 with no prefix... |
| 621 | addr = ::strtoull(s, &end, 16); |
| 622 | if (*end == '\0') { |
| 623 | if (error_ptr) |
| 624 | error_ptr->Clear(); |
| 625 | return addr; // All characters were used, return the result |
| 626 | } |
| 627 | |
| 628 | if (exe_ctx) { |
| 629 | Target *target = exe_ctx->GetTargetPtr(); |
| 630 | if (target) { |
| 631 | lldb::ValueObjectSP valobj_sp; |
| 632 | EvaluateExpressionOptions options; |
| 633 | options.SetCoerceToId(false); |
| 634 | options.SetUnwindOnError(true); |
| 635 | options.SetKeepInMemory(false); |
| 636 | options.SetTryAllThreads(true); |
| 637 | |
| 638 | ExpressionResults expr_result = target->EvaluateExpression( |
| 639 | s, exe_ctx->GetFramePtr(), valobj_sp, options); |
| 640 | |
| 641 | bool success = false; |
| 642 | if (expr_result == eExpressionCompleted) { |
| 643 | if (valobj_sp) |
| 644 | valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable( |
| 645 | valobj_sp->GetDynamicValueType(), true); |
| 646 | // Get the address to watch. |
| 647 | if (valobj_sp) |
| 648 | addr = valobj_sp->GetValueAsUnsigned(fail_value, &success); |
| 649 | if (success) { |
| 650 | if (error_ptr) |
| 651 | error_ptr->Clear(); |
| 652 | return addr; |
| 653 | } else { |
| 654 | if (error_ptr) { |
| 655 | error_set = true; |
| 656 | error_ptr->SetErrorStringWithFormat( |
| 657 | "address expression \"%s\" resulted in a value whose type " |
| 658 | "can't be converted to an address: %s", |
| 659 | s, valobj_sp->GetTypeName().GetCString()); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | } else { |
| 664 | // Since the compiler can't handle things like "main + 12" we should |
| 665 | // try to do this for now. The compiler doesn't like adding offsets |
| 666 | // to function pointer types. |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 667 | static RegularExpression g_symbol_plus_offset_regex(llvm::StringRef( |
| 668 | "^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | RegularExpression::Match regex_match(3); |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 670 | if (g_symbol_plus_offset_regex.Execute(sref, ®ex_match)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 671 | uint64_t offset = 0; |
| 672 | bool add = true; |
| 673 | std::string name; |
| 674 | std::string str; |
| 675 | if (regex_match.GetMatchAtIndex(s, 1, name)) { |
| 676 | if (regex_match.GetMatchAtIndex(s, 2, str)) { |
| 677 | add = str[0] == '+'; |
| 678 | |
| 679 | if (regex_match.GetMatchAtIndex(s, 3, str)) { |
| 680 | offset = StringConvert::ToUInt64(str.c_str(), 0, 0, &success); |
| 681 | |
| 682 | if (success) { |
| 683 | Error error; |
| 684 | addr = StringToAddress(exe_ctx, name.c_str(), |
| 685 | LLDB_INVALID_ADDRESS, &error); |
| 686 | if (addr != LLDB_INVALID_ADDRESS) { |
| 687 | if (add) |
| 688 | return addr + offset; |
| 689 | else |
| 690 | return addr - offset; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (error_ptr) { |
| 699 | error_set = true; |
| 700 | error_ptr->SetErrorStringWithFormat( |
| 701 | "address expression \"%s\" evaluation failed", s); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | if (error_ptr) { |
| 708 | if (!error_set) |
| 709 | error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"", |
| 710 | s); |
| 711 | } |
| 712 | return fail_value; |
| 713 | } |
| 714 | |
| 715 | const char *Args::StripSpaces(std::string &s, bool leading, bool trailing, |
| 716 | bool return_null_if_empty) { |
| 717 | static const char *k_white_space = " \t\v"; |
| 718 | if (!s.empty()) { |
| 719 | if (leading) { |
| 720 | size_t pos = s.find_first_not_of(k_white_space); |
| 721 | if (pos == std::string::npos) |
| 722 | s.clear(); |
| 723 | else if (pos > 0) |
| 724 | s.erase(0, pos); |
| 725 | } |
| 726 | |
| 727 | if (trailing) { |
| 728 | size_t rpos = s.find_last_not_of(k_white_space); |
| 729 | if (rpos != std::string::npos && rpos + 1 < s.size()) |
| 730 | s.erase(rpos + 1); |
| 731 | } |
| 732 | } |
| 733 | if (return_null_if_empty && s.empty()) |
| 734 | return nullptr; |
| 735 | return s.c_str(); |
| 736 | } |
| 737 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | bool Args::StringToBoolean(llvm::StringRef ref, bool fail_value, |
| 739 | bool *success_ptr) { |
Zachary Turner | 7b2e5a3 | 2016-09-16 19:09:12 +0000 | [diff] [blame] | 740 | if (success_ptr) |
| 741 | *success_ptr = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 742 | ref = ref.trim(); |
| 743 | if (ref.equals_lower("false") || ref.equals_lower("off") || |
| 744 | ref.equals_lower("no") || ref.equals_lower("0")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 745 | return false; |
| 746 | } else if (ref.equals_lower("true") || ref.equals_lower("on") || |
| 747 | ref.equals_lower("yes") || ref.equals_lower("1")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 748 | return true; |
| 749 | } |
| 750 | if (success_ptr) |
| 751 | *success_ptr = false; |
| 752 | return fail_value; |
| 753 | } |
| 754 | |
Zachary Turner | 7b2e5a3 | 2016-09-16 19:09:12 +0000 | [diff] [blame] | 755 | char Args::StringToChar(llvm::StringRef s, char fail_value, bool *success_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | if (success_ptr) |
Zachary Turner | 7b2e5a3 | 2016-09-16 19:09:12 +0000 | [diff] [blame] | 757 | *success_ptr = false; |
| 758 | if (s.size() != 1) |
| 759 | return fail_value; |
| 760 | |
| 761 | if (success_ptr) |
| 762 | *success_ptr = true; |
| 763 | return s[0]; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Zachary Turner | 6fa7681b | 2016-09-17 02:00:02 +0000 | [diff] [blame] | 766 | bool Args::StringToVersion(llvm::StringRef string, uint32_t &major, |
| 767 | uint32_t &minor, uint32_t &update) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 768 | major = UINT32_MAX; |
| 769 | minor = UINT32_MAX; |
| 770 | update = UINT32_MAX; |
| 771 | |
Zachary Turner | 6fa7681b | 2016-09-17 02:00:02 +0000 | [diff] [blame] | 772 | if (string.empty()) |
| 773 | return false; |
| 774 | |
| 775 | llvm::StringRef major_str, minor_str, update_str; |
| 776 | |
| 777 | std::tie(major_str, minor_str) = string.split('.'); |
| 778 | std::tie(minor_str, update_str) = minor_str.split('.'); |
| 779 | if (major_str.getAsInteger(10, major)) |
| 780 | return false; |
| 781 | if (!minor_str.empty() && minor_str.getAsInteger(10, minor)) |
| 782 | return false; |
| 783 | if (!update_str.empty() && update_str.getAsInteger(10, update)) |
| 784 | return false; |
| 785 | |
| 786 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | const char *Args::GetShellSafeArgument(const FileSpec &shell, |
| 790 | const char *unsafe_arg, |
| 791 | std::string &safe_arg) { |
| 792 | struct ShellDescriptor { |
| 793 | ConstString m_basename; |
| 794 | const char *m_escapables; |
| 795 | }; |
| 796 | |
| 797 | static ShellDescriptor g_Shells[] = {{ConstString("bash"), " '\"<>()&"}, |
| 798 | {ConstString("tcsh"), " '\"<>()&$"}, |
| 799 | {ConstString("sh"), " '\"<>()&"}}; |
| 800 | |
| 801 | // safe minimal set |
| 802 | const char *escapables = " '\""; |
| 803 | |
| 804 | if (auto basename = shell.GetFilename()) { |
| 805 | for (const auto &Shell : g_Shells) { |
| 806 | if (Shell.m_basename == basename) { |
| 807 | escapables = Shell.m_escapables; |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | safe_arg.assign(unsafe_arg); |
| 814 | size_t prev_pos = 0; |
| 815 | while (prev_pos < safe_arg.size()) { |
| 816 | // Escape spaces and quotes |
| 817 | size_t pos = safe_arg.find_first_of(escapables, prev_pos); |
| 818 | if (pos != std::string::npos) { |
| 819 | safe_arg.insert(pos, 1, '\\'); |
| 820 | prev_pos = pos + 2; |
| 821 | } else |
| 822 | break; |
| 823 | } |
| 824 | return safe_arg.c_str(); |
| 825 | } |
| 826 | |
| 827 | int64_t Args::StringToOptionEnum(const char *s, |
| 828 | OptionEnumValueElement *enum_values, |
| 829 | int32_t fail_value, Error &error) { |
| 830 | if (enum_values) { |
| 831 | if (s && s[0]) { |
| 832 | for (int i = 0; enum_values[i].string_value != nullptr; i++) { |
| 833 | if (strstr(enum_values[i].string_value, s) == |
| 834 | enum_values[i].string_value) { |
| 835 | error.Clear(); |
| 836 | return enum_values[i].value; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | StreamString strm; |
| 842 | strm.PutCString("invalid enumeration value, valid values are: "); |
| 843 | for (int i = 0; enum_values[i].string_value != nullptr; i++) { |
| 844 | strm.Printf("%s\"%s\"", i > 0 ? ", " : "", enum_values[i].string_value); |
| 845 | } |
| 846 | error.SetErrorString(strm.GetData()); |
| 847 | } else { |
| 848 | error.SetErrorString("invalid enumeration argument"); |
| 849 | } |
| 850 | return fail_value; |
| 851 | } |
| 852 | |
Zachary Turner | 7b2e5a3 | 2016-09-16 19:09:12 +0000 | [diff] [blame] | 853 | lldb::ScriptLanguage |
| 854 | Args::StringToScriptLanguage(llvm::StringRef s, lldb::ScriptLanguage fail_value, |
| 855 | bool *success_ptr) { |
| 856 | if (success_ptr) |
| 857 | *success_ptr = true; |
| 858 | |
| 859 | if (s.equals_lower("python")) |
| 860 | return eScriptLanguagePython; |
| 861 | if (s.equals_lower("default")) |
| 862 | return eScriptLanguageDefault; |
| 863 | if (s.equals_lower("none")) |
| 864 | return eScriptLanguageNone; |
| 865 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 866 | if (success_ptr) |
| 867 | *success_ptr = false; |
| 868 | return fail_value; |
| 869 | } |
| 870 | |
| 871 | Error Args::StringToFormat(const char *s, lldb::Format &format, |
| 872 | size_t *byte_size_ptr) { |
| 873 | format = eFormatInvalid; |
| 874 | Error error; |
| 875 | |
| 876 | if (s && s[0]) { |
| 877 | if (byte_size_ptr) { |
| 878 | if (isdigit(s[0])) { |
| 879 | char *format_char = nullptr; |
| 880 | unsigned long byte_size = ::strtoul(s, &format_char, 0); |
| 881 | if (byte_size != ULONG_MAX) |
| 882 | *byte_size_ptr = byte_size; |
| 883 | s = format_char; |
| 884 | } else |
| 885 | *byte_size_ptr = 0; |
| 886 | } |
| 887 | |
| 888 | const bool partial_match_ok = true; |
| 889 | if (!FormatManager::GetFormatFromCString(s, partial_match_ok, format)) { |
| 890 | StreamString error_strm; |
| 891 | error_strm.Printf( |
| 892 | "Invalid format character or name '%s'. Valid values are:\n", s); |
| 893 | for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) { |
| 894 | char format_char = FormatManager::GetFormatAsFormatChar(f); |
| 895 | if (format_char) |
| 896 | error_strm.Printf("'%c' or ", format_char); |
| 897 | |
| 898 | error_strm.Printf("\"%s\"", FormatManager::GetFormatAsCString(f)); |
| 899 | error_strm.EOL(); |
| 900 | } |
| 901 | |
| 902 | if (byte_size_ptr) |
| 903 | error_strm.PutCString( |
| 904 | "An optional byte size can precede the format character.\n"); |
| 905 | error.SetErrorString(error_strm.GetString().c_str()); |
| 906 | } |
| 907 | |
| 908 | if (error.Fail()) |
| 909 | return error; |
| 910 | } else { |
| 911 | error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid"); |
| 912 | } |
| 913 | return error; |
| 914 | } |
| 915 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 916 | lldb::Encoding Args::StringToEncoding(llvm::StringRef s, |
| 917 | lldb::Encoding fail_value) { |
| 918 | return llvm::StringSwitch<lldb::Encoding>(s) |
| 919 | .Case("uint", eEncodingUint) |
| 920 | .Case("sint", eEncodingSint) |
| 921 | .Case("ieee754", eEncodingIEEE754) |
| 922 | .Case("vector", eEncodingVector) |
| 923 | .Default(fail_value); |
| 924 | } |
| 925 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 926 | uint32_t Args::StringToGenericRegister(llvm::StringRef s) { |
| 927 | if (s.empty()) |
| 928 | return LLDB_INVALID_REGNUM; |
| 929 | uint32_t result = llvm::StringSwitch<uint32_t>(s) |
| 930 | .Case("pc", LLDB_REGNUM_GENERIC_PC) |
| 931 | .Case("sp", LLDB_REGNUM_GENERIC_SP) |
| 932 | .Case("fp", LLDB_REGNUM_GENERIC_FP) |
| 933 | .Cases("ra", "lr", LLDB_REGNUM_GENERIC_RA) |
| 934 | .Case("flags", LLDB_REGNUM_GENERIC_FLAGS) |
| 935 | .Case("arg1", LLDB_REGNUM_GENERIC_ARG1) |
| 936 | .Case("arg2", LLDB_REGNUM_GENERIC_ARG2) |
| 937 | .Case("arg3", LLDB_REGNUM_GENERIC_ARG3) |
| 938 | .Case("arg4", LLDB_REGNUM_GENERIC_ARG4) |
| 939 | .Case("arg5", LLDB_REGNUM_GENERIC_ARG5) |
| 940 | .Case("arg6", LLDB_REGNUM_GENERIC_ARG6) |
| 941 | .Case("arg7", LLDB_REGNUM_GENERIC_ARG7) |
| 942 | .Case("arg8", LLDB_REGNUM_GENERIC_ARG8) |
| 943 | .Default(LLDB_INVALID_REGNUM); |
| 944 | return result; |
| 945 | } |
| 946 | |
| 947 | void Args::LongestCommonPrefix(std::string &common_prefix) { |
| 948 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 949 | pos = m_args.begin(); |
| 950 | if (pos == end) |
| 951 | common_prefix.clear(); |
| 952 | else |
| 953 | common_prefix = (*pos); |
| 954 | |
| 955 | for (++pos; pos != end; ++pos) { |
| 956 | size_t new_size = (*pos).size(); |
| 957 | |
| 958 | // First trim common_prefix if it is longer than the current element: |
| 959 | if (common_prefix.size() > new_size) |
| 960 | common_prefix.erase(new_size); |
| 961 | |
| 962 | // Then trim it at the first disparity: |
| 963 | |
| 964 | for (size_t i = 0; i < common_prefix.size(); i++) { |
| 965 | if ((*pos)[i] != common_prefix[i]) { |
| 966 | common_prefix.erase(i); |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // If we've emptied the common prefix, we're done. |
| 972 | if (common_prefix.empty()) |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 977 | void Args::AddOrReplaceEnvironmentVariable(llvm::StringRef env_var_name, |
| 978 | llvm::StringRef new_value) { |
| 979 | if (env_var_name.empty() || new_value.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 980 | return; |
| 981 | |
| 982 | // Build the new entry. |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 983 | std::string var_string(env_var_name); |
| 984 | var_string += "="; |
| 985 | var_string += new_value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 986 | |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 987 | size_t index = 0; |
| 988 | if (ContainsEnvironmentVariable(env_var_name, &index)) { |
| 989 | ReplaceArgumentAtIndex(index, var_string); |
| 990 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // We didn't find it. Append it instead. |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 994 | AppendArgument(var_string); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 997 | bool Args::ContainsEnvironmentVariable(llvm::StringRef env_var_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 998 | size_t *argument_index) const { |
| 999 | // Validate args. |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 1000 | if (env_var_name.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1001 | return false; |
| 1002 | |
| 1003 | // Check each arg to see if it matches the env var name. |
| 1004 | for (size_t i = 0; i < GetArgumentCount(); ++i) { |
Todd Fiala | 150aa32 | 2016-09-22 00:59:23 +0000 | [diff] [blame] | 1005 | auto arg_value = llvm::StringRef::withNullAsEmpty(GetArgumentAtIndex(i)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1006 | |
Zachary Turner | 5c725f3 | 2016-09-19 21:56:59 +0000 | [diff] [blame] | 1007 | llvm::StringRef name, value; |
| 1008 | std::tie(name, value) = arg_value.split('='); |
| 1009 | if (name == env_var_name && !value.empty()) { |
| 1010 | if (argument_index) |
| 1011 | *argument_index = i; |
| 1012 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // We didn't find a match. |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | size_t Args::FindArgumentIndexForOption(Option *long_options, |
| 1021 | int long_options_index) { |
| 1022 | char short_buffer[3]; |
| 1023 | char long_buffer[255]; |
| 1024 | ::snprintf(short_buffer, sizeof(short_buffer), "-%c", |
| 1025 | long_options[long_options_index].val); |
| 1026 | ::snprintf(long_buffer, sizeof(long_buffer), "--%s", |
| 1027 | long_options[long_options_index].definition->long_option); |
| 1028 | size_t end = GetArgumentCount(); |
| 1029 | size_t idx = 0; |
| 1030 | while (idx < end) { |
| 1031 | if ((::strncmp(GetArgumentAtIndex(idx), short_buffer, |
| 1032 | strlen(short_buffer)) == 0) || |
| 1033 | (::strncmp(GetArgumentAtIndex(idx), long_buffer, strlen(long_buffer)) == |
| 1034 | 0)) { |
| 1035 | return idx; |
| 1036 | } |
| 1037 | ++idx; |
| 1038 | } |
| 1039 | |
| 1040 | return end; |
| 1041 | } |
| 1042 | |
| 1043 | bool Args::IsPositionalArgument(const char *arg) { |
| 1044 | if (arg == nullptr) |
| 1045 | return false; |
| 1046 | |
| 1047 | bool is_positional = true; |
| 1048 | const char *cptr = arg; |
| 1049 | |
| 1050 | if (cptr[0] == '%') { |
| 1051 | ++cptr; |
| 1052 | while (isdigit(cptr[0])) |
| 1053 | ++cptr; |
| 1054 | if (cptr[0] != '\0') |
| 1055 | is_positional = false; |
| 1056 | } else |
| 1057 | is_positional = false; |
| 1058 | |
| 1059 | return is_positional; |
| 1060 | } |
| 1061 | |
| 1062 | void Args::ParseAliasOptions(Options &options, CommandReturnObject &result, |
| 1063 | OptionArgVector *option_arg_vector, |
| 1064 | std::string &raw_input_string) { |
| 1065 | StreamString sstr; |
| 1066 | int i; |
| 1067 | Option *long_options = options.GetLongOptions(); |
| 1068 | |
| 1069 | if (long_options == nullptr) { |
| 1070 | result.AppendError("invalid long options"); |
| 1071 | result.SetStatus(eReturnStatusFailed); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | for (i = 0; long_options[i].definition != nullptr; ++i) { |
| 1076 | if (long_options[i].flag == nullptr) { |
| 1077 | sstr << (char)long_options[i].val; |
| 1078 | switch (long_options[i].definition->option_has_arg) { |
| 1079 | default: |
| 1080 | case OptionParser::eNoArgument: |
| 1081 | break; |
| 1082 | case OptionParser::eRequiredArgument: |
| 1083 | sstr << ":"; |
| 1084 | break; |
| 1085 | case OptionParser::eOptionalArgument: |
| 1086 | sstr << "::"; |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | std::unique_lock<std::mutex> lock; |
| 1093 | OptionParser::Prepare(lock); |
| 1094 | int val; |
| 1095 | while (1) { |
| 1096 | int long_options_index = -1; |
| 1097 | val = |
| 1098 | OptionParser::Parse(GetArgumentCount(), GetArgumentVector(), |
| 1099 | sstr.GetData(), long_options, &long_options_index); |
| 1100 | |
| 1101 | if (val == -1) |
| 1102 | break; |
| 1103 | |
| 1104 | if (val == '?') { |
| 1105 | result.AppendError("unknown or ambiguous option"); |
| 1106 | result.SetStatus(eReturnStatusFailed); |
| 1107 | break; |
| 1108 | } |
| 1109 | |
| 1110 | if (val == 0) |
| 1111 | continue; |
| 1112 | |
| 1113 | options.OptionSeen(val); |
| 1114 | |
| 1115 | // Look up the long option index |
| 1116 | if (long_options_index == -1) { |
| 1117 | for (int j = 0; long_options[j].definition || long_options[j].flag || |
| 1118 | long_options[j].val; |
| 1119 | ++j) { |
| 1120 | if (long_options[j].val == val) { |
| 1121 | long_options_index = j; |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | // See if the option takes an argument, and see if one was supplied. |
| 1128 | if (long_options_index >= 0) { |
| 1129 | StreamString option_str; |
| 1130 | option_str.Printf("-%c", val); |
| 1131 | const OptionDefinition *def = long_options[long_options_index].definition; |
| 1132 | int has_arg = |
| 1133 | (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg; |
| 1134 | |
| 1135 | switch (has_arg) { |
| 1136 | case OptionParser::eNoArgument: |
| 1137 | option_arg_vector->push_back(OptionArgPair( |
| 1138 | std::string(option_str.GetData()), |
| 1139 | OptionArgValue(OptionParser::eNoArgument, "<no-argument>"))); |
| 1140 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1141 | break; |
| 1142 | case OptionParser::eRequiredArgument: |
| 1143 | if (OptionParser::GetOptionArgument() != nullptr) { |
| 1144 | option_arg_vector->push_back(OptionArgPair( |
| 1145 | std::string(option_str.GetData()), |
| 1146 | OptionArgValue(OptionParser::eRequiredArgument, |
| 1147 | std::string(OptionParser::GetOptionArgument())))); |
| 1148 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1149 | } else { |
| 1150 | result.AppendErrorWithFormat( |
| 1151 | "Option '%s' is missing argument specifier.\n", |
| 1152 | option_str.GetData()); |
| 1153 | result.SetStatus(eReturnStatusFailed); |
| 1154 | } |
| 1155 | break; |
| 1156 | case OptionParser::eOptionalArgument: |
| 1157 | if (OptionParser::GetOptionArgument() != nullptr) { |
| 1158 | option_arg_vector->push_back(OptionArgPair( |
| 1159 | std::string(option_str.GetData()), |
| 1160 | OptionArgValue(OptionParser::eOptionalArgument, |
| 1161 | std::string(OptionParser::GetOptionArgument())))); |
| 1162 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1163 | } else { |
| 1164 | option_arg_vector->push_back( |
| 1165 | OptionArgPair(std::string(option_str.GetData()), |
| 1166 | OptionArgValue(OptionParser::eOptionalArgument, |
| 1167 | "<no-argument>"))); |
| 1168 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1169 | } |
| 1170 | break; |
| 1171 | default: |
| 1172 | result.AppendErrorWithFormat("error with options table; invalid value " |
| 1173 | "in has_arg field for option '%c'.\n", |
| 1174 | val); |
| 1175 | result.SetStatus(eReturnStatusFailed); |
| 1176 | break; |
| 1177 | } |
| 1178 | } else { |
| 1179 | result.AppendErrorWithFormat("Invalid option with value '%c'.\n", val); |
| 1180 | result.SetStatus(eReturnStatusFailed); |
| 1181 | } |
| 1182 | |
| 1183 | if (long_options_index >= 0) { |
| 1184 | // Find option in the argument list; also see if it was supposed to take |
| 1185 | // an argument and if one was |
| 1186 | // supplied. Remove option (and argument, if given) from the argument |
| 1187 | // list. Also remove them from |
| 1188 | // the raw_input_string, if one was passed in. |
| 1189 | size_t idx = FindArgumentIndexForOption(long_options, long_options_index); |
| 1190 | if (idx < GetArgumentCount()) { |
| 1191 | if (raw_input_string.size() > 0) { |
| 1192 | const char *tmp_arg = GetArgumentAtIndex(idx); |
| 1193 | size_t pos = raw_input_string.find(tmp_arg); |
| 1194 | if (pos != std::string::npos) |
| 1195 | raw_input_string.erase(pos, strlen(tmp_arg)); |
| 1196 | } |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 1197 | ReplaceArgumentAtIndex(idx, llvm::StringRef()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1198 | if ((long_options[long_options_index].definition->option_has_arg != |
| 1199 | OptionParser::eNoArgument) && |
| 1200 | (OptionParser::GetOptionArgument() != nullptr) && |
| 1201 | (idx + 1 < GetArgumentCount()) && |
| 1202 | (strcmp(OptionParser::GetOptionArgument(), |
| 1203 | GetArgumentAtIndex(idx + 1)) == 0)) { |
| 1204 | if (raw_input_string.size() > 0) { |
| 1205 | const char *tmp_arg = GetArgumentAtIndex(idx + 1); |
| 1206 | size_t pos = raw_input_string.find(tmp_arg); |
| 1207 | if (pos != std::string::npos) |
| 1208 | raw_input_string.erase(pos, strlen(tmp_arg)); |
| 1209 | } |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 1210 | ReplaceArgumentAtIndex(idx + 1, llvm::StringRef()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if (!result.Succeeded()) |
| 1216 | break; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void Args::ParseArgsForCompletion(Options &options, |
| 1221 | OptionElementVector &option_element_vector, |
| 1222 | uint32_t cursor_index) { |
| 1223 | StreamString sstr; |
| 1224 | Option *long_options = options.GetLongOptions(); |
| 1225 | option_element_vector.clear(); |
| 1226 | |
| 1227 | if (long_options == nullptr) { |
| 1228 | return; |
| 1229 | } |
| 1230 | |
| 1231 | // Leading : tells getopt to return a : for a missing option argument AND |
| 1232 | // to suppress error messages. |
| 1233 | |
| 1234 | sstr << ":"; |
| 1235 | for (int i = 0; long_options[i].definition != nullptr; ++i) { |
| 1236 | if (long_options[i].flag == nullptr) { |
| 1237 | sstr << (char)long_options[i].val; |
| 1238 | switch (long_options[i].definition->option_has_arg) { |
| 1239 | default: |
| 1240 | case OptionParser::eNoArgument: |
| 1241 | break; |
| 1242 | case OptionParser::eRequiredArgument: |
| 1243 | sstr << ":"; |
| 1244 | break; |
| 1245 | case OptionParser::eOptionalArgument: |
| 1246 | sstr << "::"; |
| 1247 | break; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | std::unique_lock<std::mutex> lock; |
| 1253 | OptionParser::Prepare(lock); |
| 1254 | OptionParser::EnableError(false); |
| 1255 | |
| 1256 | int val; |
| 1257 | const OptionDefinition *opt_defs = options.GetDefinitions(); |
| 1258 | |
| 1259 | // Fooey... OptionParser::Parse permutes the GetArgumentVector to move the |
| 1260 | // options to the front. |
| 1261 | // So we have to build another Arg and pass that to OptionParser::Parse so it |
| 1262 | // doesn't |
| 1263 | // change the one we have. |
| 1264 | |
| 1265 | std::vector<const char *> dummy_vec( |
| 1266 | GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1); |
| 1267 | |
| 1268 | bool failed_once = false; |
| 1269 | uint32_t dash_dash_pos = -1; |
| 1270 | |
| 1271 | while (1) { |
| 1272 | bool missing_argument = false; |
| 1273 | int long_options_index = -1; |
| 1274 | |
| 1275 | val = OptionParser::Parse( |
| 1276 | dummy_vec.size() - 1, const_cast<char *const *>(&dummy_vec.front()), |
| 1277 | sstr.GetData(), long_options, &long_options_index); |
| 1278 | |
| 1279 | if (val == -1) { |
| 1280 | // When we're completing a "--" which is the last option on line, |
| 1281 | if (failed_once) |
| 1282 | break; |
| 1283 | |
| 1284 | failed_once = true; |
| 1285 | |
| 1286 | // If this is a bare "--" we mark it as such so we can complete it |
| 1287 | // successfully later. |
| 1288 | // Handling the "--" is a little tricky, since that may mean end of |
| 1289 | // options or arguments, or the |
| 1290 | // user might want to complete options by long name. I make this work by |
| 1291 | // checking whether the |
| 1292 | // cursor is in the "--" argument, and if so I assume we're completing the |
| 1293 | // long option, otherwise |
| 1294 | // I let it pass to OptionParser::Parse which will terminate the option |
| 1295 | // parsing. |
| 1296 | // Note, in either case we continue parsing the line so we can figure out |
| 1297 | // what other options |
| 1298 | // were passed. This will be useful when we come to restricting |
| 1299 | // completions based on what other |
| 1300 | // options we've seen on the line. |
| 1301 | |
| 1302 | if (static_cast<size_t>(OptionParser::GetOptionIndex()) < |
| 1303 | dummy_vec.size() - 1 && |
| 1304 | (strcmp(dummy_vec[OptionParser::GetOptionIndex() - 1], "--") == 0)) { |
| 1305 | dash_dash_pos = OptionParser::GetOptionIndex() - 1; |
| 1306 | if (static_cast<size_t>(OptionParser::GetOptionIndex() - 1) == |
| 1307 | cursor_index) { |
| 1308 | option_element_vector.push_back( |
| 1309 | OptionArgElement(OptionArgElement::eBareDoubleDash, |
| 1310 | OptionParser::GetOptionIndex() - 1, |
| 1311 | OptionArgElement::eBareDoubleDash)); |
| 1312 | continue; |
| 1313 | } else |
| 1314 | break; |
| 1315 | } else |
| 1316 | break; |
| 1317 | } else if (val == '?') { |
| 1318 | option_element_vector.push_back( |
| 1319 | OptionArgElement(OptionArgElement::eUnrecognizedArg, |
| 1320 | OptionParser::GetOptionIndex() - 1, |
| 1321 | OptionArgElement::eUnrecognizedArg)); |
| 1322 | continue; |
| 1323 | } else if (val == 0) { |
| 1324 | continue; |
| 1325 | } else if (val == ':') { |
| 1326 | // This is a missing argument. |
| 1327 | val = OptionParser::GetOptionErrorCause(); |
| 1328 | missing_argument = true; |
| 1329 | } |
| 1330 | |
| 1331 | ((Options *)&options)->OptionSeen(val); |
| 1332 | |
| 1333 | // Look up the long option index |
| 1334 | if (long_options_index == -1) { |
| 1335 | for (int j = 0; long_options[j].definition || long_options[j].flag || |
| 1336 | long_options[j].val; |
| 1337 | ++j) { |
| 1338 | if (long_options[j].val == val) { |
| 1339 | long_options_index = j; |
| 1340 | break; |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | // See if the option takes an argument, and see if one was supplied. |
| 1346 | if (long_options_index >= 0) { |
| 1347 | int opt_defs_index = -1; |
| 1348 | for (int i = 0;; i++) { |
| 1349 | if (opt_defs[i].short_option == 0) |
| 1350 | break; |
| 1351 | else if (opt_defs[i].short_option == val) { |
| 1352 | opt_defs_index = i; |
| 1353 | break; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | const OptionDefinition *def = long_options[long_options_index].definition; |
| 1358 | int has_arg = |
| 1359 | (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg; |
| 1360 | switch (has_arg) { |
| 1361 | case OptionParser::eNoArgument: |
| 1362 | option_element_vector.push_back(OptionArgElement( |
| 1363 | opt_defs_index, OptionParser::GetOptionIndex() - 1, 0)); |
| 1364 | break; |
| 1365 | case OptionParser::eRequiredArgument: |
| 1366 | if (OptionParser::GetOptionArgument() != nullptr) { |
| 1367 | int arg_index; |
| 1368 | if (missing_argument) |
| 1369 | arg_index = -1; |
| 1370 | else |
| 1371 | arg_index = OptionParser::GetOptionIndex() - 1; |
| 1372 | |
| 1373 | option_element_vector.push_back(OptionArgElement( |
| 1374 | opt_defs_index, OptionParser::GetOptionIndex() - 2, arg_index)); |
| 1375 | } else { |
| 1376 | option_element_vector.push_back(OptionArgElement( |
| 1377 | opt_defs_index, OptionParser::GetOptionIndex() - 1, -1)); |
| 1378 | } |
| 1379 | break; |
| 1380 | case OptionParser::eOptionalArgument: |
| 1381 | if (OptionParser::GetOptionArgument() != nullptr) { |
| 1382 | option_element_vector.push_back(OptionArgElement( |
| 1383 | opt_defs_index, OptionParser::GetOptionIndex() - 2, |
| 1384 | OptionParser::GetOptionIndex() - 1)); |
| 1385 | } else { |
| 1386 | option_element_vector.push_back(OptionArgElement( |
| 1387 | opt_defs_index, OptionParser::GetOptionIndex() - 2, |
| 1388 | OptionParser::GetOptionIndex() - 1)); |
| 1389 | } |
| 1390 | break; |
| 1391 | default: |
| 1392 | // The options table is messed up. Here we'll just continue |
| 1393 | option_element_vector.push_back( |
| 1394 | OptionArgElement(OptionArgElement::eUnrecognizedArg, |
| 1395 | OptionParser::GetOptionIndex() - 1, |
| 1396 | OptionArgElement::eUnrecognizedArg)); |
| 1397 | break; |
| 1398 | } |
| 1399 | } else { |
| 1400 | option_element_vector.push_back( |
| 1401 | OptionArgElement(OptionArgElement::eUnrecognizedArg, |
| 1402 | OptionParser::GetOptionIndex() - 1, |
| 1403 | OptionArgElement::eUnrecognizedArg)); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | // Finally we have to handle the case where the cursor index points at a |
| 1408 | // single "-". We want to mark that in |
| 1409 | // the option_element_vector, but only if it is not after the "--". But it |
| 1410 | // turns out that OptionParser::Parse just ignores |
| 1411 | // an isolated "-". So we have to look it up by hand here. We only care if |
| 1412 | // it is AT the cursor position. |
| 1413 | // Note, a single quoted dash is not the same as a single dash... |
| 1414 | |
| 1415 | if ((static_cast<int32_t>(dash_dash_pos) == -1 || |
| 1416 | cursor_index < dash_dash_pos) && |
| 1417 | m_args_quote_char[cursor_index] == '\0' && |
| 1418 | strcmp(GetArgumentAtIndex(cursor_index), "-") == 0) { |
| 1419 | option_element_vector.push_back( |
| 1420 | OptionArgElement(OptionArgElement::eBareDash, cursor_index, |
| 1421 | OptionArgElement::eBareDash)); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | void Args::EncodeEscapeSequences(const char *src, std::string &dst) { |
| 1426 | dst.clear(); |
| 1427 | if (src) { |
| 1428 | for (const char *p = src; *p != '\0'; ++p) { |
| 1429 | size_t non_special_chars = ::strcspn(p, "\\"); |
| 1430 | if (non_special_chars > 0) { |
| 1431 | dst.append(p, non_special_chars); |
| 1432 | p += non_special_chars; |
| 1433 | if (*p == '\0') |
| 1434 | break; |
| 1435 | } |
| 1436 | |
| 1437 | if (*p == '\\') { |
| 1438 | ++p; // skip the slash |
| 1439 | switch (*p) { |
| 1440 | case 'a': |
| 1441 | dst.append(1, '\a'); |
| 1442 | break; |
| 1443 | case 'b': |
| 1444 | dst.append(1, '\b'); |
| 1445 | break; |
| 1446 | case 'f': |
| 1447 | dst.append(1, '\f'); |
| 1448 | break; |
| 1449 | case 'n': |
| 1450 | dst.append(1, '\n'); |
| 1451 | break; |
| 1452 | case 'r': |
| 1453 | dst.append(1, '\r'); |
| 1454 | break; |
| 1455 | case 't': |
| 1456 | dst.append(1, '\t'); |
| 1457 | break; |
| 1458 | case 'v': |
| 1459 | dst.append(1, '\v'); |
| 1460 | break; |
| 1461 | case '\\': |
| 1462 | dst.append(1, '\\'); |
| 1463 | break; |
| 1464 | case '\'': |
| 1465 | dst.append(1, '\''); |
| 1466 | break; |
| 1467 | case '"': |
| 1468 | dst.append(1, '"'); |
| 1469 | break; |
| 1470 | case '0': |
| 1471 | // 1 to 3 octal chars |
| 1472 | { |
| 1473 | // Make a string that can hold onto the initial zero char, |
| 1474 | // up to 3 octal digits, and a terminating NULL. |
| 1475 | char oct_str[5] = {'\0', '\0', '\0', '\0', '\0'}; |
| 1476 | |
| 1477 | int i; |
| 1478 | for (i = 0; (p[i] >= '0' && p[i] <= '7') && i < 4; ++i) |
| 1479 | oct_str[i] = p[i]; |
| 1480 | |
| 1481 | // We don't want to consume the last octal character since |
| 1482 | // the main for loop will do this for us, so we advance p by |
| 1483 | // one less than i (even if i is zero) |
| 1484 | p += i - 1; |
| 1485 | unsigned long octal_value = ::strtoul(oct_str, nullptr, 8); |
| 1486 | if (octal_value <= UINT8_MAX) { |
| 1487 | dst.append(1, (char)octal_value); |
| 1488 | } |
| 1489 | } |
| 1490 | break; |
| 1491 | |
| 1492 | case 'x': |
| 1493 | // hex number in the format |
| 1494 | if (isxdigit(p[1])) { |
| 1495 | ++p; // Skip the 'x' |
| 1496 | |
| 1497 | // Make a string that can hold onto two hex chars plus a |
| 1498 | // NULL terminator |
| 1499 | char hex_str[3] = {*p, '\0', '\0'}; |
| 1500 | if (isxdigit(p[1])) { |
| 1501 | ++p; // Skip the first of the two hex chars |
| 1502 | hex_str[1] = *p; |
| 1503 | } |
| 1504 | |
| 1505 | unsigned long hex_value = strtoul(hex_str, nullptr, 16); |
| 1506 | if (hex_value <= UINT8_MAX) |
| 1507 | dst.append(1, (char)hex_value); |
| 1508 | } else { |
| 1509 | dst.append(1, 'x'); |
| 1510 | } |
| 1511 | break; |
| 1512 | |
| 1513 | default: |
| 1514 | // Just desensitize any other character by just printing what |
| 1515 | // came after the '\' |
| 1516 | dst.append(1, *p); |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | void Args::ExpandEscapedCharacters(const char *src, std::string &dst) { |
| 1525 | dst.clear(); |
| 1526 | if (src) { |
| 1527 | for (const char *p = src; *p != '\0'; ++p) { |
| 1528 | if (isprint8(*p)) |
| 1529 | dst.append(1, *p); |
| 1530 | else { |
| 1531 | switch (*p) { |
| 1532 | case '\a': |
| 1533 | dst.append("\\a"); |
| 1534 | break; |
| 1535 | case '\b': |
| 1536 | dst.append("\\b"); |
| 1537 | break; |
| 1538 | case '\f': |
| 1539 | dst.append("\\f"); |
| 1540 | break; |
| 1541 | case '\n': |
| 1542 | dst.append("\\n"); |
| 1543 | break; |
| 1544 | case '\r': |
| 1545 | dst.append("\\r"); |
| 1546 | break; |
| 1547 | case '\t': |
| 1548 | dst.append("\\t"); |
| 1549 | break; |
| 1550 | case '\v': |
| 1551 | dst.append("\\v"); |
| 1552 | break; |
| 1553 | case '\'': |
| 1554 | dst.append("\\'"); |
| 1555 | break; |
| 1556 | case '"': |
| 1557 | dst.append("\\\""); |
| 1558 | break; |
| 1559 | case '\\': |
| 1560 | dst.append("\\\\"); |
| 1561 | break; |
| 1562 | default: { |
| 1563 | // Just encode as octal |
| 1564 | dst.append("\\0"); |
| 1565 | char octal_str[32]; |
| 1566 | snprintf(octal_str, sizeof(octal_str), "%o", *p); |
| 1567 | dst.append(octal_str); |
| 1568 | } break; |
| 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | std::string Args::EscapeLLDBCommandArgument(const std::string &arg, |
| 1576 | char quote_char) { |
| 1577 | const char *chars_to_escape = nullptr; |
| 1578 | switch (quote_char) { |
| 1579 | case '\0': |
| 1580 | chars_to_escape = " \t\\'\"`"; |
| 1581 | break; |
| 1582 | case '\'': |
| 1583 | chars_to_escape = ""; |
| 1584 | break; |
| 1585 | case '"': |
| 1586 | chars_to_escape = "$\"`\\"; |
| 1587 | break; |
| 1588 | default: |
| 1589 | assert(false && "Unhandled quote character"); |
| 1590 | } |
| 1591 | |
| 1592 | std::string res; |
| 1593 | res.reserve(arg.size()); |
| 1594 | for (char c : arg) { |
| 1595 | if (::strchr(chars_to_escape, c)) |
| 1596 | res.push_back('\\'); |
| 1597 | res.push_back(c); |
| 1598 | } |
| 1599 | return res; |
| 1600 | } |