blob: 13ecd116adc6abbda5d9f6ff40689a39f218eda0 [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;
896 case 'i':
897 case 'd': format = eFormatDecimal; break;
898 case 'u': format = eFormatUnsigned; break;
899 case 'x': format = eFormatHex; break;
900 case 'f':
901 case 'e':
902 case 'g': format = eFormatFloat; break;
903 case 'p': format = eFormatPointer; break;
904 case 's': format = eFormatCString; break;
905 default:
906 error.SetErrorStringWithFormat("Invalid format character '%c'. Valid values are:\n"
907 " b - binary\n"
908 " B - boolean\n"
909 " c - char\n"
910 " C - printable char\n"
911 " d - signed decimal\n"
912 " e - float\n"
913 " f - float\n"
914 " g - float\n"
915 " i - signed decimal\n"
916 " o - octal\n"
917 " s - c-string\n"
918 " u - unsigned decimal\n"
919 " x - hex\n"
920 " y - bytes\n"
921 " Y - bytes with ASCII\n", s[0]);
922 break;
923 }
924
925 if (error.Fail())
926 return error;
927 }
928 else
929 {
930 error.SetErrorStringWithFormat("%s option string.\n", s ? "empty" : "invalid");
931 }
932 return error;
933}
934
935void
936Args::LongestCommonPrefix (std::string &common_prefix)
937{
938 arg_sstr_collection::iterator pos, end = m_args.end();
939 pos = m_args.begin();
940 if (pos == end)
941 common_prefix.clear();
942 else
943 common_prefix = (*pos);
944
945 for (++pos; pos != end; ++pos)
946 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000947 size_t new_size = (*pos).size();
Chris Lattner24943d22010-06-08 16:52:24 +0000948
949 // First trim common_prefix if it is longer than the current element:
950 if (common_prefix.size() > new_size)
951 common_prefix.erase (new_size);
952
953 // Then trim it at the first disparity:
954
Greg Clayton54e7afa2010-07-09 20:39:50 +0000955 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000956 {
957 if ((*pos)[i] != common_prefix[i])
958 {
959 common_prefix.erase(i);
960 break;
961 }
962 }
963
964 // If we've emptied the common prefix, we're done.
965 if (common_prefix.empty())
966 break;
967 }
968}
969
Caroline Tice44c841d2010-12-07 19:58:26 +0000970size_t
971Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index)
972{
973 char short_buffer[3];
974 char long_buffer[255];
975 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", (char) long_options[long_options_index].val);
976 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name);
977 size_t end = GetArgumentCount ();
978 size_t idx = 0;
979 while (idx < end)
980 {
981 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
982 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
983 {
984 return idx;
985 }
986 ++idx;
987 }
988
989 return end;
990}
991
992bool
993Args::IsPositionalArgument (const char *arg)
994{
995 if (arg == NULL)
996 return false;
997
998 bool is_positional = true;
999 char *cptr = (char *) arg;
1000
1001 if (cptr[0] == '%')
1002 {
1003 ++cptr;
1004 while (isdigit (cptr[0]))
1005 ++cptr;
1006 if (cptr[0] != '\0')
1007 is_positional = false;
1008 }
1009 else
1010 is_positional = false;
1011
1012 return is_positional;
1013}
1014
Chris Lattner24943d22010-06-08 16:52:24 +00001015void
Caroline Tice5e0894e2010-10-12 17:45:19 +00001016Args::ParseAliasOptions (Options &options,
1017 CommandReturnObject &result,
Caroline Ticee0da7a52010-12-09 22:52:49 +00001018 OptionArgVector *option_arg_vector,
1019 std::string &raw_input_string)
Chris Lattner24943d22010-06-08 16:52:24 +00001020{
1021 StreamString sstr;
1022 int i;
1023 struct option *long_options = options.GetLongOptions();
1024
1025 if (long_options == NULL)
1026 {
1027 result.AppendError ("invalid long options");
1028 result.SetStatus (eReturnStatusFailed);
1029 return;
1030 }
1031
1032 for (i = 0; long_options[i].name != NULL; ++i)
1033 {
1034 if (long_options[i].flag == NULL)
1035 {
1036 sstr << (char) long_options[i].val;
1037 switch (long_options[i].has_arg)
1038 {
1039 default:
1040 case no_argument:
1041 break;
1042 case required_argument:
1043 sstr << ":";
1044 break;
1045 case optional_argument:
1046 sstr << "::";
1047 break;
1048 }
1049 }
1050 }
1051
Eli Friedmanef2bc872010-06-13 19:18:49 +00001052#ifdef __GLIBC__
1053 optind = 0;
1054#else
Chris Lattner24943d22010-06-08 16:52:24 +00001055 optreset = 1;
1056 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001057#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001058 int val;
1059 while (1)
1060 {
1061 int long_options_index = -1;
1062 val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
1063 &long_options_index);
1064
1065 if (val == -1)
1066 break;
1067
1068 if (val == '?')
1069 {
1070 result.AppendError ("unknown or ambiguous option");
1071 result.SetStatus (eReturnStatusFailed);
1072 break;
1073 }
1074
1075 if (val == 0)
1076 continue;
1077
1078 ((Options *) &options)->OptionSeen (val);
1079
1080 // Look up the long option index
1081 if (long_options_index == -1)
1082 {
1083 for (int j = 0;
1084 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1085 ++j)
1086 {
1087 if (long_options[j].val == val)
1088 {
1089 long_options_index = j;
1090 break;
1091 }
1092 }
1093 }
1094
1095 // See if the option takes an argument, and see if one was supplied.
1096 if (long_options_index >= 0)
1097 {
1098 StreamString option_str;
1099 option_str.Printf ("-%c", (char) val);
1100
1101 switch (long_options[long_options_index].has_arg)
1102 {
1103 case no_argument:
Caroline Tice44c841d2010-12-07 19:58:26 +00001104 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1105 OptionArgValue (no_argument, "<no-argument>")));
Caroline Tice2160c3f2010-09-12 04:48:45 +00001106 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001107 break;
1108 case required_argument:
1109 if (optarg != NULL)
1110 {
1111 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001112 OptionArgValue (required_argument,
1113 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001114 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1115 }
1116 else
1117 {
1118 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1119 option_str.GetData());
1120 result.SetStatus (eReturnStatusFailed);
1121 }
1122 break;
1123 case optional_argument:
1124 if (optarg != NULL)
1125 {
1126 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001127 OptionArgValue (optional_argument,
1128 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001129 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1130 }
1131 else
1132 {
1133 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001134 OptionArgValue (optional_argument, "<no-argument>")));
Chris Lattner24943d22010-06-08 16:52:24 +00001135 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1136 }
1137 break;
1138 default:
1139 result.AppendErrorWithFormat
1140 ("error with options table; invalid value in has_arg field for option '%c'.\n",
1141 (char) val);
1142 result.SetStatus (eReturnStatusFailed);
1143 break;
1144 }
1145 }
1146 else
1147 {
1148 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", (char) val);
1149 result.SetStatus (eReturnStatusFailed);
1150 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001151
1152 if (long_options_index >= 0)
1153 {
1154 // 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 +00001155 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1156 // the raw_input_string, if one was passed in.
Caroline Tice44c841d2010-12-07 19:58:26 +00001157 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1158 if (idx < GetArgumentCount())
1159 {
Caroline Ticee0da7a52010-12-09 22:52:49 +00001160 if (raw_input_string.size() > 0)
1161 {
1162 const char *tmp_arg = GetArgumentAtIndex (idx);
1163 size_t pos = raw_input_string.find (tmp_arg);
1164 if (pos != std::string::npos)
1165 raw_input_string.erase (pos, strlen (tmp_arg));
1166 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001167 ReplaceArgumentAtIndex (idx, "");
1168 if ((long_options[long_options_index].has_arg != no_argument)
1169 && (optarg != NULL)
1170 && (idx+1 < GetArgumentCount())
1171 && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0))
Caroline Ticee0da7a52010-12-09 22:52:49 +00001172 {
1173 if (raw_input_string.size() > 0)
1174 {
1175 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1176 size_t pos = raw_input_string.find (tmp_arg);
1177 if (pos != std::string::npos)
1178 raw_input_string.erase (pos, strlen (tmp_arg));
1179 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001180 ReplaceArgumentAtIndex (idx+1, "");
Caroline Ticee0da7a52010-12-09 22:52:49 +00001181 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001182 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001183 }
1184
Chris Lattner24943d22010-06-08 16:52:24 +00001185 if (!result.Succeeded())
1186 break;
1187 }
1188}
1189
1190void
1191Args::ParseArgsForCompletion
1192(
1193 Options &options,
Jim Inghamadb84292010-06-24 20:31:04 +00001194 OptionElementVector &option_element_vector,
1195 uint32_t cursor_index
Chris Lattner24943d22010-06-08 16:52:24 +00001196)
1197{
1198 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +00001199 struct option *long_options = options.GetLongOptions();
1200 option_element_vector.clear();
1201
1202 if (long_options == NULL)
1203 {
1204 return;
1205 }
1206
1207 // Leading : tells getopt to return a : for a missing option argument AND
1208 // to suppress error messages.
1209
1210 sstr << ":";
Greg Claytonbef15832010-07-14 00:18:15 +00001211 for (int i = 0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001212 {
1213 if (long_options[i].flag == NULL)
1214 {
1215 sstr << (char) long_options[i].val;
1216 switch (long_options[i].has_arg)
1217 {
1218 default:
1219 case no_argument:
1220 break;
1221 case required_argument:
1222 sstr << ":";
1223 break;
1224 case optional_argument:
1225 sstr << "::";
1226 break;
1227 }
1228 }
1229 }
1230
Eli Friedmanef2bc872010-06-13 19:18:49 +00001231#ifdef __GLIBC__
1232 optind = 0;
1233#else
Chris Lattner24943d22010-06-08 16:52:24 +00001234 optreset = 1;
1235 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001236#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001237 opterr = 0;
1238
1239 int val;
1240 const OptionDefinition *opt_defs = options.GetDefinitions();
1241
Jim Inghamadb84292010-06-24 20:31:04 +00001242 // Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
Chris Lattner24943d22010-06-08 16:52:24 +00001243 // So we have to build another Arg and pass that to getopt_long so it doesn't
Jim Inghamadb84292010-06-24 20:31:04 +00001244 // change the one we have.
Chris Lattner24943d22010-06-08 16:52:24 +00001245
Greg Clayton54e7afa2010-07-09 20:39:50 +00001246 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner24943d22010-06-08 16:52:24 +00001247
Jim Inghamadb84292010-06-24 20:31:04 +00001248 bool failed_once = false;
1249 uint32_t dash_dash_pos = -1;
1250
Chris Lattner24943d22010-06-08 16:52:24 +00001251 while (1)
1252 {
1253 bool missing_argument = false;
1254 int parse_start = optind;
1255 int long_options_index = -1;
Jim Inghamadb84292010-06-24 20:31:04 +00001256
Greg Clayton54e7afa2010-07-09 20:39:50 +00001257 val = ::getopt_long (dummy_vec.size() - 1,
Greg Clayton53d68e72010-07-20 22:52:08 +00001258 (char *const *) &dummy_vec.front(),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001259 sstr.GetData(),
1260 long_options,
Chris Lattner24943d22010-06-08 16:52:24 +00001261 &long_options_index);
1262
1263 if (val == -1)
Jim Inghamadb84292010-06-24 20:31:04 +00001264 {
1265 // When we're completing a "--" which is the last option on line,
1266 if (failed_once)
1267 break;
1268
1269 failed_once = true;
1270
1271 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1272 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1273 // user might want to complete options by long name. I make this work by checking whether the
1274 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1275 // I let it pass to getopt_long which will terminate the option parsing.
1276 // Note, in either case we continue parsing the line so we can figure out what other options
1277 // were passed. This will be useful when we come to restricting completions based on what other
1278 // options we've seen on the line.
Chris Lattner24943d22010-06-08 16:52:24 +00001279
Jim Inghamadb84292010-06-24 20:31:04 +00001280 if (optind < dummy_vec.size() - 1
1281 && (strcmp (dummy_vec[optind-1], "--") == 0))
1282 {
1283 dash_dash_pos = optind - 1;
1284 if (optind - 1 == cursor_index)
1285 {
1286 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
1287 OptionArgElement::eBareDoubleDash));
1288 continue;
1289 }
1290 else
1291 break;
1292 }
1293 else
1294 break;
1295 }
Chris Lattner24943d22010-06-08 16:52:24 +00001296 else if (val == '?')
1297 {
Jim Inghamadb84292010-06-24 20:31:04 +00001298 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1299 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001300 continue;
1301 }
1302 else if (val == 0)
1303 {
1304 continue;
1305 }
1306 else if (val == ':')
1307 {
1308 // This is a missing argument.
1309 val = optopt;
1310 missing_argument = true;
1311 }
1312
1313 ((Options *) &options)->OptionSeen (val);
1314
1315 // Look up the long option index
1316 if (long_options_index == -1)
1317 {
1318 for (int j = 0;
1319 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1320 ++j)
1321 {
1322 if (long_options[j].val == val)
1323 {
1324 long_options_index = j;
1325 break;
1326 }
1327 }
1328 }
1329
1330 // See if the option takes an argument, and see if one was supplied.
1331 if (long_options_index >= 0)
1332 {
1333 int opt_defs_index = -1;
1334 for (int i = 0; ; i++)
1335 {
1336 if (opt_defs[i].short_option == 0)
1337 break;
1338 else if (opt_defs[i].short_option == val)
1339 {
1340 opt_defs_index = i;
1341 break;
1342 }
1343 }
1344
1345 switch (long_options[long_options_index].has_arg)
1346 {
1347 case no_argument:
1348 option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0));
1349 break;
1350 case required_argument:
1351 if (optarg != NULL)
1352 {
1353 int arg_index;
1354 if (missing_argument)
1355 arg_index = -1;
1356 else
Jim Inghamadb84292010-06-24 20:31:04 +00001357 arg_index = optind - 1;
Chris Lattner24943d22010-06-08 16:52:24 +00001358
Jim Inghamadb84292010-06-24 20:31:04 +00001359 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +00001360 }
1361 else
1362 {
Jim Inghamadb84292010-06-24 20:31:04 +00001363 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1));
Chris Lattner24943d22010-06-08 16:52:24 +00001364 }
1365 break;
1366 case optional_argument:
1367 if (optarg != NULL)
1368 {
Jim Inghamadb84292010-06-24 20:31:04 +00001369 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001370 }
1371 else
1372 {
Jim Inghamadb84292010-06-24 20:31:04 +00001373 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001374 }
1375 break;
1376 default:
1377 // The options table is messed up. Here we'll just continue
Jim Inghamadb84292010-06-24 20:31:04 +00001378 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1379 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001380 break;
1381 }
1382 }
1383 else
1384 {
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 }
1388 }
Jim Inghamadb84292010-06-24 20:31:04 +00001389
1390 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1391 // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores
1392 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1393
1394 if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos)
1395 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1396 {
1397 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1398 OptionArgElement::eBareDash));
1399
1400 }
Chris Lattner24943d22010-06-08 16:52:24 +00001401}