blob: e7dd536c2c40655a8a3b4f528fb8a339f6c9e4c4 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Friedman27cd8892010-06-09 10:59:23 +000012#include <cstdlib>
Chris Lattner24943d22010-06-08 16:52:24 +000013// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamFile.h"
19#include "lldb/Core/StreamString.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000020#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Interpreter/CommandReturnObject.h"
22
Chris Lattner24943d22010-06-08 16:52:24 +000023using namespace lldb;
24using namespace lldb_private;
25
Chris Lattner24943d22010-06-08 16:52:24 +000026//----------------------------------------------------------------------
27// Args constructor
28//----------------------------------------------------------------------
29Args::Args (const char *command) :
30 m_args(),
Greg Claytonb72d0f02011-04-12 05:54:46 +000031 m_argv(),
32 m_args_quote_char()
Chris Lattner24943d22010-06-08 16:52:24 +000033{
Greg Clayton928d1302010-12-19 03:41:24 +000034 if (command)
35 SetCommandString (command);
Chris Lattner24943d22010-06-08 16:52:24 +000036}
37
38
39Args::Args (const char *command, size_t len) :
40 m_args(),
Greg Claytonb72d0f02011-04-12 05:54:46 +000041 m_argv(),
42 m_args_quote_char()
Chris Lattner24943d22010-06-08 16:52:24 +000043{
Greg Clayton928d1302010-12-19 03:41:24 +000044 if (command && len)
45 SetCommandString (command, len);
Chris Lattner24943d22010-06-08 16:52:24 +000046}
47
Chris Lattner24943d22010-06-08 16:52:24 +000048//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000049// 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//----------------------------------------------------------------------
55Args::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//----------------------------------------------------------------------
70const Args &
71Args::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 Lattner24943d22010-06-08 16:52:24 +000084// Destructor
85//----------------------------------------------------------------------
86Args::~Args ()
87{
88}
89
90void
91Args::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
115bool
116Args::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 Ticed9105c22010-12-10 00:26:54 +0000129bool
130Args::GetQuotedCommandString (std::string &command)
131{
132 command.clear ();
Greg Clayton928d1302010-12-19 03:41:24 +0000133 size_t argc = GetArgumentCount ();
134 for (size_t i = 0; i < argc; ++i)
Caroline Ticed9105c22010-12-10 00:26:54 +0000135 {
136 if (i > 0)
Greg Clayton928d1302010-12-19 03:41:24 +0000137 command.append (1, ' ');
138 char quote_char = GetArgumentQuoteCharAtIndex(i);
139 if (quote_char)
Caroline Ticed9105c22010-12-10 00:26:54 +0000140 {
Greg Clayton928d1302010-12-19 03:41:24 +0000141 command.append (1, quote_char);
142 command.append (m_argv[i]);
143 command.append (1, quote_char);
Caroline Ticed9105c22010-12-10 00:26:54 +0000144 }
145 else
Greg Clayton928d1302010-12-19 03:41:24 +0000146 command.append (m_argv[i]);
Caroline Ticed9105c22010-12-10 00:26:54 +0000147 }
148 return argc > 0;
149}
150
Chris Lattner24943d22010-06-08 16:52:24 +0000151void
152Args::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
160void
161Args::SetCommandString (const char *command)
162{
163 m_args.clear();
164 m_argv.clear();
Greg Clayton928d1302010-12-19 03:41:24 +0000165 m_args_quote_char.clear();
166
Chris Lattner24943d22010-06-08 16:52:24 +0000167 if (command && command[0])
168 {
Greg Clayton928d1302010-12-19 03:41:24 +0000169 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 Lattner24943d22010-06-08 16:52:24 +0000176 {
Greg Clayton928d1302010-12-19 03:41:24 +0000177 // 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 Lattner24943d22010-06-08 16:52:24 +0000181 // we're done.
182 if (*arg_start == '\0')
183 break;
184
Greg Clayton5d187e52011-01-08 20:28:42 +0000185 // Arguments can be split into multiple discontiguous pieces,
Greg Clayton928d1302010-12-19 03:41:24 +0000186 // 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 Lattner24943d22010-06-08 16:52:24 +0000195 std::string arg;
Greg Clayton928d1302010-12-19 03:41:24 +0000196 // 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 Lattner24943d22010-06-08 16:52:24 +0000203
Greg Clayton928d1302010-12-19 03:41:24 +0000204 do
Chris Lattner24943d22010-06-08 16:52:24 +0000205 {
Greg Clayton928d1302010-12-19 03:41:24 +0000206 arg_end = ::strcspn (arg_pos, k_space_separators_with_slash_and_quotes) + arg_pos;
207
208 switch (arg_end[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000209 {
Greg Clayton928d1302010-12-19 03:41:24 +0000210 default:
211 assert (!"Unhandled case statement, we must handle this...");
212 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000213
Greg Clayton928d1302010-12-19 03:41:24 +0000214 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 Lattner24943d22010-06-08 16:52:24 +0000224 {
Greg Clayton928d1302010-12-19 03:41:24 +0000225 case '\0':
226 arg.append (arg_piece_start);
227 arg_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000228 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000229
Greg Clayton928d1302010-12-19 03:41:24 +0000230 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 Lattner24943d22010-06-08 16:52:24 +0000247 {
Greg Clayton928d1302010-12-19 03:41:24 +0000248 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 Clayton5d187e52011-01-08 20:28:42 +0000268 // Make sure there isn't a string that precedes
Greg Clayton928d1302010-12-19 03:41:24 +0000269 // 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 Lattner24943d22010-06-08 16:52:24 +0000300 {
Greg Clayton928d1302010-12-19 03:41:24 +0000301 // 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 Lattner24943d22010-06-08 16:52:24 +0000314 }
315 else
316 {
Greg Clayton928d1302010-12-19 03:41:24 +0000317 arg_pos = end_quote + 1;
318 arg_piece_start = arg_pos;
Chris Lattner24943d22010-06-08 16:52:24 +0000319 }
Greg Clayton928d1302010-12-19 03:41:24 +0000320 quote_char = '\0';
Chris Lattner24943d22010-06-08 16:52:24 +0000321 }
322 }
Greg Clayton928d1302010-12-19 03:41:24 +0000323 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000324
Greg Clayton928d1302010-12-19 03:41:24 +0000325 case ' ':
326 case '\t':
327 if (quote_char)
Chris Lattner24943d22010-06-08 16:52:24 +0000328 {
Greg Clayton928d1302010-12-19 03:41:24 +0000329 // 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 Lattner24943d22010-06-08 16:52:24 +0000333 }
Greg Clayton928d1302010-12-19 03:41:24 +0000334 else
Chris Lattner24943d22010-06-08 16:52:24 +0000335 {
Greg Clayton928d1302010-12-19 03:41:24 +0000336 // 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 Lattner24943d22010-06-08 16:52:24 +0000341 }
Greg Clayton928d1302010-12-19 03:41:24 +0000342 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000343 }
Greg Clayton928d1302010-12-19 03:41:24 +0000344 } while (!arg_complete);
Chris Lattner24943d22010-06-08 16:52:24 +0000345
346 m_args.push_back(arg);
Greg Clayton928d1302010-12-19 03:41:24 +0000347 m_args_quote_char.push_back (first_quote_char);
Chris Lattner24943d22010-06-08 16:52:24 +0000348 }
Greg Clayton928d1302010-12-19 03:41:24 +0000349 UpdateArgvFromArgs();
Chris Lattner24943d22010-06-08 16:52:24 +0000350 }
Chris Lattner24943d22010-06-08 16:52:24 +0000351}
352
353void
354Args::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
395void
396Args::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 Clayton928d1302010-12-19 03:41:24 +0000403 // 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 Lattner24943d22010-06-08 16:52:24 +0000406}
407
408size_t
409Args::GetArgumentCount() const
410{
411 if (m_argv.empty())
412 return 0;
413 return m_argv.size() - 1;
414}
415
416const char *
417Args::GetArgumentAtIndex (size_t idx) const
418{
419 if (idx < m_argv.size())
420 return m_argv[idx];
421 return NULL;
422}
423
424char
425Args::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
432char **
433Args::GetArgumentVector()
434{
435 if (!m_argv.empty())
436 return (char **)&m_argv[0];
437 return NULL;
438}
439
440const char **
441Args::GetConstArgumentVector() const
442{
443 if (!m_argv.empty())
444 return (const char **)&m_argv[0];
445 return NULL;
446}
447
448void
449Args::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 Clayton928d1302010-12-19 03:41:24 +0000456 if (!m_args_quote_char.empty())
457 m_args_quote_char.erase(m_args_quote_char.begin());
Chris Lattner24943d22010-06-08 16:52:24 +0000458 }
459}
460
461const char *
462Args::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
470void
471Args::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
478const char *
479Args::AppendArgument (const char *arg_cstr, char quote_char)
480{
481 return InsertArgumentAtIndex (GetArgumentCount(), arg_cstr, quote_char);
482}
483
484const char *
485Args::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 Clayton928d1302010-12-19 03:41:24 +0000497 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 Lattner24943d22010-06-08 16:52:24 +0000504
505 UpdateArgvFromArgs();
506 return GetArgumentAtIndex(idx);
507}
508
509const char *
510Args::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 Clayton928d1302010-12-19 03:41:24 +0000525 if (idx >= m_args_quote_char.size())
526 m_args_quote_char.resize(idx + 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000527 m_args_quote_char[idx] = quote_char;
528 return GetArgumentAtIndex(idx);
529 }
530 return NULL;
531}
532
533void
534Args::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 Clayton928d1302010-12-19 03:41:24 +0000549 if (idx < m_args_quote_char.size())
550 m_args_quote_char.erase(m_args_quote_char.begin() + idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000551 }
552}
553
554void
555Args::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 Clayton928d1302010-12-19 03:41:24 +0000568 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
Chris Lattner24943d22010-06-08 16:52:24 +0000569 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
578Error
579Args::ParseOptions (Options &options)
580{
581 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000582 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 Claytonbef15832010-07-14 00:18:15 +0000590 for (int i=0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000591 {
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 Friedmanef2bc872010-06-13 19:18:49 +0000604#ifdef __GLIBC__
605 optind = 0;
606#else
Chris Lattner24943d22010-06-08 16:52:24 +0000607 optreset = 1;
608 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +0000609#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000610 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
665void
666Args::Clear ()
667{
668 m_args.clear ();
669 m_argv.clear ();
670 m_args_quote_char.clear();
671}
672
673int32_t
674Args::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
690uint32_t
691Args::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
708int64_t
709Args::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
725uint64_t
726Args::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
742lldb::addr_t
743Args::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
766bool
767Args::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 Claytonb1888f22011-03-19 01:12:21 +0000794const char *
795Args::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 Lattner24943d22010-06-08 16:52:24 +0000835int32_t
Greg Claytonb3448432011-03-24 21:19:54 +0000836Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, bool *success_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000837{
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
854ScriptLanguage
855Args::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
875Error
876Args::StringToFormat
877(
878 const char *s,
879 lldb::Format &format
880)
881{
882 format = eFormatInvalid;
883 Error error;
884
885 if (s && s[0])
886 {
887 switch (s[0])
888 {
889 case 'y': format = eFormatBytes; break;
890 case 'Y': format = eFormatBytesWithASCII; break;
891 case 'b': format = eFormatBinary; break;
892 case 'B': format = eFormatBoolean; break;
893 case 'c': format = eFormatChar; break;
894 case 'C': format = eFormatCharPrintable; break;
895 case 'o': format = eFormatOctal; break;
Greg Claytonff44ab42011-04-23 02:04:55 +0000896 case 'O': format = eFormatOSType; break;
Chris Lattner24943d22010-06-08 16:52:24 +0000897 case 'i':
898 case 'd': format = eFormatDecimal; break;
Greg Claytonff44ab42011-04-23 02:04:55 +0000899 case 'I': format = eFormatComplexInteger; break;
Chris Lattner24943d22010-06-08 16:52:24 +0000900 case 'u': format = eFormatUnsigned; break;
901 case 'x': format = eFormatHex; break;
Greg Claytonff44ab42011-04-23 02:04:55 +0000902 case 'X': format = eFormatComplex; break;
Chris Lattner24943d22010-06-08 16:52:24 +0000903 case 'f':
904 case 'e':
905 case 'g': format = eFormatFloat; break;
906 case 'p': format = eFormatPointer; break;
907 case 's': format = eFormatCString; break;
908 default:
909 error.SetErrorStringWithFormat("Invalid format character '%c'. Valid values are:\n"
910 " b - binary\n"
911 " B - boolean\n"
912 " c - char\n"
913 " C - printable char\n"
914 " d - signed decimal\n"
915 " e - float\n"
916 " f - float\n"
917 " g - float\n"
918 " i - signed decimal\n"
Greg Claytonff44ab42011-04-23 02:04:55 +0000919 " i - complex integer\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000920 " o - octal\n"
Greg Claytonff44ab42011-04-23 02:04:55 +0000921 " O - OSType\n"
922 " p - pointer\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000923 " s - c-string\n"
924 " u - unsigned decimal\n"
925 " x - hex\n"
Greg Claytonff44ab42011-04-23 02:04:55 +0000926 " X - complex float\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000927 " y - bytes\n"
928 " Y - bytes with ASCII\n", s[0]);
929 break;
930 }
931
932 if (error.Fail())
933 return error;
934 }
935 else
936 {
937 error.SetErrorStringWithFormat("%s option string.\n", s ? "empty" : "invalid");
938 }
939 return error;
940}
941
942void
943Args::LongestCommonPrefix (std::string &common_prefix)
944{
945 arg_sstr_collection::iterator pos, end = m_args.end();
946 pos = m_args.begin();
947 if (pos == end)
948 common_prefix.clear();
949 else
950 common_prefix = (*pos);
951
952 for (++pos; pos != end; ++pos)
953 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000954 size_t new_size = (*pos).size();
Chris Lattner24943d22010-06-08 16:52:24 +0000955
956 // First trim common_prefix if it is longer than the current element:
957 if (common_prefix.size() > new_size)
958 common_prefix.erase (new_size);
959
960 // Then trim it at the first disparity:
961
Greg Clayton54e7afa2010-07-09 20:39:50 +0000962 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000963 {
964 if ((*pos)[i] != common_prefix[i])
965 {
966 common_prefix.erase(i);
967 break;
968 }
969 }
970
971 // If we've emptied the common prefix, we're done.
972 if (common_prefix.empty())
973 break;
974 }
975}
976
Caroline Tice44c841d2010-12-07 19:58:26 +0000977size_t
978Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index)
979{
980 char short_buffer[3];
981 char long_buffer[255];
982 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", (char) long_options[long_options_index].val);
983 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name);
984 size_t end = GetArgumentCount ();
985 size_t idx = 0;
986 while (idx < end)
987 {
988 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
989 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
990 {
991 return idx;
992 }
993 ++idx;
994 }
995
996 return end;
997}
998
999bool
1000Args::IsPositionalArgument (const char *arg)
1001{
1002 if (arg == NULL)
1003 return false;
1004
1005 bool is_positional = true;
1006 char *cptr = (char *) arg;
1007
1008 if (cptr[0] == '%')
1009 {
1010 ++cptr;
1011 while (isdigit (cptr[0]))
1012 ++cptr;
1013 if (cptr[0] != '\0')
1014 is_positional = false;
1015 }
1016 else
1017 is_positional = false;
1018
1019 return is_positional;
1020}
1021
Chris Lattner24943d22010-06-08 16:52:24 +00001022void
Caroline Tice5e0894e2010-10-12 17:45:19 +00001023Args::ParseAliasOptions (Options &options,
1024 CommandReturnObject &result,
Caroline Ticee0da7a52010-12-09 22:52:49 +00001025 OptionArgVector *option_arg_vector,
1026 std::string &raw_input_string)
Chris Lattner24943d22010-06-08 16:52:24 +00001027{
1028 StreamString sstr;
1029 int i;
1030 struct option *long_options = options.GetLongOptions();
1031
1032 if (long_options == NULL)
1033 {
1034 result.AppendError ("invalid long options");
1035 result.SetStatus (eReturnStatusFailed);
1036 return;
1037 }
1038
1039 for (i = 0; long_options[i].name != NULL; ++i)
1040 {
1041 if (long_options[i].flag == NULL)
1042 {
1043 sstr << (char) long_options[i].val;
1044 switch (long_options[i].has_arg)
1045 {
1046 default:
1047 case no_argument:
1048 break;
1049 case required_argument:
1050 sstr << ":";
1051 break;
1052 case optional_argument:
1053 sstr << "::";
1054 break;
1055 }
1056 }
1057 }
1058
Eli Friedmanef2bc872010-06-13 19:18:49 +00001059#ifdef __GLIBC__
1060 optind = 0;
1061#else
Chris Lattner24943d22010-06-08 16:52:24 +00001062 optreset = 1;
1063 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001064#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001065 int val;
1066 while (1)
1067 {
1068 int long_options_index = -1;
1069 val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
1070 &long_options_index);
1071
1072 if (val == -1)
1073 break;
1074
1075 if (val == '?')
1076 {
1077 result.AppendError ("unknown or ambiguous option");
1078 result.SetStatus (eReturnStatusFailed);
1079 break;
1080 }
1081
1082 if (val == 0)
1083 continue;
1084
1085 ((Options *) &options)->OptionSeen (val);
1086
1087 // Look up the long option index
1088 if (long_options_index == -1)
1089 {
1090 for (int j = 0;
1091 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1092 ++j)
1093 {
1094 if (long_options[j].val == val)
1095 {
1096 long_options_index = j;
1097 break;
1098 }
1099 }
1100 }
1101
1102 // See if the option takes an argument, and see if one was supplied.
1103 if (long_options_index >= 0)
1104 {
1105 StreamString option_str;
1106 option_str.Printf ("-%c", (char) val);
1107
1108 switch (long_options[long_options_index].has_arg)
1109 {
1110 case no_argument:
Caroline Tice44c841d2010-12-07 19:58:26 +00001111 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1112 OptionArgValue (no_argument, "<no-argument>")));
Caroline Tice2160c3f2010-09-12 04:48:45 +00001113 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001114 break;
1115 case required_argument:
1116 if (optarg != NULL)
1117 {
1118 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001119 OptionArgValue (required_argument,
1120 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001121 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1122 }
1123 else
1124 {
1125 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1126 option_str.GetData());
1127 result.SetStatus (eReturnStatusFailed);
1128 }
1129 break;
1130 case optional_argument:
1131 if (optarg != NULL)
1132 {
1133 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001134 OptionArgValue (optional_argument,
1135 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001136 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1137 }
1138 else
1139 {
1140 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001141 OptionArgValue (optional_argument, "<no-argument>")));
Chris Lattner24943d22010-06-08 16:52:24 +00001142 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1143 }
1144 break;
1145 default:
1146 result.AppendErrorWithFormat
1147 ("error with options table; invalid value in has_arg field for option '%c'.\n",
1148 (char) val);
1149 result.SetStatus (eReturnStatusFailed);
1150 break;
1151 }
1152 }
1153 else
1154 {
1155 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", (char) val);
1156 result.SetStatus (eReturnStatusFailed);
1157 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001158
1159 if (long_options_index >= 0)
1160 {
1161 // Find option in the argument list; also see if it was supposed to take an argument and if one was
Caroline Ticee0da7a52010-12-09 22:52:49 +00001162 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1163 // the raw_input_string, if one was passed in.
Caroline Tice44c841d2010-12-07 19:58:26 +00001164 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1165 if (idx < GetArgumentCount())
1166 {
Caroline Ticee0da7a52010-12-09 22:52:49 +00001167 if (raw_input_string.size() > 0)
1168 {
1169 const char *tmp_arg = GetArgumentAtIndex (idx);
1170 size_t pos = raw_input_string.find (tmp_arg);
1171 if (pos != std::string::npos)
1172 raw_input_string.erase (pos, strlen (tmp_arg));
1173 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001174 ReplaceArgumentAtIndex (idx, "");
1175 if ((long_options[long_options_index].has_arg != no_argument)
1176 && (optarg != NULL)
1177 && (idx+1 < GetArgumentCount())
1178 && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0))
Caroline Ticee0da7a52010-12-09 22:52:49 +00001179 {
1180 if (raw_input_string.size() > 0)
1181 {
1182 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1183 size_t pos = raw_input_string.find (tmp_arg);
1184 if (pos != std::string::npos)
1185 raw_input_string.erase (pos, strlen (tmp_arg));
1186 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001187 ReplaceArgumentAtIndex (idx+1, "");
Caroline Ticee0da7a52010-12-09 22:52:49 +00001188 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001189 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001190 }
1191
Chris Lattner24943d22010-06-08 16:52:24 +00001192 if (!result.Succeeded())
1193 break;
1194 }
1195}
1196
1197void
1198Args::ParseArgsForCompletion
1199(
1200 Options &options,
Jim Inghamadb84292010-06-24 20:31:04 +00001201 OptionElementVector &option_element_vector,
1202 uint32_t cursor_index
Chris Lattner24943d22010-06-08 16:52:24 +00001203)
1204{
1205 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +00001206 struct option *long_options = options.GetLongOptions();
1207 option_element_vector.clear();
1208
1209 if (long_options == NULL)
1210 {
1211 return;
1212 }
1213
1214 // Leading : tells getopt to return a : for a missing option argument AND
1215 // to suppress error messages.
1216
1217 sstr << ":";
Greg Claytonbef15832010-07-14 00:18:15 +00001218 for (int i = 0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001219 {
1220 if (long_options[i].flag == NULL)
1221 {
1222 sstr << (char) long_options[i].val;
1223 switch (long_options[i].has_arg)
1224 {
1225 default:
1226 case no_argument:
1227 break;
1228 case required_argument:
1229 sstr << ":";
1230 break;
1231 case optional_argument:
1232 sstr << "::";
1233 break;
1234 }
1235 }
1236 }
1237
Eli Friedmanef2bc872010-06-13 19:18:49 +00001238#ifdef __GLIBC__
1239 optind = 0;
1240#else
Chris Lattner24943d22010-06-08 16:52:24 +00001241 optreset = 1;
1242 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001243#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001244 opterr = 0;
1245
1246 int val;
1247 const OptionDefinition *opt_defs = options.GetDefinitions();
1248
Jim Inghamadb84292010-06-24 20:31:04 +00001249 // Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
Chris Lattner24943d22010-06-08 16:52:24 +00001250 // So we have to build another Arg and pass that to getopt_long so it doesn't
Jim Inghamadb84292010-06-24 20:31:04 +00001251 // change the one we have.
Chris Lattner24943d22010-06-08 16:52:24 +00001252
Greg Clayton54e7afa2010-07-09 20:39:50 +00001253 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner24943d22010-06-08 16:52:24 +00001254
Jim Inghamadb84292010-06-24 20:31:04 +00001255 bool failed_once = false;
1256 uint32_t dash_dash_pos = -1;
1257
Chris Lattner24943d22010-06-08 16:52:24 +00001258 while (1)
1259 {
1260 bool missing_argument = false;
1261 int parse_start = optind;
1262 int long_options_index = -1;
Jim Inghamadb84292010-06-24 20:31:04 +00001263
Greg Clayton54e7afa2010-07-09 20:39:50 +00001264 val = ::getopt_long (dummy_vec.size() - 1,
Greg Clayton53d68e72010-07-20 22:52:08 +00001265 (char *const *) &dummy_vec.front(),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001266 sstr.GetData(),
1267 long_options,
Chris Lattner24943d22010-06-08 16:52:24 +00001268 &long_options_index);
1269
1270 if (val == -1)
Jim Inghamadb84292010-06-24 20:31:04 +00001271 {
1272 // When we're completing a "--" which is the last option on line,
1273 if (failed_once)
1274 break;
1275
1276 failed_once = true;
1277
1278 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1279 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1280 // user might want to complete options by long name. I make this work by checking whether the
1281 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1282 // I let it pass to getopt_long which will terminate the option parsing.
1283 // Note, in either case we continue parsing the line so we can figure out what other options
1284 // were passed. This will be useful when we come to restricting completions based on what other
1285 // options we've seen on the line.
Chris Lattner24943d22010-06-08 16:52:24 +00001286
Jim Inghamadb84292010-06-24 20:31:04 +00001287 if (optind < dummy_vec.size() - 1
1288 && (strcmp (dummy_vec[optind-1], "--") == 0))
1289 {
1290 dash_dash_pos = optind - 1;
1291 if (optind - 1 == cursor_index)
1292 {
1293 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
1294 OptionArgElement::eBareDoubleDash));
1295 continue;
1296 }
1297 else
1298 break;
1299 }
1300 else
1301 break;
1302 }
Chris Lattner24943d22010-06-08 16:52:24 +00001303 else if (val == '?')
1304 {
Jim Inghamadb84292010-06-24 20:31:04 +00001305 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1306 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001307 continue;
1308 }
1309 else if (val == 0)
1310 {
1311 continue;
1312 }
1313 else if (val == ':')
1314 {
1315 // This is a missing argument.
1316 val = optopt;
1317 missing_argument = true;
1318 }
1319
1320 ((Options *) &options)->OptionSeen (val);
1321
1322 // Look up the long option index
1323 if (long_options_index == -1)
1324 {
1325 for (int j = 0;
1326 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1327 ++j)
1328 {
1329 if (long_options[j].val == val)
1330 {
1331 long_options_index = j;
1332 break;
1333 }
1334 }
1335 }
1336
1337 // See if the option takes an argument, and see if one was supplied.
1338 if (long_options_index >= 0)
1339 {
1340 int opt_defs_index = -1;
1341 for (int i = 0; ; i++)
1342 {
1343 if (opt_defs[i].short_option == 0)
1344 break;
1345 else if (opt_defs[i].short_option == val)
1346 {
1347 opt_defs_index = i;
1348 break;
1349 }
1350 }
1351
1352 switch (long_options[long_options_index].has_arg)
1353 {
1354 case no_argument:
1355 option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0));
1356 break;
1357 case required_argument:
1358 if (optarg != NULL)
1359 {
1360 int arg_index;
1361 if (missing_argument)
1362 arg_index = -1;
1363 else
Jim Inghamadb84292010-06-24 20:31:04 +00001364 arg_index = optind - 1;
Chris Lattner24943d22010-06-08 16:52:24 +00001365
Jim Inghamadb84292010-06-24 20:31:04 +00001366 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +00001367 }
1368 else
1369 {
Jim Inghamadb84292010-06-24 20:31:04 +00001370 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1));
Chris Lattner24943d22010-06-08 16:52:24 +00001371 }
1372 break;
1373 case optional_argument:
1374 if (optarg != NULL)
1375 {
Jim Inghamadb84292010-06-24 20:31:04 +00001376 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001377 }
1378 else
1379 {
Jim Inghamadb84292010-06-24 20:31:04 +00001380 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001381 }
1382 break;
1383 default:
1384 // The options table is messed up. Here we'll just continue
Jim Inghamadb84292010-06-24 20:31:04 +00001385 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1386 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001387 break;
1388 }
1389 }
1390 else
1391 {
Jim Inghamadb84292010-06-24 20:31:04 +00001392 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1393 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001394 }
1395 }
Jim Inghamadb84292010-06-24 20:31:04 +00001396
1397 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1398 // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores
1399 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1400
1401 if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos)
1402 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1403 {
1404 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1405 OptionArgElement::eBareDash));
1406
1407 }
Chris Lattner24943d22010-06-08 16:52:24 +00001408}