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 | |
| 10 | // C Includes |
| 11 | #include <getopt.h> |
Eli Friedman | 27cd889 | 2010-06-09 10:59:23 +0000 | [diff] [blame] | 12 | #include <cstdlib> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Stream.h" |
| 18 | #include "lldb/Core/StreamFile.h" |
| 19 | #include "lldb/Core/StreamString.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 22 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | //---------------------------------------------------------------------- |
| 27 | // Args constructor |
| 28 | //---------------------------------------------------------------------- |
| 29 | Args::Args (const char *command) : |
| 30 | m_args(), |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 31 | m_argv(), |
| 32 | m_args_quote_char() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 34 | if (command) |
| 35 | SetCommandString (command); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
| 39 | Args::Args (const char *command, size_t len) : |
| 40 | m_args(), |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 41 | m_argv(), |
| 42 | m_args_quote_char() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 44 | if (command && len) |
| 45 | SetCommandString (command, len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | //---------------------------------------------------------------------- |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 49 | // We have to be very careful on the copy constructor of this class |
| 50 | // to make sure we copy all of the string values, but we can't copy the |
| 51 | // rhs.m_argv into m_argv since it will point to the "const char *" c |
| 52 | // strings in rhs.m_args. We need to copy the string list and update our |
| 53 | // own m_argv appropriately. |
| 54 | //---------------------------------------------------------------------- |
| 55 | Args::Args (const Args &rhs) : |
| 56 | m_args (rhs.m_args), |
| 57 | m_argv (), |
| 58 | m_args_quote_char(rhs.m_args_quote_char) |
| 59 | { |
| 60 | UpdateArgvFromArgs(); |
| 61 | } |
| 62 | |
| 63 | //---------------------------------------------------------------------- |
| 64 | // We have to be very careful on the copy constructor of this class |
| 65 | // to make sure we copy all of the string values, but we can't copy the |
| 66 | // rhs.m_argv into m_argv since it will point to the "const char *" c |
| 67 | // strings in rhs.m_args. We need to copy the string list and update our |
| 68 | // own m_argv appropriately. |
| 69 | //---------------------------------------------------------------------- |
| 70 | const Args & |
| 71 | Args::operator= (const Args &rhs) |
| 72 | { |
| 73 | // Make sure we aren't assigning to self |
| 74 | if (this != &rhs) |
| 75 | { |
| 76 | m_args = rhs.m_args; |
| 77 | m_args_quote_char = rhs.m_args_quote_char; |
| 78 | UpdateArgvFromArgs(); |
| 79 | } |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | //---------------------------------------------------------------------- |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | // Destructor |
| 85 | //---------------------------------------------------------------------- |
| 86 | Args::~Args () |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | Args::Dump (Stream *s) |
| 92 | { |
| 93 | // int argc = GetArgumentCount(); |
| 94 | // |
| 95 | // arg_sstr_collection::const_iterator pos, begin = m_args.begin(), end = m_args.end(); |
| 96 | // for (pos = m_args.begin(); pos != end; ++pos) |
| 97 | // { |
| 98 | // s->Indent(); |
| 99 | // s->Printf("args[%zu]=%s\n", std::distance(begin, pos), pos->c_str()); |
| 100 | // } |
| 101 | // s->EOL(); |
| 102 | const int argc = m_argv.size(); |
| 103 | for (int i=0; i<argc; ++i) |
| 104 | { |
| 105 | s->Indent(); |
| 106 | const char *arg_cstr = m_argv[i]; |
| 107 | if (arg_cstr) |
| 108 | s->Printf("argv[%i]=\"%s\"\n", i, arg_cstr); |
| 109 | else |
| 110 | s->Printf("argv[%i]=NULL\n", i); |
| 111 | } |
| 112 | s->EOL(); |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | Args::GetCommandString (std::string &command) |
| 117 | { |
| 118 | command.clear(); |
| 119 | int argc = GetArgumentCount(); |
| 120 | for (int i=0; i<argc; ++i) |
| 121 | { |
| 122 | if (i > 0) |
| 123 | command += ' '; |
| 124 | command += m_argv[i]; |
| 125 | } |
| 126 | return argc > 0; |
| 127 | } |
| 128 | |
Caroline Tice | d9105c2 | 2010-12-10 00:26:54 +0000 | [diff] [blame] | 129 | bool |
| 130 | Args::GetQuotedCommandString (std::string &command) |
| 131 | { |
| 132 | command.clear (); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 133 | size_t argc = GetArgumentCount (); |
| 134 | for (size_t i = 0; i < argc; ++i) |
Caroline Tice | d9105c2 | 2010-12-10 00:26:54 +0000 | [diff] [blame] | 135 | { |
| 136 | if (i > 0) |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 137 | command.append (1, ' '); |
| 138 | char quote_char = GetArgumentQuoteCharAtIndex(i); |
| 139 | if (quote_char) |
Caroline Tice | d9105c2 | 2010-12-10 00:26:54 +0000 | [diff] [blame] | 140 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 141 | command.append (1, quote_char); |
| 142 | command.append (m_argv[i]); |
| 143 | command.append (1, quote_char); |
Caroline Tice | d9105c2 | 2010-12-10 00:26:54 +0000 | [diff] [blame] | 144 | } |
| 145 | else |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 146 | command.append (m_argv[i]); |
Caroline Tice | d9105c2 | 2010-12-10 00:26:54 +0000 | [diff] [blame] | 147 | } |
| 148 | return argc > 0; |
| 149 | } |
| 150 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | void |
| 152 | Args::SetCommandString (const char *command, size_t len) |
| 153 | { |
| 154 | // Use std::string to make sure we get a NULL terminated string we can use |
| 155 | // as "command" could point to a string within a large string.... |
| 156 | std::string null_terminated_command(command, len); |
| 157 | SetCommandString(null_terminated_command.c_str()); |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | Args::SetCommandString (const char *command) |
| 162 | { |
| 163 | m_args.clear(); |
| 164 | m_argv.clear(); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 165 | m_args_quote_char.clear(); |
| 166 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | if (command && command[0]) |
| 168 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 169 | static const char *k_space_separators = " \t"; |
| 170 | static const char *k_space_separators_with_slash_and_quotes = " \t \\'\"`"; |
| 171 | const char *arg_end = NULL; |
| 172 | const char *arg_pos; |
| 173 | for (arg_pos = command; |
| 174 | arg_pos && arg_pos[0]; |
| 175 | arg_pos = arg_end) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 177 | // Skip any leading space separators |
| 178 | const char *arg_start = ::strspn (arg_pos, k_space_separators) + arg_pos; |
| 179 | |
| 180 | // 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] | 181 | // we're done. |
| 182 | if (*arg_start == '\0') |
| 183 | break; |
| 184 | |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 185 | // Arguments can be split into multiple discontiguous pieces, |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 186 | // for example: |
| 187 | // "Hello ""World" |
| 188 | // this would result in a single argument "Hello World" (without/ |
| 189 | // the quotes) since the quotes would be removed and there is |
| 190 | // not space between the strings. So we need to keep track of the |
| 191 | // current start of each argument piece in "arg_piece_start" |
| 192 | const char *arg_piece_start = arg_start; |
| 193 | arg_pos = arg_piece_start; |
| 194 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | std::string arg; |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 196 | // Since we can have multiple quotes that form a single command |
| 197 | // in a command like: "Hello "world'!' (which will make a single |
| 198 | // argument "Hello world!") we remember the first quote character |
| 199 | // we encounter and use that for the quote character. |
| 200 | char first_quote_char = '\0'; |
| 201 | char quote_char = '\0'; |
| 202 | bool arg_complete = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 203 | |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 204 | do |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 206 | arg_end = ::strcspn (arg_pos, k_space_separators_with_slash_and_quotes) + arg_pos; |
| 207 | |
| 208 | switch (arg_end[0]) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 210 | default: |
| 211 | assert (!"Unhandled case statement, we must handle this..."); |
| 212 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 213 | |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 214 | case '\0': |
| 215 | // End of C string |
| 216 | if (arg_piece_start && arg_piece_start[0]) |
| 217 | arg.append (arg_piece_start); |
| 218 | arg_complete = true; |
| 219 | break; |
| 220 | |
| 221 | case '\\': |
| 222 | // Backslash character |
| 223 | switch (arg_end[1]) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 225 | case '\0': |
| 226 | arg.append (arg_piece_start); |
| 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: |
| 231 | arg_pos = arg_end + 2; |
| 232 | break; |
| 233 | } |
| 234 | break; |
| 235 | |
| 236 | case '"': |
| 237 | case '\'': |
| 238 | case '`': |
| 239 | // Quote characters |
| 240 | if (quote_char) |
| 241 | { |
| 242 | // We found a quote character while inside a quoted |
| 243 | // character argument. If it matches our current quote |
| 244 | // character, this ends the effect of the quotes. If it |
| 245 | // doesn't we ignore it. |
| 246 | if (quote_char == arg_end[0]) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 248 | arg.append (arg_piece_start, arg_end - arg_piece_start); |
| 249 | // Clear the quote character and let parsing |
| 250 | // continue (we need to watch for things like: |
| 251 | // "Hello ""World" |
| 252 | // "Hello "World |
| 253 | // "Hello "'World' |
| 254 | // All of which will result in a single argument "Hello World" |
| 255 | quote_char = '\0'; // Note that we are no longer inside quotes |
| 256 | arg_pos = arg_end + 1; // Skip the quote character |
| 257 | arg_piece_start = arg_pos; // Note we are starting from later in the string |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | // different quote, skip it and keep going |
| 262 | arg_pos = arg_end + 1; |
| 263 | } |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | // We found the start of a quote scope. |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 268 | // Make sure there isn't a string that precedes |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 269 | // the start of a quote scope like: |
| 270 | // Hello" World" |
| 271 | // If so, then add the "Hello" to the arg |
| 272 | if (arg_end > arg_piece_start) |
| 273 | arg.append (arg_piece_start, arg_end - arg_piece_start); |
| 274 | |
| 275 | // Enter into a quote scope |
| 276 | quote_char = arg_end[0]; |
| 277 | |
| 278 | if (first_quote_char == '\0') |
| 279 | first_quote_char = quote_char; |
| 280 | |
| 281 | arg_pos = arg_end; |
| 282 | |
| 283 | if (quote_char != '`') |
| 284 | ++arg_pos; // Skip the quote character if it is not a backtick |
| 285 | |
| 286 | arg_piece_start = arg_pos; // Note we are starting from later in the string |
| 287 | |
| 288 | // Skip till the next quote character |
| 289 | const char *end_quote = ::strchr (arg_piece_start, quote_char); |
| 290 | while (end_quote && end_quote[-1] == '\\') |
| 291 | { |
| 292 | // Don't skip the quote character if it is |
| 293 | // preceded by a '\' character |
| 294 | end_quote = ::strchr (end_quote + 1, quote_char); |
| 295 | } |
| 296 | |
| 297 | if (end_quote) |
| 298 | { |
| 299 | if (end_quote > arg_piece_start) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 300 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 301 | // Keep the backtick quote on commands |
| 302 | if (quote_char == '`') |
| 303 | arg.append (arg_piece_start, end_quote + 1 - arg_piece_start); |
| 304 | else |
| 305 | arg.append (arg_piece_start, end_quote - arg_piece_start); |
| 306 | } |
| 307 | |
| 308 | // If the next character is a space or the end of |
| 309 | // string, this argument is complete... |
| 310 | if (end_quote[1] == ' ' || end_quote[1] == '\t' || end_quote[1] == '\0') |
| 311 | { |
| 312 | arg_complete = true; |
| 313 | arg_end = end_quote + 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 314 | } |
| 315 | else |
| 316 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 317 | arg_pos = end_quote + 1; |
| 318 | arg_piece_start = arg_pos; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | } |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 320 | quote_char = '\0'; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 323 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 325 | case ' ': |
| 326 | case '\t': |
| 327 | if (quote_char) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 329 | // We are currently processing a quoted character and found |
| 330 | // a space character, skip any spaces and keep trying to find |
| 331 | // the end of the argument. |
| 332 | arg_pos = ::strspn (arg_end, k_space_separators) + arg_end; |
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 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 335 | { |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 336 | // We are not inside any quotes, we just found a space after an |
| 337 | // argument |
| 338 | if (arg_end > arg_piece_start) |
| 339 | arg.append (arg_piece_start, arg_end - arg_piece_start); |
| 340 | arg_complete = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 341 | } |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 342 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | } |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 344 | } while (!arg_complete); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 345 | |
| 346 | m_args.push_back(arg); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 347 | m_args_quote_char.push_back (first_quote_char); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | } |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 349 | UpdateArgvFromArgs(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
| 354 | Args::UpdateArgsAfterOptionParsing() |
| 355 | { |
| 356 | // Now m_argv might be out of date with m_args, so we need to fix that |
| 357 | arg_cstr_collection::const_iterator argv_pos, argv_end = m_argv.end(); |
| 358 | arg_sstr_collection::iterator args_pos; |
| 359 | arg_quote_char_collection::iterator quotes_pos; |
| 360 | |
| 361 | for (argv_pos = m_argv.begin(), args_pos = m_args.begin(), quotes_pos = m_args_quote_char.begin(); |
| 362 | argv_pos != argv_end && args_pos != m_args.end(); |
| 363 | ++argv_pos) |
| 364 | { |
| 365 | const char *argv_cstr = *argv_pos; |
| 366 | if (argv_cstr == NULL) |
| 367 | break; |
| 368 | |
| 369 | while (args_pos != m_args.end()) |
| 370 | { |
| 371 | const char *args_cstr = args_pos->c_str(); |
| 372 | if (args_cstr == argv_cstr) |
| 373 | { |
| 374 | // We found the argument that matches the C string in the |
| 375 | // vector, so we can now look for the next one |
| 376 | ++args_pos; |
| 377 | ++quotes_pos; |
| 378 | break; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | quotes_pos = m_args_quote_char.erase (quotes_pos); |
| 383 | args_pos = m_args.erase (args_pos); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (args_pos != m_args.end()) |
| 389 | m_args.erase (args_pos, m_args.end()); |
| 390 | |
| 391 | if (quotes_pos != m_args_quote_char.end()) |
| 392 | m_args_quote_char.erase (quotes_pos, m_args_quote_char.end()); |
| 393 | } |
| 394 | |
| 395 | void |
| 396 | Args::UpdateArgvFromArgs() |
| 397 | { |
| 398 | m_argv.clear(); |
| 399 | arg_sstr_collection::const_iterator pos, end = m_args.end(); |
| 400 | for (pos = m_args.begin(); pos != end; ++pos) |
| 401 | m_argv.push_back(pos->c_str()); |
| 402 | m_argv.push_back(NULL); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 403 | // Make sure we have enough arg quote chars in the array |
| 404 | if (m_args_quote_char.size() < m_args.size()) |
| 405 | m_args_quote_char.resize (m_argv.size()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | size_t |
| 409 | Args::GetArgumentCount() const |
| 410 | { |
| 411 | if (m_argv.empty()) |
| 412 | return 0; |
| 413 | return m_argv.size() - 1; |
| 414 | } |
| 415 | |
| 416 | const char * |
| 417 | Args::GetArgumentAtIndex (size_t idx) const |
| 418 | { |
| 419 | if (idx < m_argv.size()) |
| 420 | return m_argv[idx]; |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | char |
| 425 | Args::GetArgumentQuoteCharAtIndex (size_t idx) const |
| 426 | { |
| 427 | if (idx < m_args_quote_char.size()) |
| 428 | return m_args_quote_char[idx]; |
| 429 | return '\0'; |
| 430 | } |
| 431 | |
| 432 | char ** |
| 433 | Args::GetArgumentVector() |
| 434 | { |
| 435 | if (!m_argv.empty()) |
| 436 | return (char **)&m_argv[0]; |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | const char ** |
| 441 | Args::GetConstArgumentVector() const |
| 442 | { |
| 443 | if (!m_argv.empty()) |
| 444 | return (const char **)&m_argv[0]; |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | Args::Shift () |
| 450 | { |
| 451 | // Don't pop the last NULL terminator from the argv array |
| 452 | if (m_argv.size() > 1) |
| 453 | { |
| 454 | m_argv.erase(m_argv.begin()); |
| 455 | m_args.pop_front(); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 456 | if (!m_args_quote_char.empty()) |
| 457 | m_args_quote_char.erase(m_args_quote_char.begin()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | const char * |
| 462 | Args::Unshift (const char *arg_cstr, char quote_char) |
| 463 | { |
| 464 | m_args.push_front(arg_cstr); |
| 465 | m_argv.insert(m_argv.begin(), m_args.front().c_str()); |
| 466 | m_args_quote_char.insert(m_args_quote_char.begin(), quote_char); |
| 467 | return GetArgumentAtIndex (0); |
| 468 | } |
| 469 | |
| 470 | void |
| 471 | Args::AppendArguments (const Args &rhs) |
| 472 | { |
| 473 | const size_t rhs_argc = rhs.GetArgumentCount(); |
| 474 | for (size_t i=0; i<rhs_argc; ++i) |
| 475 | AppendArgument(rhs.GetArgumentAtIndex(i)); |
| 476 | } |
| 477 | |
| 478 | const char * |
| 479 | Args::AppendArgument (const char *arg_cstr, char quote_char) |
| 480 | { |
| 481 | return InsertArgumentAtIndex (GetArgumentCount(), arg_cstr, quote_char); |
| 482 | } |
| 483 | |
| 484 | const char * |
| 485 | Args::InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char) |
| 486 | { |
| 487 | // Since we are using a std::list to hold onto the copied C string and |
| 488 | // we don't have direct access to the elements, we have to iterate to |
| 489 | // find the value. |
| 490 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 491 | size_t i = idx; |
| 492 | for (pos = m_args.begin(); i > 0 && pos != end; ++pos) |
| 493 | --i; |
| 494 | |
| 495 | pos = m_args.insert(pos, arg_cstr); |
| 496 | |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 497 | if (idx >= m_args_quote_char.size()) |
| 498 | { |
| 499 | m_args_quote_char.resize(idx + 1); |
| 500 | m_args_quote_char[idx] = quote_char; |
| 501 | } |
| 502 | else |
| 503 | 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] | 504 | |
| 505 | UpdateArgvFromArgs(); |
| 506 | return GetArgumentAtIndex(idx); |
| 507 | } |
| 508 | |
| 509 | const char * |
| 510 | Args::ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char) |
| 511 | { |
| 512 | // Since we are using a std::list to hold onto the copied C string and |
| 513 | // we don't have direct access to the elements, we have to iterate to |
| 514 | // find the value. |
| 515 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 516 | size_t i = idx; |
| 517 | for (pos = m_args.begin(); i > 0 && pos != end; ++pos) |
| 518 | --i; |
| 519 | |
| 520 | if (pos != end) |
| 521 | { |
| 522 | pos->assign(arg_cstr); |
| 523 | assert(idx < m_argv.size() - 1); |
| 524 | m_argv[idx] = pos->c_str(); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 525 | if (idx >= m_args_quote_char.size()) |
| 526 | m_args_quote_char.resize(idx + 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | m_args_quote_char[idx] = quote_char; |
| 528 | return GetArgumentAtIndex(idx); |
| 529 | } |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | void |
| 534 | Args::DeleteArgumentAtIndex (size_t idx) |
| 535 | { |
| 536 | // Since we are using a std::list to hold onto the copied C string and |
| 537 | // we don't have direct access to the elements, we have to iterate to |
| 538 | // find the value. |
| 539 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 540 | size_t i = idx; |
| 541 | for (pos = m_args.begin(); i > 0 && pos != end; ++pos) |
| 542 | --i; |
| 543 | |
| 544 | if (pos != end) |
| 545 | { |
| 546 | m_args.erase (pos); |
| 547 | assert(idx < m_argv.size() - 1); |
| 548 | m_argv.erase(m_argv.begin() + idx); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 549 | if (idx < m_args_quote_char.size()) |
| 550 | m_args_quote_char.erase(m_args_quote_char.begin() + idx); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | Args::SetArguments (int argc, const char **argv) |
| 556 | { |
| 557 | // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is |
| 558 | // no need to clear it here. |
| 559 | m_args.clear(); |
| 560 | m_args_quote_char.clear(); |
| 561 | |
| 562 | // Make a copy of the arguments in our internal buffer |
| 563 | size_t i; |
| 564 | // First copy each string |
| 565 | for (i=0; i<argc; ++i) |
| 566 | { |
| 567 | m_args.push_back (argv[i]); |
Greg Clayton | 928d130 | 2010-12-19 03:41:24 +0000 | [diff] [blame] | 568 | if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`')) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 569 | m_args_quote_char.push_back (argv[i][0]); |
| 570 | else |
| 571 | m_args_quote_char.push_back ('\0'); |
| 572 | } |
| 573 | |
| 574 | UpdateArgvFromArgs(); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | Error |
| 579 | Args::ParseOptions (Options &options) |
| 580 | { |
| 581 | StreamString sstr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | Error error; |
| 583 | struct option *long_options = options.GetLongOptions(); |
| 584 | if (long_options == NULL) |
| 585 | { |
| 586 | error.SetErrorStringWithFormat("Invalid long options.\n"); |
| 587 | return error; |
| 588 | } |
| 589 | |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 590 | for (int i=0; long_options[i].name != NULL; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 591 | { |
| 592 | if (long_options[i].flag == NULL) |
| 593 | { |
| 594 | sstr << (char)long_options[i].val; |
| 595 | switch (long_options[i].has_arg) |
| 596 | { |
| 597 | default: |
| 598 | case no_argument: break; |
| 599 | case required_argument: sstr << ':'; break; |
| 600 | case optional_argument: sstr << "::"; break; |
| 601 | } |
| 602 | } |
| 603 | } |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 604 | #ifdef __GLIBC__ |
| 605 | optind = 0; |
| 606 | #else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 607 | optreset = 1; |
| 608 | optind = 1; |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 609 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 610 | int val; |
| 611 | while (1) |
| 612 | { |
| 613 | int long_options_index = -1; |
| 614 | val = ::getopt_long(GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options, |
| 615 | &long_options_index); |
| 616 | if (val == -1) |
| 617 | break; |
| 618 | |
| 619 | // Did we get an error? |
| 620 | if (val == '?') |
| 621 | { |
| 622 | error.SetErrorStringWithFormat("Unknown or ambiguous option.\n"); |
| 623 | break; |
| 624 | } |
| 625 | // The option auto-set itself |
| 626 | if (val == 0) |
| 627 | continue; |
| 628 | |
| 629 | ((Options *) &options)->OptionSeen (val); |
| 630 | |
| 631 | // Lookup the long option index |
| 632 | if (long_options_index == -1) |
| 633 | { |
| 634 | for (int i=0; |
| 635 | long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val; |
| 636 | ++i) |
| 637 | { |
| 638 | if (long_options[i].val == val) |
| 639 | { |
| 640 | long_options_index = i; |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | // Call the callback with the option |
| 646 | if (long_options_index >= 0) |
| 647 | { |
| 648 | error = options.SetOptionValue(long_options_index, |
| 649 | long_options[long_options_index].has_arg == no_argument ? NULL : optarg); |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | error.SetErrorStringWithFormat("Invalid option with value '%i'.\n", val); |
| 654 | } |
| 655 | if (error.Fail()) |
| 656 | break; |
| 657 | } |
| 658 | |
| 659 | // Update our ARGV now that get options has consumed all the options |
| 660 | m_argv.erase(m_argv.begin(), m_argv.begin() + optind); |
| 661 | UpdateArgsAfterOptionParsing (); |
| 662 | return error; |
| 663 | } |
| 664 | |
| 665 | void |
| 666 | Args::Clear () |
| 667 | { |
| 668 | m_args.clear (); |
| 669 | m_argv.clear (); |
| 670 | m_args_quote_char.clear(); |
| 671 | } |
| 672 | |
| 673 | int32_t |
| 674 | Args::StringToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr) |
| 675 | { |
| 676 | if (s && s[0]) |
| 677 | { |
| 678 | char *end = NULL; |
| 679 | int32_t uval = ::strtol (s, &end, base); |
| 680 | if (*end == '\0') |
| 681 | { |
| 682 | if (success_ptr) *success_ptr = true; |
| 683 | return uval; // All characters were used, return the result |
| 684 | } |
| 685 | } |
| 686 | if (success_ptr) *success_ptr = false; |
| 687 | return fail_value; |
| 688 | } |
| 689 | |
| 690 | uint32_t |
| 691 | Args::StringToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr) |
| 692 | { |
| 693 | if (s && s[0]) |
| 694 | { |
| 695 | char *end = NULL; |
| 696 | uint32_t uval = ::strtoul (s, &end, base); |
| 697 | if (*end == '\0') |
| 698 | { |
| 699 | if (success_ptr) *success_ptr = true; |
| 700 | return uval; // All characters were used, return the result |
| 701 | } |
| 702 | } |
| 703 | if (success_ptr) *success_ptr = false; |
| 704 | return fail_value; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | int64_t |
| 709 | Args::StringToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr) |
| 710 | { |
| 711 | if (s && s[0]) |
| 712 | { |
| 713 | char *end = NULL; |
| 714 | int64_t uval = ::strtoll (s, &end, base); |
| 715 | if (*end == '\0') |
| 716 | { |
| 717 | if (success_ptr) *success_ptr = true; |
| 718 | return uval; // All characters were used, return the result |
| 719 | } |
| 720 | } |
| 721 | if (success_ptr) *success_ptr = false; |
| 722 | return fail_value; |
| 723 | } |
| 724 | |
| 725 | uint64_t |
| 726 | Args::StringToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr) |
| 727 | { |
| 728 | if (s && s[0]) |
| 729 | { |
| 730 | char *end = NULL; |
| 731 | uint64_t uval = ::strtoull (s, &end, base); |
| 732 | if (*end == '\0') |
| 733 | { |
| 734 | if (success_ptr) *success_ptr = true; |
| 735 | return uval; // All characters were used, return the result |
| 736 | } |
| 737 | } |
| 738 | if (success_ptr) *success_ptr = false; |
| 739 | return fail_value; |
| 740 | } |
| 741 | |
| 742 | lldb::addr_t |
| 743 | Args::StringToAddress (const char *s, lldb::addr_t fail_value, bool *success_ptr) |
| 744 | { |
| 745 | if (s && s[0]) |
| 746 | { |
| 747 | char *end = NULL; |
| 748 | lldb::addr_t addr = ::strtoull (s, &end, 0); |
| 749 | if (*end == '\0') |
| 750 | { |
| 751 | if (success_ptr) *success_ptr = true; |
| 752 | return addr; // All characters were used, return the result |
| 753 | } |
| 754 | // Try base 16 with no prefix... |
| 755 | addr = ::strtoull (s, &end, 16); |
| 756 | if (*end == '\0') |
| 757 | { |
| 758 | if (success_ptr) *success_ptr = true; |
| 759 | return addr; // All characters were used, return the result |
| 760 | } |
| 761 | } |
| 762 | if (success_ptr) *success_ptr = false; |
| 763 | return fail_value; |
| 764 | } |
| 765 | |
| 766 | bool |
| 767 | Args::StringToBoolean (const char *s, bool fail_value, bool *success_ptr) |
| 768 | { |
| 769 | if (s && s[0]) |
| 770 | { |
| 771 | if (::strcasecmp (s, "false") == 0 || |
| 772 | ::strcasecmp (s, "off") == 0 || |
| 773 | ::strcasecmp (s, "no") == 0 || |
| 774 | ::strcmp (s, "0") == 0) |
| 775 | { |
| 776 | if (success_ptr) |
| 777 | *success_ptr = true; |
| 778 | return false; |
| 779 | } |
| 780 | else |
| 781 | if (::strcasecmp (s, "true") == 0 || |
| 782 | ::strcasecmp (s, "on") == 0 || |
| 783 | ::strcasecmp (s, "yes") == 0 || |
| 784 | ::strcmp (s, "1") == 0) |
| 785 | { |
| 786 | if (success_ptr) *success_ptr = true; |
| 787 | return true; |
| 788 | } |
| 789 | } |
| 790 | if (success_ptr) *success_ptr = false; |
| 791 | return fail_value; |
| 792 | } |
| 793 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 794 | const char * |
| 795 | Args::StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update) |
| 796 | { |
| 797 | major = UINT32_MAX; |
| 798 | minor = UINT32_MAX; |
| 799 | update = UINT32_MAX; |
| 800 | |
| 801 | if (s && s[0]) |
| 802 | { |
| 803 | char *pos = NULL; |
| 804 | uint32_t uval32; |
| 805 | uval32 = ::strtoul (s, &pos, 0); |
| 806 | if (pos == s) |
| 807 | return s; |
| 808 | major = uval32; |
| 809 | if (*pos == '\0') |
| 810 | { |
| 811 | return pos; // Decoded major and got end of string |
| 812 | } |
| 813 | else if (*pos == '.') |
| 814 | { |
| 815 | const char *minor_cstr = pos + 1; |
| 816 | uval32 = ::strtoul (minor_cstr, &pos, 0); |
| 817 | if (pos == minor_cstr) |
| 818 | return pos; // Didn't get any digits for the minor version... |
| 819 | minor = uval32; |
| 820 | if (*pos == '.') |
| 821 | { |
| 822 | const char *update_cstr = pos + 1; |
| 823 | uval32 = ::strtoul (update_cstr, &pos, 0); |
| 824 | if (pos == update_cstr) |
| 825 | return pos; |
| 826 | update = uval32; |
| 827 | } |
| 828 | return pos; |
| 829 | } |
| 830 | } |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 835 | int32_t |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 836 | Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, bool *success_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 837 | { |
| 838 | if (enum_values && s && s[0]) |
| 839 | { |
| 840 | for (int i = 0; enum_values[i].string_value != NULL ; i++) |
| 841 | { |
| 842 | if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value) |
| 843 | { |
| 844 | if (success_ptr) *success_ptr = true; |
| 845 | return enum_values[i].value; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | if (success_ptr) *success_ptr = false; |
| 850 | |
| 851 | return fail_value; |
| 852 | } |
| 853 | |
| 854 | ScriptLanguage |
| 855 | Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr) |
| 856 | { |
| 857 | if (s && s[0]) |
| 858 | { |
| 859 | if ((::strcasecmp (s, "python") == 0) || |
| 860 | (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault)) |
| 861 | { |
| 862 | if (success_ptr) *success_ptr = true; |
| 863 | return eScriptLanguagePython; |
| 864 | } |
| 865 | if (::strcasecmp (s, "none")) |
| 866 | { |
| 867 | if (success_ptr) *success_ptr = true; |
| 868 | return eScriptLanguageNone; |
| 869 | } |
| 870 | } |
| 871 | if (success_ptr) *success_ptr = false; |
| 872 | return fail_value; |
| 873 | } |
| 874 | |
| 875 | Error |
| 876 | Args::StringToFormat |
| 877 | ( |
| 878 | const char *s, |
Greg Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 879 | lldb::Format &format, |
| 880 | uint32_t *byte_size_ptr |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 881 | ) |
| 882 | { |
| 883 | format = eFormatInvalid; |
| 884 | Error error; |
| 885 | |
| 886 | if (s && s[0]) |
| 887 | { |
Greg Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 888 | if (byte_size_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 889 | { |
Greg Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 890 | if (isdigit (s[0])) |
| 891 | { |
| 892 | char *format_char = NULL; |
| 893 | unsigned long byte_size = ::strtoul (s, &format_char, 0); |
| 894 | if (byte_size != ULONG_MAX) |
| 895 | *byte_size_ptr = byte_size; |
| 896 | s = format_char; |
| 897 | } |
| 898 | else |
| 899 | *byte_size_ptr = 0; |
| 900 | } |
| 901 | |
| 902 | bool success = s[1] == '\0'; |
| 903 | if (success) |
| 904 | { |
| 905 | switch (s[0]) |
| 906 | { |
| 907 | case 'y': format = eFormatBytes; break; |
| 908 | case 'Y': format = eFormatBytesWithASCII; break; |
| 909 | case 'b': format = eFormatBinary; break; |
| 910 | case 'B': format = eFormatBoolean; break; |
Greg Clayton | 307fa07 | 2011-06-17 23:50:44 +0000 | [diff] [blame] | 911 | case 'a': format = eFormatCharArray; break; |
Greg Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 912 | case 'c': format = eFormatChar; break; |
| 913 | case 'C': format = eFormatCharPrintable; break; |
| 914 | case 'o': format = eFormatOctal; break; |
| 915 | case 'O': format = eFormatOSType; break; |
| 916 | case 'i': |
| 917 | case 'd': format = eFormatDecimal; break; |
| 918 | case 'I': format = eFormatComplexInteger; break; |
| 919 | case 'u': format = eFormatUnsigned; break; |
| 920 | case 'x': format = eFormatHex; break; |
| 921 | case 'X': format = eFormatComplex; break; |
| 922 | case 'f': |
| 923 | case 'e': |
| 924 | case 'g': format = eFormatFloat; break; |
| 925 | case 'p': format = eFormatPointer; break; |
| 926 | case 's': format = eFormatCString; break; |
| 927 | default: |
| 928 | success = false; |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | if (!success) |
| 933 | error.SetErrorStringWithFormat ("Invalid format specification '%s'. Valid values are:\n" |
Greg Clayton | 307fa07 | 2011-06-17 23:50:44 +0000 | [diff] [blame] | 934 | " a - char buffer\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 935 | " b - binary\n" |
| 936 | " B - boolean\n" |
| 937 | " c - char\n" |
| 938 | " C - printable char\n" |
| 939 | " d - signed decimal\n" |
| 940 | " e - float\n" |
| 941 | " f - float\n" |
| 942 | " g - float\n" |
| 943 | " i - signed decimal\n" |
Greg Clayton | ff44ab4 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 944 | " i - complex integer\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 945 | " o - octal\n" |
Greg Clayton | ff44ab4 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 946 | " O - OSType\n" |
| 947 | " p - pointer\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 948 | " s - c-string\n" |
| 949 | " u - unsigned decimal\n" |
| 950 | " x - hex\n" |
Greg Clayton | ff44ab4 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 951 | " X - complex float\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 952 | " y - bytes\n" |
Greg Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 953 | " Y - bytes with ASCII\n%s", |
| 954 | s, |
| 955 | byte_size_ptr ? "An optional byte size can precede the format character.\n" : ""); |
| 956 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 957 | |
| 958 | if (error.Fail()) |
| 959 | return error; |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | error.SetErrorStringWithFormat("%s option string.\n", s ? "empty" : "invalid"); |
| 964 | } |
| 965 | return error; |
| 966 | } |
| 967 | |
| 968 | void |
| 969 | Args::LongestCommonPrefix (std::string &common_prefix) |
| 970 | { |
| 971 | arg_sstr_collection::iterator pos, end = m_args.end(); |
| 972 | pos = m_args.begin(); |
| 973 | if (pos == end) |
| 974 | common_prefix.clear(); |
| 975 | else |
| 976 | common_prefix = (*pos); |
| 977 | |
| 978 | for (++pos; pos != end; ++pos) |
| 979 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 980 | size_t new_size = (*pos).size(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 981 | |
| 982 | // First trim common_prefix if it is longer than the current element: |
| 983 | if (common_prefix.size() > new_size) |
| 984 | common_prefix.erase (new_size); |
| 985 | |
| 986 | // Then trim it at the first disparity: |
| 987 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 988 | for (size_t i = 0; i < common_prefix.size(); i++) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 989 | { |
| 990 | if ((*pos)[i] != common_prefix[i]) |
| 991 | { |
| 992 | common_prefix.erase(i); |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // If we've emptied the common prefix, we're done. |
| 998 | if (common_prefix.empty()) |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1003 | size_t |
| 1004 | Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index) |
| 1005 | { |
| 1006 | char short_buffer[3]; |
| 1007 | char long_buffer[255]; |
| 1008 | ::snprintf (short_buffer, sizeof (short_buffer), "-%c", (char) long_options[long_options_index].val); |
| 1009 | ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name); |
| 1010 | size_t end = GetArgumentCount (); |
| 1011 | size_t idx = 0; |
| 1012 | while (idx < end) |
| 1013 | { |
| 1014 | if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0) |
| 1015 | || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0)) |
| 1016 | { |
| 1017 | return idx; |
| 1018 | } |
| 1019 | ++idx; |
| 1020 | } |
| 1021 | |
| 1022 | return end; |
| 1023 | } |
| 1024 | |
| 1025 | bool |
| 1026 | Args::IsPositionalArgument (const char *arg) |
| 1027 | { |
| 1028 | if (arg == NULL) |
| 1029 | return false; |
| 1030 | |
| 1031 | bool is_positional = true; |
| 1032 | char *cptr = (char *) arg; |
| 1033 | |
| 1034 | if (cptr[0] == '%') |
| 1035 | { |
| 1036 | ++cptr; |
| 1037 | while (isdigit (cptr[0])) |
| 1038 | ++cptr; |
| 1039 | if (cptr[0] != '\0') |
| 1040 | is_positional = false; |
| 1041 | } |
| 1042 | else |
| 1043 | is_positional = false; |
| 1044 | |
| 1045 | return is_positional; |
| 1046 | } |
| 1047 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1048 | void |
Caroline Tice | 5e0894e | 2010-10-12 17:45:19 +0000 | [diff] [blame] | 1049 | Args::ParseAliasOptions (Options &options, |
| 1050 | CommandReturnObject &result, |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1051 | OptionArgVector *option_arg_vector, |
| 1052 | std::string &raw_input_string) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1053 | { |
| 1054 | StreamString sstr; |
| 1055 | int i; |
| 1056 | struct option *long_options = options.GetLongOptions(); |
| 1057 | |
| 1058 | if (long_options == NULL) |
| 1059 | { |
| 1060 | result.AppendError ("invalid long options"); |
| 1061 | result.SetStatus (eReturnStatusFailed); |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | for (i = 0; long_options[i].name != NULL; ++i) |
| 1066 | { |
| 1067 | if (long_options[i].flag == NULL) |
| 1068 | { |
| 1069 | sstr << (char) long_options[i].val; |
| 1070 | switch (long_options[i].has_arg) |
| 1071 | { |
| 1072 | default: |
| 1073 | case no_argument: |
| 1074 | break; |
| 1075 | case required_argument: |
| 1076 | sstr << ":"; |
| 1077 | break; |
| 1078 | case optional_argument: |
| 1079 | sstr << "::"; |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 1085 | #ifdef __GLIBC__ |
| 1086 | optind = 0; |
| 1087 | #else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1088 | optreset = 1; |
| 1089 | optind = 1; |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 1090 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1091 | int val; |
| 1092 | while (1) |
| 1093 | { |
| 1094 | int long_options_index = -1; |
| 1095 | val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options, |
| 1096 | &long_options_index); |
| 1097 | |
| 1098 | if (val == -1) |
| 1099 | break; |
| 1100 | |
| 1101 | if (val == '?') |
| 1102 | { |
| 1103 | result.AppendError ("unknown or ambiguous option"); |
| 1104 | result.SetStatus (eReturnStatusFailed); |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | if (val == 0) |
| 1109 | continue; |
| 1110 | |
| 1111 | ((Options *) &options)->OptionSeen (val); |
| 1112 | |
| 1113 | // Look up the long option index |
| 1114 | if (long_options_index == -1) |
| 1115 | { |
| 1116 | for (int j = 0; |
| 1117 | long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val; |
| 1118 | ++j) |
| 1119 | { |
| 1120 | if (long_options[j].val == val) |
| 1121 | { |
| 1122 | long_options_index = j; |
| 1123 | break; |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | // See if the option takes an argument, and see if one was supplied. |
| 1129 | if (long_options_index >= 0) |
| 1130 | { |
| 1131 | StreamString option_str; |
| 1132 | option_str.Printf ("-%c", (char) val); |
| 1133 | |
| 1134 | switch (long_options[long_options_index].has_arg) |
| 1135 | { |
| 1136 | case no_argument: |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1137 | option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()), |
| 1138 | OptionArgValue (no_argument, "<no-argument>"))); |
Caroline Tice | 2160c3f | 2010-09-12 04:48:45 +0000 | [diff] [blame] | 1139 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | case required_argument: |
| 1142 | if (optarg != NULL) |
| 1143 | { |
| 1144 | option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()), |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1145 | OptionArgValue (required_argument, |
| 1146 | std::string (optarg)))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1147 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1148 | } |
| 1149 | else |
| 1150 | { |
| 1151 | result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n", |
| 1152 | option_str.GetData()); |
| 1153 | result.SetStatus (eReturnStatusFailed); |
| 1154 | } |
| 1155 | break; |
| 1156 | case optional_argument: |
| 1157 | if (optarg != NULL) |
| 1158 | { |
| 1159 | option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()), |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1160 | OptionArgValue (optional_argument, |
| 1161 | std::string (optarg)))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1162 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()), |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1167 | OptionArgValue (optional_argument, "<no-argument>"))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1168 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1169 | } |
| 1170 | break; |
| 1171 | default: |
| 1172 | result.AppendErrorWithFormat |
| 1173 | ("error with options table; invalid value in has_arg field for option '%c'.\n", |
| 1174 | (char) val); |
| 1175 | result.SetStatus (eReturnStatusFailed); |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
| 1179 | else |
| 1180 | { |
| 1181 | result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", (char) val); |
| 1182 | result.SetStatus (eReturnStatusFailed); |
| 1183 | } |
Caroline Tice | 5e0894e | 2010-10-12 17:45:19 +0000 | [diff] [blame] | 1184 | |
| 1185 | if (long_options_index >= 0) |
| 1186 | { |
| 1187 | // 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] | 1188 | // supplied. Remove option (and argument, if given) from the argument list. Also remove them from |
| 1189 | // the raw_input_string, if one was passed in. |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1190 | size_t idx = FindArgumentIndexForOption (long_options, long_options_index); |
| 1191 | if (idx < GetArgumentCount()) |
| 1192 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1193 | if (raw_input_string.size() > 0) |
| 1194 | { |
| 1195 | const char *tmp_arg = GetArgumentAtIndex (idx); |
| 1196 | size_t pos = raw_input_string.find (tmp_arg); |
| 1197 | if (pos != std::string::npos) |
| 1198 | raw_input_string.erase (pos, strlen (tmp_arg)); |
| 1199 | } |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1200 | ReplaceArgumentAtIndex (idx, ""); |
| 1201 | if ((long_options[long_options_index].has_arg != no_argument) |
| 1202 | && (optarg != NULL) |
| 1203 | && (idx+1 < GetArgumentCount()) |
| 1204 | && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0)) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1205 | { |
| 1206 | if (raw_input_string.size() > 0) |
| 1207 | { |
| 1208 | const char *tmp_arg = GetArgumentAtIndex (idx+1); |
| 1209 | size_t pos = raw_input_string.find (tmp_arg); |
| 1210 | if (pos != std::string::npos) |
| 1211 | raw_input_string.erase (pos, strlen (tmp_arg)); |
| 1212 | } |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1213 | ReplaceArgumentAtIndex (idx+1, ""); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1214 | } |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1215 | } |
Caroline Tice | 5e0894e | 2010-10-12 17:45:19 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1218 | if (!result.Succeeded()) |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | void |
| 1224 | Args::ParseArgsForCompletion |
| 1225 | ( |
| 1226 | Options &options, |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1227 | OptionElementVector &option_element_vector, |
| 1228 | uint32_t cursor_index |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1229 | ) |
| 1230 | { |
| 1231 | StreamString sstr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1232 | struct option *long_options = options.GetLongOptions(); |
| 1233 | option_element_vector.clear(); |
| 1234 | |
| 1235 | if (long_options == NULL) |
| 1236 | { |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | // Leading : tells getopt to return a : for a missing option argument AND |
| 1241 | // to suppress error messages. |
| 1242 | |
| 1243 | sstr << ":"; |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 1244 | for (int i = 0; long_options[i].name != NULL; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1245 | { |
| 1246 | if (long_options[i].flag == NULL) |
| 1247 | { |
| 1248 | sstr << (char) long_options[i].val; |
| 1249 | switch (long_options[i].has_arg) |
| 1250 | { |
| 1251 | default: |
| 1252 | case no_argument: |
| 1253 | break; |
| 1254 | case required_argument: |
| 1255 | sstr << ":"; |
| 1256 | break; |
| 1257 | case optional_argument: |
| 1258 | sstr << "::"; |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 1264 | #ifdef __GLIBC__ |
| 1265 | optind = 0; |
| 1266 | #else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1267 | optreset = 1; |
| 1268 | optind = 1; |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 1269 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1270 | opterr = 0; |
| 1271 | |
| 1272 | int val; |
| 1273 | const OptionDefinition *opt_defs = options.GetDefinitions(); |
| 1274 | |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1275 | // 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] | 1276 | // 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] | 1277 | // change the one we have. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1278 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1279 | std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1280 | |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1281 | bool failed_once = false; |
| 1282 | uint32_t dash_dash_pos = -1; |
| 1283 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1284 | while (1) |
| 1285 | { |
| 1286 | bool missing_argument = false; |
| 1287 | int parse_start = optind; |
| 1288 | int long_options_index = -1; |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1289 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1290 | val = ::getopt_long (dummy_vec.size() - 1, |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1291 | (char *const *) &dummy_vec.front(), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1292 | sstr.GetData(), |
| 1293 | long_options, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1294 | &long_options_index); |
| 1295 | |
| 1296 | if (val == -1) |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1297 | { |
| 1298 | // When we're completing a "--" which is the last option on line, |
| 1299 | if (failed_once) |
| 1300 | break; |
| 1301 | |
| 1302 | failed_once = true; |
| 1303 | |
| 1304 | // If this is a bare "--" we mark it as such so we can complete it successfully later. |
| 1305 | // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the |
| 1306 | // user might want to complete options by long name. I make this work by checking whether the |
| 1307 | // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise |
| 1308 | // I let it pass to getopt_long which will terminate the option parsing. |
| 1309 | // Note, in either case we continue parsing the line so we can figure out what other options |
| 1310 | // were passed. This will be useful when we come to restricting completions based on what other |
| 1311 | // options we've seen on the line. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1312 | |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1313 | if (optind < dummy_vec.size() - 1 |
| 1314 | && (strcmp (dummy_vec[optind-1], "--") == 0)) |
| 1315 | { |
| 1316 | dash_dash_pos = optind - 1; |
| 1317 | if (optind - 1 == cursor_index) |
| 1318 | { |
| 1319 | option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1, |
| 1320 | OptionArgElement::eBareDoubleDash)); |
| 1321 | continue; |
| 1322 | } |
| 1323 | else |
| 1324 | break; |
| 1325 | } |
| 1326 | else |
| 1327 | break; |
| 1328 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1329 | else if (val == '?') |
| 1330 | { |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1331 | option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1, |
| 1332 | OptionArgElement::eUnrecognizedArg)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1333 | continue; |
| 1334 | } |
| 1335 | else if (val == 0) |
| 1336 | { |
| 1337 | continue; |
| 1338 | } |
| 1339 | else if (val == ':') |
| 1340 | { |
| 1341 | // This is a missing argument. |
| 1342 | val = optopt; |
| 1343 | missing_argument = true; |
| 1344 | } |
| 1345 | |
| 1346 | ((Options *) &options)->OptionSeen (val); |
| 1347 | |
| 1348 | // Look up the long option index |
| 1349 | if (long_options_index == -1) |
| 1350 | { |
| 1351 | for (int j = 0; |
| 1352 | long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val; |
| 1353 | ++j) |
| 1354 | { |
| 1355 | if (long_options[j].val == val) |
| 1356 | { |
| 1357 | long_options_index = j; |
| 1358 | break; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | // See if the option takes an argument, and see if one was supplied. |
| 1364 | if (long_options_index >= 0) |
| 1365 | { |
| 1366 | int opt_defs_index = -1; |
| 1367 | for (int i = 0; ; i++) |
| 1368 | { |
| 1369 | if (opt_defs[i].short_option == 0) |
| 1370 | break; |
| 1371 | else if (opt_defs[i].short_option == val) |
| 1372 | { |
| 1373 | opt_defs_index = i; |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | switch (long_options[long_options_index].has_arg) |
| 1379 | { |
| 1380 | case no_argument: |
| 1381 | option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0)); |
| 1382 | break; |
| 1383 | case required_argument: |
| 1384 | if (optarg != NULL) |
| 1385 | { |
| 1386 | int arg_index; |
| 1387 | if (missing_argument) |
| 1388 | arg_index = -1; |
| 1389 | else |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1390 | arg_index = optind - 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1391 | |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1392 | 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] | 1393 | } |
| 1394 | else |
| 1395 | { |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1396 | option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1397 | } |
| 1398 | break; |
| 1399 | case optional_argument: |
| 1400 | if (optarg != NULL) |
| 1401 | { |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1402 | 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] | 1403 | } |
| 1404 | else |
| 1405 | { |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1406 | 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] | 1407 | } |
| 1408 | break; |
| 1409 | default: |
| 1410 | // The options table is messed up. Here we'll just continue |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1411 | option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1, |
| 1412 | OptionArgElement::eUnrecognizedArg)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | } |
| 1415 | } |
| 1416 | else |
| 1417 | { |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1418 | option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1, |
| 1419 | OptionArgElement::eUnrecognizedArg)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in |
| 1424 | // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores |
| 1425 | // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position. |
| 1426 | |
| 1427 | if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos) |
| 1428 | && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0) |
| 1429 | { |
| 1430 | option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index, |
| 1431 | OptionArgElement::eBareDash)); |
| 1432 | |
| 1433 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1434 | } |