blob: 101a1c26a1564c138005d5bfbf1d145d5d1e1626 [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 Claytonb3448432011-03-24 21:19:54 +0000837Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, bool *success_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000838{
839 if (enum_values && s && s[0])
840 {
841 for (int i = 0; enum_values[i].string_value != NULL ; i++)
842 {
843 if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value)
844 {
845 if (success_ptr) *success_ptr = true;
846 return enum_values[i].value;
847 }
848 }
849 }
850 if (success_ptr) *success_ptr = false;
851
852 return fail_value;
853}
854
855ScriptLanguage
856Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr)
857{
858 if (s && s[0])
859 {
860 if ((::strcasecmp (s, "python") == 0) ||
861 (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault))
862 {
863 if (success_ptr) *success_ptr = true;
864 return eScriptLanguagePython;
865 }
866 if (::strcasecmp (s, "none"))
867 {
868 if (success_ptr) *success_ptr = true;
869 return eScriptLanguageNone;
870 }
871 }
872 if (success_ptr) *success_ptr = false;
873 return fail_value;
874}
875
876Error
877Args::StringToFormat
878(
879 const char *s,
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000880 lldb::Format &format,
881 uint32_t *byte_size_ptr
Chris Lattner24943d22010-06-08 16:52:24 +0000882)
883{
884 format = eFormatInvalid;
885 Error error;
886
887 if (s && s[0])
888 {
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000889 if (byte_size_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000890 {
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000891 if (isdigit (s[0]))
892 {
893 char *format_char = NULL;
894 unsigned long byte_size = ::strtoul (s, &format_char, 0);
895 if (byte_size != ULONG_MAX)
896 *byte_size_ptr = byte_size;
897 s = format_char;
898 }
899 else
900 *byte_size_ptr = 0;
901 }
902
Greg Clayton3182eff2011-06-23 21:22:24 +0000903 const bool partial_match_ok = true;
904 if (!FormatManager::GetFormatFromCString (s, partial_match_ok, format))
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000905 {
Greg Clayton3182eff2011-06-23 21:22:24 +0000906 StreamString error_strm;
907 error_strm.Printf ("Invalid format character or name '%s'. Valid values are:\n", s);
Peter Collingbournef4d4fcc2011-06-24 01:12:22 +0000908 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000909 {
Greg Clayton3182eff2011-06-23 21:22:24 +0000910 char format_char = FormatManager::GetFormatAsFormatChar(f);
911 if (format_char)
912 error_strm.Printf ("'%c' or ", format_char);
913
914 error_strm.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
915 error_strm.EOL();
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000916 }
Greg Clayton3182eff2011-06-23 21:22:24 +0000917
918 if (byte_size_ptr)
919 error_strm.PutCString ("An optional byte size can precede the format character.\n");
920 error.SetErrorString(error_strm.GetString().c_str());
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000921 }
Chris Lattner24943d22010-06-08 16:52:24 +0000922
923 if (error.Fail())
924 return error;
925 }
926 else
927 {
928 error.SetErrorStringWithFormat("%s option string.\n", s ? "empty" : "invalid");
929 }
930 return error;
931}
932
933void
934Args::LongestCommonPrefix (std::string &common_prefix)
935{
936 arg_sstr_collection::iterator pos, end = m_args.end();
937 pos = m_args.begin();
938 if (pos == end)
939 common_prefix.clear();
940 else
941 common_prefix = (*pos);
942
943 for (++pos; pos != end; ++pos)
944 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000945 size_t new_size = (*pos).size();
Chris Lattner24943d22010-06-08 16:52:24 +0000946
947 // First trim common_prefix if it is longer than the current element:
948 if (common_prefix.size() > new_size)
949 common_prefix.erase (new_size);
950
951 // Then trim it at the first disparity:
952
Greg Clayton54e7afa2010-07-09 20:39:50 +0000953 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000954 {
955 if ((*pos)[i] != common_prefix[i])
956 {
957 common_prefix.erase(i);
958 break;
959 }
960 }
961
962 // If we've emptied the common prefix, we're done.
963 if (common_prefix.empty())
964 break;
965 }
966}
967
Caroline Tice44c841d2010-12-07 19:58:26 +0000968size_t
969Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index)
970{
971 char short_buffer[3];
972 char long_buffer[255];
973 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", (char) long_options[long_options_index].val);
974 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name);
975 size_t end = GetArgumentCount ();
976 size_t idx = 0;
977 while (idx < end)
978 {
979 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
980 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
981 {
982 return idx;
983 }
984 ++idx;
985 }
986
987 return end;
988}
989
990bool
991Args::IsPositionalArgument (const char *arg)
992{
993 if (arg == NULL)
994 return false;
995
996 bool is_positional = true;
997 char *cptr = (char *) arg;
998
999 if (cptr[0] == '%')
1000 {
1001 ++cptr;
1002 while (isdigit (cptr[0]))
1003 ++cptr;
1004 if (cptr[0] != '\0')
1005 is_positional = false;
1006 }
1007 else
1008 is_positional = false;
1009
1010 return is_positional;
1011}
1012
Chris Lattner24943d22010-06-08 16:52:24 +00001013void
Caroline Tice5e0894e2010-10-12 17:45:19 +00001014Args::ParseAliasOptions (Options &options,
1015 CommandReturnObject &result,
Caroline Ticee0da7a52010-12-09 22:52:49 +00001016 OptionArgVector *option_arg_vector,
1017 std::string &raw_input_string)
Chris Lattner24943d22010-06-08 16:52:24 +00001018{
1019 StreamString sstr;
1020 int i;
1021 struct option *long_options = options.GetLongOptions();
1022
1023 if (long_options == NULL)
1024 {
1025 result.AppendError ("invalid long options");
1026 result.SetStatus (eReturnStatusFailed);
1027 return;
1028 }
1029
1030 for (i = 0; long_options[i].name != NULL; ++i)
1031 {
1032 if (long_options[i].flag == NULL)
1033 {
1034 sstr << (char) long_options[i].val;
1035 switch (long_options[i].has_arg)
1036 {
1037 default:
1038 case no_argument:
1039 break;
1040 case required_argument:
1041 sstr << ":";
1042 break;
1043 case optional_argument:
1044 sstr << "::";
1045 break;
1046 }
1047 }
1048 }
1049
Eli Friedmanef2bc872010-06-13 19:18:49 +00001050#ifdef __GLIBC__
1051 optind = 0;
1052#else
Chris Lattner24943d22010-06-08 16:52:24 +00001053 optreset = 1;
1054 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001055#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001056 int val;
1057 while (1)
1058 {
1059 int long_options_index = -1;
1060 val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
1061 &long_options_index);
1062
1063 if (val == -1)
1064 break;
1065
1066 if (val == '?')
1067 {
1068 result.AppendError ("unknown or ambiguous option");
1069 result.SetStatus (eReturnStatusFailed);
1070 break;
1071 }
1072
1073 if (val == 0)
1074 continue;
1075
1076 ((Options *) &options)->OptionSeen (val);
1077
1078 // Look up the long option index
1079 if (long_options_index == -1)
1080 {
1081 for (int j = 0;
1082 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1083 ++j)
1084 {
1085 if (long_options[j].val == val)
1086 {
1087 long_options_index = j;
1088 break;
1089 }
1090 }
1091 }
1092
1093 // See if the option takes an argument, and see if one was supplied.
1094 if (long_options_index >= 0)
1095 {
1096 StreamString option_str;
1097 option_str.Printf ("-%c", (char) val);
1098
1099 switch (long_options[long_options_index].has_arg)
1100 {
1101 case no_argument:
Caroline Tice44c841d2010-12-07 19:58:26 +00001102 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1103 OptionArgValue (no_argument, "<no-argument>")));
Caroline Tice2160c3f2010-09-12 04:48:45 +00001104 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001105 break;
1106 case required_argument:
1107 if (optarg != NULL)
1108 {
1109 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001110 OptionArgValue (required_argument,
1111 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001112 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1113 }
1114 else
1115 {
1116 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1117 option_str.GetData());
1118 result.SetStatus (eReturnStatusFailed);
1119 }
1120 break;
1121 case optional_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 (optional_argument,
1126 std::string (optarg))));
Chris Lattner24943d22010-06-08 16:52:24 +00001127 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1128 }
1129 else
1130 {
1131 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Tice44c841d2010-12-07 19:58:26 +00001132 OptionArgValue (optional_argument, "<no-argument>")));
Chris Lattner24943d22010-06-08 16:52:24 +00001133 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1134 }
1135 break;
1136 default:
1137 result.AppendErrorWithFormat
1138 ("error with options table; invalid value in has_arg field for option '%c'.\n",
1139 (char) val);
1140 result.SetStatus (eReturnStatusFailed);
1141 break;
1142 }
1143 }
1144 else
1145 {
1146 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", (char) val);
1147 result.SetStatus (eReturnStatusFailed);
1148 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001149
1150 if (long_options_index >= 0)
1151 {
1152 // 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 +00001153 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1154 // the raw_input_string, if one was passed in.
Caroline Tice44c841d2010-12-07 19:58:26 +00001155 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1156 if (idx < GetArgumentCount())
1157 {
Caroline Ticee0da7a52010-12-09 22:52:49 +00001158 if (raw_input_string.size() > 0)
1159 {
1160 const char *tmp_arg = GetArgumentAtIndex (idx);
1161 size_t pos = raw_input_string.find (tmp_arg);
1162 if (pos != std::string::npos)
1163 raw_input_string.erase (pos, strlen (tmp_arg));
1164 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001165 ReplaceArgumentAtIndex (idx, "");
1166 if ((long_options[long_options_index].has_arg != no_argument)
1167 && (optarg != NULL)
1168 && (idx+1 < GetArgumentCount())
1169 && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0))
Caroline Ticee0da7a52010-12-09 22:52:49 +00001170 {
1171 if (raw_input_string.size() > 0)
1172 {
1173 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1174 size_t pos = raw_input_string.find (tmp_arg);
1175 if (pos != std::string::npos)
1176 raw_input_string.erase (pos, strlen (tmp_arg));
1177 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001178 ReplaceArgumentAtIndex (idx+1, "");
Caroline Ticee0da7a52010-12-09 22:52:49 +00001179 }
Caroline Tice44c841d2010-12-07 19:58:26 +00001180 }
Caroline Tice5e0894e2010-10-12 17:45:19 +00001181 }
1182
Chris Lattner24943d22010-06-08 16:52:24 +00001183 if (!result.Succeeded())
1184 break;
1185 }
1186}
1187
1188void
1189Args::ParseArgsForCompletion
1190(
1191 Options &options,
Jim Inghamadb84292010-06-24 20:31:04 +00001192 OptionElementVector &option_element_vector,
1193 uint32_t cursor_index
Chris Lattner24943d22010-06-08 16:52:24 +00001194)
1195{
1196 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +00001197 struct option *long_options = options.GetLongOptions();
1198 option_element_vector.clear();
1199
1200 if (long_options == NULL)
1201 {
1202 return;
1203 }
1204
1205 // Leading : tells getopt to return a : for a missing option argument AND
1206 // to suppress error messages.
1207
1208 sstr << ":";
Greg Claytonbef15832010-07-14 00:18:15 +00001209 for (int i = 0; long_options[i].name != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001210 {
1211 if (long_options[i].flag == NULL)
1212 {
1213 sstr << (char) long_options[i].val;
1214 switch (long_options[i].has_arg)
1215 {
1216 default:
1217 case no_argument:
1218 break;
1219 case required_argument:
1220 sstr << ":";
1221 break;
1222 case optional_argument:
1223 sstr << "::";
1224 break;
1225 }
1226 }
1227 }
1228
Eli Friedmanef2bc872010-06-13 19:18:49 +00001229#ifdef __GLIBC__
1230 optind = 0;
1231#else
Chris Lattner24943d22010-06-08 16:52:24 +00001232 optreset = 1;
1233 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +00001234#endif
Chris Lattner24943d22010-06-08 16:52:24 +00001235 opterr = 0;
1236
1237 int val;
1238 const OptionDefinition *opt_defs = options.GetDefinitions();
1239
Jim Inghamadb84292010-06-24 20:31:04 +00001240 // Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
Chris Lattner24943d22010-06-08 16:52:24 +00001241 // So we have to build another Arg and pass that to getopt_long so it doesn't
Jim Inghamadb84292010-06-24 20:31:04 +00001242 // change the one we have.
Chris Lattner24943d22010-06-08 16:52:24 +00001243
Greg Clayton54e7afa2010-07-09 20:39:50 +00001244 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner24943d22010-06-08 16:52:24 +00001245
Jim Inghamadb84292010-06-24 20:31:04 +00001246 bool failed_once = false;
1247 uint32_t dash_dash_pos = -1;
1248
Chris Lattner24943d22010-06-08 16:52:24 +00001249 while (1)
1250 {
1251 bool missing_argument = false;
1252 int parse_start = optind;
1253 int long_options_index = -1;
Jim Inghamadb84292010-06-24 20:31:04 +00001254
Greg Clayton54e7afa2010-07-09 20:39:50 +00001255 val = ::getopt_long (dummy_vec.size() - 1,
Greg Clayton53d68e72010-07-20 22:52:08 +00001256 (char *const *) &dummy_vec.front(),
Greg Clayton54e7afa2010-07-09 20:39:50 +00001257 sstr.GetData(),
1258 long_options,
Chris Lattner24943d22010-06-08 16:52:24 +00001259 &long_options_index);
1260
1261 if (val == -1)
Jim Inghamadb84292010-06-24 20:31:04 +00001262 {
1263 // When we're completing a "--" which is the last option on line,
1264 if (failed_once)
1265 break;
1266
1267 failed_once = true;
1268
1269 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1270 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1271 // user might want to complete options by long name. I make this work by checking whether the
1272 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1273 // I let it pass to getopt_long which will terminate the option parsing.
1274 // Note, in either case we continue parsing the line so we can figure out what other options
1275 // were passed. This will be useful when we come to restricting completions based on what other
1276 // options we've seen on the line.
Chris Lattner24943d22010-06-08 16:52:24 +00001277
Jim Inghamadb84292010-06-24 20:31:04 +00001278 if (optind < dummy_vec.size() - 1
1279 && (strcmp (dummy_vec[optind-1], "--") == 0))
1280 {
1281 dash_dash_pos = optind - 1;
1282 if (optind - 1 == cursor_index)
1283 {
1284 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
1285 OptionArgElement::eBareDoubleDash));
1286 continue;
1287 }
1288 else
1289 break;
1290 }
1291 else
1292 break;
1293 }
Chris Lattner24943d22010-06-08 16:52:24 +00001294 else if (val == '?')
1295 {
Jim Inghamadb84292010-06-24 20:31:04 +00001296 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1297 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001298 continue;
1299 }
1300 else if (val == 0)
1301 {
1302 continue;
1303 }
1304 else if (val == ':')
1305 {
1306 // This is a missing argument.
1307 val = optopt;
1308 missing_argument = true;
1309 }
1310
1311 ((Options *) &options)->OptionSeen (val);
1312
1313 // Look up the long option index
1314 if (long_options_index == -1)
1315 {
1316 for (int j = 0;
1317 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1318 ++j)
1319 {
1320 if (long_options[j].val == val)
1321 {
1322 long_options_index = j;
1323 break;
1324 }
1325 }
1326 }
1327
1328 // See if the option takes an argument, and see if one was supplied.
1329 if (long_options_index >= 0)
1330 {
1331 int opt_defs_index = -1;
1332 for (int i = 0; ; i++)
1333 {
1334 if (opt_defs[i].short_option == 0)
1335 break;
1336 else if (opt_defs[i].short_option == val)
1337 {
1338 opt_defs_index = i;
1339 break;
1340 }
1341 }
1342
1343 switch (long_options[long_options_index].has_arg)
1344 {
1345 case no_argument:
1346 option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0));
1347 break;
1348 case required_argument:
1349 if (optarg != NULL)
1350 {
1351 int arg_index;
1352 if (missing_argument)
1353 arg_index = -1;
1354 else
Jim Inghamadb84292010-06-24 20:31:04 +00001355 arg_index = optind - 1;
Chris Lattner24943d22010-06-08 16:52:24 +00001356
Jim Inghamadb84292010-06-24 20:31:04 +00001357 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, arg_index));
Chris Lattner24943d22010-06-08 16:52:24 +00001358 }
1359 else
1360 {
Jim Inghamadb84292010-06-24 20:31:04 +00001361 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1));
Chris Lattner24943d22010-06-08 16:52:24 +00001362 }
1363 break;
1364 case optional_argument:
1365 if (optarg != NULL)
1366 {
Jim Inghamadb84292010-06-24 20:31:04 +00001367 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001368 }
1369 else
1370 {
Jim Inghamadb84292010-06-24 20:31:04 +00001371 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner24943d22010-06-08 16:52:24 +00001372 }
1373 break;
1374 default:
1375 // The options table is messed up. Here we'll just continue
Jim Inghamadb84292010-06-24 20:31:04 +00001376 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1377 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001378 break;
1379 }
1380 }
1381 else
1382 {
Jim Inghamadb84292010-06-24 20:31:04 +00001383 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1384 OptionArgElement::eUnrecognizedArg));
Chris Lattner24943d22010-06-08 16:52:24 +00001385 }
1386 }
Jim Inghamadb84292010-06-24 20:31:04 +00001387
1388 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1389 // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores
1390 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1391
1392 if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos)
1393 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1394 {
1395 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1396 OptionArgElement::eBareDash));
1397
1398 }
Chris Lattner24943d22010-06-08 16:52:24 +00001399}