blob: 61652a6c1096b801ab47c47b23174bf858259037 [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"
Vince Harron5275aaa2015-01-15 20:08:35 +000022#include "lldb/Host/StringConvert.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000023#include "lldb/Interpreter/Options.h"
Zachary Turnerd37221d2014-07-09 16:31:49 +000024#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytonb9d5df52012-12-06 22:49:16 +000026#include "lldb/Target/Process.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030using namespace lldb;
31using namespace lldb_private;
32
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033//----------------------------------------------------------------------
34// Args constructor
35//----------------------------------------------------------------------
36Args::Args (const char *command) :
37 m_args(),
Greg Clayton8b82f082011-04-12 05:54:46 +000038 m_argv(),
39 m_args_quote_char()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040{
Greg Clayton6ad07dd2010-12-19 03:41:24 +000041 if (command)
42 SetCommandString (command);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
45
46Args::Args (const char *command, size_t len) :
47 m_args(),
Greg Clayton8b82f082011-04-12 05:54:46 +000048 m_argv(),
49 m_args_quote_char()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
Greg Clayton6ad07dd2010-12-19 03:41:24 +000051 if (command && len)
52 SetCommandString (command, len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053}
54
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000056// We have to be very careful on the copy constructor of this class
57// to make sure we copy all of the string values, but we can't copy the
58// rhs.m_argv into m_argv since it will point to the "const char *" c
59// strings in rhs.m_args. We need to copy the string list and update our
60// own m_argv appropriately.
61//----------------------------------------------------------------------
62Args::Args (const Args &rhs) :
63 m_args (rhs.m_args),
64 m_argv (),
65 m_args_quote_char(rhs.m_args_quote_char)
66{
67 UpdateArgvFromArgs();
68}
69
70//----------------------------------------------------------------------
71// We have to be very careful on the copy constructor of this class
72// to make sure we copy all of the string values, but we can't copy the
73// rhs.m_argv into m_argv since it will point to the "const char *" c
74// strings in rhs.m_args. We need to copy the string list and update our
75// own m_argv appropriately.
76//----------------------------------------------------------------------
77const Args &
78Args::operator= (const Args &rhs)
79{
80 // Make sure we aren't assigning to self
81 if (this != &rhs)
82 {
83 m_args = rhs.m_args;
84 m_args_quote_char = rhs.m_args_quote_char;
85 UpdateArgvFromArgs();
86 }
87 return *this;
88}
89
90//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091// Destructor
92//----------------------------------------------------------------------
93Args::~Args ()
94{
95}
96
97void
98Args::Dump (Stream *s)
99{
Greg Claytonc7bece562013-01-25 18:06:21 +0000100 const size_t argc = m_argv.size();
101 for (size_t i=0; i<argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 {
103 s->Indent();
104 const char *arg_cstr = m_argv[i];
105 if (arg_cstr)
Greg Claytonc7bece562013-01-25 18:06:21 +0000106 s->Printf("argv[%zi]=\"%s\"\n", i, arg_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 else
Greg Claytonc7bece562013-01-25 18:06:21 +0000108 s->Printf("argv[%zi]=NULL\n", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
110 s->EOL();
111}
112
113bool
Greg Claytonda91b172012-04-25 22:30:32 +0000114Args::GetCommandString (std::string &command) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115{
116 command.clear();
Greg Claytonc7bece562013-01-25 18:06:21 +0000117 const size_t argc = GetArgumentCount();
118 for (size_t i=0; i<argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 {
120 if (i > 0)
121 command += ' ';
122 command += m_argv[i];
123 }
124 return argc > 0;
125}
126
Caroline Tice2d5289d2010-12-10 00:26:54 +0000127bool
Greg Claytonda91b172012-04-25 22:30:32 +0000128Args::GetQuotedCommandString (std::string &command) const
Caroline Tice2d5289d2010-12-10 00:26:54 +0000129{
130 command.clear ();
Greg Claytonc7bece562013-01-25 18:06:21 +0000131 const size_t argc = GetArgumentCount();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000132 for (size_t i = 0; i < argc; ++i)
Caroline Tice2d5289d2010-12-10 00:26:54 +0000133 {
134 if (i > 0)
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000135 command.append (1, ' ');
136 char quote_char = GetArgumentQuoteCharAtIndex(i);
137 if (quote_char)
Caroline Tice2d5289d2010-12-10 00:26:54 +0000138 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000139 command.append (1, quote_char);
140 command.append (m_argv[i]);
141 command.append (1, quote_char);
Caroline Tice2d5289d2010-12-10 00:26:54 +0000142 }
143 else
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000144 command.append (m_argv[i]);
Caroline Tice2d5289d2010-12-10 00:26:54 +0000145 }
146 return argc > 0;
147}
148
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149void
150Args::SetCommandString (const char *command, size_t len)
151{
152 // Use std::string to make sure we get a NULL terminated string we can use
153 // as "command" could point to a string within a large string....
154 std::string null_terminated_command(command, len);
155 SetCommandString(null_terminated_command.c_str());
156}
157
158void
159Args::SetCommandString (const char *command)
160{
161 m_args.clear();
162 m_argv.clear();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000163 m_args_quote_char.clear();
164
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 if (command && command[0])
166 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000167 static const char *k_space_separators = " \t";
Johnny Chena28b89c2012-01-19 19:22:41 +0000168 static const char *k_space_separators_with_slash_and_quotes = " \t \\'\"";
Ed Masted78c9572014-04-20 00:31:37 +0000169 const char *arg_end = nullptr;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000170 const char *arg_pos;
171 for (arg_pos = command;
172 arg_pos && arg_pos[0];
173 arg_pos = arg_end)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000175 // Skip any leading space separators
176 const char *arg_start = ::strspn (arg_pos, k_space_separators) + arg_pos;
177
178 // If there were only space separators to the end of the line, then
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 // we're done.
180 if (*arg_start == '\0')
181 break;
182
Greg Clayton710dd5a2011-01-08 20:28:42 +0000183 // Arguments can be split into multiple discontiguous pieces,
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000184 // for example:
185 // "Hello ""World"
186 // this would result in a single argument "Hello World" (without/
187 // the quotes) since the quotes would be removed and there is
188 // not space between the strings. So we need to keep track of the
189 // current start of each argument piece in "arg_piece_start"
190 const char *arg_piece_start = arg_start;
191 arg_pos = arg_piece_start;
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 std::string arg;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000194 // Since we can have multiple quotes that form a single command
195 // in a command like: "Hello "world'!' (which will make a single
196 // argument "Hello world!") we remember the first quote character
197 // we encounter and use that for the quote character.
198 char first_quote_char = '\0';
199 char quote_char = '\0';
200 bool arg_complete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000202 do
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000204 arg_end = ::strcspn (arg_pos, k_space_separators_with_slash_and_quotes) + arg_pos;
205
206 switch (arg_end[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000208 default:
209 assert (!"Unhandled case statement, we must handle this...");
210 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000212 case '\0':
213 // End of C string
214 if (arg_piece_start && arg_piece_start[0])
215 arg.append (arg_piece_start);
216 arg_complete = true;
217 break;
218
219 case '\\':
220 // Backslash character
221 switch (arg_end[1])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000223 case '\0':
224 arg.append (arg_piece_start);
Greg Clayton0c943132012-03-15 17:10:48 +0000225 ++arg_end;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000226 arg_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000229 default:
Jim Inghama39fb7a2012-07-21 00:12:58 +0000230 if (quote_char == '\0')
231 {
232 arg.append (arg_piece_start, arg_end - arg_piece_start);
Adrian Prantl5eeaf742013-06-18 18:24:04 +0000233 if (arg_end[1] != '\0')
Jim Inghama39fb7a2012-07-21 00:12:58 +0000234 {
235 arg.append (arg_end + 1, 1);
236 arg_pos = arg_end + 2;
237 arg_piece_start = arg_pos;
238 }
239 }
240 else
241 arg_pos = arg_end + 2;
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000242 break;
243 }
244 break;
245
246 case '"':
247 case '\'':
248 case '`':
249 // Quote characters
250 if (quote_char)
251 {
252 // We found a quote character while inside a quoted
253 // character argument. If it matches our current quote
254 // character, this ends the effect of the quotes. If it
255 // doesn't we ignore it.
256 if (quote_char == arg_end[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000258 arg.append (arg_piece_start, arg_end - arg_piece_start);
259 // Clear the quote character and let parsing
260 // continue (we need to watch for things like:
261 // "Hello ""World"
262 // "Hello "World
263 // "Hello "'World'
264 // All of which will result in a single argument "Hello World"
265 quote_char = '\0'; // Note that we are no longer inside quotes
266 arg_pos = arg_end + 1; // Skip the quote character
267 arg_piece_start = arg_pos; // Note we are starting from later in the string
268 }
269 else
270 {
271 // different quote, skip it and keep going
272 arg_pos = arg_end + 1;
273 }
274 }
275 else
276 {
277 // We found the start of a quote scope.
Greg Clayton710dd5a2011-01-08 20:28:42 +0000278 // Make sure there isn't a string that precedes
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000279 // the start of a quote scope like:
280 // Hello" World"
281 // If so, then add the "Hello" to the arg
282 if (arg_end > arg_piece_start)
283 arg.append (arg_piece_start, arg_end - arg_piece_start);
284
285 // Enter into a quote scope
286 quote_char = arg_end[0];
287
288 if (first_quote_char == '\0')
289 first_quote_char = quote_char;
290
291 arg_pos = arg_end;
Johnny Chena28b89c2012-01-19 19:22:41 +0000292 ++arg_pos; // Skip the quote character
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000293 arg_piece_start = arg_pos; // Note we are starting from later in the string
294
295 // Skip till the next quote character
296 const char *end_quote = ::strchr (arg_piece_start, quote_char);
297 while (end_quote && end_quote[-1] == '\\')
298 {
299 // Don't skip the quote character if it is
300 // preceded by a '\' character
301 end_quote = ::strchr (end_quote + 1, quote_char);
302 }
303
304 if (end_quote)
305 {
306 if (end_quote > arg_piece_start)
Johnny Chena28b89c2012-01-19 19:22:41 +0000307 arg.append (arg_piece_start, end_quote - arg_piece_start);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000308
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 Lattner30fdc8d2010-06-08 16:52:24 +0000315 }
316 else
317 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000318 arg_pos = end_quote + 1;
319 arg_piece_start = arg_pos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000321 quote_char = '\0';
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 }
Greg Clayton0c943132012-03-15 17:10:48 +0000323 else
324 {
325 // Consume the rest of the string as there was no terminating quote
326 arg.append(arg_piece_start);
327 arg_end = arg_piece_start + strlen(arg_piece_start);
328 arg_complete = true;
329 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000331 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000333 case ' ':
334 case '\t':
335 if (quote_char)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000337 // We are currently processing a quoted character and found
338 // a space character, skip any spaces and keep trying to find
339 // the end of the argument.
340 arg_pos = ::strspn (arg_end, k_space_separators) + arg_end;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000342 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 {
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000344 // We are not inside any quotes, we just found a space after an
345 // argument
346 if (arg_end > arg_piece_start)
347 arg.append (arg_piece_start, arg_end - arg_piece_start);
348 arg_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000350 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000352 } while (!arg_complete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353
354 m_args.push_back(arg);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000355 m_args_quote_char.push_back (first_quote_char);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356 }
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000357 UpdateArgvFromArgs();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
361void
362Args::UpdateArgsAfterOptionParsing()
363{
364 // Now m_argv might be out of date with m_args, so we need to fix that
365 arg_cstr_collection::const_iterator argv_pos, argv_end = m_argv.end();
366 arg_sstr_collection::iterator args_pos;
367 arg_quote_char_collection::iterator quotes_pos;
368
369 for (argv_pos = m_argv.begin(), args_pos = m_args.begin(), quotes_pos = m_args_quote_char.begin();
370 argv_pos != argv_end && args_pos != m_args.end();
371 ++argv_pos)
372 {
373 const char *argv_cstr = *argv_pos;
Ed Masted78c9572014-04-20 00:31:37 +0000374 if (argv_cstr == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 break;
376
377 while (args_pos != m_args.end())
378 {
379 const char *args_cstr = args_pos->c_str();
380 if (args_cstr == argv_cstr)
381 {
382 // We found the argument that matches the C string in the
383 // vector, so we can now look for the next one
384 ++args_pos;
385 ++quotes_pos;
386 break;
387 }
388 else
389 {
390 quotes_pos = m_args_quote_char.erase (quotes_pos);
391 args_pos = m_args.erase (args_pos);
392 }
393 }
394 }
395
396 if (args_pos != m_args.end())
397 m_args.erase (args_pos, m_args.end());
398
399 if (quotes_pos != m_args_quote_char.end())
400 m_args_quote_char.erase (quotes_pos, m_args_quote_char.end());
401}
402
403void
404Args::UpdateArgvFromArgs()
405{
406 m_argv.clear();
407 arg_sstr_collection::const_iterator pos, end = m_args.end();
408 for (pos = m_args.begin(); pos != end; ++pos)
409 m_argv.push_back(pos->c_str());
Ed Masted78c9572014-04-20 00:31:37 +0000410 m_argv.push_back(nullptr);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000411 // Make sure we have enough arg quote chars in the array
412 if (m_args_quote_char.size() < m_args.size())
413 m_args_quote_char.resize (m_argv.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
416size_t
417Args::GetArgumentCount() const
418{
419 if (m_argv.empty())
420 return 0;
421 return m_argv.size() - 1;
422}
423
424const char *
425Args::GetArgumentAtIndex (size_t idx) const
426{
427 if (idx < m_argv.size())
428 return m_argv[idx];
Ed Masted78c9572014-04-20 00:31:37 +0000429 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
432char
433Args::GetArgumentQuoteCharAtIndex (size_t idx) const
434{
435 if (idx < m_args_quote_char.size())
436 return m_args_quote_char[idx];
437 return '\0';
438}
439
440char **
441Args::GetArgumentVector()
442{
443 if (!m_argv.empty())
444 return (char **)&m_argv[0];
Ed Masted78c9572014-04-20 00:31:37 +0000445 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446}
447
448const char **
449Args::GetConstArgumentVector() const
450{
451 if (!m_argv.empty())
452 return (const char **)&m_argv[0];
Ed Masted78c9572014-04-20 00:31:37 +0000453 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454}
455
456void
457Args::Shift ()
458{
459 // Don't pop the last NULL terminator from the argv array
460 if (m_argv.size() > 1)
461 {
462 m_argv.erase(m_argv.begin());
463 m_args.pop_front();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000464 if (!m_args_quote_char.empty())
465 m_args_quote_char.erase(m_args_quote_char.begin());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 }
467}
468
469const char *
470Args::Unshift (const char *arg_cstr, char quote_char)
471{
472 m_args.push_front(arg_cstr);
473 m_argv.insert(m_argv.begin(), m_args.front().c_str());
474 m_args_quote_char.insert(m_args_quote_char.begin(), quote_char);
475 return GetArgumentAtIndex (0);
476}
477
478void
479Args::AppendArguments (const Args &rhs)
480{
481 const size_t rhs_argc = rhs.GetArgumentCount();
482 for (size_t i=0; i<rhs_argc; ++i)
483 AppendArgument(rhs.GetArgumentAtIndex(i));
484}
485
Greg Clayton982c9762011-11-03 21:22:33 +0000486void
487Args::AppendArguments (const char **argv)
488{
489 if (argv)
490 {
491 for (uint32_t i=0; argv[i]; ++i)
492 AppendArgument(argv[i]);
493 }
494}
495
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496const char *
497Args::AppendArgument (const char *arg_cstr, char quote_char)
498{
499 return InsertArgumentAtIndex (GetArgumentCount(), arg_cstr, quote_char);
500}
501
502const char *
503Args::InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
504{
505 // Since we are using a std::list to hold onto the copied C string and
506 // we don't have direct access to the elements, we have to iterate to
507 // find the value.
508 arg_sstr_collection::iterator pos, end = m_args.end();
509 size_t i = idx;
510 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
511 --i;
512
513 pos = m_args.insert(pos, arg_cstr);
514
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000515 if (idx >= m_args_quote_char.size())
516 {
517 m_args_quote_char.resize(idx + 1);
518 m_args_quote_char[idx] = quote_char;
519 }
520 else
521 m_args_quote_char.insert(m_args_quote_char.begin() + idx, quote_char);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522
523 UpdateArgvFromArgs();
524 return GetArgumentAtIndex(idx);
525}
526
527const char *
528Args::ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
529{
530 // Since we are using a std::list to hold onto the copied C string and
531 // we don't have direct access to the elements, we have to iterate to
532 // find the value.
533 arg_sstr_collection::iterator pos, end = m_args.end();
534 size_t i = idx;
535 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
536 --i;
537
538 if (pos != end)
539 {
540 pos->assign(arg_cstr);
541 assert(idx < m_argv.size() - 1);
542 m_argv[idx] = pos->c_str();
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000543 if (idx >= m_args_quote_char.size())
544 m_args_quote_char.resize(idx + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 m_args_quote_char[idx] = quote_char;
546 return GetArgumentAtIndex(idx);
547 }
Ed Masted78c9572014-04-20 00:31:37 +0000548 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549}
550
551void
552Args::DeleteArgumentAtIndex (size_t idx)
553{
554 // Since we are using a std::list to hold onto the copied C string and
555 // we don't have direct access to the elements, we have to iterate to
556 // find the value.
557 arg_sstr_collection::iterator pos, end = m_args.end();
558 size_t i = idx;
559 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
560 --i;
561
562 if (pos != end)
563 {
564 m_args.erase (pos);
565 assert(idx < m_argv.size() - 1);
566 m_argv.erase(m_argv.begin() + idx);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000567 if (idx < m_args_quote_char.size())
568 m_args_quote_char.erase(m_args_quote_char.begin() + idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 }
570}
571
572void
Greg Claytonc7bece562013-01-25 18:06:21 +0000573Args::SetArguments (size_t argc, const char **argv)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574{
575 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
576 // no need to clear it here.
577 m_args.clear();
578 m_args_quote_char.clear();
579
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 // First copy each string
Greg Clayton982c9762011-11-03 21:22:33 +0000581 for (size_t i=0; i<argc; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 {
583 m_args.push_back (argv[i]);
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000584 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 m_args_quote_char.push_back (argv[i][0]);
586 else
587 m_args_quote_char.push_back ('\0');
588 }
589
590 UpdateArgvFromArgs();
591}
592
Greg Clayton982c9762011-11-03 21:22:33 +0000593void
594Args::SetArguments (const char **argv)
595{
596 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
597 // no need to clear it here.
598 m_args.clear();
599 m_args_quote_char.clear();
600
601 if (argv)
602 {
603 // First copy each string
604 for (size_t i=0; argv[i]; ++i)
605 {
606 m_args.push_back (argv[i]);
607 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
608 m_args_quote_char.push_back (argv[i][0]);
609 else
610 m_args_quote_char.push_back ('\0');
611 }
612 }
613
614 UpdateArgvFromArgs();
615}
616
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617
618Error
619Args::ParseOptions (Options &options)
620{
621 StreamString sstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 Error error;
Virgile Belloe2607b52013-09-05 16:42:23 +0000623 Option *long_options = options.GetLongOptions();
Ed Masted78c9572014-04-20 00:31:37 +0000624 if (long_options == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000626 error.SetErrorStringWithFormat("invalid long options");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 return error;
628 }
629
Zachary Turnerd37221d2014-07-09 16:31:49 +0000630 for (int i=0; long_options[i].definition != nullptr; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 {
Ed Masted78c9572014-04-20 00:31:37 +0000632 if (long_options[i].flag == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 {
Daniel Malea90b0c842012-12-05 20:24:57 +0000634 if (isprint8(long_options[i].val))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000636 sstr << (char)long_options[i].val;
Zachary Turnerd37221d2014-07-09 16:31:49 +0000637 switch (long_options[i].definition->option_has_arg)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000638 {
639 default:
Virgile Belloe2607b52013-09-05 16:42:23 +0000640 case OptionParser::eNoArgument: break;
641 case OptionParser::eRequiredArgument: sstr << ':'; break;
642 case OptionParser::eOptionalArgument: sstr << "::"; break;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000643 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 }
645 }
646 }
Virgile Belloe2607b52013-09-05 16:42:23 +0000647 OptionParser::Prepare();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 int val;
649 while (1)
650 {
651 int long_options_index = -1;
Virgile Belloe2607b52013-09-05 16:42:23 +0000652 val = OptionParser::Parse(GetArgumentCount(),
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000653 GetArgumentVector(),
654 sstr.GetData(),
655 long_options,
656 &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 if (val == -1)
658 break;
659
660 // Did we get an error?
661 if (val == '?')
662 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000663 error.SetErrorStringWithFormat("unknown or ambiguous option");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 break;
665 }
666 // The option auto-set itself
667 if (val == 0)
668 continue;
669
670 ((Options *) &options)->OptionSeen (val);
671
672 // Lookup the long option index
673 if (long_options_index == -1)
674 {
675 for (int i=0;
Zachary Turnerd37221d2014-07-09 16:31:49 +0000676 long_options[i].definition || long_options[i].flag || long_options[i].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 ++i)
678 {
679 if (long_options[i].val == val)
680 {
681 long_options_index = i;
682 break;
683 }
684 }
685 }
686 // Call the callback with the option
Jason Molenda320e7b32014-10-16 01:26:51 +0000687 if (long_options_index >= 0 && long_options[long_options_index].definition)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 {
Zachary Turnerd37221d2014-07-09 16:31:49 +0000689 const OptionDefinition *def = long_options[long_options_index].definition;
690 CommandInterpreter &interpreter = options.GetInterpreter();
691 OptionValidator *validator = def->validator;
692 if (validator && !validator->IsValid(*interpreter.GetPlatform(true), interpreter.GetExecutionContext()))
693 {
694 error.SetErrorStringWithFormat("Option \"%s\" invalid. %s", def->long_option, def->validator->LongConditionString());
695 }
696 else
697 {
698 error = options.SetOptionValue(long_options_index,
Todd Fiala2c77a422014-10-10 01:11:39 +0000699 (def->option_has_arg == OptionParser::eNoArgument) ? nullptr : OptionParser::GetOptionArgument());
Zachary Turnerd37221d2014-07-09 16:31:49 +0000700 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 }
702 else
703 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000704 error.SetErrorStringWithFormat("invalid option with value '%i'", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 }
706 if (error.Fail())
707 break;
708 }
709
710 // Update our ARGV now that get options has consumed all the options
Virgile Belloe2607b52013-09-05 16:42:23 +0000711 m_argv.erase(m_argv.begin(), m_argv.begin() + OptionParser::GetOptionIndex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 UpdateArgsAfterOptionParsing ();
713 return error;
714}
715
716void
717Args::Clear ()
718{
719 m_args.clear ();
720 m_argv.clear ();
721 m_args_quote_char.clear();
722}
723
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724lldb::addr_t
Greg Claytonb9d5df52012-12-06 22:49:16 +0000725Args::StringToAddress (const ExecutionContext *exe_ctx, const char *s, lldb::addr_t fail_value, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726{
Greg Claytonb9d5df52012-12-06 22:49:16 +0000727 bool error_set = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 if (s && s[0])
729 {
Ed Masted78c9572014-04-20 00:31:37 +0000730 char *end = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 lldb::addr_t addr = ::strtoull (s, &end, 0);
732 if (*end == '\0')
733 {
Greg Claytonb9d5df52012-12-06 22:49:16 +0000734 if (error_ptr)
735 error_ptr->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 return addr; // All characters were used, return the result
737 }
738 // Try base 16 with no prefix...
739 addr = ::strtoull (s, &end, 16);
740 if (*end == '\0')
741 {
Greg Claytonb9d5df52012-12-06 22:49:16 +0000742 if (error_ptr)
743 error_ptr->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 return addr; // All characters were used, return the result
745 }
Greg Claytonb9d5df52012-12-06 22:49:16 +0000746
747 if (exe_ctx)
748 {
749 Target *target = exe_ctx->GetTargetPtr();
750 if (target)
751 {
752 lldb::ValueObjectSP valobj_sp;
753 EvaluateExpressionOptions options;
754 options.SetCoerceToId(false);
755 options.SetUnwindOnError(true);
756 options.SetKeepInMemory(false);
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000757 options.SetTryAllThreads(true);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000758
Jim Ingham1624a2d2014-05-05 02:26:40 +0000759 ExpressionResults expr_result = target->EvaluateExpression(s,
Enrico Granataca0e5ad2014-10-09 23:09:40 +0000760 exe_ctx->GetFramePtr(),
761 valobj_sp,
762 options);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000763
764 bool success = false;
Jim Ingham8646d3c2014-05-05 02:47:44 +0000765 if (expr_result == eExpressionCompleted)
Greg Claytonb9d5df52012-12-06 22:49:16 +0000766 {
Enrico Granataca0e5ad2014-10-09 23:09:40 +0000767 if (valobj_sp)
768 valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(valobj_sp->GetDynamicValueType(), true);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000769 // Get the address to watch.
Enrico Granataca0e5ad2014-10-09 23:09:40 +0000770 if (valobj_sp)
771 addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000772 if (success)
773 {
774 if (error_ptr)
775 error_ptr->Clear();
776 return addr;
777 }
778 else
779 {
780 if (error_ptr)
781 {
782 error_set = true;
783 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());
784 }
785 }
786
787 }
788 else
789 {
790 // Since the compiler can't handle things like "main + 12" we should
791 // try to do this for now. The compliler doesn't like adding offsets
792 // to function pointer types.
Greg Claytonbc43cab2013-04-03 21:37:16 +0000793 static RegularExpression g_symbol_plus_offset_regex("^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
794 RegularExpression::Match regex_match(3);
795 if (g_symbol_plus_offset_regex.Execute(s, &regex_match))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000796 {
797 uint64_t offset = 0;
798 bool add = true;
799 std::string name;
800 std::string str;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000801 if (regex_match.GetMatchAtIndex(s, 1, name))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000802 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000803 if (regex_match.GetMatchAtIndex(s, 2, str))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000804 {
805 add = str[0] == '+';
806
Greg Claytonbc43cab2013-04-03 21:37:16 +0000807 if (regex_match.GetMatchAtIndex(s, 3, str))
Greg Claytonb9d5df52012-12-06 22:49:16 +0000808 {
Vince Harron5275aaa2015-01-15 20:08:35 +0000809 offset = StringConvert::ToUInt64(str.c_str(), 0, 0, &success);
Greg Claytonb9d5df52012-12-06 22:49:16 +0000810
811 if (success)
812 {
813 Error error;
814 addr = StringToAddress (exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
815 if (addr != LLDB_INVALID_ADDRESS)
816 {
817 if (add)
818 return addr + offset;
819 else
820 return addr - offset;
821 }
822 }
823 }
824 }
825 }
826 }
827
828 if (error_ptr)
829 {
830 error_set = true;
831 error_ptr->SetErrorStringWithFormat("address expression \"%s\" evaluation failed", s);
832 }
833 }
834 }
835 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 }
Greg Claytonb9d5df52012-12-06 22:49:16 +0000837 if (error_ptr)
838 {
839 if (!error_set)
840 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"", s);
841 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842 return fail_value;
843}
844
Greg Clayton30820f02013-03-05 23:52:49 +0000845const char *
846Args::StripSpaces (std::string &s, bool leading, bool trailing, bool return_null_if_empty)
847{
848 static const char *k_white_space = " \t\v";
849 if (!s.empty())
850 {
851 if (leading)
852 {
853 size_t pos = s.find_first_not_of (k_white_space);
854 if (pos == std::string::npos)
855 s.clear();
856 else if (pos > 0)
857 s.erase(0, pos);
858 }
859
860 if (trailing)
861 {
862 size_t rpos = s.find_last_not_of(k_white_space);
863 if (rpos != std::string::npos && rpos + 1 < s.size())
864 s.erase(rpos + 1);
865 }
866 }
867 if (return_null_if_empty && s.empty())
Ed Masted78c9572014-04-20 00:31:37 +0000868 return nullptr;
Greg Clayton30820f02013-03-05 23:52:49 +0000869 return s.c_str();
870}
871
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872bool
873Args::StringToBoolean (const char *s, bool fail_value, bool *success_ptr)
874{
875 if (s && s[0])
876 {
877 if (::strcasecmp (s, "false") == 0 ||
878 ::strcasecmp (s, "off") == 0 ||
879 ::strcasecmp (s, "no") == 0 ||
880 ::strcmp (s, "0") == 0)
881 {
882 if (success_ptr)
883 *success_ptr = true;
884 return false;
885 }
886 else
887 if (::strcasecmp (s, "true") == 0 ||
888 ::strcasecmp (s, "on") == 0 ||
889 ::strcasecmp (s, "yes") == 0 ||
890 ::strcmp (s, "1") == 0)
891 {
892 if (success_ptr) *success_ptr = true;
893 return true;
894 }
895 }
896 if (success_ptr) *success_ptr = false;
897 return fail_value;
898}
899
Zachary Turner3e7442b2015-01-12 20:44:02 +0000900char
901Args::StringToChar(const char *s, char fail_value, bool *success_ptr)
902{
903 bool success = false;
904 char result = fail_value;
905
906 if (s)
907 {
908 size_t length = strlen(s);
909 if (length == 1)
910 {
911 success = true;
912 result = s[0];
913 }
914 }
915 if (success_ptr)
916 *success_ptr = success;
917 return result;
918}
919
Greg Claytonded470d2011-03-19 01:12:21 +0000920const char *
921Args::StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update)
922{
923 major = UINT32_MAX;
924 minor = UINT32_MAX;
925 update = UINT32_MAX;
926
927 if (s && s[0])
928 {
Ed Masted78c9572014-04-20 00:31:37 +0000929 char *pos = nullptr;
Greg Claytonc7bece562013-01-25 18:06:21 +0000930 unsigned long uval32 = ::strtoul (s, &pos, 0);
Greg Claytonded470d2011-03-19 01:12:21 +0000931 if (pos == s)
932 return s;
933 major = uval32;
934 if (*pos == '\0')
935 {
936 return pos; // Decoded major and got end of string
937 }
938 else if (*pos == '.')
939 {
940 const char *minor_cstr = pos + 1;
941 uval32 = ::strtoul (minor_cstr, &pos, 0);
942 if (pos == minor_cstr)
943 return pos; // Didn't get any digits for the minor version...
944 minor = uval32;
945 if (*pos == '.')
946 {
947 const char *update_cstr = pos + 1;
948 uval32 = ::strtoul (update_cstr, &pos, 0);
949 if (pos == update_cstr)
950 return pos;
951 update = uval32;
952 }
953 return pos;
954 }
955 }
Ed Masted78c9572014-04-20 00:31:37 +0000956 return nullptr;
Greg Claytonded470d2011-03-19 01:12:21 +0000957}
958
Greg Clayton144f3a92011-11-15 03:53:30 +0000959const char *
960Args::GetShellSafeArgument (const char *unsafe_arg, std::string &safe_arg)
961{
962 safe_arg.assign (unsafe_arg);
963 size_t prev_pos = 0;
964 while (prev_pos < safe_arg.size())
965 {
966 // Escape spaces and quotes
967 size_t pos = safe_arg.find_first_of(" '\"", prev_pos);
968 if (pos != std::string::npos)
969 {
970 safe_arg.insert (pos, 1, '\\');
971 prev_pos = pos + 2;
972 }
973 else
974 break;
975 }
976 return safe_arg.c_str();
977}
978
Greg Claytonded470d2011-03-19 01:12:21 +0000979
Greg Claytonc7bece562013-01-25 18:06:21 +0000980int64_t
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000981Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982{
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000983 if (enum_values)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000985 if (s && s[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 {
Ed Masted78c9572014-04-20 00:31:37 +0000987 for (int i = 0; enum_values[i].string_value != nullptr ; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000989 if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value)
990 {
991 error.Clear();
992 return enum_values[i].value;
993 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994 }
995 }
Greg Claytoncf0e4f02011-10-07 18:58:12 +0000996
997 StreamString strm;
998 strm.PutCString ("invalid enumeration value, valid values are: ");
Ed Masted78c9572014-04-20 00:31:37 +0000999 for (int i = 0; enum_values[i].string_value != nullptr; i++)
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001000 {
1001 strm.Printf ("%s\"%s\"",
1002 i > 0 ? ", " : "",
1003 enum_values[i].string_value);
1004 }
1005 error.SetErrorString(strm.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006 }
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001007 else
1008 {
1009 error.SetErrorString ("invalid enumeration argument");
1010 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 return fail_value;
1012}
1013
1014ScriptLanguage
1015Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr)
1016{
1017 if (s && s[0])
1018 {
1019 if ((::strcasecmp (s, "python") == 0) ||
1020 (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault))
1021 {
1022 if (success_ptr) *success_ptr = true;
1023 return eScriptLanguagePython;
1024 }
1025 if (::strcasecmp (s, "none"))
1026 {
1027 if (success_ptr) *success_ptr = true;
1028 return eScriptLanguageNone;
1029 }
1030 }
1031 if (success_ptr) *success_ptr = false;
1032 return fail_value;
1033}
1034
1035Error
1036Args::StringToFormat
1037(
1038 const char *s,
Greg Clayton68ebae62011-04-28 20:55:26 +00001039 lldb::Format &format,
Greg Claytonc7bece562013-01-25 18:06:21 +00001040 size_t *byte_size_ptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041)
1042{
1043 format = eFormatInvalid;
1044 Error error;
1045
1046 if (s && s[0])
1047 {
Greg Clayton68ebae62011-04-28 20:55:26 +00001048 if (byte_size_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049 {
Greg Clayton68ebae62011-04-28 20:55:26 +00001050 if (isdigit (s[0]))
1051 {
Ed Masted78c9572014-04-20 00:31:37 +00001052 char *format_char = nullptr;
Greg Clayton68ebae62011-04-28 20:55:26 +00001053 unsigned long byte_size = ::strtoul (s, &format_char, 0);
1054 if (byte_size != ULONG_MAX)
1055 *byte_size_ptr = byte_size;
1056 s = format_char;
1057 }
1058 else
1059 *byte_size_ptr = 0;
1060 }
1061
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001062 const bool partial_match_ok = true;
1063 if (!FormatManager::GetFormatFromCString (s, partial_match_ok, format))
Greg Clayton68ebae62011-04-28 20:55:26 +00001064 {
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001065 StreamString error_strm;
1066 error_strm.Printf ("Invalid format character or name '%s'. Valid values are:\n", s);
Peter Collingbourne44c9b372011-06-24 01:12:22 +00001067 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
Greg Clayton68ebae62011-04-28 20:55:26 +00001068 {
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001069 char format_char = FormatManager::GetFormatAsFormatChar(f);
1070 if (format_char)
1071 error_strm.Printf ("'%c' or ", format_char);
1072
1073 error_strm.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
1074 error_strm.EOL();
Greg Clayton68ebae62011-04-28 20:55:26 +00001075 }
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001076
1077 if (byte_size_ptr)
1078 error_strm.PutCString ("An optional byte size can precede the format character.\n");
1079 error.SetErrorString(error_strm.GetString().c_str());
Greg Clayton68ebae62011-04-28 20:55:26 +00001080 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081
1082 if (error.Fail())
1083 return error;
1084 }
1085 else
1086 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001087 error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088 }
1089 return error;
1090}
1091
Greg Clayton2443cbd2012-08-24 01:42:50 +00001092lldb::Encoding
1093Args::StringToEncoding (const char *s, lldb::Encoding fail_value)
1094{
1095 if (s && s[0])
1096 {
1097 if (strcmp(s, "uint") == 0)
1098 return eEncodingUint;
1099 else if (strcmp(s, "sint") == 0)
1100 return eEncodingSint;
1101 else if (strcmp(s, "ieee754") == 0)
1102 return eEncodingIEEE754;
1103 else if (strcmp(s, "vector") == 0)
1104 return eEncodingVector;
1105 }
1106 return fail_value;
1107}
1108
1109uint32_t
1110Args::StringToGenericRegister (const char *s)
1111{
1112 if (s && s[0])
1113 {
1114 if (strcmp(s, "pc") == 0)
1115 return LLDB_REGNUM_GENERIC_PC;
1116 else if (strcmp(s, "sp") == 0)
1117 return LLDB_REGNUM_GENERIC_SP;
1118 else if (strcmp(s, "fp") == 0)
1119 return LLDB_REGNUM_GENERIC_FP;
Jason Molenda2fc43a32014-05-09 04:09:48 +00001120 else if (strcmp(s, "ra") == 0 || strcmp(s, "lr") == 0)
Greg Clayton2443cbd2012-08-24 01:42:50 +00001121 return LLDB_REGNUM_GENERIC_RA;
1122 else if (strcmp(s, "flags") == 0)
1123 return LLDB_REGNUM_GENERIC_FLAGS;
1124 else if (strncmp(s, "arg", 3) == 0)
1125 {
1126 if (s[3] && s[4] == '\0')
1127 {
1128 switch (s[3])
1129 {
1130 case '1': return LLDB_REGNUM_GENERIC_ARG1;
1131 case '2': return LLDB_REGNUM_GENERIC_ARG2;
1132 case '3': return LLDB_REGNUM_GENERIC_ARG3;
1133 case '4': return LLDB_REGNUM_GENERIC_ARG4;
1134 case '5': return LLDB_REGNUM_GENERIC_ARG5;
1135 case '6': return LLDB_REGNUM_GENERIC_ARG6;
1136 case '7': return LLDB_REGNUM_GENERIC_ARG7;
1137 case '8': return LLDB_REGNUM_GENERIC_ARG8;
1138 }
1139 }
1140 }
1141 }
1142 return LLDB_INVALID_REGNUM;
1143}
1144
1145
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146void
1147Args::LongestCommonPrefix (std::string &common_prefix)
1148{
1149 arg_sstr_collection::iterator pos, end = m_args.end();
1150 pos = m_args.begin();
1151 if (pos == end)
1152 common_prefix.clear();
1153 else
1154 common_prefix = (*pos);
1155
1156 for (++pos; pos != end; ++pos)
1157 {
Greg Claytonc982c762010-07-09 20:39:50 +00001158 size_t new_size = (*pos).size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159
1160 // First trim common_prefix if it is longer than the current element:
1161 if (common_prefix.size() > new_size)
1162 common_prefix.erase (new_size);
1163
1164 // Then trim it at the first disparity:
1165
Greg Claytonc982c762010-07-09 20:39:50 +00001166 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 {
1168 if ((*pos)[i] != common_prefix[i])
1169 {
1170 common_prefix.erase(i);
1171 break;
1172 }
1173 }
1174
1175 // If we've emptied the common prefix, we're done.
1176 if (common_prefix.empty())
1177 break;
1178 }
1179}
1180
Caroline Ticed9d63362010-12-07 19:58:26 +00001181size_t
Virgile Belloe2607b52013-09-05 16:42:23 +00001182Args::FindArgumentIndexForOption (Option *long_options, int long_options_index)
Caroline Ticed9d63362010-12-07 19:58:26 +00001183{
1184 char short_buffer[3];
1185 char long_buffer[255];
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001186 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", long_options[long_options_index].val);
Zachary Turnerd37221d2014-07-09 16:31:49 +00001187 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].definition->long_option);
Caroline Ticed9d63362010-12-07 19:58:26 +00001188 size_t end = GetArgumentCount ();
1189 size_t idx = 0;
1190 while (idx < end)
1191 {
1192 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
1193 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
1194 {
1195 return idx;
1196 }
1197 ++idx;
1198 }
1199
1200 return end;
1201}
1202
1203bool
1204Args::IsPositionalArgument (const char *arg)
1205{
Ed Masted78c9572014-04-20 00:31:37 +00001206 if (arg == nullptr)
Caroline Ticed9d63362010-12-07 19:58:26 +00001207 return false;
1208
1209 bool is_positional = true;
1210 char *cptr = (char *) arg;
1211
1212 if (cptr[0] == '%')
1213 {
1214 ++cptr;
1215 while (isdigit (cptr[0]))
1216 ++cptr;
1217 if (cptr[0] != '\0')
1218 is_positional = false;
1219 }
1220 else
1221 is_positional = false;
1222
1223 return is_positional;
1224}
1225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226void
Caroline Tice636d6ed2010-10-12 17:45:19 +00001227Args::ParseAliasOptions (Options &options,
1228 CommandReturnObject &result,
Caroline Tice844d2302010-12-09 22:52:49 +00001229 OptionArgVector *option_arg_vector,
1230 std::string &raw_input_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231{
1232 StreamString sstr;
1233 int i;
Virgile Belloe2607b52013-09-05 16:42:23 +00001234 Option *long_options = options.GetLongOptions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235
Ed Masted78c9572014-04-20 00:31:37 +00001236 if (long_options == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 {
1238 result.AppendError ("invalid long options");
1239 result.SetStatus (eReturnStatusFailed);
1240 return;
1241 }
1242
Zachary Turnerd37221d2014-07-09 16:31:49 +00001243 for (i = 0; long_options[i].definition != nullptr; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 {
Ed Masted78c9572014-04-20 00:31:37 +00001245 if (long_options[i].flag == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246 {
1247 sstr << (char) long_options[i].val;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001248 switch (long_options[i].definition->option_has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 {
1250 default:
Virgile Belloe2607b52013-09-05 16:42:23 +00001251 case OptionParser::eNoArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001253 case OptionParser::eRequiredArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 sstr << ":";
1255 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001256 case OptionParser::eOptionalArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257 sstr << "::";
1258 break;
1259 }
1260 }
1261 }
1262
Virgile Belloe2607b52013-09-05 16:42:23 +00001263 OptionParser::Prepare();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 int val;
1265 while (1)
1266 {
1267 int long_options_index = -1;
Virgile Belloe2607b52013-09-05 16:42:23 +00001268 val = OptionParser::Parse (GetArgumentCount(),
Greg Claytonb7ad58a2013-04-04 20:35:24 +00001269 GetArgumentVector(),
1270 sstr.GetData(),
1271 long_options,
1272 &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273
1274 if (val == -1)
1275 break;
1276
1277 if (val == '?')
1278 {
1279 result.AppendError ("unknown or ambiguous option");
1280 result.SetStatus (eReturnStatusFailed);
1281 break;
1282 }
1283
1284 if (val == 0)
1285 continue;
1286
Greg Clayton78d10192014-04-07 21:37:03 +00001287 options.OptionSeen (val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288
1289 // Look up the long option index
1290 if (long_options_index == -1)
1291 {
1292 for (int j = 0;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001293 long_options[j].definition || long_options[j].flag || long_options[j].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294 ++j)
1295 {
1296 if (long_options[j].val == val)
1297 {
1298 long_options_index = j;
1299 break;
1300 }
1301 }
1302 }
1303
1304 // See if the option takes an argument, and see if one was supplied.
1305 if (long_options_index >= 0)
1306 {
1307 StreamString option_str;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001308 option_str.Printf ("-%c", val);
Zachary Turnerd37221d2014-07-09 16:31:49 +00001309 const OptionDefinition *def = long_options[long_options_index].definition;
1310 int has_arg = (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311
Zachary Turnerd37221d2014-07-09 16:31:49 +00001312 switch (has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001314 case OptionParser::eNoArgument:
Caroline Ticed9d63362010-12-07 19:58:26 +00001315 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001316 OptionArgValue (OptionParser::eNoArgument, "<no-argument>")));
Caroline Tice5172e6c2010-09-12 04:48:45 +00001317 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001319 case OptionParser::eRequiredArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001320 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 {
1322 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001323 OptionArgValue (OptionParser::eRequiredArgument,
1324 std::string (OptionParser::GetOptionArgument()))));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001325 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1326 }
1327 else
1328 {
1329 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1330 option_str.GetData());
1331 result.SetStatus (eReturnStatusFailed);
1332 }
1333 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001334 case OptionParser::eOptionalArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001335 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 {
1337 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001338 OptionArgValue (OptionParser::eOptionalArgument,
1339 std::string (OptionParser::GetOptionArgument()))));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1341 }
1342 else
1343 {
1344 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Virgile Belloe2607b52013-09-05 16:42:23 +00001345 OptionArgValue (OptionParser::eOptionalArgument, "<no-argument>")));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1347 }
1348 break;
1349 default:
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001350 result.AppendErrorWithFormat ("error with options table; invalid value in has_arg field for option '%c'.\n", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351 result.SetStatus (eReturnStatusFailed);
1352 break;
1353 }
1354 }
1355 else
1356 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001357 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 result.SetStatus (eReturnStatusFailed);
1359 }
Caroline Tice636d6ed2010-10-12 17:45:19 +00001360
1361 if (long_options_index >= 0)
1362 {
1363 // 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 +00001364 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1365 // the raw_input_string, if one was passed in.
Caroline Ticed9d63362010-12-07 19:58:26 +00001366 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1367 if (idx < GetArgumentCount())
1368 {
Caroline Tice844d2302010-12-09 22:52:49 +00001369 if (raw_input_string.size() > 0)
1370 {
1371 const char *tmp_arg = GetArgumentAtIndex (idx);
1372 size_t pos = raw_input_string.find (tmp_arg);
1373 if (pos != std::string::npos)
1374 raw_input_string.erase (pos, strlen (tmp_arg));
1375 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001376 ReplaceArgumentAtIndex (idx, "");
Zachary Turnerd37221d2014-07-09 16:31:49 +00001377 if ((long_options[long_options_index].definition->option_has_arg != OptionParser::eNoArgument)
Ed Masted78c9572014-04-20 00:31:37 +00001378 && (OptionParser::GetOptionArgument() != nullptr)
Caroline Ticed9d63362010-12-07 19:58:26 +00001379 && (idx+1 < GetArgumentCount())
Virgile Belloe2607b52013-09-05 16:42:23 +00001380 && (strcmp (OptionParser::GetOptionArgument(), GetArgumentAtIndex(idx+1)) == 0))
Caroline Tice844d2302010-12-09 22:52:49 +00001381 {
1382 if (raw_input_string.size() > 0)
1383 {
1384 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1385 size_t pos = raw_input_string.find (tmp_arg);
1386 if (pos != std::string::npos)
1387 raw_input_string.erase (pos, strlen (tmp_arg));
1388 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001389 ReplaceArgumentAtIndex (idx+1, "");
Caroline Tice844d2302010-12-09 22:52:49 +00001390 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001391 }
Caroline Tice636d6ed2010-10-12 17:45:19 +00001392 }
1393
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 if (!result.Succeeded())
1395 break;
1396 }
1397}
1398
1399void
1400Args::ParseArgsForCompletion
1401(
1402 Options &options,
Jim Inghamd43e0092010-06-24 20:31:04 +00001403 OptionElementVector &option_element_vector,
1404 uint32_t cursor_index
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405)
1406{
1407 StreamString sstr;
Virgile Belloe2607b52013-09-05 16:42:23 +00001408 Option *long_options = options.GetLongOptions();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 option_element_vector.clear();
1410
Ed Masted78c9572014-04-20 00:31:37 +00001411 if (long_options == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 {
1413 return;
1414 }
1415
1416 // Leading : tells getopt to return a : for a missing option argument AND
1417 // to suppress error messages.
1418
1419 sstr << ":";
Zachary Turnerd37221d2014-07-09 16:31:49 +00001420 for (int i = 0; long_options[i].definition != nullptr; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421 {
Ed Masted78c9572014-04-20 00:31:37 +00001422 if (long_options[i].flag == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 {
1424 sstr << (char) long_options[i].val;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001425 switch (long_options[i].definition->option_has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 {
1427 default:
Virgile Belloe2607b52013-09-05 16:42:23 +00001428 case OptionParser::eNoArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001430 case OptionParser::eRequiredArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 sstr << ":";
1432 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001433 case OptionParser::eOptionalArgument:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434 sstr << "::";
1435 break;
1436 }
1437 }
1438 }
1439
Virgile Belloe2607b52013-09-05 16:42:23 +00001440 OptionParser::Prepare();
1441 OptionParser::EnableError(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442
1443 int val;
1444 const OptionDefinition *opt_defs = options.GetDefinitions();
1445
Virgile Belloe2607b52013-09-05 16:42:23 +00001446 // Fooey... OptionParser::Parse permutes the GetArgumentVector to move the options to the front.
1447 // So we have to build another Arg and pass that to OptionParser::Parse so it doesn't
Jim Inghamd43e0092010-06-24 20:31:04 +00001448 // change the one we have.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449
Greg Claytonc982c762010-07-09 20:39:50 +00001450 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451
Jim Inghamd43e0092010-06-24 20:31:04 +00001452 bool failed_once = false;
1453 uint32_t dash_dash_pos = -1;
1454
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 while (1)
1456 {
1457 bool missing_argument = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 int long_options_index = -1;
Jim Inghamd43e0092010-06-24 20:31:04 +00001459
Virgile Belloe2607b52013-09-05 16:42:23 +00001460 val = OptionParser::Parse (dummy_vec.size() - 1,
Greg Claytonb7ad58a2013-04-04 20:35:24 +00001461 (char *const *) &dummy_vec.front(),
1462 sstr.GetData(),
1463 long_options,
1464 &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465
1466 if (val == -1)
Jim Inghamd43e0092010-06-24 20:31:04 +00001467 {
1468 // When we're completing a "--" which is the last option on line,
1469 if (failed_once)
1470 break;
1471
1472 failed_once = true;
1473
1474 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1475 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1476 // user might want to complete options by long name. I make this work by checking whether the
1477 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
Virgile Belloe2607b52013-09-05 16:42:23 +00001478 // I let it pass to OptionParser::Parse which will terminate the option parsing.
Jim Inghamd43e0092010-06-24 20:31:04 +00001479 // Note, in either case we continue parsing the line so we can figure out what other options
1480 // were passed. This will be useful when we come to restricting completions based on what other
1481 // options we've seen on the line.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001483 if (static_cast<size_t>(OptionParser::GetOptionIndex()) < dummy_vec.size() - 1
Virgile Belloe2607b52013-09-05 16:42:23 +00001484 && (strcmp (dummy_vec[OptionParser::GetOptionIndex()-1], "--") == 0))
Jim Inghamd43e0092010-06-24 20:31:04 +00001485 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001486 dash_dash_pos = OptionParser::GetOptionIndex() - 1;
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001487 if (static_cast<size_t>(OptionParser::GetOptionIndex() - 1) == cursor_index)
Jim Inghamd43e0092010-06-24 20:31:04 +00001488 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001489 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001490 OptionArgElement::eBareDoubleDash));
1491 continue;
1492 }
1493 else
1494 break;
1495 }
1496 else
1497 break;
1498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499 else if (val == '?')
1500 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001501 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001502 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 continue;
1504 }
1505 else if (val == 0)
1506 {
1507 continue;
1508 }
1509 else if (val == ':')
1510 {
1511 // This is a missing argument.
Virgile Belloe2607b52013-09-05 16:42:23 +00001512 val = OptionParser::GetOptionErrorCause();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 missing_argument = true;
1514 }
1515
1516 ((Options *) &options)->OptionSeen (val);
1517
1518 // Look up the long option index
1519 if (long_options_index == -1)
1520 {
1521 for (int j = 0;
Zachary Turnerd37221d2014-07-09 16:31:49 +00001522 long_options[j].definition || long_options[j].flag || long_options[j].val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001523 ++j)
1524 {
1525 if (long_options[j].val == val)
1526 {
1527 long_options_index = j;
1528 break;
1529 }
1530 }
1531 }
1532
1533 // See if the option takes an argument, and see if one was supplied.
1534 if (long_options_index >= 0)
1535 {
1536 int opt_defs_index = -1;
1537 for (int i = 0; ; i++)
1538 {
1539 if (opt_defs[i].short_option == 0)
1540 break;
1541 else if (opt_defs[i].short_option == val)
1542 {
1543 opt_defs_index = i;
1544 break;
1545 }
1546 }
1547
Zachary Turnerd37221d2014-07-09 16:31:49 +00001548 const OptionDefinition *def = long_options[long_options_index].definition;
1549 int has_arg = (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
1550 switch (has_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001551 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001552 case OptionParser::eNoArgument:
1553 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 1, 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001555 case OptionParser::eRequiredArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001556 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001557 {
1558 int arg_index;
1559 if (missing_argument)
1560 arg_index = -1;
1561 else
Virgile Belloe2607b52013-09-05 16:42:23 +00001562 arg_index = OptionParser::GetOptionIndex() - 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563
Virgile Belloe2607b52013-09-05 16:42:23 +00001564 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565 }
1566 else
1567 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001568 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 1, -1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001569 }
1570 break;
Virgile Belloe2607b52013-09-05 16:42:23 +00001571 case OptionParser::eOptionalArgument:
Ed Masted78c9572014-04-20 00:31:37 +00001572 if (OptionParser::GetOptionArgument() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001574 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, OptionParser::GetOptionIndex() - 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575 }
1576 else
1577 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001578 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, OptionParser::GetOptionIndex() - 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 }
1580 break;
1581 default:
1582 // The options table is messed up. Here we'll just continue
Virgile Belloe2607b52013-09-05 16:42:23 +00001583 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001584 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585 break;
1586 }
1587 }
1588 else
1589 {
Virgile Belloe2607b52013-09-05 16:42:23 +00001590 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
Jim Inghamd43e0092010-06-24 20:31:04 +00001591 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592 }
1593 }
Jim Inghamd43e0092010-06-24 20:31:04 +00001594
1595 // 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 +00001596 // 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 +00001597 // 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 +00001598 // Note, a single quoted dash is not the same as a single dash...
Jim Inghamd43e0092010-06-24 20:31:04 +00001599
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001600 if ((static_cast<int32_t>(dash_dash_pos) == -1 || cursor_index < dash_dash_pos)
Jim Inghamdc498e52014-08-27 22:06:58 +00001601 && m_args_quote_char[cursor_index] == '\0'
Jim Inghamd43e0092010-06-24 20:31:04 +00001602 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1603 {
1604 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1605 OptionArgElement::eBareDash));
1606
1607 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001608}
Greg Clayton4c054102012-09-01 00:38:36 +00001609
1610void
1611Args::EncodeEscapeSequences (const char *src, std::string &dst)
1612{
1613 dst.clear();
1614 if (src)
1615 {
1616 for (const char *p = src; *p != '\0'; ++p)
1617 {
1618 size_t non_special_chars = ::strcspn (p, "\\");
1619 if (non_special_chars > 0)
1620 {
1621 dst.append(p, non_special_chars);
1622 p += non_special_chars;
1623 if (*p == '\0')
1624 break;
1625 }
1626
1627 if (*p == '\\')
1628 {
1629 ++p; // skip the slash
1630 switch (*p)
1631 {
1632 case 'a' : dst.append(1, '\a'); break;
1633 case 'b' : dst.append(1, '\b'); break;
1634 case 'f' : dst.append(1, '\f'); break;
1635 case 'n' : dst.append(1, '\n'); break;
1636 case 'r' : dst.append(1, '\r'); break;
1637 case 't' : dst.append(1, '\t'); break;
1638 case 'v' : dst.append(1, '\v'); break;
1639 case '\\': dst.append(1, '\\'); break;
1640 case '\'': dst.append(1, '\''); break;
1641 case '"' : dst.append(1, '"'); break;
1642 case '0' :
1643 // 1 to 3 octal chars
1644 {
1645 // Make a string that can hold onto the initial zero char,
1646 // up to 3 octal digits, and a terminating NULL.
1647 char oct_str[5] = { '\0', '\0', '\0', '\0', '\0' };
1648
1649 int i;
1650 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1651 oct_str[i] = p[i];
1652
1653 // We don't want to consume the last octal character since
1654 // the main for loop will do this for us, so we advance p by
1655 // one less than i (even if i is zero)
1656 p += i - 1;
Ed Masted78c9572014-04-20 00:31:37 +00001657 unsigned long octal_value = ::strtoul (oct_str, nullptr, 8);
Greg Clayton4c054102012-09-01 00:38:36 +00001658 if (octal_value <= UINT8_MAX)
1659 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001660 dst.append(1, (char)octal_value);
Greg Clayton4c054102012-09-01 00:38:36 +00001661 }
1662 }
1663 break;
1664
1665 case 'x':
1666 // hex number in the format
1667 if (isxdigit(p[1]))
1668 {
1669 ++p; // Skip the 'x'
1670
1671 // Make a string that can hold onto two hex chars plus a
1672 // NULL terminator
1673 char hex_str[3] = { *p, '\0', '\0' };
1674 if (isxdigit(p[1]))
1675 {
1676 ++p; // Skip the first of the two hex chars
1677 hex_str[1] = *p;
1678 }
1679
Ed Masted78c9572014-04-20 00:31:37 +00001680 unsigned long hex_value = strtoul (hex_str, nullptr, 16);
Greg Clayton4c054102012-09-01 00:38:36 +00001681 if (hex_value <= UINT8_MAX)
1682 dst.append (1, (char)hex_value);
1683 }
1684 else
1685 {
1686 dst.append(1, 'x');
1687 }
1688 break;
1689
1690 default:
1691 // Just desensitize any other character by just printing what
1692 // came after the '\'
1693 dst.append(1, *p);
1694 break;
1695
1696 }
1697 }
1698 }
1699 }
1700}
1701
1702
1703void
1704Args::ExpandEscapedCharacters (const char *src, std::string &dst)
1705{
1706 dst.clear();
1707 if (src)
1708 {
1709 for (const char *p = src; *p != '\0'; ++p)
1710 {
Daniel Malea90b0c842012-12-05 20:24:57 +00001711 if (isprint8(*p))
Greg Clayton4c054102012-09-01 00:38:36 +00001712 dst.append(1, *p);
1713 else
1714 {
1715 switch (*p)
1716 {
1717 case '\a': dst.append("\\a"); break;
1718 case '\b': dst.append("\\b"); break;
1719 case '\f': dst.append("\\f"); break;
1720 case '\n': dst.append("\\n"); break;
1721 case '\r': dst.append("\\r"); break;
1722 case '\t': dst.append("\\t"); break;
1723 case '\v': dst.append("\\v"); break;
1724 case '\'': dst.append("\\'"); break;
1725 case '"': dst.append("\\\""); break;
1726 case '\\': dst.append("\\\\"); break;
1727 default:
1728 {
1729 // Just encode as octal
1730 dst.append("\\0");
1731 char octal_str[32];
1732 snprintf(octal_str, sizeof(octal_str), "%o", *p);
1733 dst.append(octal_str);
1734 }
1735 break;
1736 }
1737 }
1738 }
1739 }
1740}
1741