blob: 93933802f57cc4ca66a4bb100ecc7f33894b4449 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012// C Includes
Eli Friedman5661f922010-06-09 10:59:23 +000013#include <cstdlib>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014// C++ Includes
15// Other libraries and framework includes
16// Project includes
Jim Ingham40af72e2010-06-15 19:49:27 +000017#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Stream.h"
19#include "lldb/Core/StreamFile.h"
20#include "lldb/Core/StreamString.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000021#include "lldb/DataFormatters/FormatManager.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000022#include "lldb/Interpreter/Options.h"
Zachary Turnerd37221d2014-07-09 16:31:49 +000023#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytonb9d5df52012-12-06 22:49:16 +000025#include "lldb/Target/Process.h"
26//#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000027#include "lldb/Target/StackFrame.h"
Greg Claytonb9d5df52012-12-06 22:49:16 +000028#include "lldb/Target/Target.h"
29//#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031using namespace lldb;
32using namespace lldb_private;
33
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034//----------------------------------------------------------------------
35// Args constructor
36//----------------------------------------------------------------------
37Args::Args (const char *command) :
38 m_args(),
Greg Clayton8b82f082011-04-12 05:54:46 +000039 m_argv(),
40 m_args_quote_char()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
Greg Clayton6ad07dd2010-12-19 03:41:24 +000042 if (command)
43 SetCommandString (command);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044}
45
46
47Args::Args (const char *command, size_t len) :
48 m_args(),
Greg Clayton8b82f082011-04-12 05:54:46 +000049 m_argv(),
50 m_args_quote_char()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051{
Greg Clayton6ad07dd2010-12-19 03:41:24 +000052 if (command && len)
53 SetCommandString (command, len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000057// We have to be very careful on the copy constructor of this class
58// to make sure we copy all of the string values, but we can't copy the
59// rhs.m_argv into m_argv since it will point to the "const char *" c
60// strings in rhs.m_args. We need to copy the string list and update our
61// own m_argv appropriately.
62//----------------------------------------------------------------------
63Args::Args (const Args &rhs) :
64 m_args (rhs.m_args),
65 m_argv (),
66 m_args_quote_char(rhs.m_args_quote_char)
67{
68 UpdateArgvFromArgs();
69}
70
71//----------------------------------------------------------------------
72// We have to be very careful on the copy constructor of this class
73// to make sure we copy all of the string values, but we can't copy the
74// rhs.m_argv into m_argv since it will point to the "const char *" c
75// strings in rhs.m_args. We need to copy the string list and update our
76// own m_argv appropriately.
77//----------------------------------------------------------------------
78const Args &
79Args::operator= (const Args &rhs)
80{
81 // Make sure we aren't assigning to self
82 if (this != &rhs)
83 {
84 m_args = rhs.m_args;
85 m_args_quote_char = rhs.m_args_quote_char;
86 UpdateArgvFromArgs();
87 }
88 return *this;
89}
90
91//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092// Destructor
93//----------------------------------------------------------------------
94Args::~Args ()
95{
96}
97
98void
99Args::Dump (Stream *s)
100{
Greg Claytonc7bece562013-01-25 18:06:21 +0000101 const size_t argc = m_argv.size();
102 for (size_t i=0; i<argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 {
104 s->Indent();
105 const char *arg_cstr = m_argv[i];
106 if (arg_cstr)
Greg Claytonc7bece562013-01-25 18:06:21 +0000107 s->Printf("argv[%zi]=\"%s\"\n", i, arg_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 else
Greg Claytonc7bece562013-01-25 18:06:21 +0000109 s->Printf("argv[%zi]=NULL\n", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 }
111 s->EOL();
112}
113
114bool
Greg Claytonda91b172012-04-25 22:30:32 +0000115Args::GetCommandString (std::string &command) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116{
117 command.clear();
Greg Claytonc7bece562013-01-25 18:06:21 +0000118 const size_t argc = GetArgumentCount();
119 for (size_t i=0; i<argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 {
121 if (i > 0)
122 command += ' ';
123 command += m_argv[i];
124 }
125 return argc > 0;
126}
127
Caroline Tice2d5289d2010-12-10 00:26:54 +0000128bool
Greg Claytonda91b172012-04-25 22:30:32 +0000129Args::GetQuotedCommandString (std::string &command) const
Caroline Tice2d5289d2010-12-10 00:26:54 +0000130{
131 command.clear ();
Greg Claytonc7bece562013-01-25 18:06:21 +0000132 const size_t argc = GetArgumentCount();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000133 for (size_t i = 0; i < argc; ++i)
Caroline Tice2d5289d2010-12-10 00:26:54 +0000134 {
135 if (i > 0)
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000136 command.append (1, ' ');
137 char quote_char = GetArgumentQuoteCharAtIndex(i);
138 if (quote_char)
Caroline Tice2d5289d2010-12-10 00:26:54 +0000139 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000140 command.append (1, quote_char);
141 command.append (m_argv[i]);
142 command.append (1, quote_char);
Caroline Tice2d5289d2010-12-10 00:26:54 +0000143 }
144 else
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000145 command.append (m_argv[i]);
Caroline Tice2d5289d2010-12-10 00:26:54 +0000146 }
147 return argc > 0;
148}
149
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150void
151Args::SetCommandString (const char *command, size_t len)
152{
153 // Use std::string to make sure we get a NULL terminated string we can use
154 // as "command" could point to a string within a large string....
155 std::string null_terminated_command(command, len);
156 SetCommandString(null_terminated_command.c_str());
157}
158
159void
160Args::SetCommandString (const char *command)
161{
162 m_args.clear();
163 m_argv.clear();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000164 m_args_quote_char.clear();
165
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 if (command && command[0])
167 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000168 static const char *k_space_separators = " \t";
Johnny Chena28b89c2012-01-19 19:22:41 +0000169 static const char *k_space_separators_with_slash_and_quotes = " \t \\'\"";
Ed Masted78c9572014-04-20 00:31:37 +0000170 const char *arg_end = nullptr;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000171 const char *arg_pos;
172 for (arg_pos = command;
173 arg_pos && arg_pos[0];
174 arg_pos = arg_end)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000176 // Skip any leading space separators
177 const char *arg_start = ::strspn (arg_pos, k_space_separators) + arg_pos;
178
179 // If there were only space separators to the end of the line, then
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 // we're done.
181 if (*arg_start == '\0')
182 break;
183
Greg Clayton710dd5a2011-01-08 20:28:42 +0000184 // Arguments can be split into multiple discontiguous pieces,
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000185 // for example:
186 // "Hello ""World"
187 // this would result in a single argument "Hello World" (without/
188 // the quotes) since the quotes would be removed and there is
189 // not space between the strings. So we need to keep track of the
190 // current start of each argument piece in "arg_piece_start"
191 const char *arg_piece_start = arg_start;
192 arg_pos = arg_piece_start;
193
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 std::string arg;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000195 // Since we can have multiple quotes that form a single command
196 // in a command like: "Hello "world'!' (which will make a single
197 // argument "Hello world!") we remember the first quote character
198 // we encounter and use that for the quote character.
199 char first_quote_char = '\0';
200 char quote_char = '\0';
201 bool arg_complete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000203 do
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000205 arg_end = ::strcspn (arg_pos, k_space_separators_with_slash_and_quotes) + arg_pos;
206
207 switch (arg_end[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000209 default:
210 assert (!"Unhandled case statement, we must handle this...");
211 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000213 case '\0':
214 // End of C string
215 if (arg_piece_start && arg_piece_start[0])
216 arg.append (arg_piece_start);
217 arg_complete = true;
218 break;
219
220 case '\\':
221 // Backslash character
222 switch (arg_end[1])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000224 case '\0':
225 arg.append (arg_piece_start);
Greg Clayton0c943132012-03-15 17:10:48 +0000226 ++arg_end;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000227 arg_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000230 default:
Jim Inghama39fb7a2012-07-21 00:12:58 +0000231 if (quote_char == '\0')
232 {
233 arg.append (arg_piece_start, arg_end - arg_piece_start);
Adrian Prantl5eeaf742013-06-18 18:24:04 +0000234 if (arg_end[1] != '\0')
Jim Inghama39fb7a2012-07-21 00:12:58 +0000235 {
236 arg.append (arg_end + 1, 1);
237 arg_pos = arg_end + 2;
238 arg_piece_start = arg_pos;
239 }
240 }
241 else
242 arg_pos = arg_end + 2;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000243 break;
244 }
245 break;
246
247 case '"':
248 case '\'':
249 case '`':
250 // Quote characters
251 if (quote_char)
252 {
253 // We found a quote character while inside a quoted
254 // character argument. If it matches our current quote
255 // character, this ends the effect of the quotes. If it
256 // doesn't we ignore it.
257 if (quote_char == arg_end[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000259 arg.append (arg_piece_start, arg_end - arg_piece_start);
260 // Clear the quote character and let parsing
261 // continue (we need to watch for things like:
262 // "Hello ""World"
263 // "Hello "World
264 // "Hello "'World'
265 // All of which will result in a single argument "Hello World"
266 quote_char = '\0'; // Note that we are no longer inside quotes
267 arg_pos = arg_end + 1; // Skip the quote character
268 arg_piece_start = arg_pos; // Note we are starting from later in the string
269 }
270 else
271 {
272 // different quote, skip it and keep going
273 arg_pos = arg_end + 1;
274 }
275 }
276 else
277 {
278 // We found the start of a quote scope.
Greg Clayton710dd5a2011-01-08 20:28:42 +0000279 // Make sure there isn't a string that precedes
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000280 // the start of a quote scope like:
281 // Hello" World"
282 // If so, then add the "Hello" to the arg
283 if (arg_end > arg_piece_start)
284 arg.append (arg_piece_start, arg_end - arg_piece_start);
285
286 // Enter into a quote scope
287 quote_char = arg_end[0];
288
289 if (first_quote_char == '\0')
290 first_quote_char = quote_char;
291
292 arg_pos = arg_end;
Johnny Chena28b89c2012-01-19 19:22:41 +0000293 ++arg_pos; // Skip the quote character
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000294 arg_piece_start = arg_pos; // Note we are starting from later in the string
295
296 // Skip till the next quote character
297 const char *end_quote = ::strchr (arg_piece_start, quote_char);
298 while (end_quote && end_quote[-1] == '\\')
299 {
300 // Don't skip the quote character if it is
301 // preceded by a '\' character
302 end_quote = ::strchr (end_quote + 1, quote_char);
303 }
304
305 if (end_quote)
306 {
307 if (end_quote > arg_piece_start)
Johnny Chena28b89c2012-01-19 19:22:41 +0000308 arg.append (arg_piece_start, end_quote - arg_piece_start);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000309
310 // If the next character is a space or the end of
311 // string, this argument is complete...
312 if (end_quote[1] == ' ' || end_quote[1] == '\t' || end_quote[1] == '\0')
313 {
314 arg_complete = true;
315 arg_end = end_quote + 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 }
317 else
318 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000319 arg_pos = end_quote + 1;
320 arg_piece_start = arg_pos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000322 quote_char = '\0';
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 }
Greg Clayton0c943132012-03-15 17:10:48 +0000324 else
325 {
326 // Consume the rest of the string as there was no terminating quote
327 arg.append(arg_piece_start);
328 arg_end = arg_piece_start + strlen(arg_piece_start);
329 arg_complete = true;
330 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000332 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000334 case ' ':
335 case '\t':
336 if (quote_char)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000338 // We are currently processing a quoted character and found
339 // a space character, skip any spaces and keep trying to find
340 // the end of the argument.
341 arg_pos = ::strspn (arg_end, k_space_separators) + arg_end;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000343 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000345 // We are not inside any quotes, we just found a space after an
346 // argument
347 if (arg_end > arg_piece_start)
348 arg.append (arg_piece_start, arg_end - arg_piece_start);
349 arg_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000351 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000353 } while (!arg_complete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354
355 m_args.push_back(arg);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000356 m_args_quote_char.push_back (first_quote_char);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000358 UpdateArgvFromArgs();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360}
361
362void
363Args::UpdateArgsAfterOptionParsing()
364{
365 // Now m_argv might be out of date with m_args, so we need to fix that
366 arg_cstr_collection::const_iterator argv_pos, argv_end = m_argv.end();
367 arg_sstr_collection::iterator args_pos;
368 arg_quote_char_collection::iterator quotes_pos;
369
370 for (argv_pos = m_argv.begin(), args_pos = m_args.begin(), quotes_pos = m_args_quote_char.begin();
371 argv_pos != argv_end && args_pos != m_args.end();
372 ++argv_pos)
373 {
374 const char *argv_cstr = *argv_pos;
Ed Masted78c9572014-04-20 00:31:37 +0000375 if (argv_cstr == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 break;
377
378 while (args_pos != m_args.end())
379 {
380 const char *args_cstr = args_pos->c_str();
381 if (args_cstr == argv_cstr)
382 {
383 // We found the argument that matches the C string in the
384 // vector, so we can now look for the next one
385 ++args_pos;
386 ++quotes_pos;
387 break;
388 }
389 else
390 {
391 quotes_pos = m_args_quote_char.erase (quotes_pos);
392 args_pos = m_args.erase (args_pos);
393 }
394 }
395 }
396
397 if (args_pos != m_args.end())
398 m_args.erase (args_pos, m_args.end());
399
400 if (quotes_pos != m_args_quote_char.end())
401 m_args_quote_char.erase (quotes_pos, m_args_quote_char.end());
402}
403
404void
405Args::UpdateArgvFromArgs()
406{
407 m_argv.clear();
408 arg_sstr_collection::const_iterator pos, end = m_args.end();
409 for (pos = m_args.begin(); pos != end; ++pos)
410 m_argv.push_back(pos->c_str());
Ed Masted78c9572014-04-20 00:31:37 +0000411 m_argv.push_back(nullptr);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000412 // Make sure we have enough arg quote chars in the array
413 if (m_args_quote_char.size() < m_args.size())
414 m_args_quote_char.resize (m_argv.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
417size_t
418Args::GetArgumentCount() const
419{
420 if (m_argv.empty())
421 return 0;
422 return m_argv.size() - 1;
423}
424
425const char *
426Args::GetArgumentAtIndex (size_t idx) const
427{
428 if (idx < m_argv.size())
429 return m_argv[idx];
Ed Masted78c9572014-04-20 00:31:37 +0000430 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
433char
434Args::GetArgumentQuoteCharAtIndex (size_t idx) const
435{
436 if (idx < m_args_quote_char.size())
437 return m_args_quote_char[idx];
438 return '\0';
439}
440
441char **
442Args::GetArgumentVector()
443{
444 if (!m_argv.empty())
445 return (char **)&m_argv[0];
Ed Masted78c9572014-04-20 00:31:37 +0000446 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
449const char **
450Args::GetConstArgumentVector() const
451{
452 if (!m_argv.empty())
453 return (const char **)&m_argv[0];
Ed Masted78c9572014-04-20 00:31:37 +0000454 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455}
456
457void
458Args::Shift ()
459{
460 // Don't pop the last NULL terminator from the argv array
461 if (m_argv.size() > 1)
462 {
463 m_argv.erase(m_argv.begin());
464 m_args.pop_front();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000465 if (!m_args_quote_char.empty())
466 m_args_quote_char.erase(m_args_quote_char.begin());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 }
468}
469
470const char *
471Args::Unshift (const char *arg_cstr, char quote_char)
472{
473 m_args.push_front(arg_cstr);
474 m_argv.insert(m_argv.begin(), m_args.front().c_str());
475 m_args_quote_char.insert(m_args_quote_char.begin(), quote_char);
476 return GetArgumentAtIndex (0);
477}
478
479void
480Args::AppendArguments (const Args &rhs)
481{
482 const size_t rhs_argc = rhs.GetArgumentCount();
483 for (size_t i=0; i<rhs_argc; ++i)
484 AppendArgument(rhs.GetArgumentAtIndex(i));
485}
486
Greg Clayton982c9762011-11-03 21:22:33 +0000487void
488Args::AppendArguments (const char **argv)
489{
490 if (argv)
491 {
492 for (uint32_t i=0; argv[i]; ++i)
493 AppendArgument(argv[i]);
494 }
495}
496
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497const char *
498Args::AppendArgument (const char *arg_cstr, char quote_char)
499{
500 return InsertArgumentAtIndex (GetArgumentCount(), arg_cstr, quote_char);
501}
502
503const char *
504Args::InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
505{
506 // Since we are using a std::list to hold onto the copied C string and
507 // we don't have direct access to the elements, we have to iterate to
508 // find the value.
509 arg_sstr_collection::iterator pos, end = m_args.end();
510 size_t i = idx;
511 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
512 --i;
513
514 pos = m_args.insert(pos, arg_cstr);
515
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000516 if (idx >= m_args_quote_char.size())
517 {
518 m_args_quote_char.resize(idx + 1);
519 m_args_quote_char[idx] = quote_char;
520 }
521 else
522 m_args_quote_char.insert(m_args_quote_char.begin() + idx, quote_char);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523
524 UpdateArgvFromArgs();
525 return GetArgumentAtIndex(idx);
526}
527
528const char *
529Args::ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
530{
531 // Since we are using a std::list to hold onto the copied C string and
532 // we don't have direct access to the elements, we have to iterate to
533 // find the value.
534 arg_sstr_collection::iterator pos, end = m_args.end();
535 size_t i = idx;
536 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
537 --i;
538
539 if (pos != end)
540 {
541 pos->assign(arg_cstr);
542 assert(idx < m_argv.size() - 1);
543 m_argv[idx] = pos->c_str();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000544 if (idx >= m_args_quote_char.size())
545 m_args_quote_char.resize(idx + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 m_args_quote_char[idx] = quote_char;
547 return GetArgumentAtIndex(idx);
548 }
Ed Masted78c9572014-04-20 00:31:37 +0000549 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550}
551
552void
553Args::DeleteArgumentAtIndex (size_t idx)
554{
555 // Since we are using a std::list to hold onto the copied C string and
556 // we don't have direct access to the elements, we have to iterate to
557 // find the value.
558 arg_sstr_collection::iterator pos, end = m_args.end();
559 size_t i = idx;
560 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
561 --i;
562
563 if (pos != end)
564 {
565 m_args.erase (pos);
566 assert(idx < m_argv.size() - 1);
567 m_argv.erase(m_argv.begin() + idx);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000568 if (idx < m_args_quote_char.size())
569 m_args_quote_char.erase(m_args_quote_char.begin() + idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 }
571}
572
573void
Greg Claytonc7bece562013-01-25 18:06:21 +0000574Args::SetArguments (size_t argc, const char **argv)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575{
576 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
577 // no need to clear it here.
578 m_args.clear();
579 m_args_quote_char.clear();
580
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 // First copy each string
Greg Clayton982c9762011-11-03 21:22:33 +0000582 for (size_t i=0; i<argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 {
584 m_args.push_back (argv[i]);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000585 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 m_args_quote_char.push_back (argv[i][0]);
587 else
588 m_args_quote_char.push_back ('\0');
589 }
590
591 UpdateArgvFromArgs();
592}
593
Greg Clayton982c9762011-11-03 21:22:33 +0000594void
595Args::SetArguments (const char **argv)
596{
597 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
598 // no need to clear it here.
599 m_args.clear();
600 m_args_quote_char.clear();
601
602 if (argv)
603 {
604 // First copy each string
605 for (size_t i=0; argv[i]; ++i)
606 {
607 m_args.push_back (argv[i]);
608 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
609 m_args_quote_char.push_back (argv[i][0]);
610 else
611 m_args_quote_char.push_back ('\0');
612 }
613 }
614
615 UpdateArgvFromArgs();
616}
617
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618
619Error
620Args::ParseOptions (Options &options)
621{
622 StreamString sstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 Error error;
Virgile Belloe2607b52013-09-05 16:42:23 +0000624 Option *long_options = options.GetLongOptions();
Ed Masted78c9572014-04-20 00:31:37 +0000625 if (long_options == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000627 error.SetErrorStringWithFormat("invalid long options");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 return error;
629 }
630
Zachary Turnerd37221d2014-07-09 16:31:49 +0000631 for (int i=0; long_options[i].definition != nullptr; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 {
Ed Masted78c9572014-04-20 00:31:37 +0000633 if (long_options[i].flag == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 {
Daniel Malea90b0c842012-12-05 20:24:57 +0000635 if (isprint8(long_options[i].val))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000637 sstr << (char)long_options[i].val;
Zachary Turnerd37221d2014-07-09 16:31:49 +0000638 switch (long_options[i].definition->option_has_arg)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000639 {
640 default:
Virgile Belloe2607b52013-09-05 16:42:23 +0000641 case OptionParser::eNoArgument: break;
642 case OptionParser::eRequiredArgument: sstr << ':'; break;
643 case OptionParser::eOptionalArgument: sstr << "::"; break;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000644 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645 }
646 }
647 }
Virgile Belloe2607b52013-09-05 16:42:23 +0000648 OptionParser::Prepare();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 int val;
650 while (1)
651 {
652 int long_options_index = -1;
Virgile Belloe2607b52013-09-05 16:42:23 +0000653 val = OptionParser::Parse(GetArgumentCount(),
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000654 GetArgumentVector(),
655 sstr.GetData(),
656 long_options,
657 &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 if (val == -1)
659 break;
660
661 // Did we get an error?
662 if (val == '?')
663 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000664 error.SetErrorStringWithFormat("unknown or ambiguous option");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 break;
666 }
667 // The option auto-set itself
668 if (val == 0)
669 continue;
670
671 ((Options *) &options)->OptionSeen (val);
672
673 // Lookup the long option index
674 if (long_options_index == -1)
675 {
676 for (int i=0;
Zachary Turnerd37221d2014-07-09 16:31:49 +0000677 long_options[i].definition || long_options[i].flag || long_options[i].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678 ++i)
679 {
680 if (long_options[i].val == val)
681 {
682 long_options_index = i;
683 break;
684 }
685 }
686 }
687 // Call the callback with the option
Jason Molenda320e7b32014-10-16 01:26:51 +0000688 if (long_options_index >= 0 && long_options[long_options_index].definition)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 {
Zachary Turnerd37221d2014-07-09 16:31:49 +0000690 const OptionDefinition *def = long_options[long_options_index].definition;
691 CommandInterpreter &interpreter = options.GetInterpreter();
692 OptionValidator *validator = def->validator;
693 if (validator && !validator->IsValid(*interpreter.GetPlatform(true), interpreter.GetExecutionContext()))
694 {
695 error.SetErrorStringWithFormat("Option \"%s\" invalid. %s", def->long_option, def->validator->LongConditionString());
696 }
697 else
698 {
699 error = options.SetOptionValue(long_options_index,
Todd Fiala2c77a422014-10-10 01:11:39 +0000700 (def->option_has_arg == OptionParser::eNoArgument) ? nullptr : OptionParser::GetOptionArgument());
Zachary Turnerd37221d2014-07-09 16:31:49 +0000701 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 }
703 else
704 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000705 error.SetErrorStringWithFormat("invalid option with value '%i'", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 }
707 if (error.Fail())
708 break;
709 }
710
711 // Update our ARGV now that get options has consumed all the options
Virgile Belloe2607b52013-09-05 16:42:23 +0000712 m_argv.erase(m_argv.begin(), m_argv.begin() + OptionParser::GetOptionIndex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 UpdateArgsAfterOptionParsing ();
714 return error;
715}
716
717void
718Args::Clear ()
719{
720 m_args.clear ();
721 m_argv.clear ();
722 m_args_quote_char.clear();
723}
724
725int32_t
726Args::StringToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr)
727{
728 if (s && s[0])
729 {
Ed Masted78c9572014-04-20 00:31:37 +0000730 char *end = nullptr;
Greg Claytonc7bece562013-01-25 18:06:21 +0000731 const long sval = ::strtol (s, &end, base);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 if (*end == '\0')
733 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000734 if (success_ptr)
735 *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
736 return (int32_t)sval; // All characters were used, return the result
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 }
738 }
739 if (success_ptr) *success_ptr = false;
740 return fail_value;
741}
742
743uint32_t
744Args::StringToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr)
745{
746 if (s && s[0])
747 {
Ed Masted78c9572014-04-20 00:31:37 +0000748 char *end = nullptr;
Greg Claytonc7bece562013-01-25 18:06:21 +0000749 const unsigned long uval = ::strtoul (s, &end, base);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 if (*end == '\0')
751 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000752 if (success_ptr)
753 *success_ptr = (uval <= UINT32_MAX);
754 return (uint32_t)uval; // All characters were used, return the result
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755 }
756 }
757 if (success_ptr) *success_ptr = false;
758 return fail_value;
759}
760
761
762int64_t
763Args::StringToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr)
764{
765 if (s && s[0])
766 {
Ed Masted78c9572014-04-20 00:31:37 +0000767 char *end = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 int64_t uval = ::strtoll (s, &end, base);
769 if (*end == '\0')
770 {
771 if (success_ptr) *success_ptr = true;
772 return uval; // All characters were used, return the result
773 }
774 }
775 if (success_ptr) *success_ptr = false;
776 return fail_value;
777}
778
779uint64_t
780Args::StringToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr)
781{
782 if (s && s[0])
783 {
Ed Masted78c9572014-04-20 00:31:37 +0000784 char *end = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 uint64_t uval = ::strtoull (s, &end, base);
786 if (*end == '\0')
787 {
788 if (success_ptr) *success_ptr = true;
789 return uval; // All characters were used, return the result
790 }
791 }
792 if (success_ptr) *success_ptr = false;
793 return fail_value;
794}
795
796lldb::addr_t
Greg Claytonb9d5df52012-12-06 22:49:16 +0000797Args::StringToAddress (const ExecutionContext *exe_ctx, const char *s, lldb::addr_t fail_value, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798{
Greg Claytonb9d5df52012-12-06 22:49:16 +0000799 bool error_set = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 if (s && s[0])
801 {
Ed Masted78c9572014-04-20 00:31:37 +0000802 char *end = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 lldb::addr_t addr = ::strtoull (s, &end, 0);
804 if (*end == '\0')
805 {
Greg Claytonb9d5df52012-12-06 22:49:16 +0000806 if (error_ptr)
807 error_ptr->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 return addr; // All characters were used, return the result
809 }
810 // Try base 16 with no prefix...
811 addr = ::strtoull (s, &end, 16);
812 if (*end == '\0')
813 {
Greg Claytonb9d5df52012-12-06 22:49:16 +0000814 if (error_ptr)
815 error_ptr->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 return addr; // All characters were used, return the result
817 }
Greg Claytonb9d5df52012-12-06 22:49:16 +0000818
819 if (exe_ctx)
820 {
821 Target *target = exe_ctx->GetTargetPtr();
822 if (target)
823 {
824 lldb::ValueObjectSP valobj_sp;
825 EvaluateExpressionOptions options;
826 options.SetCoerceToId(false);
827 options.SetUnwindOnError(true);
828 options.SetKeepInMemory(false);
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000829 options.SetTryAllThreads(true);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000830
Jim Ingham1624a2d2014-05-05 02:26:40 +0000831 ExpressionResults expr_result = target->EvaluateExpression(s,
Enrico Granataca0e5ad2014-10-09 23:09:40 +0000832 exe_ctx->GetFramePtr(),
833 valobj_sp,
834 options);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000835
836 bool success = false;
Jim Ingham8646d3c2014-05-05 02:47:44 +0000837 if (expr_result == eExpressionCompleted)
Greg Claytonb9d5df52012-12-06 22:49:16 +0000838 {
Enrico Granataca0e5ad2014-10-09 23:09:40 +0000839 if (valobj_sp)
840 valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(valobj_sp->GetDynamicValueType(), true);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000841 // Get the address to watch.
Enrico Granataca0e5ad2014-10-09 23:09:40 +0000842 if (valobj_sp)
843 addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000844 if (success)
845 {
846 if (error_ptr)
847 error_ptr->Clear();
848 return addr;
849 }
850 else
851 {
852 if (error_ptr)
853 {
854 error_set = true;
855 error_ptr->SetErrorStringWithFormat("address expression \"%s\" resulted in a value whose type can't be converted to an address: %s", s, valobj_sp->GetTypeName().GetCString());
856 }
857 }
858
859 }
860 else
861 {
862 // Since the compiler can't handle things like "main + 12" we should
863 // try to do this for now. The compliler doesn't like adding offsets
864 // to function pointer types.
Greg Claytonbc43cab2013-04-03 21:37:16 +0000865 static RegularExpression g_symbol_plus_offset_regex("^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
866 RegularExpression::Match regex_match(3);
867 if (g_symbol_plus_offset_regex.Execute(s, &regex_match))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000868 {
869 uint64_t offset = 0;
870 bool add = true;
871 std::string name;
872 std::string str;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000873 if (regex_match.GetMatchAtIndex(s, 1, name))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000874 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000875 if (regex_match.GetMatchAtIndex(s, 2, str))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000876 {
877 add = str[0] == '+';
878
Greg Claytonbc43cab2013-04-03 21:37:16 +0000879 if (regex_match.GetMatchAtIndex(s, 3, str))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000880 {
881 offset = Args::StringToUInt64(str.c_str(), 0, 0, &success);
882
883 if (success)
884 {
885 Error error;
886 addr = StringToAddress (exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
887 if (addr != LLDB_INVALID_ADDRESS)
888 {
889 if (add)
890 return addr + offset;
891 else
892 return addr - offset;
893 }
894 }
895 }
896 }
897 }
898 }
899
900 if (error_ptr)
901 {
902 error_set = true;
903 error_ptr->SetErrorStringWithFormat("address expression \"%s\" evaluation failed", s);
904 }
905 }
906 }
907 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 }
Greg Claytonb9d5df52012-12-06 22:49:16 +0000909 if (error_ptr)
910 {
911 if (!error_set)
912 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"", s);
913 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 return fail_value;
915}
916
Greg Clayton30820f02013-03-05 23:52:49 +0000917const char *
918Args::StripSpaces (std::string &s, bool leading, bool trailing, bool return_null_if_empty)
919{
920 static const char *k_white_space = " \t\v";
921 if (!s.empty())
922 {
923 if (leading)
924 {
925 size_t pos = s.find_first_not_of (k_white_space);
926 if (pos == std::string::npos)
927 s.clear();
928 else if (pos > 0)
929 s.erase(0, pos);
930 }
931
932 if (trailing)
933 {
934 size_t rpos = s.find_last_not_of(k_white_space);
935 if (rpos != std::string::npos && rpos + 1 < s.size())
936 s.erase(rpos + 1);
937 }
938 }
939 if (return_null_if_empty && s.empty())
Ed Masted78c9572014-04-20 00:31:37 +0000940 return nullptr;
Greg Clayton30820f02013-03-05 23:52:49 +0000941 return s.c_str();
942}
943
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944bool
945Args::StringToBoolean (const char *s, bool fail_value, bool *success_ptr)
946{
947 if (s && s[0])
948 {
949 if (::strcasecmp (s, "false") == 0 ||
950 ::strcasecmp (s, "off") == 0 ||
951 ::strcasecmp (s, "no") == 0 ||
952 ::strcmp (s, "0") == 0)
953 {
954 if (success_ptr)
955 *success_ptr = true;
956 return false;
957 }
958 else
959 if (::strcasecmp (s, "true") == 0 ||
960 ::strcasecmp (s, "on") == 0 ||
961 ::strcasecmp (s, "yes") == 0 ||
962 ::strcmp (s, "1") == 0)
963 {
964 if (success_ptr) *success_ptr = true;
965 return true;
966 }
967 }
968 if (success_ptr) *success_ptr = false;
969 return fail_value;
970}
971
Zachary Turner3e7442b2015-01-12 20:44:02 +0000972char
973Args::StringToChar(const char *s, char fail_value, bool *success_ptr)
974{
975 bool success = false;
976 char result = fail_value;
977
978 if (s)
979 {
980 size_t length = strlen(s);
981 if (length == 1)
982 {
983 success = true;
984 result = s[0];
985 }
986 }
987 if (success_ptr)
988 *success_ptr = success;
989 return result;
990}
991
Greg Claytonded470d2011-03-19 01:12:21 +0000992const char *
993Args::StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update)
994{
995 major = UINT32_MAX;
996 minor = UINT32_MAX;
997 update = UINT32_MAX;
998
999 if (s && s[0])
1000 {
Ed Masted78c9572014-04-20 00:31:37 +00001001 char *pos = nullptr;
Greg Claytonc7bece562013-01-25 18:06:21 +00001002 unsigned long uval32 = ::strtoul (s, &pos, 0);
Greg Claytonded470d2011-03-19 01:12:21 +00001003 if (pos == s)
1004 return s;
1005 major = uval32;
1006 if (*pos == '\0')
1007 {
1008 return pos; // Decoded major and got end of string
1009 }
1010 else if (*pos == '.')
1011 {
1012 const char *minor_cstr = pos + 1;
1013 uval32 = ::strtoul (minor_cstr, &pos, 0);
1014 if (pos == minor_cstr)
1015 return pos; // Didn't get any digits for the minor version...
1016 minor = uval32;
1017 if (*pos == '.')
1018 {
1019 const char *update_cstr = pos + 1;
1020 uval32 = ::strtoul (update_cstr, &pos, 0);
1021 if (pos == update_cstr)
1022 return pos;
1023 update = uval32;
1024 }
1025 return pos;
1026 }
1027 }
Ed Masted78c9572014-04-20 00:31:37 +00001028 return nullptr;
Greg Claytonded470d2011-03-19 01:12:21 +00001029}
1030
Greg Clayton144f3a92011-11-15 03:53:30 +00001031const char *
1032Args::GetShellSafeArgument (const char *unsafe_arg, std::string &safe_arg)
1033{
1034 safe_arg.assign (unsafe_arg);
1035 size_t prev_pos = 0;
1036 while (prev_pos < safe_arg.size())
1037 {
1038 // Escape spaces and quotes
1039 size_t pos = safe_arg.find_first_of(" '\"", prev_pos);
1040 if (pos != std::string::npos)
1041 {
1042 safe_arg.insert (pos, 1, '\\');
1043 prev_pos = pos + 2;
1044 }
1045 else
1046 break;
1047 }
1048 return safe_arg.c_str();
1049}
1050
Greg Claytonded470d2011-03-19 01:12:21 +00001051
Greg Claytonc7bece562013-01-25 18:06:21 +00001052int64_t
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001053Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054{
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001055 if (enum_values)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001057 if (s && s[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 {
Ed Masted78c9572014-04-20 00:31:37 +00001059 for (int i = 0; enum_values[i].string_value != nullptr ; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001061 if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value)
1062 {
1063 error.Clear();
1064 return enum_values[i].value;
1065 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 }
1067 }
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001068
1069 StreamString strm;
1070 strm.PutCString ("invalid enumeration value, valid values are: ");
Ed Masted78c9572014-04-20 00:31:37 +00001071 for (int i = 0; enum_values[i].string_value != nullptr; i++)
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001072 {
1073 strm.Printf ("%s\"%s\"",
1074 i > 0 ? ", " : "",
1075 enum_values[i].string_value);
1076 }
1077 error.SetErrorString(strm.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078 }
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001079 else
1080 {
1081 error.SetErrorString ("invalid enumeration argument");
1082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 return fail_value;
1084}
1085
1086ScriptLanguage
1087Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr)
1088{
1089 if (s && s[0])
1090 {
1091 if ((::strcasecmp (s, "python") == 0) ||
1092 (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault))
1093 {
1094 if (success_ptr) *success_ptr = true;
1095 return eScriptLanguagePython;
1096 }
1097 if (::strcasecmp (s, "none"))
1098 {
1099 if (success_ptr) *success_ptr = true;
1100 return eScriptLanguageNone;
1101 }
1102 }
1103 if (success_ptr) *success_ptr = false;
1104 return fail_value;
1105}
1106
1107Error
1108Args::StringToFormat
1109(
1110 const char *s,
Greg Clayton68ebae62011-04-28 20:55:26 +00001111 lldb::Format &format,
Greg Claytonc7bece562013-01-25 18:06:21 +00001112 size_t *byte_size_ptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113)
1114{
1115 format = eFormatInvalid;
1116 Error error;
1117
1118 if (s && s[0])
1119 {
Greg Clayton68ebae62011-04-28 20:55:26 +00001120 if (byte_size_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 {
Greg Clayton68ebae62011-04-28 20:55:26 +00001122 if (isdigit (s[0]))
1123 {
Ed Masted78c9572014-04-20 00:31:37 +00001124 char *format_char = nullptr;
Greg Clayton68ebae62011-04-28 20:55:26 +00001125 unsigned long byte_size = ::strtoul (s, &format_char, 0);
1126 if (byte_size != ULONG_MAX)
1127 *byte_size_ptr = byte_size;
1128 s = format_char;
1129 }
1130 else
1131 *byte_size_ptr = 0;
1132 }
1133
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001134 const bool partial_match_ok = true;
1135 if (!FormatManager::GetFormatFromCString (s, partial_match_ok, format))
Greg Clayton68ebae62011-04-28 20:55:26 +00001136 {
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001137 StreamString error_strm;
1138 error_strm.Printf ("Invalid format character or name '%s'. Valid values are:\n", s);
Peter Collingbourne44c9b372011-06-24 01:12:22 +00001139 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
Greg Clayton68ebae62011-04-28 20:55:26 +00001140 {
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001141 char format_char = FormatManager::GetFormatAsFormatChar(f);
1142 if (format_char)
1143 error_strm.Printf ("'%c' or ", format_char);
1144
1145 error_strm.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
1146 error_strm.EOL();
Greg Clayton68ebae62011-04-28 20:55:26 +00001147 }
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001148
1149 if (byte_size_ptr)
1150 error_strm.PutCString ("An optional byte size can precede the format character.\n");
1151 error.SetErrorString(error_strm.GetString().c_str());
Greg Clayton68ebae62011-04-28 20:55:26 +00001152 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153
1154 if (error.Fail())
1155 return error;
1156 }
1157 else
1158 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001159 error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 }
1161 return error;
1162}
1163
Greg Clayton2443cbd2012-08-24 01:42:50 +00001164lldb::Encoding
1165Args::StringToEncoding (const char *s, lldb::Encoding fail_value)
1166{
1167 if (s && s[0])
1168 {
1169 if (strcmp(s, "uint") == 0)
1170 return eEncodingUint;
1171 else if (strcmp(s, "sint") == 0)
1172 return eEncodingSint;
1173 else if (strcmp(s, "ieee754") == 0)
1174 return eEncodingIEEE754;
1175 else if (strcmp(s, "vector") == 0)
1176 return eEncodingVector;
1177 }
1178 return fail_value;
1179}
1180
1181uint32_t
1182Args::StringToGenericRegister (const char *s)
1183{
1184 if (s && s[0])
1185 {
1186 if (strcmp(s, "pc") == 0)
1187 return LLDB_REGNUM_GENERIC_PC;
1188 else if (strcmp(s, "sp") == 0)
1189 return LLDB_REGNUM_GENERIC_SP;
1190 else if (strcmp(s, "fp") == 0)
1191 return LLDB_REGNUM_GENERIC_FP;
Jason Molenda2fc43a32014-05-09 04:09:48 +00001192 else if (strcmp(s, "ra") == 0 || strcmp(s, "lr") == 0)
Greg Clayton2443cbd2012-08-24 01:42:50 +00001193 return LLDB_REGNUM_GENERIC_RA;
1194 else if (strcmp(s, "flags") == 0)
1195 return LLDB_REGNUM_GENERIC_FLAGS;
1196 else if (strncmp(s, "arg", 3) == 0)
1197 {
1198 if (s[3] && s[4] == '\0')
1199 {
1200 switch (s[3])
1201 {
1202 case '1': return LLDB_REGNUM_GENERIC_ARG1;
1203 case '2': return LLDB_REGNUM_GENERIC_ARG2;
1204 case '3': return LLDB_REGNUM_GENERIC_ARG3;
1205 case '4': return LLDB_REGNUM_GENERIC_ARG4;
1206 case '5': return LLDB_REGNUM_GENERIC_ARG5;
1207 case '6': return LLDB_REGNUM_GENERIC_ARG6;
1208 case '7': return LLDB_REGNUM_GENERIC_ARG7;
1209 case '8': return LLDB_REGNUM_GENERIC_ARG8;
1210 }
1211 }
1212 }
1213 }
1214 return LLDB_INVALID_REGNUM;
1215}
1216
1217
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218void
1219Args::LongestCommonPrefix (std::string &common_prefix)
1220{
1221 arg_sstr_collection::iterator pos, end = m_args.end();
1222 pos = m_args.begin();
1223 if (pos == end)
1224 common_prefix.clear();
1225 else
1226 common_prefix = (*pos);
1227
1228 for (++pos; pos != end; ++pos)
1229 {
Greg Claytonc982c762010-07-09 20:39:50 +00001230 size_t new_size = (*pos).size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231
1232 // First trim common_prefix if it is longer than the current element:
1233 if (common_prefix.size() > new_size)
1234 common_prefix.erase (new_size);
1235
1236 // Then trim it at the first disparity:
1237
Greg Claytonc982c762010-07-09 20:39:50 +00001238 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 {
1240 if ((*pos)[i] != common_prefix[i])
1241 {
1242 common_prefix.erase(i);
1243 break;
1244 }
1245 }
1246
1247 // If we've emptied the common prefix, we're done.
1248 if (common_prefix.empty())
1249 break;
1250 }
1251}
1252
Caroline Ticed9d63362010-12-07 19:58:26 +00001253size_t
Virgile Belloe2607b52013-09-05 16:42:23 +00001254Args::FindArgumentIndexForOption (Option *long_options, int long_options_index)
Caroline Ticed9d63362010-12-07 19:58:26 +00001255{
1256 char short_buffer[3];
1257 char long_buffer[255];
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001258 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", long_options[long_options_index].val);
Zachary Turnerd37221d2014-07-09 16:31:49 +00001259 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].definition->long_option);
Caroline Ticed9d63362010-12-07 19:58:26 +00001260 size_t end = GetArgumentCount ();
1261 size_t idx = 0;
1262 while (idx < end)
1263 {
1264 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
1265 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
1266 {
1267 return idx;
1268 }
1269 ++idx;
1270 }
1271
1272 return end;
1273}
1274
1275bool
1276Args::IsPositionalArgument (const char *arg)
1277{
Ed Masted78c9572014-04-20 00:31:37 +00001278 if (arg == nullptr)
Caroline Ticed9d63362010-12-07 19:58:26 +00001279 return false;
1280
1281 bool is_positional = true;
1282 char *cptr = (char *) arg;
1283
1284 if (cptr[0] == '%')
1285 {
1286 ++cptr;
1287 while (isdigit (cptr[0]))
1288 ++cptr;
1289 if (cptr[0] != '\0')
1290 is_positional = false;
1291 }
1292 else
1293 is_positional = false;
1294
1295 return is_positional;
1296}
1297
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298void
Caroline Tice636d6ed2010-10-12 17:45:19 +00001299Args::ParseAliasOptions (Options &options,
1300 CommandReturnObject &result,
Caroline Tice844d2302010-12-09 22:52:49 +00001301 OptionArgVector *option_arg_vector,
1302 std::string &raw_input_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303{
1304 StreamString sstr;
1305 int i;
Virgile Belloe2607b52013-09-05 16:42:23 +00001306 Option *long_options = options.GetLongOptions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307
Ed Masted78c9572014-04-20 00:31:37 +00001308 if (long_options == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001309 {
1310 result.AppendError ("invalid long options");
1311 result.SetStatus (eReturnStatusFailed);
1312 return;
1313 }
1314
Zachary Turnerd37221d2014-07-09 16:31:49 +00001315 for (i = 0; long_options[i].definition != nullptr; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 {
Ed Masted78c9572014-04-20 00:31:37 +00001317 if (long_options[i].flag == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 {
1319 sstr << (char) long_options[i].val;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001320 switch (long_options[i].definition->option_has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 {
1322 default:
Virgile Belloe2607b52013-09-05 16:42:23 +00001323 case OptionParser::eNoArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001325 case OptionParser::eRequiredArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326 sstr << ":";
1327 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001328 case OptionParser::eOptionalArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 sstr << "::";
1330 break;
1331 }
1332 }
1333 }
1334
Virgile Belloe2607b52013-09-05 16:42:23 +00001335 OptionParser::Prepare();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 int val;
1337 while (1)
1338 {
1339 int long_options_index = -1;
Virgile Belloe2607b52013-09-05 16:42:23 +00001340 val = OptionParser::Parse (GetArgumentCount(),
Greg Claytonb7ad58a2013-04-04 20:35:24 +00001341 GetArgumentVector(),
1342 sstr.GetData(),
1343 long_options,
1344 &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345
1346 if (val == -1)
1347 break;
1348
1349 if (val == '?')
1350 {
1351 result.AppendError ("unknown or ambiguous option");
1352 result.SetStatus (eReturnStatusFailed);
1353 break;
1354 }
1355
1356 if (val == 0)
1357 continue;
1358
Greg Clayton78d10192014-04-07 21:37:03 +00001359 options.OptionSeen (val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360
1361 // Look up the long option index
1362 if (long_options_index == -1)
1363 {
1364 for (int j = 0;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001365 long_options[j].definition || long_options[j].flag || long_options[j].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366 ++j)
1367 {
1368 if (long_options[j].val == val)
1369 {
1370 long_options_index = j;
1371 break;
1372 }
1373 }
1374 }
1375
1376 // See if the option takes an argument, and see if one was supplied.
1377 if (long_options_index >= 0)
1378 {
1379 StreamString option_str;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001380 option_str.Printf ("-%c", val);
Zachary Turnerd37221d2014-07-09 16:31:49 +00001381 const OptionDefinition *def = long_options[long_options_index].definition;
1382 int has_arg = (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383
Zachary Turnerd37221d2014-07-09 16:31:49 +00001384 switch (has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001386 case OptionParser::eNoArgument:
Caroline Ticed9d63362010-12-07 19:58:26 +00001387 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001388 OptionArgValue (OptionParser::eNoArgument, "<no-argument>")));
Caroline Tice5172e6c2010-09-12 04:48:45 +00001389 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001391 case OptionParser::eRequiredArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001392 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 {
1394 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001395 OptionArgValue (OptionParser::eRequiredArgument,
1396 std::string (OptionParser::GetOptionArgument()))));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1398 }
1399 else
1400 {
1401 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1402 option_str.GetData());
1403 result.SetStatus (eReturnStatusFailed);
1404 }
1405 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001406 case OptionParser::eOptionalArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001407 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 {
1409 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001410 OptionArgValue (OptionParser::eOptionalArgument,
1411 std::string (OptionParser::GetOptionArgument()))));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1413 }
1414 else
1415 {
1416 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001417 OptionArgValue (OptionParser::eOptionalArgument, "<no-argument>")));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001418 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1419 }
1420 break;
1421 default:
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001422 result.AppendErrorWithFormat ("error with options table; invalid value in has_arg field for option '%c'.\n", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 result.SetStatus (eReturnStatusFailed);
1424 break;
1425 }
1426 }
1427 else
1428 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001429 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430 result.SetStatus (eReturnStatusFailed);
1431 }
Caroline Tice636d6ed2010-10-12 17:45:19 +00001432
1433 if (long_options_index >= 0)
1434 {
1435 // Find option in the argument list; also see if it was supposed to take an argument and if one was
Caroline Tice844d2302010-12-09 22:52:49 +00001436 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1437 // the raw_input_string, if one was passed in.
Caroline Ticed9d63362010-12-07 19:58:26 +00001438 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1439 if (idx < GetArgumentCount())
1440 {
Caroline Tice844d2302010-12-09 22:52:49 +00001441 if (raw_input_string.size() > 0)
1442 {
1443 const char *tmp_arg = GetArgumentAtIndex (idx);
1444 size_t pos = raw_input_string.find (tmp_arg);
1445 if (pos != std::string::npos)
1446 raw_input_string.erase (pos, strlen (tmp_arg));
1447 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001448 ReplaceArgumentAtIndex (idx, "");
Zachary Turnerd37221d2014-07-09 16:31:49 +00001449 if ((long_options[long_options_index].definition->option_has_arg != OptionParser::eNoArgument)
Ed Masted78c9572014-04-20 00:31:37 +00001450 && (OptionParser::GetOptionArgument() != nullptr)
Caroline Ticed9d63362010-12-07 19:58:26 +00001451 && (idx+1 < GetArgumentCount())
Virgile Belloe2607b52013-09-05 16:42:23 +00001452 && (strcmp (OptionParser::GetOptionArgument(), GetArgumentAtIndex(idx+1)) == 0))
Caroline Tice844d2302010-12-09 22:52:49 +00001453 {
1454 if (raw_input_string.size() > 0)
1455 {
1456 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1457 size_t pos = raw_input_string.find (tmp_arg);
1458 if (pos != std::string::npos)
1459 raw_input_string.erase (pos, strlen (tmp_arg));
1460 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001461 ReplaceArgumentAtIndex (idx+1, "");
Caroline Tice844d2302010-12-09 22:52:49 +00001462 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001463 }
Caroline Tice636d6ed2010-10-12 17:45:19 +00001464 }
1465
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466 if (!result.Succeeded())
1467 break;
1468 }
1469}
1470
1471void
1472Args::ParseArgsForCompletion
1473(
1474 Options &options,
Jim Inghamd43e0092010-06-24 20:31:04 +00001475 OptionElementVector &option_element_vector,
1476 uint32_t cursor_index
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477)
1478{
1479 StreamString sstr;
Virgile Belloe2607b52013-09-05 16:42:23 +00001480 Option *long_options = options.GetLongOptions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 option_element_vector.clear();
1482
Ed Masted78c9572014-04-20 00:31:37 +00001483 if (long_options == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 {
1485 return;
1486 }
1487
1488 // Leading : tells getopt to return a : for a missing option argument AND
1489 // to suppress error messages.
1490
1491 sstr << ":";
Zachary Turnerd37221d2014-07-09 16:31:49 +00001492 for (int i = 0; long_options[i].definition != nullptr; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493 {
Ed Masted78c9572014-04-20 00:31:37 +00001494 if (long_options[i].flag == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 {
1496 sstr << (char) long_options[i].val;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001497 switch (long_options[i].definition->option_has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498 {
1499 default:
Virgile Belloe2607b52013-09-05 16:42:23 +00001500 case OptionParser::eNoArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001502 case OptionParser::eRequiredArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 sstr << ":";
1504 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001505 case OptionParser::eOptionalArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 sstr << "::";
1507 break;
1508 }
1509 }
1510 }
1511
Virgile Belloe2607b52013-09-05 16:42:23 +00001512 OptionParser::Prepare();
1513 OptionParser::EnableError(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514
1515 int val;
1516 const OptionDefinition *opt_defs = options.GetDefinitions();
1517
Virgile Belloe2607b52013-09-05 16:42:23 +00001518 // Fooey... OptionParser::Parse permutes the GetArgumentVector to move the options to the front.
1519 // So we have to build another Arg and pass that to OptionParser::Parse so it doesn't
Jim Inghamd43e0092010-06-24 20:31:04 +00001520 // change the one we have.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521
Greg Claytonc982c762010-07-09 20:39:50 +00001522 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001523
Jim Inghamd43e0092010-06-24 20:31:04 +00001524 bool failed_once = false;
1525 uint32_t dash_dash_pos = -1;
1526
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001527 while (1)
1528 {
1529 bool missing_argument = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001530 int long_options_index = -1;
Jim Inghamd43e0092010-06-24 20:31:04 +00001531
Virgile Belloe2607b52013-09-05 16:42:23 +00001532 val = OptionParser::Parse (dummy_vec.size() - 1,
Greg Claytonb7ad58a2013-04-04 20:35:24 +00001533 (char *const *) &dummy_vec.front(),
1534 sstr.GetData(),
1535 long_options,
1536 &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537
1538 if (val == -1)
Jim Inghamd43e0092010-06-24 20:31:04 +00001539 {
1540 // When we're completing a "--" which is the last option on line,
1541 if (failed_once)
1542 break;
1543
1544 failed_once = true;
1545
1546 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1547 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1548 // user might want to complete options by long name. I make this work by checking whether the
1549 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
Virgile Belloe2607b52013-09-05 16:42:23 +00001550 // I let it pass to OptionParser::Parse which will terminate the option parsing.
Jim Inghamd43e0092010-06-24 20:31:04 +00001551 // Note, in either case we continue parsing the line so we can figure out what other options
1552 // were passed. This will be useful when we come to restricting completions based on what other
1553 // options we've seen on the line.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001555 if (static_cast<size_t>(OptionParser::GetOptionIndex()) < dummy_vec.size() - 1
Virgile Belloe2607b52013-09-05 16:42:23 +00001556 && (strcmp (dummy_vec[OptionParser::GetOptionIndex()-1], "--") == 0))
Jim Inghamd43e0092010-06-24 20:31:04 +00001557 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001558 dash_dash_pos = OptionParser::GetOptionIndex() - 1;
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001559 if (static_cast<size_t>(OptionParser::GetOptionIndex() - 1) == cursor_index)
Jim Inghamd43e0092010-06-24 20:31:04 +00001560 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001561 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001562 OptionArgElement::eBareDoubleDash));
1563 continue;
1564 }
1565 else
1566 break;
1567 }
1568 else
1569 break;
1570 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571 else if (val == '?')
1572 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001573 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001574 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575 continue;
1576 }
1577 else if (val == 0)
1578 {
1579 continue;
1580 }
1581 else if (val == ':')
1582 {
1583 // This is a missing argument.
Virgile Belloe2607b52013-09-05 16:42:23 +00001584 val = OptionParser::GetOptionErrorCause();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585 missing_argument = true;
1586 }
1587
1588 ((Options *) &options)->OptionSeen (val);
1589
1590 // Look up the long option index
1591 if (long_options_index == -1)
1592 {
1593 for (int j = 0;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001594 long_options[j].definition || long_options[j].flag || long_options[j].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595 ++j)
1596 {
1597 if (long_options[j].val == val)
1598 {
1599 long_options_index = j;
1600 break;
1601 }
1602 }
1603 }
1604
1605 // See if the option takes an argument, and see if one was supplied.
1606 if (long_options_index >= 0)
1607 {
1608 int opt_defs_index = -1;
1609 for (int i = 0; ; i++)
1610 {
1611 if (opt_defs[i].short_option == 0)
1612 break;
1613 else if (opt_defs[i].short_option == val)
1614 {
1615 opt_defs_index = i;
1616 break;
1617 }
1618 }
1619
Zachary Turnerd37221d2014-07-09 16:31:49 +00001620 const OptionDefinition *def = long_options[long_options_index].definition;
1621 int has_arg = (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
1622 switch (has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001623 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001624 case OptionParser::eNoArgument:
1625 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 1, 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001626 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001627 case OptionParser::eRequiredArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001628 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629 {
1630 int arg_index;
1631 if (missing_argument)
1632 arg_index = -1;
1633 else
Virgile Belloe2607b52013-09-05 16:42:23 +00001634 arg_index = OptionParser::GetOptionIndex() - 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635
Virgile Belloe2607b52013-09-05 16:42:23 +00001636 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637 }
1638 else
1639 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001640 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 1, -1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001641 }
1642 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001643 case OptionParser::eOptionalArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001644 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001645 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001646 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, OptionParser::GetOptionIndex() - 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647 }
1648 else
1649 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001650 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, OptionParser::GetOptionIndex() - 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651 }
1652 break;
1653 default:
1654 // The options table is messed up. Here we'll just continue
Virgile Belloe2607b52013-09-05 16:42:23 +00001655 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001656 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657 break;
1658 }
1659 }
1660 else
1661 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001662 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001663 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664 }
1665 }
Jim Inghamd43e0092010-06-24 20:31:04 +00001666
1667 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
Virgile Belloe2607b52013-09-05 16:42:23 +00001668 // the option_element_vector, but only if it is not after the "--". But it turns out that OptionParser::Parse just ignores
Jim Inghamd43e0092010-06-24 20:31:04 +00001669 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
Jim Inghamdc498e52014-08-27 22:06:58 +00001670 // Note, a single quoted dash is not the same as a single dash...
Jim Inghamd43e0092010-06-24 20:31:04 +00001671
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001672 if ((static_cast<int32_t>(dash_dash_pos) == -1 || cursor_index < dash_dash_pos)
Jim Inghamdc498e52014-08-27 22:06:58 +00001673 && m_args_quote_char[cursor_index] == '\0'
Jim Inghamd43e0092010-06-24 20:31:04 +00001674 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1675 {
1676 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1677 OptionArgElement::eBareDash));
1678
1679 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680}
Greg Clayton4c054102012-09-01 00:38:36 +00001681
1682void
1683Args::EncodeEscapeSequences (const char *src, std::string &dst)
1684{
1685 dst.clear();
1686 if (src)
1687 {
1688 for (const char *p = src; *p != '\0'; ++p)
1689 {
1690 size_t non_special_chars = ::strcspn (p, "\\");
1691 if (non_special_chars > 0)
1692 {
1693 dst.append(p, non_special_chars);
1694 p += non_special_chars;
1695 if (*p == '\0')
1696 break;
1697 }
1698
1699 if (*p == '\\')
1700 {
1701 ++p; // skip the slash
1702 switch (*p)
1703 {
1704 case 'a' : dst.append(1, '\a'); break;
1705 case 'b' : dst.append(1, '\b'); break;
1706 case 'f' : dst.append(1, '\f'); break;
1707 case 'n' : dst.append(1, '\n'); break;
1708 case 'r' : dst.append(1, '\r'); break;
1709 case 't' : dst.append(1, '\t'); break;
1710 case 'v' : dst.append(1, '\v'); break;
1711 case '\\': dst.append(1, '\\'); break;
1712 case '\'': dst.append(1, '\''); break;
1713 case '"' : dst.append(1, '"'); break;
1714 case '0' :
1715 // 1 to 3 octal chars
1716 {
1717 // Make a string that can hold onto the initial zero char,
1718 // up to 3 octal digits, and a terminating NULL.
1719 char oct_str[5] = { '\0', '\0', '\0', '\0', '\0' };
1720
1721 int i;
1722 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1723 oct_str[i] = p[i];
1724
1725 // We don't want to consume the last octal character since
1726 // the main for loop will do this for us, so we advance p by
1727 // one less than i (even if i is zero)
1728 p += i - 1;
Ed Masted78c9572014-04-20 00:31:37 +00001729 unsigned long octal_value = ::strtoul (oct_str, nullptr, 8);
Greg Clayton4c054102012-09-01 00:38:36 +00001730 if (octal_value <= UINT8_MAX)
1731 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001732 dst.append(1, (char)octal_value);
Greg Clayton4c054102012-09-01 00:38:36 +00001733 }
1734 }
1735 break;
1736
1737 case 'x':
1738 // hex number in the format
1739 if (isxdigit(p[1]))
1740 {
1741 ++p; // Skip the 'x'
1742
1743 // Make a string that can hold onto two hex chars plus a
1744 // NULL terminator
1745 char hex_str[3] = { *p, '\0', '\0' };
1746 if (isxdigit(p[1]))
1747 {
1748 ++p; // Skip the first of the two hex chars
1749 hex_str[1] = *p;
1750 }
1751
Ed Masted78c9572014-04-20 00:31:37 +00001752 unsigned long hex_value = strtoul (hex_str, nullptr, 16);
Greg Clayton4c054102012-09-01 00:38:36 +00001753 if (hex_value <= UINT8_MAX)
1754 dst.append (1, (char)hex_value);
1755 }
1756 else
1757 {
1758 dst.append(1, 'x');
1759 }
1760 break;
1761
1762 default:
1763 // Just desensitize any other character by just printing what
1764 // came after the '\'
1765 dst.append(1, *p);
1766 break;
1767
1768 }
1769 }
1770 }
1771 }
1772}
1773
1774
1775void
1776Args::ExpandEscapedCharacters (const char *src, std::string &dst)
1777{
1778 dst.clear();
1779 if (src)
1780 {
1781 for (const char *p = src; *p != '\0'; ++p)
1782 {
Daniel Malea90b0c842012-12-05 20:24:57 +00001783 if (isprint8(*p))
Greg Clayton4c054102012-09-01 00:38:36 +00001784 dst.append(1, *p);
1785 else
1786 {
1787 switch (*p)
1788 {
1789 case '\a': dst.append("\\a"); break;
1790 case '\b': dst.append("\\b"); break;
1791 case '\f': dst.append("\\f"); break;
1792 case '\n': dst.append("\\n"); break;
1793 case '\r': dst.append("\\r"); break;
1794 case '\t': dst.append("\\t"); break;
1795 case '\v': dst.append("\\v"); break;
1796 case '\'': dst.append("\\'"); break;
1797 case '"': dst.append("\\\""); break;
1798 case '\\': dst.append("\\\\"); break;
1799 default:
1800 {
1801 // Just encode as octal
1802 dst.append("\\0");
1803 char octal_str[32];
1804 snprintf(octal_str, sizeof(octal_str), "%o", *p);
1805 dst.append(octal_str);
1806 }
1807 break;
1808 }
1809 }
1810 }
1811 }
1812}
1813