blob: 7205cb600c84d4ddc4620dd9b0c3984253bba26b [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"
Greg Clayton3182eff2011-06-23 21:22:24 +000017#include "lldb/Core/FormatManager.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Stream.h"
19#include "lldb/Core/StreamFile.h"
20#include "lldb/Core/StreamString.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000021#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Interpreter/CommandReturnObject.h"
23
Chris Lattner24943d22010-06-08 16:52:24 +000024using namespace lldb;
25using namespace lldb_private;
26
Chris Lattner24943d22010-06-08 16:52:24 +000027//----------------------------------------------------------------------
28// Args constructor
29//----------------------------------------------------------------------
30Args::Args (const char *command) :
31 m_args(),
Greg Claytonb72d0f02011-04-12 05:54:46 +000032 m_argv(),
33 m_args_quote_char()
Chris Lattner24943d22010-06-08 16:52:24 +000034{
Greg Clayton928d1302010-12-19 03:41:24 +000035 if (command)
36 SetCommandString (command);
Chris Lattner24943d22010-06-08 16:52:24 +000037}
38
39
40Args::Args (const char *command, size_t len) :
41 m_args(),
Greg Claytonb72d0f02011-04-12 05:54:46 +000042 m_argv(),
43 m_args_quote_char()
Chris Lattner24943d22010-06-08 16:52:24 +000044{
Greg Clayton928d1302010-12-19 03:41:24 +000045 if (command && len)
46 SetCommandString (command, len);
Chris Lattner24943d22010-06-08 16:52:24 +000047}
48
Chris Lattner24943d22010-06-08 16:52:24 +000049//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000050// We have to be very careful on the copy constructor of this class
51// to make sure we copy all of the string values, but we can't copy the
52// rhs.m_argv into m_argv since it will point to the "const char *" c
53// strings in rhs.m_args. We need to copy the string list and update our
54// own m_argv appropriately.
55//----------------------------------------------------------------------
56Args::Args (const Args &rhs) :
57 m_args (rhs.m_args),
58 m_argv (),
59 m_args_quote_char(rhs.m_args_quote_char)
60{
61 UpdateArgvFromArgs();
62}
63
64//----------------------------------------------------------------------
65// We have to be very careful on the copy constructor of this class
66// to make sure we copy all of the string values, but we can't copy the
67// rhs.m_argv into m_argv since it will point to the "const char *" c
68// strings in rhs.m_args. We need to copy the string list and update our
69// own m_argv appropriately.
70//----------------------------------------------------------------------
71const Args &
72Args::operator= (const Args &rhs)
73{
74 // Make sure we aren't assigning to self
75 if (this != &rhs)
76 {
77 m_args = rhs.m_args;
78 m_args_quote_char = rhs.m_args_quote_char;
79 UpdateArgvFromArgs();
80 }
81 return *this;
82}
83
84//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000085// Destructor
86//----------------------------------------------------------------------
87Args::~Args ()
88{
89}
90
91void
92Args::Dump (Stream *s)
93{
94// int argc = GetArgumentCount();
95//
96// arg_sstr_collection::const_iterator pos, begin = m_args.begin(), end = m_args.end();
97// for (pos = m_args.begin(); pos != end; ++pos)
98// {
99// s->Indent();
100// s->Printf("args[%zu]=%s\n", std::distance(begin, pos), pos->c_str());
101// }
102// s->EOL();
103 const int argc = m_argv.size();
104 for (int i=0; i<argc; ++i)
105 {
106 s->Indent();
107 const char *arg_cstr = m_argv[i];
108 if (arg_cstr)
109 s->Printf("argv[%i]=\"%s\"\n", i, arg_cstr);
110 else
111 s->Printf("argv[%i]=NULL\n", i);
112 }
113 s->EOL();
114}
115
116bool
117Args::GetCommandString (std::string &command)
118{
119 command.clear();
120 int argc = GetArgumentCount();
121 for (int i=0; i<argc; ++i)
122 {
123 if (i > 0)
124 command += ' ';
125 command += m_argv[i];
126 }
127 return argc > 0;
128}
129
Caroline Ticed9105c22010-12-10 00:26:54 +0000130bool
131Args::GetQuotedCommandString (std::string &command)
132{
133 command.clear ();
Greg Clayton928d1302010-12-19 03:41:24 +0000134 size_t argc = GetArgumentCount ();
135 for (size_t i = 0; i < argc; ++i)
Caroline Ticed9105c22010-12-10 00:26:54 +0000136 {
137 if (i > 0)
Greg Clayton928d1302010-12-19 03:41:24 +0000138 command.append (1, ' ');
139 char quote_char = GetArgumentQuoteCharAtIndex(i);
140 if (quote_char)
Caroline Ticed9105c22010-12-10 00:26:54 +0000141 {
Greg Clayton928d1302010-12-19 03:41:24 +0000142 command.append (1, quote_char);
143 command.append (m_argv[i]);
144 command.append (1, quote_char);
Caroline Ticed9105c22010-12-10 00:26:54 +0000145 }
146 else
Greg Clayton928d1302010-12-19 03:41:24 +0000147 command.append (m_argv[i]);
Caroline Ticed9105c22010-12-10 00:26:54 +0000148 }
149 return argc > 0;
150}
151
Chris Lattner24943d22010-06-08 16:52:24 +0000152void
153Args::SetCommandString (const char *command, size_t len)
154{
155 // Use std::string to make sure we get a NULL terminated string we can use
156 // as "command" could point to a string within a large string....
157 std::string null_terminated_command(command, len);
158 SetCommandString(null_terminated_command.c_str());
159}
160
161void
162Args::SetCommandString (const char *command)
163{
164 m_args.clear();
165 m_argv.clear();
Greg Clayton928d1302010-12-19 03:41:24 +0000166 m_args_quote_char.clear();
167
Chris Lattner24943d22010-06-08 16:52:24 +0000168 if (command && command[0])
169 {
Greg Clayton928d1302010-12-19 03:41:24 +0000170 static const char *k_space_separators = " \t";
171 static const char *k_space_separators_with_slash_and_quotes = " \t \\'\"`";
172 const char *arg_end = NULL;
173 const char *arg_pos;
174 for (arg_pos = command;
175 arg_pos && arg_pos[0];
176 arg_pos = arg_end)
Chris Lattner24943d22010-06-08 16:52:24 +0000177 {
Greg Clayton928d1302010-12-19 03:41:24 +0000178 // Skip any leading space separators
179 const char *arg_start = ::strspn (arg_pos, k_space_separators) + arg_pos;
180
181 // If there were only space separators to the end of the line, then
Chris Lattner24943d22010-06-08 16:52:24 +0000182 // we're done.
183 if (*arg_start == '\0')
184 break;
185
Greg Clayton5d187e52011-01-08 20:28:42 +0000186 // Arguments can be split into multiple discontiguous pieces,
Greg Clayton928d1302010-12-19 03:41:24 +0000187 // for example:
188 // "Hello ""World"
189 // this would result in a single argument "Hello World" (without/
190 // the quotes) since the quotes would be removed and there is
191 // not space between the strings. So we need to keep track of the
192 // current start of each argument piece in "arg_piece_start"
193 const char *arg_piece_start = arg_start;
194 arg_pos = arg_piece_start;
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196 std::string arg;
Greg Clayton928d1302010-12-19 03:41:24 +0000197 // Since we can have multiple quotes that form a single command
198 // in a command like: "Hello "world'!' (which will make a single
199 // argument "Hello world!") we remember the first quote character
200 // we encounter and use that for the quote character.
201 char first_quote_char = '\0';
202 char quote_char = '\0';
203 bool arg_complete = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000204
Greg Clayton928d1302010-12-19 03:41:24 +0000205 do
Chris Lattner24943d22010-06-08 16:52:24 +0000206 {
Greg Clayton928d1302010-12-19 03:41:24 +0000207 arg_end = ::strcspn (arg_pos, k_space_separators_with_slash_and_quotes) + arg_pos;
208
209 switch (arg_end[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000210 {
Greg Clayton928d1302010-12-19 03:41:24 +0000211 default:
212 assert (!"Unhandled case statement, we must handle this...");
213 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000214
Greg Clayton928d1302010-12-19 03:41:24 +0000215 case '\0':
216 // End of C string
217 if (arg_piece_start && arg_piece_start[0])
218 arg.append (arg_piece_start);
219 arg_complete = true;
220 break;
221
222 case '\\':
223 // Backslash character
224 switch (arg_end[1])
Chris Lattner24943d22010-06-08 16:52:24 +0000225 {
Greg Clayton928d1302010-12-19 03:41:24 +0000226 case '\0':
227 arg.append (arg_piece_start);
228 arg_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000229 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000230
Greg Clayton928d1302010-12-19 03:41:24 +0000231 default:
232 arg_pos = arg_end + 2;
233 break;
234 }
235 break;
236
237 case '"':
238 case '\'':
239 case '`':
240 // Quote characters
241 if (quote_char)
242 {
243 // We found a quote character while inside a quoted
244 // character argument. If it matches our current quote
245 // character, this ends the effect of the quotes. If it
246 // doesn't we ignore it.
247 if (quote_char == arg_end[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000248 {
Greg Clayton928d1302010-12-19 03:41:24 +0000249 arg.append (arg_piece_start, arg_end - arg_piece_start);
250 // Clear the quote character and let parsing
251 // continue (we need to watch for things like:
252 // "Hello ""World"
253 // "Hello "World
254 // "Hello "'World'
255 // All of which will result in a single argument "Hello World"
256 quote_char = '\0'; // Note that we are no longer inside quotes
257 arg_pos = arg_end + 1; // Skip the quote character
258 arg_piece_start = arg_pos; // Note we are starting from later in the string
259 }
260 else
261 {
262 // different quote, skip it and keep going
263 arg_pos = arg_end + 1;
264 }
265 }
266 else
267 {
268 // We found the start of a quote scope.
Greg Clayton5d187e52011-01-08 20:28:42 +0000269 // Make sure there isn't a string that precedes
Greg Clayton928d1302010-12-19 03:41:24 +0000270 // the start of a quote scope like:
271 // Hello" World"
272 // If so, then add the "Hello" to the arg
273 if (arg_end > arg_piece_start)
274 arg.append (arg_piece_start, arg_end - arg_piece_start);
275
276 // Enter into a quote scope
277 quote_char = arg_end[0];
278
279 if (first_quote_char == '\0')
280 first_quote_char = quote_char;
281
282 arg_pos = arg_end;
283
284 if (quote_char != '`')
285 ++arg_pos; // Skip the quote character if it is not a backtick
286
287 arg_piece_start = arg_pos; // Note we are starting from later in the string
288
289 // Skip till the next quote character
290 const char *end_quote = ::strchr (arg_piece_start, quote_char);
291 while (end_quote && end_quote[-1] == '\\')
292 {
293 // Don't skip the quote character if it is
294 // preceded by a '\' character
295 end_quote = ::strchr (end_quote + 1, quote_char);
296 }
297
298 if (end_quote)
299 {
300 if (end_quote > arg_piece_start)
Chris Lattner24943d22010-06-08 16:52:24 +0000301 {
Greg Clayton928d1302010-12-19 03:41:24 +0000302 // Keep the backtick quote on commands
303 if (quote_char == '`')
304 arg.append (arg_piece_start, end_quote + 1 - arg_piece_start);
305 else
306 arg.append (arg_piece_start, end_quote - arg_piece_start);
307 }
308
309 // If the next character is a space or the end of
310 // string, this argument is complete...
311 if (end_quote[1] == ' ' || end_quote[1] == '\t' || end_quote[1] == '\0')
312 {
313 arg_complete = true;
314 arg_end = end_quote + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000315 }
316 else
317 {
Greg Clayton928d1302010-12-19 03:41:24 +0000318 arg_pos = end_quote + 1;
319 arg_piece_start = arg_pos;
Chris Lattner24943d22010-06-08 16:52:24 +0000320 }
Greg Clayton928d1302010-12-19 03:41:24 +0000321 quote_char = '\0';
Chris Lattner24943d22010-06-08 16:52:24 +0000322 }
323 }
Greg Clayton928d1302010-12-19 03:41:24 +0000324 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000325
Greg Clayton928d1302010-12-19 03:41:24 +0000326 case ' ':
327 case '\t':
328 if (quote_char)
Chris Lattner24943d22010-06-08 16:52:24 +0000329 {
Greg Clayton928d1302010-12-19 03:41:24 +0000330 // We are currently processing a quoted character and found
331 // a space character, skip any spaces and keep trying to find
332 // the end of the argument.
333 arg_pos = ::strspn (arg_end, k_space_separators) + arg_end;
Chris Lattner24943d22010-06-08 16:52:24 +0000334 }
Greg Clayton928d1302010-12-19 03:41:24 +0000335 else
Chris Lattner24943d22010-06-08 16:52:24 +0000336 {
Greg Clayton928d1302010-12-19 03:41:24 +0000337 // We are not inside any quotes, we just found a space after an
338 // argument
339 if (arg_end > arg_piece_start)
340 arg.append (arg_piece_start, arg_end - arg_piece_start);
341 arg_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000342 }
Greg Clayton928d1302010-12-19 03:41:24 +0000343 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000344 }
Greg Clayton928d1302010-12-19 03:41:24 +0000345 } while (!arg_complete);
Chris Lattner24943d22010-06-08 16:52:24 +0000346
347 m_args.push_back(arg);
Greg Clayton928d1302010-12-19 03:41:24 +0000348 m_args_quote_char.push_back (first_quote_char);
Chris Lattner24943d22010-06-08 16:52:24 +0000349 }
Greg Clayton928d1302010-12-19 03:41:24 +0000350 UpdateArgvFromArgs();
Chris Lattner24943d22010-06-08 16:52:24 +0000351 }
Chris Lattner24943d22010-06-08 16:52:24 +0000352}
353
354void
355Args::UpdateArgsAfterOptionParsing()
356{
357 // Now m_argv might be out of date with m_args, so we need to fix that
358 arg_cstr_collection::const_iterator argv_pos, argv_end = m_argv.end();
359 arg_sstr_collection::iterator args_pos;
360 arg_quote_char_collection::iterator quotes_pos;
361
362 for (argv_pos = m_argv.begin(), args_pos = m_args.begin(), quotes_pos = m_args_quote_char.begin();
363 argv_pos != argv_end && args_pos != m_args.end();
364 ++argv_pos)
365 {
366 const char *argv_cstr = *argv_pos;
367 if (argv_cstr == NULL)
368 break;
369
370 while (args_pos != m_args.end())
371 {
372 const char *args_cstr = args_pos->c_str();
373 if (args_cstr == argv_cstr)
374 {
375 // We found the argument that matches the C string in the
376 // vector, so we can now look for the next one
377 ++args_pos;
378 ++quotes_pos;
379 break;
380 }
381 else
382 {
383 quotes_pos = m_args_quote_char.erase (quotes_pos);
384 args_pos = m_args.erase (args_pos);
385 }
386 }
387 }
388
389 if (args_pos != m_args.end())
390 m_args.erase (args_pos, m_args.end());
391
392 if (quotes_pos != m_args_quote_char.end())
393 m_args_quote_char.erase (quotes_pos, m_args_quote_char.end());
394}
395
396void
397Args::UpdateArgvFromArgs()
398{
399 m_argv.clear();
400 arg_sstr_collection::const_iterator pos, end = m_args.end();
401 for (pos = m_args.begin(); pos != end; ++pos)
402 m_argv.push_back(pos->c_str());
403 m_argv.push_back(NULL);
Greg Clayton928d1302010-12-19 03:41:24 +0000404 // Make sure we have enough arg quote chars in the array
405 if (m_args_quote_char.size() < m_args.size())
406 m_args_quote_char.resize (m_argv.size());
Chris Lattner24943d22010-06-08 16:52:24 +0000407}
408
409size_t
410Args::GetArgumentCount() const
411{
412 if (m_argv.empty())
413 return 0;
414 return m_argv.size() - 1;
415}
416
417const char *
418Args::GetArgumentAtIndex (size_t idx) const
419{
420 if (idx < m_argv.size())
421 return m_argv[idx];
422 return NULL;
423}
424
425char
426Args::GetArgumentQuoteCharAtIndex (size_t idx) const
427{
428 if (idx < m_args_quote_char.size())
429 return m_args_quote_char[idx];
430 return '\0';
431}
432
433char **
434Args::GetArgumentVector()
435{
436 if (!m_argv.empty())
437 return (char **)&m_argv[0];
438 return NULL;
439}
440
441const char **
442Args::GetConstArgumentVector() const
443{
444 if (!m_argv.empty())
445 return (const char **)&m_argv[0];
446 return NULL;
447}
448
449void
450Args::Shift ()
451{
452 // Don't pop the last NULL terminator from the argv array
453 if (m_argv.size() > 1)
454 {
455 m_argv.erase(m_argv.begin());
456 m_args.pop_front();
Greg Clayton928d1302010-12-19 03:41:24 +0000457 if (!m_args_quote_char.empty())
458 m_args_quote_char.erase(m_args_quote_char.begin());
Chris Lattner24943d22010-06-08 16:52:24 +0000459 }
460}
461
462const char *
463Args::Unshift (const char *arg_cstr, char quote_char)
464{
465 m_args.push_front(arg_cstr);
466 m_argv.insert(m_argv.begin(), m_args.front().c_str());
467 m_args_quote_char.insert(m_args_quote_char.begin(), quote_char);
468 return GetArgumentAtIndex (0);
469}
470
471void
472Args::AppendArguments (const Args &rhs)
473{
474 const size_t rhs_argc = rhs.GetArgumentCount();
475 for (size_t i=0; i<rhs_argc; ++i)
476 AppendArgument(rhs.GetArgumentAtIndex(i));
477}
478
479const char *
480Args::AppendArgument (const char *arg_cstr, char quote_char)
481{
482 return InsertArgumentAtIndex (GetArgumentCount(), arg_cstr, quote_char);
483}
484
485const char *
486Args::InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
487{
488 // Since we are using a std::list to hold onto the copied C string and
489 // we don't have direct access to the elements, we have to iterate to
490 // find the value.
491 arg_sstr_collection::iterator pos, end = m_args.end();
492 size_t i = idx;
493 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
494 --i;
495
496 pos = m_args.insert(pos, arg_cstr);
497
Greg Clayton928d1302010-12-19 03:41:24 +0000498 if (idx >= m_args_quote_char.size())
499 {
500 m_args_quote_char.resize(idx + 1);
501 m_args_quote_char[idx] = quote_char;
502 }
503 else
504 m_args_quote_char.insert(m_args_quote_char.begin() + idx, quote_char);
Chris Lattner24943d22010-06-08 16:52:24 +0000505
506 UpdateArgvFromArgs();
507 return GetArgumentAtIndex(idx);
508}
509
510const char *
511Args::ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
512{
513 // Since we are using a std::list to hold onto the copied C string and
514 // we don't have direct access to the elements, we have to iterate to
515 // find the value.
516 arg_sstr_collection::iterator pos, end = m_args.end();
517 size_t i = idx;
518 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
519 --i;
520
521 if (pos != end)
522 {
523 pos->assign(arg_cstr);
524 assert(idx < m_argv.size() - 1);
525 m_argv[idx] = pos->c_str();
Greg Clayton928d1302010-12-19 03:41:24 +0000526 if (idx >= m_args_quote_char.size())
527 m_args_quote_char.resize(idx + 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000528 m_args_quote_char[idx] = quote_char;
529 return GetArgumentAtIndex(idx);
530 }
531 return NULL;
532}
533
534void
535Args::DeleteArgumentAtIndex (size_t idx)
536{
537 // Since we are using a std::list to hold onto the copied C string and
538 // we don't have direct access to the elements, we have to iterate to
539 // find the value.
540 arg_sstr_collection::iterator pos, end = m_args.end();
541 size_t i = idx;
542 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
543 --i;
544
545 if (pos != end)
546 {
547 m_args.erase (pos);
548 assert(idx < m_argv.size() - 1);
549 m_argv.erase(m_argv.begin() + idx);
Greg Clayton928d1302010-12-19 03:41:24 +0000550 if (idx < m_args_quote_char.size())
551 m_args_quote_char.erase(m_args_quote_char.begin() + idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000552 }
553}
554
555void
556Args::SetArguments (int argc, const char **argv)
557{
558 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
559 // no need to clear it here.
560 m_args.clear();
561 m_args_quote_char.clear();
562
563 // Make a copy of the arguments in our internal buffer
564 size_t i;
565 // First copy each string
566 for (i=0; i<argc; ++i)
567 {
568 m_args.push_back (argv[i]);
Greg Clayton928d1302010-12-19 03:41:24 +0000569 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
Chris Lattner24943d22010-06-08 16:52:24 +0000570 m_args_quote_char.push_back (argv[i][0]);
571 else
572 m_args_quote_char.push_back ('\0');
573 }
574
575 UpdateArgvFromArgs();
576}
577
578
579Error
580Args::ParseOptions (Options &options)
581{
582 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000583 Error error;
584 struct option *long_options = options.GetLongOptions();
585 if (long_options == NULL)
586 {
587 error.SetErrorStringWithFormat("Invalid long options.\n");
588 return error;
589 }
590
Greg Claytonbef15832010-07-14 00:18:15 +0000591 for (int i=0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000592 {
593 if (long_options[i].flag == NULL)
594 {
595 sstr << (char)long_options[i].val;
596 switch (long_options[i].has_arg)
597 {
598 default:
599 case no_argument: break;
600 case required_argument: sstr << ':'; break;
601 case optional_argument: sstr << "::"; break;
602 }
603 }
604 }
Eli Friedmanef2bc872010-06-13 19:18:49 +0000605#ifdef __GLIBC__
606 optind = 0;
607#else
Chris Lattner24943d22010-06-08 16:52:24 +0000608 optreset = 1;
609 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +0000610#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000611 int val;
612 while (1)
613 {
614 int long_options_index = -1;
615 val = ::getopt_long(GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
616 &long_options_index);
617 if (val == -1)
618 break;
619
620 // Did we get an error?
621 if (val == '?')
622 {
623 error.SetErrorStringWithFormat("Unknown or ambiguous option.\n");
624 break;
625 }
626 // The option auto-set itself
627 if (val == 0)
628 continue;
629
630 ((Options *) &options)->OptionSeen (val);
631
632 // Lookup the long option index
633 if (long_options_index == -1)
634 {
635 for (int i=0;
636 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
637 ++i)
638 {
639 if (long_options[i].val == val)
640 {
641 long_options_index = i;
642 break;
643 }
644 }
645 }
646 // Call the callback with the option
647 if (long_options_index >= 0)
648 {
649 error = options.SetOptionValue(long_options_index,
650 long_options[long_options_index].has_arg == no_argument ? NULL : optarg);
651 }
652 else
653 {
654 error.SetErrorStringWithFormat("Invalid option with value '%i'.\n", val);
655 }
656 if (error.Fail())
657 break;
658 }
659
660 // Update our ARGV now that get options has consumed all the options
661 m_argv.erase(m_argv.begin(), m_argv.begin() + optind);
662 UpdateArgsAfterOptionParsing ();
663 return error;
664}
665
666void
667Args::Clear ()
668{
669 m_args.clear ();
670 m_argv.clear ();
671 m_args_quote_char.clear();
672}
673
674int32_t
675Args::StringToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr)
676{
677 if (s && s[0])
678 {
679 char *end = NULL;
680 int32_t uval = ::strtol (s, &end, base);
681 if (*end == '\0')
682 {
683 if (success_ptr) *success_ptr = true;
684 return uval; // All characters were used, return the result
685 }
686 }
687 if (success_ptr) *success_ptr = false;
688 return fail_value;
689}
690
691uint32_t
692Args::StringToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr)
693{
694 if (s && s[0])
695 {
696 char *end = NULL;
697 uint32_t uval = ::strtoul (s, &end, base);
698 if (*end == '\0')
699 {
700 if (success_ptr) *success_ptr = true;
701 return uval; // All characters were used, return the result
702 }
703 }
704 if (success_ptr) *success_ptr = false;
705 return fail_value;
706}
707
708
709int64_t
710Args::StringToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr)
711{
712 if (s && s[0])
713 {
714 char *end = NULL;
715 int64_t uval = ::strtoll (s, &end, base);
716 if (*end == '\0')
717 {
718 if (success_ptr) *success_ptr = true;
719 return uval; // All characters were used, return the result
720 }
721 }
722 if (success_ptr) *success_ptr = false;
723 return fail_value;
724}
725
726uint64_t
727Args::StringToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr)
728{
729 if (s && s[0])
730 {
731 char *end = NULL;
732 uint64_t uval = ::strtoull (s, &end, base);
733 if (*end == '\0')
734 {
735 if (success_ptr) *success_ptr = true;
736 return uval; // All characters were used, return the result
737 }
738 }
739 if (success_ptr) *success_ptr = false;
740 return fail_value;
741}
742
743lldb::addr_t
744Args::StringToAddress (const char *s, lldb::addr_t fail_value, bool *success_ptr)
745{
746 if (s && s[0])
747 {
748 char *end = NULL;
749 lldb::addr_t addr = ::strtoull (s, &end, 0);
750 if (*end == '\0')
751 {
752 if (success_ptr) *success_ptr = true;
753 return addr; // All characters were used, return the result
754 }
755 // Try base 16 with no prefix...
756 addr = ::strtoull (s, &end, 16);
757 if (*end == '\0')
758 {
759 if (success_ptr) *success_ptr = true;
760 return addr; // All characters were used, return the result
761 }
762 }
763 if (success_ptr) *success_ptr = false;
764 return fail_value;
765}
766
767bool
768Args::StringToBoolean (const char *s, bool fail_value, bool *success_ptr)
769{
770 if (s && s[0])
771 {
772 if (::strcasecmp (s, "false") == 0 ||
773 ::strcasecmp (s, "off") == 0 ||
774 ::strcasecmp (s, "no") == 0 ||
775 ::strcmp (s, "0") == 0)
776 {
777 if (success_ptr)
778 *success_ptr = true;
779 return false;
780 }
781 else
782 if (::strcasecmp (s, "true") == 0 ||
783 ::strcasecmp (s, "on") == 0 ||
784 ::strcasecmp (s, "yes") == 0 ||
785 ::strcmp (s, "1") == 0)
786 {
787 if (success_ptr) *success_ptr = true;
788 return true;
789 }
790 }
791 if (success_ptr) *success_ptr = false;
792 return fail_value;
793}
794
Greg Claytonb1888f22011-03-19 01:12:21 +0000795const char *
796Args::StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update)
797{
798 major = UINT32_MAX;
799 minor = UINT32_MAX;
800 update = UINT32_MAX;
801
802 if (s && s[0])
803 {
804 char *pos = NULL;
805 uint32_t uval32;
806 uval32 = ::strtoul (s, &pos, 0);
807 if (pos == s)
808 return s;
809 major = uval32;
810 if (*pos == '\0')
811 {
812 return pos; // Decoded major and got end of string
813 }
814 else if (*pos == '.')
815 {
816 const char *minor_cstr = pos + 1;
817 uval32 = ::strtoul (minor_cstr, &pos, 0);
818 if (pos == minor_cstr)
819 return pos; // Didn't get any digits for the minor version...
820 minor = uval32;
821 if (*pos == '.')
822 {
823 const char *update_cstr = pos + 1;
824 uval32 = ::strtoul (update_cstr, &pos, 0);
825 if (pos == update_cstr)
826 return pos;
827 update = uval32;
828 }
829 return pos;
830 }
831 }
832 return 0;
833}
834
835
Chris Lattner24943d22010-06-08 16:52:24 +0000836int32_t
Greg Clayton61aca5d2011-10-07 18:58:12 +0000837Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000838{
Greg Clayton61aca5d2011-10-07 18:58:12 +0000839 if (enum_values)
Chris Lattner24943d22010-06-08 16:52:24 +0000840 {
Greg Clayton61aca5d2011-10-07 18:58:12 +0000841 if (s && s[0])
Chris Lattner24943d22010-06-08 16:52:24 +0000842 {
Greg Clayton61aca5d2011-10-07 18:58:12 +0000843 for (int i = 0; enum_values[i].string_value != NULL ; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000844 {
Greg Clayton61aca5d2011-10-07 18:58:12 +0000845 if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value)
846 {
847 error.Clear();
848 return enum_values[i].value;
849 }
Chris Lattner24943d22010-06-08 16:52:24 +0000850 }
851 }
Greg Clayton61aca5d2011-10-07 18:58:12 +0000852
853 StreamString strm;
854 strm.PutCString ("invalid enumeration value, valid values are: ");
855 for (int i = 0; enum_values[i].string_value != NULL; i++)
856 {
857 strm.Printf ("%s\"%s\"",
858 i > 0 ? ", " : "",
859 enum_values[i].string_value);
860 }
861 error.SetErrorString(strm.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000862 }
Greg Clayton61aca5d2011-10-07 18:58:12 +0000863 else
864 {
865 error.SetErrorString ("invalid enumeration argument");
866 }
Chris Lattner24943d22010-06-08 16:52:24 +0000867 return fail_value;
868}
869
870ScriptLanguage
871Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr)
872{
873 if (s && s[0])
874 {
875 if ((::strcasecmp (s, "python") == 0) ||
876 (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault))
877 {
878 if (success_ptr) *success_ptr = true;
879 return eScriptLanguagePython;
880 }
881 if (::strcasecmp (s, "none"))
882 {
883 if (success_ptr) *success_ptr = true;
884 return eScriptLanguageNone;
885 }
886 }
887 if (success_ptr) *success_ptr = false;
888 return fail_value;
889}
890
891Error
892Args::StringToFormat
893(
894 const char *s,
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000895 lldb::Format &format,
896 uint32_t *byte_size_ptr
Chris Lattner24943d22010-06-08 16:52:24 +0000897)
898{
899 format = eFormatInvalid;
900 Error error;
901
902 if (s && s[0])
903 {
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000904 if (byte_size_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000905 {
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000906 if (isdigit (s[0]))
907 {
908 char *format_char = NULL;
909 unsigned long byte_size = ::strtoul (s, &format_char, 0);
910 if (byte_size != ULONG_MAX)
911 *byte_size_ptr = byte_size;
912 s = format_char;
913 }
914 else
915 *byte_size_ptr = 0;
916 }
917
Greg Clayton3182eff2011-06-23 21:22:24 +0000918 const bool partial_match_ok = true;
919 if (!FormatManager::GetFormatFromCString (s, partial_match_ok, format))
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000920 {
Greg Clayton3182eff2011-06-23 21:22:24 +0000921 StreamString error_strm;
922 error_strm.Printf ("Invalid format character or name '%s'. Valid values are:\n", s);
Peter Collingbournef4d4fcc2011-06-24 01:12:22 +0000923 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000924 {
Greg Clayton3182eff2011-06-23 21:22:24 +0000925 char format_char = FormatManager::GetFormatAsFormatChar(f);
926 if (format_char)
927 error_strm.Printf ("'%c' or ", format_char);
928
929 error_strm.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
930 error_strm.EOL();
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000931 }
Greg Clayton3182eff2011-06-23 21:22:24 +0000932
933 if (byte_size_ptr)
934 error_strm.PutCString ("An optional byte size can precede the format character.\n");
935 error.SetErrorString(error_strm.GetString().c_str());
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000936 }
Chris Lattner24943d22010-06-08 16:52:24 +0000937
938 if (error.Fail())
939 return error;
940 }
941 else
942 {
943 error.SetErrorStringWithFormat("%s option string.\n", s ? "empty" : "invalid");
944 }
945 return error;
946}
947
948void
949Args::LongestCommonPrefix (std::string &common_prefix)
950{
951 arg_sstr_collection::iterator pos, end = m_args.end();
952 pos = m_args.begin();
953 if (pos == end)
954 common_prefix.clear();
955 else
956 common_prefix = (*pos);
957
958 for (++pos; pos != end; ++pos)
959 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000960 size_t new_size = (*pos).size();
Chris Lattner24943d22010-06-08 16:52:24 +0000961
962 // First trim common_prefix if it is longer than the current element:
963 if (common_prefix.size() > new_size)
964 common_prefix.erase (new_size);
965
966 // Then trim it at the first disparity:
967
Greg Clayton54e7afa2010-07-09 20:39:50 +0000968 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000969 {
970 if ((*pos)[i] != common_prefix[i])
971 {
972 common_prefix.erase(i);
973 break;
974 }
975 }
976
977 // If we've emptied the common prefix, we're done.
978 if (common_prefix.empty())
979 break;
980 }
981}
982
Caroline Tice44c841d2010-12-07 19:58:26 +0000983size_t
984Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index)
985{
986 char short_buffer[3];
987 char long_buffer[255];
988 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", (char) long_options[long_options_index].val);
989 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name);
990 size_t end = GetArgumentCount ();
991 size_t idx = 0;
992 while (idx < end)
993 {
994 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
995 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
996 {
997 return idx;
998 }
999 ++idx;
1000 }
1001
1002 return end;
1003}
1004
1005bool
1006Args::IsPositionalArgument (const char *arg)
1007{
1008 if (arg == NULL)
1009 return false;
1010
1011 bool is_positional = true;
1012 char *cptr = (char *) arg;
1013
1014 if (cptr[0] == '%')
1015 {
1016 ++cptr;
1017 while (isdigit (cptr[0]))
1018 ++cptr;
1019 if (cptr[0] != '\0')
1020 is_positional = false;
1021 }
1022 else
1023 is_positional = false;
1024
1025 return is_positional;
1026}
1027
Chris Lattner24943d22010-06-08 16:52:24 +00001028void
Caroline Tice5e0894e2010-10-12 17:45:19 +00001029Args::ParseAliasOptions (Options &options,
1030 CommandReturnObject &result,
Caroline Ticee0da7a52010-12-09 22:52:49 +00001031 OptionArgVector *option_arg_vector,
1032 std::string &raw_input_string)
Chris Lattner24943d22010-06-08 16:52:24 +00001033{
1034 StreamString sstr;
1035 int i;
1036 struct option *long_options = options.GetLongOptions();
1037
1038 if (long_options == NULL)
1039 {
1040 result.AppendError ("invalid long options");
1041 result.SetStatus (eReturnStatusFailed);
1042 return;
1043 }
1044
1045 for (i = 0; long_options[i].name != NULL; ++i)
1046 {
1047 if (long_options[i].flag == NULL)
1048 {
1049 sstr << (char) long_options[i].val;
1050 switch (long_options[i].has_arg)
1051 {
1052 default:
1053 case no_argument:
1054 break;
1055 case required_argument:
1056 sstr << ":";
1057 break;
1058 case optional_argument:
1059 sstr << "::";
1060 break;
1061 }
1062 }
1063 }
1064
Eli Friedmanef2bc872010-06-13 19:18:49 +00001065#ifdef __GLIBC__
1066 optind = 0;
1067#else
Chris Lattner24943d22010-06-08 16:52:24 +00001068 optreset = 1;
1069 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001070#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001071 int val;
1072 while (1)
1073 {
1074 int long_options_index = -1;
1075 val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
1076 &long_options_index);
1077
1078 if (val == -1)
1079 break;
1080
1081 if (val == '?')
1082 {
1083 result.AppendError ("unknown or ambiguous option");
1084 result.SetStatus (eReturnStatusFailed);
1085 break;
1086 }
1087
1088 if (val == 0)
1089 continue;
1090
1091 ((Options *) &options)->OptionSeen (val);
1092
1093 // Look up the long option index
1094 if (long_options_index == -1)
1095 {
1096 for (int j = 0;
1097 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1098 ++j)
1099 {
1100 if (long_options[j].val == val)
1101 {
1102 long_options_index = j;
1103 break;
1104 }
1105 }
1106 }
1107
1108 // See if the option takes an argument, and see if one was supplied.
1109 if (long_options_index >= 0)
1110 {
1111 StreamString option_str;
1112 option_str.Printf ("-%c", (char) val);
1113
1114 switch (long_options[long_options_index].has_arg)
1115 {
1116 case no_argument:
Caroline Tice44c841d2010-12-07 19:58:26 +00001117 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1118 OptionArgValue (no_argument, "<no-argument>")));
Caroline Tice2160c3f2010-09-12 04:48:45 +00001119 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001120 break;
1121 case required_argument:
1122 if (optarg != NULL)
1123 {
1124 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001125 OptionArgValue (required_argument,
1126 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001127 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1128 }
1129 else
1130 {
1131 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1132 option_str.GetData());
1133 result.SetStatus (eReturnStatusFailed);
1134 }
1135 break;
1136 case optional_argument:
1137 if (optarg != NULL)
1138 {
1139 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001140 OptionArgValue (optional_argument,
1141 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001142 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1143 }
1144 else
1145 {
1146 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001147 OptionArgValue (optional_argument, "<no-argument>")));
Chris Lattner24943d22010-06-08 16:52:24 +00001148 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1149 }
1150 break;
1151 default:
1152 result.AppendErrorWithFormat
1153 ("error with options table; invalid value in has_arg field for option '%c'.\n",
1154 (char) val);
1155 result.SetStatus (eReturnStatusFailed);
1156 break;
1157 }
1158 }
1159 else
1160 {
1161 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", (char) val);
1162 result.SetStatus (eReturnStatusFailed);
1163 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001164
1165 if (long_options_index >= 0)
1166 {
1167 // 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 +00001168 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1169 // the raw_input_string, if one was passed in.
Caroline Tice44c841d2010-12-07 19:58:26 +00001170 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1171 if (idx < GetArgumentCount())
1172 {
Caroline Ticee0da7a52010-12-09 22:52:49 +00001173 if (raw_input_string.size() > 0)
1174 {
1175 const char *tmp_arg = GetArgumentAtIndex (idx);
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, "");
1181 if ((long_options[long_options_index].has_arg != no_argument)
1182 && (optarg != NULL)
1183 && (idx+1 < GetArgumentCount())
1184 && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0))
Caroline Ticee0da7a52010-12-09 22:52:49 +00001185 {
1186 if (raw_input_string.size() > 0)
1187 {
1188 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1189 size_t pos = raw_input_string.find (tmp_arg);
1190 if (pos != std::string::npos)
1191 raw_input_string.erase (pos, strlen (tmp_arg));
1192 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001193 ReplaceArgumentAtIndex (idx+1, "");
Caroline Ticee0da7a52010-12-09 22:52:49 +00001194 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001195 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001196 }
1197
Chris Lattner24943d22010-06-08 16:52:24 +00001198 if (!result.Succeeded())
1199 break;
1200 }
1201}
1202
1203void
1204Args::ParseArgsForCompletion
1205(
1206 Options &options,
Jim Inghamadb84292010-06-24 20:31:04 +00001207 OptionElementVector &option_element_vector,
1208 uint32_t cursor_index
Chris Lattner24943d22010-06-08 16:52:24 +00001209)
1210{
1211 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +00001212 struct option *long_options = options.GetLongOptions();
1213 option_element_vector.clear();
1214
1215 if (long_options == NULL)
1216 {
1217 return;
1218 }
1219
1220 // Leading : tells getopt to return a : for a missing option argument AND
1221 // to suppress error messages.
1222
1223 sstr << ":";
Greg Claytonbef15832010-07-14 00:18:15 +00001224 for (int i = 0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001225 {
1226 if (long_options[i].flag == NULL)
1227 {
1228 sstr << (char) long_options[i].val;
1229 switch (long_options[i].has_arg)
1230 {
1231 default:
1232 case no_argument:
1233 break;
1234 case required_argument:
1235 sstr << ":";
1236 break;
1237 case optional_argument:
1238 sstr << "::";
1239 break;
1240 }
1241 }
1242 }
1243
Eli Friedmanef2bc872010-06-13 19:18:49 +00001244#ifdef __GLIBC__
1245 optind = 0;
1246#else
Chris Lattner24943d22010-06-08 16:52:24 +00001247 optreset = 1;
1248 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001249#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001250 opterr = 0;
1251
1252 int val;
1253 const OptionDefinition *opt_defs = options.GetDefinitions();
1254
Jim Inghamadb84292010-06-24 20:31:04 +00001255 // Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
Chris Lattner24943d22010-06-08 16:52:24 +00001256 // So we have to build another Arg and pass that to getopt_long so it doesn't
Jim Inghamadb84292010-06-24 20:31:04 +00001257 // change the one we have.
Chris Lattner24943d22010-06-08 16:52:24 +00001258
Greg Clayton54e7afa2010-07-09 20:39:50 +00001259 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner24943d22010-06-08 16:52:24 +00001260
Jim Inghamadb84292010-06-24 20:31:04 +00001261 bool failed_once = false;
1262 uint32_t dash_dash_pos = -1;
1263
Chris Lattner24943d22010-06-08 16:52:24 +00001264 while (1)
1265 {
1266 bool missing_argument = false;
1267 int parse_start = optind;
1268 int long_options_index = -1;
Jim Inghamadb84292010-06-24 20:31:04 +00001269
Greg Clayton54e7afa2010-07-09 20:39:50 +00001270 val = ::getopt_long (dummy_vec.size() - 1,
Greg Clayton53d68e72010-07-20 22:52:08 +00001271 (char *const *) &dummy_vec.front(),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001272 sstr.GetData(),
1273 long_options,
Chris Lattner24943d22010-06-08 16:52:24 +00001274 &long_options_index);
1275
1276 if (val == -1)
Jim Inghamadb84292010-06-24 20:31:04 +00001277 {
1278 // When we're completing a "--" which is the last option on line,
1279 if (failed_once)
1280 break;
1281
1282 failed_once = true;
1283
1284 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1285 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1286 // user might want to complete options by long name. I make this work by checking whether the
1287 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1288 // I let it pass to getopt_long which will terminate the option parsing.
1289 // Note, in either case we continue parsing the line so we can figure out what other options
1290 // were passed. This will be useful when we come to restricting completions based on what other
1291 // options we've seen on the line.
Chris Lattner24943d22010-06-08 16:52:24 +00001292
Jim Inghamadb84292010-06-24 20:31:04 +00001293 if (optind < dummy_vec.size() - 1
1294 && (strcmp (dummy_vec[optind-1], "--") == 0))
1295 {
1296 dash_dash_pos = optind - 1;
1297 if (optind - 1 == cursor_index)
1298 {
1299 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
1300 OptionArgElement::eBareDoubleDash));
1301 continue;
1302 }
1303 else
1304 break;
1305 }
1306 else
1307 break;
1308 }
Chris Lattner24943d22010-06-08 16:52:24 +00001309 else if (val == '?')
1310 {
Jim Inghamadb84292010-06-24 20:31:04 +00001311 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1312 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001313 continue;
1314 }
1315 else if (val == 0)
1316 {
1317 continue;
1318 }
1319 else if (val == ':')
1320 {
1321 // This is a missing argument.
1322 val = optopt;
1323 missing_argument = true;
1324 }
1325
1326 ((Options *) &options)->OptionSeen (val);
1327
1328 // Look up the long option index
1329 if (long_options_index == -1)
1330 {
1331 for (int j = 0;
1332 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1333 ++j)
1334 {
1335 if (long_options[j].val == val)
1336 {
1337 long_options_index = j;
1338 break;
1339 }
1340 }
1341 }
1342
1343 // See if the option takes an argument, and see if one was supplied.
1344 if (long_options_index >= 0)
1345 {
1346 int opt_defs_index = -1;
1347 for (int i = 0; ; i++)
1348 {
1349 if (opt_defs[i].short_option == 0)
1350 break;
1351 else if (opt_defs[i].short_option == val)
1352 {
1353 opt_defs_index = i;
1354 break;
1355 }
1356 }
1357
1358 switch (long_options[long_options_index].has_arg)
1359 {
1360 case no_argument:
1361 option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0));
1362 break;
1363 case required_argument:
1364 if (optarg != NULL)
1365 {
1366 int arg_index;
1367 if (missing_argument)
1368 arg_index = -1;
1369 else
Jim Inghamadb84292010-06-24 20:31:04 +00001370 arg_index = optind - 1;
Chris Lattner24943d22010-06-08 16:52:24 +00001371
Jim Inghamadb84292010-06-24 20:31:04 +00001372 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +00001373 }
1374 else
1375 {
Jim Inghamadb84292010-06-24 20:31:04 +00001376 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1));
Chris Lattner24943d22010-06-08 16:52:24 +00001377 }
1378 break;
1379 case optional_argument:
1380 if (optarg != NULL)
1381 {
Jim Inghamadb84292010-06-24 20:31:04 +00001382 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001383 }
1384 else
1385 {
Jim Inghamadb84292010-06-24 20:31:04 +00001386 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001387 }
1388 break;
1389 default:
1390 // The options table is messed up. Here we'll just continue
Jim Inghamadb84292010-06-24 20:31:04 +00001391 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1392 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001393 break;
1394 }
1395 }
1396 else
1397 {
Jim Inghamadb84292010-06-24 20:31:04 +00001398 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1399 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001400 }
1401 }
Jim Inghamadb84292010-06-24 20:31:04 +00001402
1403 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1404 // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores
1405 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1406
1407 if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos)
1408 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1409 {
1410 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1411 OptionArgElement::eBareDash));
1412
1413 }
Chris Lattner24943d22010-06-08 16:52:24 +00001414}