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