1 //===-- Args.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 // Other libraries and framework includes
15 #include "lldb/Interpreter/Args.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Core/StreamString.h"
19 #include "lldb/DataFormatters/FormatManager.h"
20 #include "lldb/Host/StringConvert.h"
21 #include "lldb/Interpreter/Options.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/StackFrame.h"
26 #include "lldb/Target/Target.h"
29 using namespace lldb_private;
31 //----------------------------------------------------------------------
33 //----------------------------------------------------------------------
34 Args::Args (llvm::StringRef command) :
39 SetCommandString (command);
43 //----------------------------------------------------------------------
44 // We have to be very careful on the copy constructor of this class
45 // to make sure we copy all of the string values, but we can't copy the
46 // rhs.m_argv into m_argv since it will point to the "const char *" c
47 // strings in rhs.m_args. We need to copy the string list and update our
48 // own m_argv appropriately.
49 //----------------------------------------------------------------------
50 Args::Args (const Args &rhs) :
53 m_args_quote_char(rhs.m_args_quote_char)
58 //----------------------------------------------------------------------
59 // We have to be very careful on the copy constructor of this class
60 // to make sure we copy all of the string values, but we can't copy the
61 // rhs.m_argv into m_argv since it will point to the "const char *" c
62 // strings in rhs.m_args. We need to copy the string list and update our
63 // own m_argv appropriately.
64 //----------------------------------------------------------------------
66 Args::operator= (const Args &rhs)
68 // Make sure we aren't assigning to self
72 m_args_quote_char = rhs.m_args_quote_char;
78 //----------------------------------------------------------------------
80 //----------------------------------------------------------------------
86 Args::Dump (Stream *s)
88 const size_t argc = m_argv.size();
89 for (size_t i=0; i<argc; ++i)
92 const char *arg_cstr = m_argv[i];
94 s->Printf("argv[%zi]=\"%s\"\n", i, arg_cstr);
96 s->Printf("argv[%zi]=NULL\n", i);
102 Args::GetCommandString (std::string &command) const
105 const size_t argc = GetArgumentCount();
106 for (size_t i=0; i<argc; ++i)
110 command += m_argv[i];
116 Args::GetQuotedCommandString (std::string &command) const
119 const size_t argc = GetArgumentCount();
120 for (size_t i = 0; i < argc; ++i)
123 command.append (1, ' ');
124 char quote_char = GetArgumentQuoteCharAtIndex(i);
127 command.append (1, quote_char);
128 command.append (m_argv[i]);
129 command.append (1, quote_char);
132 command.append (m_argv[i]);
137 // A helper function for argument parsing.
138 // Parses the initial part of the first argument using normal double quote rules:
139 // backslash escapes the double quote and itself. The parsed string is appended to the second
140 // argument. The function returns the unparsed portion of the string, starting at the closing
142 static llvm::StringRef
143 ParseDoubleQuotes(llvm::StringRef quoted, std::string &result)
145 // Inside double quotes, '\' and '"' are special.
146 static const char *k_escapable_characters = "\"\\";
149 // Skip over over regular characters and append them.
150 size_t regular = quoted.find_first_of(k_escapable_characters);
151 result += quoted.substr(0, regular);
152 quoted = quoted.substr(regular);
154 // If we have reached the end of string or the closing quote, we're done.
155 if (quoted.empty() || quoted.front() == '"')
158 // We have found a backslash.
159 quoted = quoted.drop_front();
163 // A lone backslash at the end of string, let's just append it.
168 // If the character after the backslash is not a whitelisted escapable character, we
169 // leave the character sequence untouched.
170 if (strchr(k_escapable_characters, quoted.front()) == nullptr)
173 result += quoted.front();
174 quoted = quoted.drop_front();
180 // A helper function for SetCommandString.
181 // Parses a single argument from the command string, processing quotes and backslashes in a
182 // shell-like manner. The parsed argument is appended to the m_args array. The function returns
183 // the unparsed portion of the string, starting at the first unqouted, unescaped whitespace
186 Args::ParseSingleArgument(llvm::StringRef command)
188 // Argument can be split into multiple discontiguous pieces,
191 // this would result in a single argument "Hello World" (without/
192 // the quotes) since the quotes would be removed and there is
193 // not space between the strings.
197 // Since we can have multiple quotes that form a single command
198 // in a command like: "Hello "world'!' (which will make a single
199 // argument "Hello world!") we remember the first quote character
200 // we encounter and use that for the quote character.
201 char first_quote_char = '\0';
203 bool arg_complete = false;
206 // Skip over over regular characters and append them.
207 size_t regular = command.find_first_of(" \t\"'`\\");
208 arg += command.substr(0, regular);
209 command = command.substr(regular);
214 char special = command.front();
215 command = command.drop_front();
225 // If the character after the backslash is not a whitelisted escapable character, we
226 // leave the character sequence untouched.
227 if (strchr(" \t\\'\"`", command.front()) == nullptr)
230 arg += command.front();
231 command = command.drop_front();
237 // We are not inside any quotes, we just found a space after an
238 // argument. We are done.
245 // We found the start of a quote scope.
246 if (first_quote_char == '\0')
247 first_quote_char = special;
250 command = ParseDoubleQuotes(command, arg);
253 // For single quotes, we simply skip ahead to the matching quote character
254 // (or the end of the string).
255 size_t quoted = command.find(special);
256 arg += command.substr(0, quoted);
257 command = command.substr(quoted);
260 // If we found a closing quote, skip it.
261 if (! command.empty())
262 command = command.drop_front();
266 } while (!arg_complete);
268 m_args.push_back(arg);
269 m_args_quote_char.push_back (first_quote_char);
274 Args::SetCommandString (llvm::StringRef command)
278 m_args_quote_char.clear();
280 static const char *k_space_separators = " \t";
281 command = command.ltrim(k_space_separators);
282 while (!command.empty())
284 command = ParseSingleArgument(command);
285 command = command.ltrim(k_space_separators);
288 UpdateArgvFromArgs();
292 Args::UpdateArgsAfterOptionParsing()
294 // Now m_argv might be out of date with m_args, so we need to fix that
295 arg_cstr_collection::const_iterator argv_pos, argv_end = m_argv.end();
296 arg_sstr_collection::iterator args_pos;
297 arg_quote_char_collection::iterator quotes_pos;
299 for (argv_pos = m_argv.begin(), args_pos = m_args.begin(), quotes_pos = m_args_quote_char.begin();
300 argv_pos != argv_end && args_pos != m_args.end();
303 const char *argv_cstr = *argv_pos;
304 if (argv_cstr == nullptr)
307 while (args_pos != m_args.end())
309 const char *args_cstr = args_pos->c_str();
310 if (args_cstr == argv_cstr)
312 // We found the argument that matches the C string in the
313 // vector, so we can now look for the next one
320 quotes_pos = m_args_quote_char.erase (quotes_pos);
321 args_pos = m_args.erase (args_pos);
326 if (args_pos != m_args.end())
327 m_args.erase (args_pos, m_args.end());
329 if (quotes_pos != m_args_quote_char.end())
330 m_args_quote_char.erase (quotes_pos, m_args_quote_char.end());
334 Args::UpdateArgvFromArgs()
337 arg_sstr_collection::const_iterator pos, end = m_args.end();
338 for (pos = m_args.begin(); pos != end; ++pos)
339 m_argv.push_back(pos->c_str());
340 m_argv.push_back(nullptr);
341 // Make sure we have enough arg quote chars in the array
342 if (m_args_quote_char.size() < m_args.size())
343 m_args_quote_char.resize (m_argv.size());
347 Args::GetArgumentCount() const
351 return m_argv.size() - 1;
355 Args::GetArgumentAtIndex (size_t idx) const
357 if (idx < m_argv.size())
363 Args::GetArgumentQuoteCharAtIndex (size_t idx) const
365 if (idx < m_args_quote_char.size())
366 return m_args_quote_char[idx];
371 Args::GetArgumentVector()
374 return const_cast<char **>(&m_argv[0]);
379 Args::GetConstArgumentVector() const
382 return const_cast<const char **>(&m_argv[0]);
389 // Don't pop the last NULL terminator from the argv array
390 if (m_argv.size() > 1)
392 m_argv.erase(m_argv.begin());
394 if (!m_args_quote_char.empty())
395 m_args_quote_char.erase(m_args_quote_char.begin());
400 Args::Unshift (const char *arg_cstr, char quote_char)
402 m_args.push_front(arg_cstr);
403 m_argv.insert(m_argv.begin(), m_args.front().c_str());
404 m_args_quote_char.insert(m_args_quote_char.begin(), quote_char);
405 return GetArgumentAtIndex (0);
409 Args::AppendArguments (const Args &rhs)
411 const size_t rhs_argc = rhs.GetArgumentCount();
412 for (size_t i=0; i<rhs_argc; ++i)
413 AppendArgument(rhs.GetArgumentAtIndex(i),
414 rhs.GetArgumentQuoteCharAtIndex(i));
418 Args::AppendArguments (const char **argv)
422 for (uint32_t i=0; argv[i]; ++i)
423 AppendArgument(argv[i]);
428 Args::AppendArgument (const char *arg_cstr, char quote_char)
430 return InsertArgumentAtIndex (GetArgumentCount(), arg_cstr, quote_char);
434 Args::InsertArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
436 // Since we are using a std::list to hold onto the copied C string and
437 // we don't have direct access to the elements, we have to iterate to
439 arg_sstr_collection::iterator pos, end = m_args.end();
441 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
444 pos = m_args.insert(pos, arg_cstr);
446 if (idx >= m_args_quote_char.size())
448 m_args_quote_char.resize(idx + 1);
449 m_args_quote_char[idx] = quote_char;
452 m_args_quote_char.insert(m_args_quote_char.begin() + idx, quote_char);
454 UpdateArgvFromArgs();
455 return GetArgumentAtIndex(idx);
459 Args::ReplaceArgumentAtIndex (size_t idx, const char *arg_cstr, char quote_char)
461 // Since we are using a std::list to hold onto the copied C string and
462 // we don't have direct access to the elements, we have to iterate to
464 arg_sstr_collection::iterator pos, end = m_args.end();
466 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
471 pos->assign(arg_cstr);
472 assert(idx < m_argv.size() - 1);
473 m_argv[idx] = pos->c_str();
474 if (idx >= m_args_quote_char.size())
475 m_args_quote_char.resize(idx + 1);
476 m_args_quote_char[idx] = quote_char;
477 return GetArgumentAtIndex(idx);
483 Args::DeleteArgumentAtIndex (size_t idx)
485 // Since we are using a std::list to hold onto the copied C string and
486 // we don't have direct access to the elements, we have to iterate to
488 arg_sstr_collection::iterator pos, end = m_args.end();
490 for (pos = m_args.begin(); i > 0 && pos != end; ++pos)
496 assert(idx < m_argv.size() - 1);
497 m_argv.erase(m_argv.begin() + idx);
498 if (idx < m_args_quote_char.size())
499 m_args_quote_char.erase(m_args_quote_char.begin() + idx);
504 Args::SetArguments (size_t argc, const char **argv)
506 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
507 // no need to clear it here.
509 m_args_quote_char.clear();
511 // First copy each string
512 for (size_t i=0; i<argc; ++i)
514 m_args.push_back (argv[i]);
515 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
516 m_args_quote_char.push_back (argv[i][0]);
518 m_args_quote_char.push_back ('\0');
521 UpdateArgvFromArgs();
525 Args::SetArguments (const char **argv)
527 // m_argv will be rebuilt in UpdateArgvFromArgs() below, so there is
528 // no need to clear it here.
530 m_args_quote_char.clear();
534 // First copy each string
535 for (size_t i=0; argv[i]; ++i)
537 m_args.push_back (argv[i]);
538 if ((argv[i][0] == '\'') || (argv[i][0] == '"') || (argv[i][0] == '`'))
539 m_args_quote_char.push_back (argv[i][0]);
541 m_args_quote_char.push_back ('\0');
545 UpdateArgvFromArgs();
550 Args::ParseOptions (Options &options)
554 Option *long_options = options.GetLongOptions();
555 if (long_options == nullptr)
557 error.SetErrorStringWithFormat("invalid long options");
561 for (int i=0; long_options[i].definition != nullptr; ++i)
563 if (long_options[i].flag == nullptr)
565 if (isprint8(long_options[i].val))
567 sstr << (char)long_options[i].val;
568 switch (long_options[i].definition->option_has_arg)
571 case OptionParser::eNoArgument: break;
572 case OptionParser::eRequiredArgument: sstr << ':'; break;
573 case OptionParser::eOptionalArgument: sstr << "::"; break;
578 Mutex::Locker options_locker(NULL);
579 OptionParser::Prepare(options_locker);
583 int long_options_index = -1;
584 val = OptionParser::Parse(GetArgumentCount(),
588 &long_options_index);
592 // Did we get an error?
595 error.SetErrorStringWithFormat("unknown or ambiguous option");
598 // The option auto-set itself
602 ((Options *) &options)->OptionSeen (val);
604 // Lookup the long option index
605 if (long_options_index == -1)
608 long_options[i].definition || long_options[i].flag || long_options[i].val;
611 if (long_options[i].val == val)
613 long_options_index = i;
618 // Call the callback with the option
619 if (long_options_index >= 0 && long_options[long_options_index].definition)
621 const OptionDefinition *def = long_options[long_options_index].definition;
622 CommandInterpreter &interpreter = options.GetInterpreter();
623 OptionValidator *validator = def->validator;
624 if (validator && !validator->IsValid(*interpreter.GetPlatform(true), interpreter.GetExecutionContext()))
626 error.SetErrorStringWithFormat("Option \"%s\" invalid. %s", def->long_option, def->validator->LongConditionString());
630 error = options.SetOptionValue(long_options_index,
631 (def->option_has_arg == OptionParser::eNoArgument) ? nullptr : OptionParser::GetOptionArgument());
636 error.SetErrorStringWithFormat("invalid option with value '%i'", val);
642 // Update our ARGV now that get options has consumed all the options
643 m_argv.erase(m_argv.begin(), m_argv.begin() + OptionParser::GetOptionIndex());
644 UpdateArgsAfterOptionParsing ();
653 m_args_quote_char.clear();
657 Args::StringToAddress (const ExecutionContext *exe_ctx, const char *s, lldb::addr_t fail_value, Error *error_ptr)
659 bool error_set = false;
663 lldb::addr_t addr = ::strtoull (s, &end, 0);
668 return addr; // All characters were used, return the result
670 // Try base 16 with no prefix...
671 addr = ::strtoull (s, &end, 16);
676 return addr; // All characters were used, return the result
681 Target *target = exe_ctx->GetTargetPtr();
684 lldb::ValueObjectSP valobj_sp;
685 EvaluateExpressionOptions options;
686 options.SetCoerceToId(false);
687 options.SetUnwindOnError(true);
688 options.SetKeepInMemory(false);
689 options.SetTryAllThreads(true);
691 ExpressionResults expr_result = target->EvaluateExpression(s,
692 exe_ctx->GetFramePtr(),
696 bool success = false;
697 if (expr_result == eExpressionCompleted)
700 valobj_sp = valobj_sp->GetQualifiedRepresentationIfAvailable(valobj_sp->GetDynamicValueType(), true);
701 // Get the address to watch.
703 addr = valobj_sp->GetValueAsUnsigned(fail_value, &success);
715 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());
722 // Since the compiler can't handle things like "main + 12" we should
723 // try to do this for now. The compiler doesn't like adding offsets
724 // to function pointer types.
725 static RegularExpression g_symbol_plus_offset_regex("^(.*)([-\\+])[[:space:]]*(0x[0-9A-Fa-f]+|[0-9]+)[[:space:]]*$");
726 RegularExpression::Match regex_match(3);
727 if (g_symbol_plus_offset_regex.Execute(s, ®ex_match))
733 if (regex_match.GetMatchAtIndex(s, 1, name))
735 if (regex_match.GetMatchAtIndex(s, 2, str))
739 if (regex_match.GetMatchAtIndex(s, 3, str))
741 offset = StringConvert::ToUInt64(str.c_str(), 0, 0, &success);
746 addr = StringToAddress (exe_ctx, name.c_str(), LLDB_INVALID_ADDRESS, &error);
747 if (addr != LLDB_INVALID_ADDRESS)
750 return addr + offset;
752 return addr - offset;
763 error_ptr->SetErrorStringWithFormat("address expression \"%s\" evaluation failed", s);
772 error_ptr->SetErrorStringWithFormat("invalid address expression \"%s\"", s);
778 Args::StripSpaces (std::string &s, bool leading, bool trailing, bool return_null_if_empty)
780 static const char *k_white_space = " \t\v";
785 size_t pos = s.find_first_not_of (k_white_space);
786 if (pos == std::string::npos)
794 size_t rpos = s.find_last_not_of(k_white_space);
795 if (rpos != std::string::npos && rpos + 1 < s.size())
799 if (return_null_if_empty && s.empty())
805 Args::StringToBoolean (const char *s, bool fail_value, bool *success_ptr)
807 llvm::StringRef ref = llvm::StringRef(s).trim();
808 if (ref.equals_lower("false") ||
809 ref.equals_lower("off") ||
810 ref.equals_lower("no") ||
811 ref.equals_lower("0"))
818 if (ref.equals_lower("true") ||
819 ref.equals_lower("on") ||
820 ref.equals_lower("yes") ||
821 ref.equals_lower("1"))
823 if (success_ptr) *success_ptr = true;
826 if (success_ptr) *success_ptr = false;
831 Args::StringToChar(const char *s, char fail_value, bool *success_ptr)
833 bool success = false;
834 char result = fail_value;
838 size_t length = strlen(s);
846 *success_ptr = success;
851 Args::StringToVersion (const char *s, uint32_t &major, uint32_t &minor, uint32_t &update)
860 unsigned long uval32 = ::strtoul (s, &pos, 0);
866 return pos; // Decoded major and got end of string
868 else if (*pos == '.')
870 const char *minor_cstr = pos + 1;
871 uval32 = ::strtoul (minor_cstr, &pos, 0);
872 if (pos == minor_cstr)
873 return pos; // Didn't get any digits for the minor version...
877 const char *update_cstr = pos + 1;
878 uval32 = ::strtoul (update_cstr, &pos, 0);
879 if (pos == update_cstr)
890 Args::GetShellSafeArgument (const char *unsafe_arg, std::string &safe_arg)
892 safe_arg.assign (unsafe_arg);
894 while (prev_pos < safe_arg.size())
896 // Escape spaces and quotes
897 size_t pos = safe_arg.find_first_of(" '\"", prev_pos);
898 if (pos != std::string::npos)
900 safe_arg.insert (pos, 1, '\\');
906 return safe_arg.c_str();
911 Args::StringToOptionEnum (const char *s, OptionEnumValueElement *enum_values, int32_t fail_value, Error &error)
917 for (int i = 0; enum_values[i].string_value != nullptr ; i++)
919 if (strstr(enum_values[i].string_value, s) == enum_values[i].string_value)
922 return enum_values[i].value;
928 strm.PutCString ("invalid enumeration value, valid values are: ");
929 for (int i = 0; enum_values[i].string_value != nullptr; i++)
931 strm.Printf ("%s\"%s\"",
933 enum_values[i].string_value);
935 error.SetErrorString(strm.GetData());
939 error.SetErrorString ("invalid enumeration argument");
945 Args::StringToScriptLanguage (const char *s, ScriptLanguage fail_value, bool *success_ptr)
949 if ((::strcasecmp (s, "python") == 0) ||
950 (::strcasecmp (s, "default") == 0 && eScriptLanguagePython == eScriptLanguageDefault))
952 if (success_ptr) *success_ptr = true;
953 return eScriptLanguagePython;
955 if (::strcasecmp (s, "none"))
957 if (success_ptr) *success_ptr = true;
958 return eScriptLanguageNone;
961 if (success_ptr) *success_ptr = false;
969 lldb::Format &format,
970 size_t *byte_size_ptr
973 format = eFormatInvalid;
982 char *format_char = nullptr;
983 unsigned long byte_size = ::strtoul (s, &format_char, 0);
984 if (byte_size != ULONG_MAX)
985 *byte_size_ptr = byte_size;
992 const bool partial_match_ok = true;
993 if (!FormatManager::GetFormatFromCString (s, partial_match_ok, format))
995 StreamString error_strm;
996 error_strm.Printf ("Invalid format character or name '%s'. Valid values are:\n", s);
997 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
999 char format_char = FormatManager::GetFormatAsFormatChar(f);
1001 error_strm.Printf ("'%c' or ", format_char);
1003 error_strm.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
1008 error_strm.PutCString ("An optional byte size can precede the format character.\n");
1009 error.SetErrorString(error_strm.GetString().c_str());
1017 error.SetErrorStringWithFormat("%s option string", s ? "empty" : "invalid");
1023 Args::StringToEncoding (const char *s, lldb::Encoding fail_value)
1027 if (strcmp(s, "uint") == 0)
1028 return eEncodingUint;
1029 else if (strcmp(s, "sint") == 0)
1030 return eEncodingSint;
1031 else if (strcmp(s, "ieee754") == 0)
1032 return eEncodingIEEE754;
1033 else if (strcmp(s, "vector") == 0)
1034 return eEncodingVector;
1040 Args::StringToGenericRegister (const char *s)
1044 if (strcmp(s, "pc") == 0)
1045 return LLDB_REGNUM_GENERIC_PC;
1046 else if (strcmp(s, "sp") == 0)
1047 return LLDB_REGNUM_GENERIC_SP;
1048 else if (strcmp(s, "fp") == 0)
1049 return LLDB_REGNUM_GENERIC_FP;
1050 else if (strcmp(s, "ra") == 0 || strcmp(s, "lr") == 0)
1051 return LLDB_REGNUM_GENERIC_RA;
1052 else if (strcmp(s, "flags") == 0)
1053 return LLDB_REGNUM_GENERIC_FLAGS;
1054 else if (strncmp(s, "arg", 3) == 0)
1056 if (s[3] && s[4] == '\0')
1060 case '1': return LLDB_REGNUM_GENERIC_ARG1;
1061 case '2': return LLDB_REGNUM_GENERIC_ARG2;
1062 case '3': return LLDB_REGNUM_GENERIC_ARG3;
1063 case '4': return LLDB_REGNUM_GENERIC_ARG4;
1064 case '5': return LLDB_REGNUM_GENERIC_ARG5;
1065 case '6': return LLDB_REGNUM_GENERIC_ARG6;
1066 case '7': return LLDB_REGNUM_GENERIC_ARG7;
1067 case '8': return LLDB_REGNUM_GENERIC_ARG8;
1072 return LLDB_INVALID_REGNUM;
1077 Args::LongestCommonPrefix (std::string &common_prefix)
1079 arg_sstr_collection::iterator pos, end = m_args.end();
1080 pos = m_args.begin();
1082 common_prefix.clear();
1084 common_prefix = (*pos);
1086 for (++pos; pos != end; ++pos)
1088 size_t new_size = (*pos).size();
1090 // First trim common_prefix if it is longer than the current element:
1091 if (common_prefix.size() > new_size)
1092 common_prefix.erase (new_size);
1094 // Then trim it at the first disparity:
1096 for (size_t i = 0; i < common_prefix.size(); i++)
1098 if ((*pos)[i] != common_prefix[i])
1100 common_prefix.erase(i);
1105 // If we've emptied the common prefix, we're done.
1106 if (common_prefix.empty())
1112 Args::FindArgumentIndexForOption (Option *long_options, int long_options_index)
1114 char short_buffer[3];
1115 char long_buffer[255];
1116 ::snprintf (short_buffer, sizeof (short_buffer), "-%c", long_options[long_options_index].val);
1117 ::snprintf (long_buffer, sizeof (long_buffer), "--%s", long_options[long_options_index].definition->long_option);
1118 size_t end = GetArgumentCount ();
1122 if ((::strncmp (GetArgumentAtIndex (idx), short_buffer, strlen (short_buffer)) == 0)
1123 || (::strncmp (GetArgumentAtIndex (idx), long_buffer, strlen (long_buffer)) == 0))
1134 Args::IsPositionalArgument (const char *arg)
1139 bool is_positional = true;
1140 const char *cptr = arg;
1145 while (isdigit (cptr[0]))
1147 if (cptr[0] != '\0')
1148 is_positional = false;
1151 is_positional = false;
1153 return is_positional;
1157 Args::ParseAliasOptions (Options &options,
1158 CommandReturnObject &result,
1159 OptionArgVector *option_arg_vector,
1160 std::string &raw_input_string)
1164 Option *long_options = options.GetLongOptions();
1166 if (long_options == nullptr)
1168 result.AppendError ("invalid long options");
1169 result.SetStatus (eReturnStatusFailed);
1173 for (i = 0; long_options[i].definition != nullptr; ++i)
1175 if (long_options[i].flag == nullptr)
1177 sstr << (char) long_options[i].val;
1178 switch (long_options[i].definition->option_has_arg)
1181 case OptionParser::eNoArgument:
1183 case OptionParser::eRequiredArgument:
1186 case OptionParser::eOptionalArgument:
1193 Mutex::Locker options_locker(NULL);
1194 OptionParser::Prepare(options_locker);
1198 int long_options_index = -1;
1199 val = OptionParser::Parse (GetArgumentCount(),
1200 GetArgumentVector(),
1203 &long_options_index);
1210 result.AppendError ("unknown or ambiguous option");
1211 result.SetStatus (eReturnStatusFailed);
1218 options.OptionSeen (val);
1220 // Look up the long option index
1221 if (long_options_index == -1)
1224 long_options[j].definition || long_options[j].flag || long_options[j].val;
1227 if (long_options[j].val == val)
1229 long_options_index = j;
1235 // See if the option takes an argument, and see if one was supplied.
1236 if (long_options_index >= 0)
1238 StreamString option_str;
1239 option_str.Printf ("-%c", val);
1240 const OptionDefinition *def = long_options[long_options_index].definition;
1241 int has_arg = (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
1245 case OptionParser::eNoArgument:
1246 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1247 OptionArgValue (OptionParser::eNoArgument, "<no-argument>")));
1248 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1250 case OptionParser::eRequiredArgument:
1251 if (OptionParser::GetOptionArgument() != nullptr)
1253 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1254 OptionArgValue (OptionParser::eRequiredArgument,
1255 std::string (OptionParser::GetOptionArgument()))));
1256 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1260 result.AppendErrorWithFormat ("Option '%s' is missing argument specifier.\n",
1261 option_str.GetData());
1262 result.SetStatus (eReturnStatusFailed);
1265 case OptionParser::eOptionalArgument:
1266 if (OptionParser::GetOptionArgument() != nullptr)
1268 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1269 OptionArgValue (OptionParser::eOptionalArgument,
1270 std::string (OptionParser::GetOptionArgument()))));
1271 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1275 option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
1276 OptionArgValue (OptionParser::eOptionalArgument, "<no-argument>")));
1277 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1281 result.AppendErrorWithFormat ("error with options table; invalid value in has_arg field for option '%c'.\n", val);
1282 result.SetStatus (eReturnStatusFailed);
1288 result.AppendErrorWithFormat ("Invalid option with value '%c'.\n", val);
1289 result.SetStatus (eReturnStatusFailed);
1292 if (long_options_index >= 0)
1294 // Find option in the argument list; also see if it was supposed to take an argument and if one was
1295 // supplied. Remove option (and argument, if given) from the argument list. Also remove them from
1296 // the raw_input_string, if one was passed in.
1297 size_t idx = FindArgumentIndexForOption (long_options, long_options_index);
1298 if (idx < GetArgumentCount())
1300 if (raw_input_string.size() > 0)
1302 const char *tmp_arg = GetArgumentAtIndex (idx);
1303 size_t pos = raw_input_string.find (tmp_arg);
1304 if (pos != std::string::npos)
1305 raw_input_string.erase (pos, strlen (tmp_arg));
1307 ReplaceArgumentAtIndex (idx, "");
1308 if ((long_options[long_options_index].definition->option_has_arg != OptionParser::eNoArgument)
1309 && (OptionParser::GetOptionArgument() != nullptr)
1310 && (idx+1 < GetArgumentCount())
1311 && (strcmp (OptionParser::GetOptionArgument(), GetArgumentAtIndex(idx+1)) == 0))
1313 if (raw_input_string.size() > 0)
1315 const char *tmp_arg = GetArgumentAtIndex (idx+1);
1316 size_t pos = raw_input_string.find (tmp_arg);
1317 if (pos != std::string::npos)
1318 raw_input_string.erase (pos, strlen (tmp_arg));
1320 ReplaceArgumentAtIndex (idx+1, "");
1325 if (!result.Succeeded())
1331 Args::ParseArgsForCompletion
1334 OptionElementVector &option_element_vector,
1335 uint32_t cursor_index
1339 Option *long_options = options.GetLongOptions();
1340 option_element_vector.clear();
1342 if (long_options == nullptr)
1347 // Leading : tells getopt to return a : for a missing option argument AND
1348 // to suppress error messages.
1351 for (int i = 0; long_options[i].definition != nullptr; ++i)
1353 if (long_options[i].flag == nullptr)
1355 sstr << (char) long_options[i].val;
1356 switch (long_options[i].definition->option_has_arg)
1359 case OptionParser::eNoArgument:
1361 case OptionParser::eRequiredArgument:
1364 case OptionParser::eOptionalArgument:
1371 Mutex::Locker options_locker(NULL);
1372 OptionParser::Prepare(options_locker);
1373 OptionParser::EnableError(false);
1376 const OptionDefinition *opt_defs = options.GetDefinitions();
1378 // Fooey... OptionParser::Parse permutes the GetArgumentVector to move the options to the front.
1379 // So we have to build another Arg and pass that to OptionParser::Parse so it doesn't
1380 // change the one we have.
1382 std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
1384 bool failed_once = false;
1385 uint32_t dash_dash_pos = -1;
1389 bool missing_argument = false;
1390 int long_options_index = -1;
1392 val = OptionParser::Parse (dummy_vec.size() - 1,
1393 const_cast<char *const *>(&dummy_vec.front()),
1396 &long_options_index);
1400 // When we're completing a "--" which is the last option on line,
1406 // If this is a bare "--" we mark it as such so we can complete it successfully later.
1407 // Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
1408 // user might want to complete options by long name. I make this work by checking whether the
1409 // cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
1410 // I let it pass to OptionParser::Parse which will terminate the option parsing.
1411 // Note, in either case we continue parsing the line so we can figure out what other options
1412 // were passed. This will be useful when we come to restricting completions based on what other
1413 // options we've seen on the line.
1415 if (static_cast<size_t>(OptionParser::GetOptionIndex()) < dummy_vec.size() - 1
1416 && (strcmp (dummy_vec[OptionParser::GetOptionIndex()-1], "--") == 0))
1418 dash_dash_pos = OptionParser::GetOptionIndex() - 1;
1419 if (static_cast<size_t>(OptionParser::GetOptionIndex() - 1) == cursor_index)
1421 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, OptionParser::GetOptionIndex() - 1,
1422 OptionArgElement::eBareDoubleDash));
1431 else if (val == '?')
1433 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
1434 OptionArgElement::eUnrecognizedArg));
1441 else if (val == ':')
1443 // This is a missing argument.
1444 val = OptionParser::GetOptionErrorCause();
1445 missing_argument = true;
1448 ((Options *) &options)->OptionSeen (val);
1450 // Look up the long option index
1451 if (long_options_index == -1)
1454 long_options[j].definition || long_options[j].flag || long_options[j].val;
1457 if (long_options[j].val == val)
1459 long_options_index = j;
1465 // See if the option takes an argument, and see if one was supplied.
1466 if (long_options_index >= 0)
1468 int opt_defs_index = -1;
1469 for (int i = 0; ; i++)
1471 if (opt_defs[i].short_option == 0)
1473 else if (opt_defs[i].short_option == val)
1480 const OptionDefinition *def = long_options[long_options_index].definition;
1481 int has_arg = (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
1484 case OptionParser::eNoArgument:
1485 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 1, 0));
1487 case OptionParser::eRequiredArgument:
1488 if (OptionParser::GetOptionArgument() != nullptr)
1491 if (missing_argument)
1494 arg_index = OptionParser::GetOptionIndex() - 1;
1496 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, arg_index));
1500 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 1, -1));
1503 case OptionParser::eOptionalArgument:
1504 if (OptionParser::GetOptionArgument() != nullptr)
1506 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, OptionParser::GetOptionIndex() - 1));
1510 option_element_vector.push_back (OptionArgElement (opt_defs_index, OptionParser::GetOptionIndex() - 2, OptionParser::GetOptionIndex() - 1));
1514 // The options table is messed up. Here we'll just continue
1515 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
1516 OptionArgElement::eUnrecognizedArg));
1522 option_element_vector.push_back (OptionArgElement (OptionArgElement::eUnrecognizedArg, OptionParser::GetOptionIndex() - 1,
1523 OptionArgElement::eUnrecognizedArg));
1527 // Finally we have to handle the case where the cursor index points at a single "-". We want to mark that in
1528 // the option_element_vector, but only if it is not after the "--". But it turns out that OptionParser::Parse just ignores
1529 // an isolated "-". So we have to look it up by hand here. We only care if it is AT the cursor position.
1530 // Note, a single quoted dash is not the same as a single dash...
1532 if ((static_cast<int32_t>(dash_dash_pos) == -1 || cursor_index < dash_dash_pos)
1533 && m_args_quote_char[cursor_index] == '\0'
1534 && strcmp (GetArgumentAtIndex(cursor_index), "-") == 0)
1536 option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDash, cursor_index,
1537 OptionArgElement::eBareDash));
1543 Args::EncodeEscapeSequences (const char *src, std::string &dst)
1548 for (const char *p = src; *p != '\0'; ++p)
1550 size_t non_special_chars = ::strcspn (p, "\\");
1551 if (non_special_chars > 0)
1553 dst.append(p, non_special_chars);
1554 p += non_special_chars;
1561 ++p; // skip the slash
1564 case 'a' : dst.append(1, '\a'); break;
1565 case 'b' : dst.append(1, '\b'); break;
1566 case 'f' : dst.append(1, '\f'); break;
1567 case 'n' : dst.append(1, '\n'); break;
1568 case 'r' : dst.append(1, '\r'); break;
1569 case 't' : dst.append(1, '\t'); break;
1570 case 'v' : dst.append(1, '\v'); break;
1571 case '\\': dst.append(1, '\\'); break;
1572 case '\'': dst.append(1, '\''); break;
1573 case '"' : dst.append(1, '"'); break;
1575 // 1 to 3 octal chars
1577 // Make a string that can hold onto the initial zero char,
1578 // up to 3 octal digits, and a terminating NULL.
1579 char oct_str[5] = { '\0', '\0', '\0', '\0', '\0' };
1582 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1585 // We don't want to consume the last octal character since
1586 // the main for loop will do this for us, so we advance p by
1587 // one less than i (even if i is zero)
1589 unsigned long octal_value = ::strtoul (oct_str, nullptr, 8);
1590 if (octal_value <= UINT8_MAX)
1592 dst.append(1, (char)octal_value);
1598 // hex number in the format
1601 ++p; // Skip the 'x'
1603 // Make a string that can hold onto two hex chars plus a
1605 char hex_str[3] = { *p, '\0', '\0' };
1608 ++p; // Skip the first of the two hex chars
1612 unsigned long hex_value = strtoul (hex_str, nullptr, 16);
1613 if (hex_value <= UINT8_MAX)
1614 dst.append (1, (char)hex_value);
1623 // Just desensitize any other character by just printing what
1624 // came after the '\'
1636 Args::ExpandEscapedCharacters (const char *src, std::string &dst)
1641 for (const char *p = src; *p != '\0'; ++p)
1649 case '\a': dst.append("\\a"); break;
1650 case '\b': dst.append("\\b"); break;
1651 case '\f': dst.append("\\f"); break;
1652 case '\n': dst.append("\\n"); break;
1653 case '\r': dst.append("\\r"); break;
1654 case '\t': dst.append("\\t"); break;
1655 case '\v': dst.append("\\v"); break;
1656 case '\'': dst.append("\\'"); break;
1657 case '"': dst.append("\\\""); break;
1658 case '\\': dst.append("\\\\"); break;
1661 // Just encode as octal
1664 snprintf(octal_str, sizeof(octal_str), "%o", *p);
1665 dst.append(octal_str);
1675 Args::EscapeLLDBCommandArgument (const std::string& arg, char quote_char)
1677 const char* chars_to_escape = nullptr;
1681 chars_to_escape = " \t\\'\"`";
1684 chars_to_escape = "";
1687 chars_to_escape = "$\"`\\";
1690 assert(false && "Unhandled quote character");
1694 res.reserve(arg.size());
1697 if (::strchr(chars_to_escape, c))
1698 res.push_back('\\');