blob: 16f26e6ef2beeef792ed8f30cdc047bb75145f61 [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
13#include <getopt.h>
Eli Friedman5661f922010-06-09 10:59:23 +000014#include <cstdlib>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham40af72e2010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamFile.h"
21#include "lldb/Core/StreamString.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000022#include "lldb/DataFormatters/FormatManager.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000023#include "lldb/Interpreter/Options.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"
27#include "lldb/Target/StackFrame.h"
28#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 \\'\"";
Greg Clayton6ad07dd2010-12-19 03:41:24 +0000170 const char *arg_end = NULL;
171 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);
234 if (arg_end + 1 != '\0')
235 {
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;
375 if (argv_cstr == NULL)
376 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());
411 m_argv.push_back(NULL);
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];
430 return NULL;
431}
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];
446 return NULL;
447}
448
449const char **
450Args::GetConstArgumentVector() const
451{
452 if (!m_argv.empty())
453 return (const char **)&m_argv[0];
454 return NULL;
455}
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 }
549 return NULL;
550}
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;
624 struct option *long_options = options.GetLongOptions();
625 if (long_options == NULL)
626 {
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
Greg Claytonb1320972010-07-14 00:18:15 +0000631 for (int i=0; long_options[i].name != NULL; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 {
633 if (long_options[i].flag == NULL)
634 {
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;
638 switch (long_options[i].has_arg)
639 {
640 default:
641 case no_argument: break;
642 case required_argument: sstr << ':'; break;
643 case optional_argument: sstr << "::"; break;
644 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645 }
646 }
647 }
Eli Friedmanadb35022010-06-13 19:18:49 +0000648#ifdef __GLIBC__
649 optind = 0;
650#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 optreset = 1;
652 optind = 1;
Eli Friedmanadb35022010-06-13 19:18:49 +0000653#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 int val;
655 while (1)
656 {
657 int long_options_index = -1;
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000658 val = ::getopt_long(GetArgumentCount(),
659 GetArgumentVector(),
660 sstr.GetData(),
661 long_options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 &long_options_index);
663 if (val == -1)
664 break;
665
666 // Did we get an error?
667 if (val == '?')
668 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000669 error.SetErrorStringWithFormat("unknown or ambiguous option");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 break;
671 }
672 // The option auto-set itself
673 if (val == 0)
674 continue;
675
676 ((Options *) &options)->OptionSeen (val);
677
678 // Lookup the long option index
679 if (long_options_index == -1)
680 {
681 for (int i=0;
682 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
683 ++i)
684 {
685 if (long_options[i].val == val)
686 {
687 long_options_index = i;
688 break;
689 }
690 }
691 }
692 // Call the callback with the option
693 if (long_options_index >= 0)
694 {
695 error = options.SetOptionValue(long_options_index,
696 long_options[long_options_index].has_arg == no_argument ? NULL : optarg);
697 }
698 else
699 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000700 error.SetErrorStringWithFormat("invalid option with value '%i'", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 }
702 if (error.Fail())
703 break;
704 }
705
706 // Update our ARGV now that get options has consumed all the options
707 m_argv.erase(m_argv.begin(), m_argv.begin() + optind);
708 UpdateArgsAfterOptionParsing ();
709 return error;
710}
711
712void
713Args::Clear ()
714{
715 m_args.clear ();
716 m_argv.clear ();
717 m_args_quote_char.clear();
718}
719
720int32_t
721Args::StringToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr)
722{
723 if (s && s[0])
724 {
725 char *end = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +0000726 const long sval = ::strtol (s, &end, base);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 if (*end == '\0')
728 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000729 if (success_ptr)
730 *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
731 return (int32_t)sval; // All characters were used, return the result
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
733 }
734 if (success_ptr) *success_ptr = false;
735 return fail_value;
736}
737
738uint32_t
739Args::StringToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr)
740{
741 if (s && s[0])
742 {
743 char *end = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +0000744 const unsigned long uval = ::strtoul (s, &end, base);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 if (*end == '\0')
746 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000747 if (success_ptr)
748 *success_ptr = (uval <= UINT32_MAX);
749 return (uint32_t)uval; // All characters were used, return the result
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 }
751 }
752 if (success_ptr) *success_ptr = false;
753 return fail_value;
754}
755
756
757int64_t
758Args::StringToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr)
759{
760 if (s && s[0])
761 {
762 char *end = NULL;
763 int64_t uval = ::strtoll (s, &end, base);
764 if (*end == '\0')
765 {
766 if (success_ptr) *success_ptr = true;
767 return uval; // All characters were used, return the result
768 }
769 }
770 if (success_ptr) *success_ptr = false;
771 return fail_value;
772}
773
774uint64_t
775Args::StringToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr)
776{
777 if (s && s[0])
778 {
779 char *end = NULL;
780 uint64_t uval = ::strtoull (s, &end, base);
781 if (*end == '\0')
782 {
783 if (success_ptr) *success_ptr = true;
784 return uval; // All characters were used, return the result
785 }
786 }
787 if (success_ptr) *success_ptr = false;
788 return fail_value;
789}
790
791lldb::addr_t
Greg Claytonb9d5df52012-12-06 22:49:16 +0000792Args::StringToAddress (const ExecutionContext *exe_ctx, const char *s, lldb::addr_t fail_value, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793{
Greg Claytonb9d5df52012-12-06 22:49:16 +0000794 bool error_set = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 if (s && s[0])
796 {
797 char *end = NULL;
798 lldb::addr_t addr = ::strtoull (s, &end, 0);
799 if (*end == '\0')
800 {
Greg Claytonb9d5df52012-12-06 22:49:16 +0000801 if (error_ptr)
802 error_ptr->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 return addr; // All characters were used, return the result
804 }
805 // Try base 16 with no prefix...
806 addr = ::strtoull (s, &end, 16);
807 if (*end == '\0')
808 {
Greg Claytonb9d5df52012-12-06 22:49:16 +0000809 if (error_ptr)
810 error_ptr->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 return addr; // All characters were used, return the result
812 }
Greg Claytonb9d5df52012-12-06 22:49:16 +0000813
814 if (exe_ctx)
815 {
816 Target *target = exe_ctx->GetTargetPtr();
817 if (target)
818 {
819 lldb::ValueObjectSP valobj_sp;
820 EvaluateExpressionOptions options;
821 options.SetCoerceToId(false);
822 options.SetUnwindOnError(true);
823 options.SetKeepInMemory(false);
824 options.SetRunOthers(true);
825
826 ExecutionResults expr_result = target->EvaluateExpression(s,
827 exe_ctx->GetFramePtr(),
828 valobj_sp,
829 options);
830
831 bool success = false;
832 if (expr_result == eExecutionCompleted)
833 {
834 // Get the address to watch.
835 addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
836 if (success)
837 {
838 if (error_ptr)
839 error_ptr->Clear();
840 return addr;
841 }
842 else
843 {
844 if (error_ptr)
845 {
846 error_set = true;
847 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());
848 }
849 }
850
851 }
852 else
853 {
854 // Since the compiler can't handle things like "main + 12" we should
855 // try to do this for now. The compliler doesn't like adding offsets
856 // to function pointer types.
857 RegularExpression symbol_plus_offset_regex("^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
858 if (symbol_plus_offset_regex.Execute(s, 3))
859 {
860 uint64_t offset = 0;
861 bool add = true;
862 std::string name;
863 std::string str;
864 if (symbol_plus_offset_regex.GetMatchAtIndex(s, 1, name))
865 {
866 if (symbol_plus_offset_regex.GetMatchAtIndex(s, 2, str))
867 {
868 add = str[0] == '+';
869
870 if (symbol_plus_offset_regex.GetMatchAtIndex(s, 3, str))
871 {
872 offset = Args::StringToUInt64(str.c_str(), 0, 0, &success);
873
874 if (success)
875 {
876 Error error;
877 addr = StringToAddress (exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
878 if (addr != LLDB_INVALID_ADDRESS)
879 {
880 if (add)
881 return addr + offset;
882 else
883 return addr - offset;
884 }
885 }
886 }
887 }
888 }
889 }
890
891 if (error_ptr)
892 {
893 error_set = true;
894 error_ptr->SetErrorStringWithFormat("address expression \"%s\" evaluation failed", s);
895 }
896 }
897 }
898 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 }
Greg Claytonb9d5df52012-12-06 22:49:16 +0000900 if (error_ptr)
901 {
902 if (!error_set)
903 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"", s);
904 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905 return fail_value;
906}
907
Greg Clayton30820f02013-03-05 23:52:49 +0000908const char *
909Args::StripSpaces (std::string &s, bool leading, bool trailing, bool return_null_if_empty)
910{
911 static const char *k_white_space = " \t\v";
912 if (!s.empty())
913 {
914 if (leading)
915 {
916 size_t pos = s.find_first_not_of (k_white_space);
917 if (pos == std::string::npos)
918 s.clear();
919 else if (pos > 0)
920 s.erase(0, pos);
921 }
922
923 if (trailing)
924 {
925 size_t rpos = s.find_last_not_of(k_white_space);
926 if (rpos != std::string::npos && rpos + 1 < s.size())
927 s.erase(rpos + 1);
928 }
929 }
930 if (return_null_if_empty && s.empty())
931 return NULL;
932 return s.c_str();
933}
934
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935bool
936Args::StringToBoolean (const char *s, bool fail_value, bool *success_ptr)
937{
938 if (s && s[0])
939 {
940 if (::strcasecmp (s, "false") == 0 ||
941 ::strcasecmp (s, "off") == 0 ||
942 ::strcasecmp (s, "no") == 0 ||
943 ::strcmp (s, "0") == 0)
944 {
945 if (success_ptr)
946 *success_ptr = true;
947 return false;
948 }
949 else
950 if (::strcasecmp (s, "true") == 0 ||
951 ::strcasecmp (s, "on") == 0 ||
952 ::strcasecmp (s, "yes") == 0 ||
953 ::strcmp (s, "1") == 0)
954 {
955 if (success_ptr) *success_ptr = true;
956 return true;
957 }
958 }
959 if (success_ptr) *success_ptr = false;
960 return fail_value;
961}
962
Greg Claytonded470d2011-03-19 01:12:21 +0000963const char *
964Args::StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update)
965{
966 major = UINT32_MAX;
967 minor = UINT32_MAX;
968 update = UINT32_MAX;
969
970 if (s && s[0])
971 {
972 char *pos = NULL;
Greg Claytonc7bece562013-01-25 18:06:21 +0000973 unsigned long uval32 = ::strtoul (s, &pos, 0);
Greg Claytonded470d2011-03-19 01:12:21 +0000974 if (pos == s)
975 return s;
976 major = uval32;
977 if (*pos == '\0')
978 {
979 return pos; // Decoded major and got end of string
980 }
981 else if (*pos == '.')
982 {
983 const char *minor_cstr = pos + 1;
984 uval32 = ::strtoul (minor_cstr, &pos, 0);
985 if (pos == minor_cstr)
986 return pos; // Didn't get any digits for the minor version...
987 minor = uval32;
988 if (*pos == '.')
989 {
990 const char *update_cstr = pos + 1;
991 uval32 = ::strtoul (update_cstr, &pos, 0);
992 if (pos == update_cstr)
993 return pos;
994 update = uval32;
995 }
996 return pos;
997 }
998 }
999 return 0;
1000}
1001
Greg Clayton144f3a92011-11-15 03:53:30 +00001002const char *
1003Args::GetShellSafeArgument (const char *unsafe_arg, std::string &safe_arg)
1004{
1005 safe_arg.assign (unsafe_arg);
1006 size_t prev_pos = 0;
1007 while (prev_pos < safe_arg.size())
1008 {
1009 // Escape spaces and quotes
1010 size_t pos = safe_arg.find_first_of(" '\"", prev_pos);
1011 if (pos != std::string::npos)
1012 {
1013 safe_arg.insert (pos, 1, '\\');
1014 prev_pos = pos + 2;
1015 }
1016 else
1017 break;
1018 }
1019 return safe_arg.c_str();
1020}
1021
Greg Claytonded470d2011-03-19 01:12:21 +00001022
Greg Claytonc7bece562013-01-25 18:06:21 +00001023int64_t
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001024Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025{
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001026 if (enum_values)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001028 if (s && s[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001030 for (int i = 0; enum_values[i].string_value != NULL ; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031 {
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001032 if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value)
1033 {
1034 error.Clear();
1035 return enum_values[i].value;
1036 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037 }
1038 }
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001039
1040 StreamString strm;
1041 strm.PutCString ("invalid enumeration value, valid values are: ");
1042 for (int i = 0; enum_values[i].string_value != NULL; i++)
1043 {
1044 strm.Printf ("%s\"%s\"",
1045 i > 0 ? ", " : "",
1046 enum_values[i].string_value);
1047 }
1048 error.SetErrorString(strm.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049 }
Greg Claytoncf0e4f02011-10-07 18:58:12 +00001050 else
1051 {
1052 error.SetErrorString ("invalid enumeration argument");
1053 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 return fail_value;
1055}
1056
1057ScriptLanguage
1058Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr)
1059{
1060 if (s && s[0])
1061 {
1062 if ((::strcasecmp (s, "python") == 0) ||
1063 (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault))
1064 {
1065 if (success_ptr) *success_ptr = true;
1066 return eScriptLanguagePython;
1067 }
1068 if (::strcasecmp (s, "none"))
1069 {
1070 if (success_ptr) *success_ptr = true;
1071 return eScriptLanguageNone;
1072 }
1073 }
1074 if (success_ptr) *success_ptr = false;
1075 return fail_value;
1076}
1077
1078Error
1079Args::StringToFormat
1080(
1081 const char *s,
Greg Clayton68ebae62011-04-28 20:55:26 +00001082 lldb::Format &format,
Greg Claytonc7bece562013-01-25 18:06:21 +00001083 size_t *byte_size_ptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084)
1085{
1086 format = eFormatInvalid;
1087 Error error;
1088
1089 if (s && s[0])
1090 {
Greg Clayton68ebae62011-04-28 20:55:26 +00001091 if (byte_size_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092 {
Greg Clayton68ebae62011-04-28 20:55:26 +00001093 if (isdigit (s[0]))
1094 {
1095 char *format_char = NULL;
1096 unsigned long byte_size = ::strtoul (s, &format_char, 0);
1097 if (byte_size != ULONG_MAX)
1098 *byte_size_ptr = byte_size;
1099 s = format_char;
1100 }
1101 else
1102 *byte_size_ptr = 0;
1103 }
1104
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001105 const bool partial_match_ok = true;
1106 if (!FormatManager::GetFormatFromCString (s, partial_match_ok, format))
Greg Clayton68ebae62011-04-28 20:55:26 +00001107 {
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001108 StreamString error_strm;
1109 error_strm.Printf ("Invalid format character or name '%s'. Valid values are:\n", s);
Peter Collingbourne44c9b372011-06-24 01:12:22 +00001110 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
Greg Clayton68ebae62011-04-28 20:55:26 +00001111 {
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001112 char format_char = FormatManager::GetFormatAsFormatChar(f);
1113 if (format_char)
1114 error_strm.Printf ("'%c' or ", format_char);
1115
1116 error_strm.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
1117 error_strm.EOL();
Greg Clayton68ebae62011-04-28 20:55:26 +00001118 }
Greg Claytonbb7f31f2011-06-23 21:22:24 +00001119
1120 if (byte_size_ptr)
1121 error_strm.PutCString ("An optional byte size can precede the format character.\n");
1122 error.SetErrorString(error_strm.GetString().c_str());
Greg Clayton68ebae62011-04-28 20:55:26 +00001123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124
1125 if (error.Fail())
1126 return error;
1127 }
1128 else
1129 {
Greg Clayton86edbf42011-10-26 00:56:27 +00001130 error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 }
1132 return error;
1133}
1134
Greg Clayton2443cbd2012-08-24 01:42:50 +00001135lldb::Encoding
1136Args::StringToEncoding (const char *s, lldb::Encoding fail_value)
1137{
1138 if (s && s[0])
1139 {
1140 if (strcmp(s, "uint") == 0)
1141 return eEncodingUint;
1142 else if (strcmp(s, "sint") == 0)
1143 return eEncodingSint;
1144 else if (strcmp(s, "ieee754") == 0)
1145 return eEncodingIEEE754;
1146 else if (strcmp(s, "vector") == 0)
1147 return eEncodingVector;
1148 }
1149 return fail_value;
1150}
1151
1152uint32_t
1153Args::StringToGenericRegister (const char *s)
1154{
1155 if (s && s[0])
1156 {
1157 if (strcmp(s, "pc") == 0)
1158 return LLDB_REGNUM_GENERIC_PC;
1159 else if (strcmp(s, "sp") == 0)
1160 return LLDB_REGNUM_GENERIC_SP;
1161 else if (strcmp(s, "fp") == 0)
1162 return LLDB_REGNUM_GENERIC_FP;
1163 else if (strcmp(s, "ra") == 0)
1164 return LLDB_REGNUM_GENERIC_RA;
1165 else if (strcmp(s, "flags") == 0)
1166 return LLDB_REGNUM_GENERIC_FLAGS;
1167 else if (strncmp(s, "arg", 3) == 0)
1168 {
1169 if (s[3] && s[4] == '\0')
1170 {
1171 switch (s[3])
1172 {
1173 case '1': return LLDB_REGNUM_GENERIC_ARG1;
1174 case '2': return LLDB_REGNUM_GENERIC_ARG2;
1175 case '3': return LLDB_REGNUM_GENERIC_ARG3;
1176 case '4': return LLDB_REGNUM_GENERIC_ARG4;
1177 case '5': return LLDB_REGNUM_GENERIC_ARG5;
1178 case '6': return LLDB_REGNUM_GENERIC_ARG6;
1179 case '7': return LLDB_REGNUM_GENERIC_ARG7;
1180 case '8': return LLDB_REGNUM_GENERIC_ARG8;
1181 }
1182 }
1183 }
1184 }
1185 return LLDB_INVALID_REGNUM;
1186}
1187
1188
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189void
1190Args::LongestCommonPrefix (std::string &common_prefix)
1191{
1192 arg_sstr_collection::iterator pos, end = m_args.end();
1193 pos = m_args.begin();
1194 if (pos == end)
1195 common_prefix.clear();
1196 else
1197 common_prefix = (*pos);
1198
1199 for (++pos; pos != end; ++pos)
1200 {
Greg Claytonc982c762010-07-09 20:39:50 +00001201 size_t new_size = (*pos).size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202
1203 // First trim common_prefix if it is longer than the current element:
1204 if (common_prefix.size() > new_size)
1205 common_prefix.erase (new_size);
1206
1207 // Then trim it at the first disparity:
1208
Greg Claytonc982c762010-07-09 20:39:50 +00001209 for (size_t i = 0; i < common_prefix.size(); i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210 {
1211 if ((*pos)[i] != common_prefix[i])
1212 {
1213 common_prefix.erase(i);
1214 break;
1215 }
1216 }
1217
1218 // If we've emptied the common prefix, we're done.
1219 if (common_prefix.empty())
1220 break;
1221 }
1222}
1223
Caroline Ticed9d63362010-12-07 19:58:26 +00001224size_t
1225Args::FindArgumentIndexForOption (struct option *long_options, int long_options_index)
1226{
1227 char short_buffer[3];
1228 char long_buffer[255];
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001229 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", long_options[long_options_index].val);
Caroline Ticed9d63362010-12-07 19:58:26 +00001230 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].name);
1231 size_t end = GetArgumentCount ();
1232 size_t idx = 0;
1233 while (idx < end)
1234 {
1235 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
1236 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
1237 {
1238 return idx;
1239 }
1240 ++idx;
1241 }
1242
1243 return end;
1244}
1245
1246bool
1247Args::IsPositionalArgument (const char *arg)
1248{
1249 if (arg == NULL)
1250 return false;
1251
1252 bool is_positional = true;
1253 char *cptr = (char *) arg;
1254
1255 if (cptr[0] == '%')
1256 {
1257 ++cptr;
1258 while (isdigit (cptr[0]))
1259 ++cptr;
1260 if (cptr[0] != '\0')
1261 is_positional = false;
1262 }
1263 else
1264 is_positional = false;
1265
1266 return is_positional;
1267}
1268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269void
Caroline Tice636d6ed2010-10-12 17:45:19 +00001270Args::ParseAliasOptions (Options &options,
1271 CommandReturnObject &result,
Caroline Tice844d2302010-12-09 22:52:49 +00001272 OptionArgVector *option_arg_vector,
1273 std::string &raw_input_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274{
1275 StreamString sstr;
1276 int i;
1277 struct option *long_options = options.GetLongOptions();
1278
1279 if (long_options == NULL)
1280 {
1281 result.AppendError ("invalid long options");
1282 result.SetStatus (eReturnStatusFailed);
1283 return;
1284 }
1285
1286 for (i = 0; long_options[i].name != NULL; ++i)
1287 {
1288 if (long_options[i].flag == NULL)
1289 {
1290 sstr << (char) long_options[i].val;
1291 switch (long_options[i].has_arg)
1292 {
1293 default:
1294 case no_argument:
1295 break;
1296 case required_argument:
1297 sstr << ":";
1298 break;
1299 case optional_argument:
1300 sstr << "::";
1301 break;
1302 }
1303 }
1304 }
1305
Eli Friedmanadb35022010-06-13 19:18:49 +00001306#ifdef __GLIBC__
1307 optind = 0;
1308#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001309 optreset = 1;
1310 optind = 1;
Eli Friedmanadb35022010-06-13 19:18:49 +00001311#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 int val;
1313 while (1)
1314 {
1315 int long_options_index = -1;
1316 val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
1317 &long_options_index);
1318
1319 if (val == -1)
1320 break;
1321
1322 if (val == '?')
1323 {
1324 result.AppendError ("unknown or ambiguous option");
1325 result.SetStatus (eReturnStatusFailed);
1326 break;
1327 }
1328
1329 if (val == 0)
1330 continue;
1331
1332 ((Options *) &options)->OptionSeen (val);
1333
1334 // Look up the long option index
1335 if (long_options_index == -1)
1336 {
1337 for (int j = 0;
1338 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1339 ++j)
1340 {
1341 if (long_options[j].val == val)
1342 {
1343 long_options_index = j;
1344 break;
1345 }
1346 }
1347 }
1348
1349 // See if the option takes an argument, and see if one was supplied.
1350 if (long_options_index >= 0)
1351 {
1352 StreamString option_str;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001353 option_str.Printf ("-%c", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354
1355 switch (long_options[long_options_index].has_arg)
1356 {
1357 case no_argument:
Caroline Ticed9d63362010-12-07 19:58:26 +00001358 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1359 OptionArgValue (no_argument, "<no-argument>")));
Caroline Tice5172e6c2010-09-12 04:48:45 +00001360 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 break;
1362 case required_argument:
1363 if (optarg != NULL)
1364 {
1365 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Ticed9d63362010-12-07 19:58:26 +00001366 OptionArgValue (required_argument,
1367 std::string (optarg))));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1369 }
1370 else
1371 {
1372 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1373 option_str.GetData());
1374 result.SetStatus (eReturnStatusFailed);
1375 }
1376 break;
1377 case optional_argument:
1378 if (optarg != NULL)
1379 {
1380 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Ticed9d63362010-12-07 19:58:26 +00001381 OptionArgValue (optional_argument,
1382 std::string (optarg))));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1384 }
1385 else
1386 {
1387 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
Caroline Ticed9d63362010-12-07 19:58:26 +00001388 OptionArgValue (optional_argument, "<no-argument>")));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1390 }
1391 break;
1392 default:
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001393 result.AppendErrorWithFormat ("error with options table; invalid value in has_arg field for option '%c'.\n", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 result.SetStatus (eReturnStatusFailed);
1395 break;
1396 }
1397 }
1398 else
1399 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +00001400 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401 result.SetStatus (eReturnStatusFailed);
1402 }
Caroline Tice636d6ed2010-10-12 17:45:19 +00001403
1404 if (long_options_index >= 0)
1405 {
1406 // 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 +00001407 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1408 // the raw_input_string, if one was passed in.
Caroline Ticed9d63362010-12-07 19:58:26 +00001409 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1410 if (idx < GetArgumentCount())
1411 {
Caroline Tice844d2302010-12-09 22:52:49 +00001412 if (raw_input_string.size() > 0)
1413 {
1414 const char *tmp_arg = GetArgumentAtIndex (idx);
1415 size_t pos = raw_input_string.find (tmp_arg);
1416 if (pos != std::string::npos)
1417 raw_input_string.erase (pos, strlen (tmp_arg));
1418 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001419 ReplaceArgumentAtIndex (idx, "");
1420 if ((long_options[long_options_index].has_arg != no_argument)
1421 && (optarg != NULL)
1422 && (idx+1 < GetArgumentCount())
1423 && (strcmp (optarg, GetArgumentAtIndex(idx+1)) == 0))
Caroline Tice844d2302010-12-09 22:52:49 +00001424 {
1425 if (raw_input_string.size() > 0)
1426 {
1427 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1428 size_t pos = raw_input_string.find (tmp_arg);
1429 if (pos != std::string::npos)
1430 raw_input_string.erase (pos, strlen (tmp_arg));
1431 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001432 ReplaceArgumentAtIndex (idx+1, "");
Caroline Tice844d2302010-12-09 22:52:49 +00001433 }
Caroline Ticed9d63362010-12-07 19:58:26 +00001434 }
Caroline Tice636d6ed2010-10-12 17:45:19 +00001435 }
1436
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 if (!result.Succeeded())
1438 break;
1439 }
1440}
1441
1442void
1443Args::ParseArgsForCompletion
1444(
1445 Options &options,
Jim Inghamd43e0092010-06-24 20:31:04 +00001446 OptionElementVector &option_element_vector,
1447 uint32_t cursor_index
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448)
1449{
1450 StreamString sstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 struct option *long_options = options.GetLongOptions();
1452 option_element_vector.clear();
1453
1454 if (long_options == NULL)
1455 {
1456 return;
1457 }
1458
1459 // Leading : tells getopt to return a : for a missing option argument AND
1460 // to suppress error messages.
1461
1462 sstr << ":";
Greg Claytonb1320972010-07-14 00:18:15 +00001463 for (int i = 0; long_options[i].name != NULL; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464 {
1465 if (long_options[i].flag == NULL)
1466 {
1467 sstr << (char) long_options[i].val;
1468 switch (long_options[i].has_arg)
1469 {
1470 default:
1471 case no_argument:
1472 break;
1473 case required_argument:
1474 sstr << ":";
1475 break;
1476 case optional_argument:
1477 sstr << "::";
1478 break;
1479 }
1480 }
1481 }
1482
Eli Friedmanadb35022010-06-13 19:18:49 +00001483#ifdef __GLIBC__
1484 optind = 0;
1485#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486 optreset = 1;
1487 optind = 1;
Eli Friedmanadb35022010-06-13 19:18:49 +00001488#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 opterr = 0;
1490
1491 int val;
1492 const OptionDefinition *opt_defs = options.GetDefinitions();
1493
Jim Inghamd43e0092010-06-24 20:31:04 +00001494 // Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 // So we have to build another Arg and pass that to getopt_long so it doesn't
Jim Inghamd43e0092010-06-24 20:31:04 +00001496 // change the one we have.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497
Greg Claytonc982c762010-07-09 20:39:50 +00001498 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499
Jim Inghamd43e0092010-06-24 20:31:04 +00001500 bool failed_once = false;
1501 uint32_t dash_dash_pos = -1;
1502
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 while (1)
1504 {
1505 bool missing_argument = false;
1506 int parse_start = optind;
1507 int long_options_index = -1;
Jim Inghamd43e0092010-06-24 20:31:04 +00001508
Greg Claytonc982c762010-07-09 20:39:50 +00001509 val = ::getopt_long (dummy_vec.size() - 1,
Greg Clayton471b31c2010-07-20 22:52:08 +00001510 (char *const *) &dummy_vec.front(),
Greg Claytonc982c762010-07-09 20:39:50 +00001511 sstr.GetData(),
1512 long_options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 &long_options_index);
1514
1515 if (val == -1)
Jim Inghamd43e0092010-06-24 20:31:04 +00001516 {
1517 // When we're completing a "--" which is the last option on line,
1518 if (failed_once)
1519 break;
1520
1521 failed_once = true;
1522
1523 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1524 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1525 // user might want to complete options by long name. I make this work by checking whether the
1526 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1527 // I let it pass to getopt_long which will terminate the option parsing.
1528 // Note, in either case we continue parsing the line so we can figure out what other options
1529 // were passed. This will be useful when we come to restricting completions based on what other
1530 // options we've seen on the line.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531
Jim Inghamd43e0092010-06-24 20:31:04 +00001532 if (optind < dummy_vec.size() - 1
1533 && (strcmp (dummy_vec[optind-1], "--") == 0))
1534 {
1535 dash_dash_pos = optind - 1;
1536 if (optind - 1 == cursor_index)
1537 {
1538 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
1539 OptionArgElement::eBareDoubleDash));
1540 continue;
1541 }
1542 else
1543 break;
1544 }
1545 else
1546 break;
1547 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001548 else if (val == '?')
1549 {
Jim Inghamd43e0092010-06-24 20:31:04 +00001550 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1551 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552 continue;
1553 }
1554 else if (val == 0)
1555 {
1556 continue;
1557 }
1558 else if (val == ':')
1559 {
1560 // This is a missing argument.
1561 val = optopt;
1562 missing_argument = true;
1563 }
1564
1565 ((Options *) &options)->OptionSeen (val);
1566
1567 // Look up the long option index
1568 if (long_options_index == -1)
1569 {
1570 for (int j = 0;
1571 long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
1572 ++j)
1573 {
1574 if (long_options[j].val == val)
1575 {
1576 long_options_index = j;
1577 break;
1578 }
1579 }
1580 }
1581
1582 // See if the option takes an argument, and see if one was supplied.
1583 if (long_options_index >= 0)
1584 {
1585 int opt_defs_index = -1;
1586 for (int i = 0; ; i++)
1587 {
1588 if (opt_defs[i].short_option == 0)
1589 break;
1590 else if (opt_defs[i].short_option == val)
1591 {
1592 opt_defs_index = i;
1593 break;
1594 }
1595 }
1596
1597 switch (long_options[long_options_index].has_arg)
1598 {
1599 case no_argument:
1600 option_element_vector.push_back (OptionArgElement (opt_defs_index, parse_start, 0));
1601 break;
1602 case required_argument:
1603 if (optarg != NULL)
1604 {
1605 int arg_index;
1606 if (missing_argument)
1607 arg_index = -1;
1608 else
Jim Inghamd43e0092010-06-24 20:31:04 +00001609 arg_index = optind - 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610
Jim Inghamd43e0092010-06-24 20:31:04 +00001611 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, arg_index));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001612 }
1613 else
1614 {
Jim Inghamd43e0092010-06-24 20:31:04 +00001615 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 1, -1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616 }
1617 break;
1618 case optional_argument:
1619 if (optarg != NULL)
1620 {
Jim Inghamd43e0092010-06-24 20:31:04 +00001621 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001622 }
1623 else
1624 {
Jim Inghamd43e0092010-06-24 20:31:04 +00001625 option_element_vector.push_back (OptionArgElement (opt_defs_index, optind - 2, optind - 1));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001626 }
1627 break;
1628 default:
1629 // The options table is messed up. Here we'll just continue
Jim Inghamd43e0092010-06-24 20:31:04 +00001630 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1631 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632 break;
1633 }
1634 }
1635 else
1636 {
Jim Inghamd43e0092010-06-24 20:31:04 +00001637 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, optind - 1,
1638 OptionArgElement::eUnrecognizedArg));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639 }
1640 }
Jim Inghamd43e0092010-06-24 20:31:04 +00001641
1642 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1643 // the option_element_vector, but only if it is not after the "--". But it turns out that getopt_long just ignores
1644 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1645
1646 if ((dash_dash_pos == -1 || cursor_index < dash_dash_pos)
1647 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1648 {
1649 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1650 OptionArgElement::eBareDash));
1651
1652 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653}
Greg Clayton4c054102012-09-01 00:38:36 +00001654
1655void
1656Args::EncodeEscapeSequences (const char *src, std::string &dst)
1657{
1658 dst.clear();
1659 if (src)
1660 {
1661 for (const char *p = src; *p != '\0'; ++p)
1662 {
1663 size_t non_special_chars = ::strcspn (p, "\\");
1664 if (non_special_chars > 0)
1665 {
1666 dst.append(p, non_special_chars);
1667 p += non_special_chars;
1668 if (*p == '\0')
1669 break;
1670 }
1671
1672 if (*p == '\\')
1673 {
1674 ++p; // skip the slash
1675 switch (*p)
1676 {
1677 case 'a' : dst.append(1, '\a'); break;
1678 case 'b' : dst.append(1, '\b'); break;
1679 case 'f' : dst.append(1, '\f'); break;
1680 case 'n' : dst.append(1, '\n'); break;
1681 case 'r' : dst.append(1, '\r'); break;
1682 case 't' : dst.append(1, '\t'); break;
1683 case 'v' : dst.append(1, '\v'); break;
1684 case '\\': dst.append(1, '\\'); break;
1685 case '\'': dst.append(1, '\''); break;
1686 case '"' : dst.append(1, '"'); break;
1687 case '0' :
1688 // 1 to 3 octal chars
1689 {
1690 // Make a string that can hold onto the initial zero char,
1691 // up to 3 octal digits, and a terminating NULL.
1692 char oct_str[5] = { '\0', '\0', '\0', '\0', '\0' };
1693
1694 int i;
1695 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1696 oct_str[i] = p[i];
1697
1698 // We don't want to consume the last octal character since
1699 // the main for loop will do this for us, so we advance p by
1700 // one less than i (even if i is zero)
1701 p += i - 1;
1702 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
1703 if (octal_value <= UINT8_MAX)
1704 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001705 dst.append(1, (char)octal_value);
Greg Clayton4c054102012-09-01 00:38:36 +00001706 }
1707 }
1708 break;
1709
1710 case 'x':
1711 // hex number in the format
1712 if (isxdigit(p[1]))
1713 {
1714 ++p; // Skip the 'x'
1715
1716 // Make a string that can hold onto two hex chars plus a
1717 // NULL terminator
1718 char hex_str[3] = { *p, '\0', '\0' };
1719 if (isxdigit(p[1]))
1720 {
1721 ++p; // Skip the first of the two hex chars
1722 hex_str[1] = *p;
1723 }
1724
1725 unsigned long hex_value = strtoul (hex_str, NULL, 16);
1726 if (hex_value <= UINT8_MAX)
1727 dst.append (1, (char)hex_value);
1728 }
1729 else
1730 {
1731 dst.append(1, 'x');
1732 }
1733 break;
1734
1735 default:
1736 // Just desensitize any other character by just printing what
1737 // came after the '\'
1738 dst.append(1, *p);
1739 break;
1740
1741 }
1742 }
1743 }
1744 }
1745}
1746
1747
1748void
1749Args::ExpandEscapedCharacters (const char *src, std::string &dst)
1750{
1751 dst.clear();
1752 if (src)
1753 {
1754 for (const char *p = src; *p != '\0'; ++p)
1755 {
Daniel Malea90b0c842012-12-05 20:24:57 +00001756 if (isprint8(*p))
Greg Clayton4c054102012-09-01 00:38:36 +00001757 dst.append(1, *p);
1758 else
1759 {
1760 switch (*p)
1761 {
1762 case '\a': dst.append("\\a"); break;
1763 case '\b': dst.append("\\b"); break;
1764 case '\f': dst.append("\\f"); break;
1765 case '\n': dst.append("\\n"); break;
1766 case '\r': dst.append("\\r"); break;
1767 case '\t': dst.append("\\t"); break;
1768 case '\v': dst.append("\\v"); break;
1769 case '\'': dst.append("\\'"); break;
1770 case '"': dst.append("\\\""); break;
1771 case '\\': dst.append("\\\\"); break;
1772 default:
1773 {
1774 // Just encode as octal
1775 dst.append("\\0");
1776 char octal_str[32];
1777 snprintf(octal_str, sizeof(octal_str), "%o", *p);
1778 dst.append(octal_str);
1779 }
1780 break;
1781 }
1782 }
1783 }
1784 }
1785}
1786