blob: 95183a0686c197dd41fd6b72c5e0df86964543a1 [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,
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000879 lldb::Format &format,
880 uint32_t *byte_size_ptr
Chris Lattner24943d22010-06-08 16:52:24 +0000881)
882{
883 format = eFormatInvalid;
884 Error error;
885
886 if (s && s[0])
887 {
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000888 if (byte_size_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000889 {
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000890 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;
911 case 'c': format = eFormatChar; break;
912 case 'C': format = eFormatCharPrintable; break;
913 case 'o': format = eFormatOctal; break;
914 case 'O': format = eFormatOSType; break;
915 case 'i':
916 case 'd': format = eFormatDecimal; break;
917 case 'I': format = eFormatComplexInteger; break;
918 case 'u': format = eFormatUnsigned; break;
919 case 'x': format = eFormatHex; break;
920 case 'X': format = eFormatComplex; break;
921 case 'f':
922 case 'e':
923 case 'g': format = eFormatFloat; break;
924 case 'p': format = eFormatPointer; break;
925 case 's': format = eFormatCString; break;
926 default:
927 success = false;
928 break;
929 }
930 }
931 if (!success)
932 error.SetErrorStringWithFormat ("Invalid format specification '%s'. Valid values are:\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000933 " b - binary\n"
934 " B - boolean\n"
935 " c - char\n"
936 " C - printable char\n"
937 " d - signed decimal\n"
938 " e - float\n"
939 " f - float\n"
940 " g - float\n"
941 " i - signed decimal\n"
Greg Claytonff44ab42011-04-23 02:04:55 +0000942 " i - complex integer\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000943 " o - octal\n"
Greg Claytonff44ab42011-04-23 02:04:55 +0000944 " O - OSType\n"
945 " p - pointer\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000946 " s - c-string\n"
947 " u - unsigned decimal\n"
948 " x - hex\n"
Greg Claytonff44ab42011-04-23 02:04:55 +0000949 " X - complex float\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000950 " y - bytes\n"
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000951 " Y - bytes with ASCII\n%s",
952 s,
953 byte_size_ptr ? "An optional byte size can precede the format character.\n" : "");
954
Chris Lattner24943d22010-06-08 16:52:24 +0000955
956 if (error.Fail())
957 return error;
958 }
959 else
960 {
961 error.SetErrorStringWithFormat("%s option string.\n", s ? "empty" : "invalid");
962 }
963 return error;
964}
965
966void
967Args::LongestCommonPrefix (std::string &common_prefix)
968{
969 arg_sstr_collection::iterator pos, end = m_args.end();
970 pos = m_args.begin();
971 if (pos == end)
972 common_prefix.clear();
973 else
974 common_prefix = (*pos);
975
976 for (++pos; pos != end; ++pos)
977 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000978 size_t new_size = (*pos).size();
Chris Lattner24943d22010-06-08 16:52:24 +0000979
980 // First trim common_prefix if it is longer than the current element:
981 if (common_prefix.size() > new_size)
982 common_prefix.erase (new_size);
983
984 // Then trim it at the first disparity:
985
Greg Clayton54e7afa2010-07-09 20:39:50 +0000986 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000987 {
988 if ((*pos)[i] != common_prefix[i])
989 {
990 common_prefix.erase(i);
991 break;
992 }
993 }
994
995 // If we've emptied the common prefix, we're done.
996 if (common_prefix.empty())
997 break;
998 }
999}
1000
Caroline Tice44c841d2010-12-07 19:58:26 +00001001size_t
1002Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index)
1003{
1004 char short_buffer[3];
1005 char long_buffer[255];
1006 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", (char) long_options[long_options_index].val);
1007 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name);
1008 size_t end = GetArgumentCount ();
1009 size_t idx = 0;
1010 while (idx < end)
1011 {
1012 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
1013 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
1014 {
1015 return idx;
1016 }
1017 ++idx;
1018 }
1019
1020 return end;
1021}
1022
1023bool
1024Args::IsPositionalArgument (const char *arg)
1025{
1026 if (arg == NULL)
1027 return false;
1028
1029 bool is_positional = true;
1030 char *cptr = (char *) arg;
1031
1032 if (cptr[0] == '%')
1033 {
1034 ++cptr;
1035 while (isdigit (cptr[0]))
1036 ++cptr;
1037 if (cptr[0] != '\0')
1038 is_positional = false;
1039 }
1040 else
1041 is_positional = false;
1042
1043 return is_positional;
1044}
1045
Chris Lattner24943d22010-06-08 16:52:24 +00001046void
Caroline Tice5e0894e2010-10-12 17:45:19 +00001047Args::ParseAliasOptions (Options &options,
1048 CommandReturnObject &result,
Caroline Ticee0da7a52010-12-09 22:52:49 +00001049 OptionArgVector *option_arg_vector,
1050 std::string &raw_input_string)
Chris Lattner24943d22010-06-08 16:52:24 +00001051{
1052 StreamString sstr;
1053 int i;
1054 struct option *long_options = options.GetLongOptions();
1055
1056 if (long_options == NULL)
1057 {
1058 result.AppendError ("invalid long options");
1059 result.SetStatus (eReturnStatusFailed);
1060 return;
1061 }
1062
1063 for (i = 0; long_options[i].name != NULL; ++i)
1064 {
1065 if (long_options[i].flag == NULL)
1066 {
1067 sstr << (char) long_options[i].val;
1068 switch (long_options[i].has_arg)
1069 {
1070 default:
1071 case no_argument:
1072 break;
1073 case required_argument:
1074 sstr << ":";
1075 break;
1076 case optional_argument:
1077 sstr << "::";
1078 break;
1079 }
1080 }
1081 }
1082
Eli Friedmanef2bc872010-06-13 19:18:49 +00001083#ifdef __GLIBC__
1084 optind = 0;
1085#else
Chris Lattner24943d22010-06-08 16:52:24 +00001086 optreset = 1;
1087 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001088#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001089 int val;
1090 while (1)
1091 {
1092 int long_options_index = -1;
1093 val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
1094 &long_options_index);
1095
1096 if (val == -1)
1097 break;
1098
1099 if (val == '?')
1100 {
1101 result.AppendError ("unknown or ambiguous option");
1102 result.SetStatus (eReturnStatusFailed);
1103 break;
1104 }
1105
1106 if (val == 0)
1107 continue;
1108
1109 ((Options *) &options)->OptionSeen (val);
1110
1111 // Look up the long option index
1112 if (long_options_index == -1)
1113 {
1114 for (int j = 0;
1115 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1116 ++j)
1117 {
1118 if (long_options[j].val == val)
1119 {
1120 long_options_index = j;
1121 break;
1122 }
1123 }
1124 }
1125
1126 // See if the option takes an argument, and see if one was supplied.
1127 if (long_options_index >= 0)
1128 {
1129 StreamString option_str;
1130 option_str.Printf ("-%c", (char) val);
1131
1132 switch (long_options[long_options_index].has_arg)
1133 {
1134 case no_argument:
Caroline Tice44c841d2010-12-07 19:58:26 +00001135 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1136 OptionArgValue (no_argument, "<no-argument>")));
Caroline Tice2160c3f2010-09-12 04:48:45 +00001137 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001138 break;
1139 case required_argument:
1140 if (optarg != NULL)
1141 {
1142 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001143 OptionArgValue (required_argument,
1144 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001145 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1146 }
1147 else
1148 {
1149 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1150 option_str.GetData());
1151 result.SetStatus (eReturnStatusFailed);
1152 }
1153 break;
1154 case optional_argument:
1155 if (optarg != NULL)
1156 {
1157 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001158 OptionArgValue (optional_argument,
1159 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001160 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1161 }
1162 else
1163 {
1164 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001165 OptionArgValue (optional_argument, "<no-argument>")));
Chris Lattner24943d22010-06-08 16:52:24 +00001166 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1167 }
1168 break;
1169 default:
1170 result.AppendErrorWithFormat
1171 ("error with options table; invalid value in has_arg field for option '%c'.\n",
1172 (char) val);
1173 result.SetStatus (eReturnStatusFailed);
1174 break;
1175 }
1176 }
1177 else
1178 {
1179 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", (char) val);
1180 result.SetStatus (eReturnStatusFailed);
1181 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001182
1183 if (long_options_index >= 0)
1184 {
1185 // 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 +00001186 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1187 // the raw_input_string, if one was passed in.
Caroline Tice44c841d2010-12-07 19:58:26 +00001188 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1189 if (idx < GetArgumentCount())
1190 {
Caroline Ticee0da7a52010-12-09 22:52:49 +00001191 if (raw_input_string.size() > 0)
1192 {
1193 const char *tmp_arg = GetArgumentAtIndex (idx);
1194 size_t pos = raw_input_string.find (tmp_arg);
1195 if (pos != std::string::npos)
1196 raw_input_string.erase (pos, strlen (tmp_arg));
1197 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001198 ReplaceArgumentAtIndex (idx, "");
1199 if ((long_options[long_options_index].has_arg != no_argument)
1200 && (optarg != NULL)
1201 && (idx+1 < GetArgumentCount())
1202 && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0))
Caroline Ticee0da7a52010-12-09 22:52:49 +00001203 {
1204 if (raw_input_string.size() > 0)
1205 {
1206 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1207 size_t pos = raw_input_string.find (tmp_arg);
1208 if (pos != std::string::npos)
1209 raw_input_string.erase (pos, strlen (tmp_arg));
1210 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001211 ReplaceArgumentAtIndex (idx+1, "");
Caroline Ticee0da7a52010-12-09 22:52:49 +00001212 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001213 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001214 }
1215
Chris Lattner24943d22010-06-08 16:52:24 +00001216 if (!result.Succeeded())
1217 break;
1218 }
1219}
1220
1221void
1222Args::ParseArgsForCompletion
1223(
1224 Options &options,
Jim Inghamadb84292010-06-24 20:31:04 +00001225 OptionElementVector &option_element_vector,
1226 uint32_t cursor_index
Chris Lattner24943d22010-06-08 16:52:24 +00001227)
1228{
1229 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +00001230 struct option *long_options = options.GetLongOptions();
1231 option_element_vector.clear();
1232
1233 if (long_options == NULL)
1234 {
1235 return;
1236 }
1237
1238 // Leading : tells getopt to return a : for a missing option argument AND
1239 // to suppress error messages.
1240
1241 sstr << ":";
Greg Claytonbef15832010-07-14 00:18:15 +00001242 for (int i = 0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001243 {
1244 if (long_options[i].flag == NULL)
1245 {
1246 sstr << (char) long_options[i].val;
1247 switch (long_options[i].has_arg)
1248 {
1249 default:
1250 case no_argument:
1251 break;
1252 case required_argument:
1253 sstr << ":";
1254 break;
1255 case optional_argument:
1256 sstr << "::";
1257 break;
1258 }
1259 }
1260 }
1261
Eli Friedmanef2bc872010-06-13 19:18:49 +00001262#ifdef __GLIBC__
1263 optind = 0;
1264#else
Chris Lattner24943d22010-06-08 16:52:24 +00001265 optreset = 1;
1266 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001267#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001268 opterr = 0;
1269
1270 int val;
1271 const OptionDefinition *opt_defs = options.GetDefinitions();
1272
Jim Inghamadb84292010-06-24 20:31:04 +00001273 // Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
Chris Lattner24943d22010-06-08 16:52:24 +00001274 // So we have to build another Arg and pass that to getopt_long so it doesn't
Jim Inghamadb84292010-06-24 20:31:04 +00001275 // change the one we have.
Chris Lattner24943d22010-06-08 16:52:24 +00001276
Greg Clayton54e7afa2010-07-09 20:39:50 +00001277 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner24943d22010-06-08 16:52:24 +00001278
Jim Inghamadb84292010-06-24 20:31:04 +00001279 bool failed_once = false;
1280 uint32_t dash_dash_pos = -1;
1281
Chris Lattner24943d22010-06-08 16:52:24 +00001282 while (1)
1283 {
1284 bool missing_argument = false;
1285 int parse_start = optind;
1286 int long_options_index = -1;
Jim Inghamadb84292010-06-24 20:31:04 +00001287
Greg Clayton54e7afa2010-07-09 20:39:50 +00001288 val = ::getopt_long (dummy_vec.size() - 1,
Greg Clayton53d68e72010-07-20 22:52:08 +00001289 (char *const *) &dummy_vec.front(),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001290 sstr.GetData(),
1291 long_options,
Chris Lattner24943d22010-06-08 16:52:24 +00001292 &long_options_index);
1293
1294 if (val == -1)
Jim Inghamadb84292010-06-24 20:31:04 +00001295 {
1296 // When we're completing a "--" which is the last option on line,
1297 if (failed_once)
1298 break;
1299
1300 failed_once = true;
1301
1302 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1303 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1304 // user might want to complete options by long name. I make this work by checking whether the
1305 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1306 // I let it pass to getopt_long which will terminate the option parsing.
1307 // Note, in either case we continue parsing the line so we can figure out what other options
1308 // were passed. This will be useful when we come to restricting completions based on what other
1309 // options we've seen on the line.
Chris Lattner24943d22010-06-08 16:52:24 +00001310
Jim Inghamadb84292010-06-24 20:31:04 +00001311 if (optind < dummy_vec.size() - 1
1312 && (strcmp (dummy_vec[optind-1], "--") == 0))
1313 {
1314 dash_dash_pos = optind - 1;
1315 if (optind - 1 == cursor_index)
1316 {
1317 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
1318 OptionArgElement::eBareDoubleDash));
1319 continue;
1320 }
1321 else
1322 break;
1323 }
1324 else
1325 break;
1326 }
Chris Lattner24943d22010-06-08 16:52:24 +00001327 else if (val == '?')
1328 {
Jim Inghamadb84292010-06-24 20:31:04 +00001329 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1330 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001331 continue;
1332 }
1333 else if (val == 0)
1334 {
1335 continue;
1336 }
1337 else if (val == ':')
1338 {
1339 // This is a missing argument.
1340 val = optopt;
1341 missing_argument = true;
1342 }
1343
1344 ((Options *) &options)->OptionSeen (val);
1345
1346 // Look up the long option index
1347 if (long_options_index == -1)
1348 {
1349 for (int j = 0;
1350 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1351 ++j)
1352 {
1353 if (long_options[j].val == val)
1354 {
1355 long_options_index = j;
1356 break;
1357 }
1358 }
1359 }
1360
1361 // See if the option takes an argument, and see if one was supplied.
1362 if (long_options_index >= 0)
1363 {
1364 int opt_defs_index = -1;
1365 for (int i = 0; ; i++)
1366 {
1367 if (opt_defs[i].short_option == 0)
1368 break;
1369 else if (opt_defs[i].short_option == val)
1370 {
1371 opt_defs_index = i;
1372 break;
1373 }
1374 }
1375
1376 switch (long_options[long_options_index].has_arg)
1377 {
1378 case no_argument:
1379 option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0));
1380 break;
1381 case required_argument:
1382 if (optarg != NULL)
1383 {
1384 int arg_index;
1385 if (missing_argument)
1386 arg_index = -1;
1387 else
Jim Inghamadb84292010-06-24 20:31:04 +00001388 arg_index = optind - 1;
Chris Lattner24943d22010-06-08 16:52:24 +00001389
Jim Inghamadb84292010-06-24 20:31:04 +00001390 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +00001391 }
1392 else
1393 {
Jim Inghamadb84292010-06-24 20:31:04 +00001394 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1));
Chris Lattner24943d22010-06-08 16:52:24 +00001395 }
1396 break;
1397 case optional_argument:
1398 if (optarg != NULL)
1399 {
Jim Inghamadb84292010-06-24 20:31:04 +00001400 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001401 }
1402 else
1403 {
Jim Inghamadb84292010-06-24 20:31:04 +00001404 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001405 }
1406 break;
1407 default:
1408 // The options table is messed up. Here we'll just continue
Jim Inghamadb84292010-06-24 20:31:04 +00001409 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1410 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001411 break;
1412 }
1413 }
1414 else
1415 {
Jim Inghamadb84292010-06-24 20:31:04 +00001416 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1417 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001418 }
1419 }
Jim Inghamadb84292010-06-24 20:31:04 +00001420
1421 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1422 // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores
1423 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1424
1425 if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos)
1426 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1427 {
1428 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1429 OptionArgElement::eBareDash));
1430
1431 }
Chris Lattner24943d22010-06-08 16:52:24 +00001432}