[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- Args.cpp ----------------------------------------------------------===//
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-04-18 02:53:35 +08:00
|
|
|
#include "lldb/Utility/Args.h"
|
2018-04-10 17:03:59 +08:00
|
|
|
#include "lldb/Utility/ConstString.h"
|
|
|
|
#include "lldb/Utility/FileSpec.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2018-04-10 17:03:59 +08:00
|
|
|
#include "lldb/Utility/StringList.h"
|
2016-08-30 03:58:14 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
// A helper function for argument parsing.
|
|
|
|
// Parses the initial part of the first argument using normal double quote
|
2018-05-01 00:49:04 +08:00
|
|
|
// rules: backslash escapes the double quote and itself. The parsed string is
|
|
|
|
// appended to the second argument. The function returns the unparsed portion
|
|
|
|
// of the string, starting at the closing quote.
|
2015-03-02 20:46:22 +08:00
|
|
|
static llvm::StringRef ParseDoubleQuotes(llvm::StringRef quoted,
|
|
|
|
std::string &result) {
|
|
|
|
// Inside double quotes, '\' and '"' are special.
|
|
|
|
static const char *k_escapable_characters = "\"\\";
|
|
|
|
while (true) {
|
|
|
|
// Skip over over regular characters and append them.
|
|
|
|
size_t regular = quoted.find_first_of(k_escapable_characters);
|
|
|
|
result += quoted.substr(0, regular);
|
|
|
|
quoted = quoted.substr(regular);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
// If we have reached the end of string or the closing quote, we're done.
|
|
|
|
if (quoted.empty() || quoted.front() == '"')
|
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
// We have found a backslash.
|
|
|
|
quoted = quoted.drop_front();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
if (quoted.empty()) {
|
|
|
|
// A lone backslash at the end of string, let's just append it.
|
|
|
|
result += '\\';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the character after the backslash is not a whitelisted escapable
|
2018-05-01 00:49:04 +08:00
|
|
|
// character, we leave the character sequence untouched.
|
2015-03-02 20:46:22 +08:00
|
|
|
if (strchr(k_escapable_characters, quoted.front()) == nullptr)
|
|
|
|
result += '\\';
|
|
|
|
|
|
|
|
result += quoted.front();
|
|
|
|
quoted = quoted.drop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
return quoted;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
static size_t ArgvToArgc(const char **argv) {
|
|
|
|
if (!argv)
|
|
|
|
return 0;
|
|
|
|
size_t count = 0;
|
|
|
|
while (*argv++)
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-07-11 04:17:38 +08:00
|
|
|
// Trims all whitespace that can separate command line arguments from the left
|
|
|
|
// side of the string.
|
|
|
|
static llvm::StringRef ltrimForArgs(llvm::StringRef str) {
|
|
|
|
static const char *k_space_separators = " \t";
|
|
|
|
return str.ltrim(k_space_separators);
|
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
// A helper function for SetCommandString. Parses a single argument from the
|
|
|
|
// command string, processing quotes and backslashes in a shell-like manner.
|
|
|
|
// The function returns a tuple consisting of the parsed argument, the quote
|
|
|
|
// char used, and the unparsed portion of the string starting at the first
|
|
|
|
// unqouted, unescaped whitespace character.
|
|
|
|
static std::tuple<std::string, char, llvm::StringRef>
|
|
|
|
ParseSingleArgument(llvm::StringRef command) {
|
|
|
|
// Argument can be split into multiple discontiguous pieces, for example:
|
|
|
|
// "Hello ""World"
|
|
|
|
// this would result in a single argument "Hello World" (without the quotes)
|
|
|
|
// since the quotes would be removed and there is not space between the
|
|
|
|
// strings.
|
2015-03-02 20:46:22 +08:00
|
|
|
std::string arg;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Since we can have multiple quotes that form a single command in a command
|
|
|
|
// like: "Hello "world'!' (which will make a single argument "Hello world!")
|
|
|
|
// we remember the first quote character we encounter and use that for the
|
|
|
|
// quote character.
|
2015-03-02 20:46:22 +08:00
|
|
|
char first_quote_char = '\0';
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
bool arg_complete = false;
|
2016-09-07 04:57:50 +08:00
|
|
|
do {
|
2015-03-02 20:46:22 +08:00
|
|
|
// Skip over over regular characters and append them.
|
2019-06-08 05:13:30 +08:00
|
|
|
size_t regular = command.find_first_of(" \t\r\"'`\\");
|
2015-03-02 20:46:22 +08:00
|
|
|
arg += command.substr(0, regular);
|
|
|
|
command = command.substr(regular);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
if (command.empty())
|
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
char special = command.front();
|
|
|
|
command = command.drop_front();
|
|
|
|
switch (special) {
|
|
|
|
case '\\':
|
|
|
|
if (command.empty()) {
|
|
|
|
arg += '\\';
|
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
// If the character after the backslash is not a whitelisted escapable
|
2018-05-01 00:49:04 +08:00
|
|
|
// character, we leave the character sequence untouched.
|
2015-03-02 20:46:22 +08:00
|
|
|
if (strchr(" \t\\'\"`", command.front()) == nullptr)
|
|
|
|
arg += '\\';
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
arg += command.front();
|
|
|
|
command = command.drop_front();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2019-06-08 05:13:30 +08:00
|
|
|
case '\r':
|
2018-05-01 00:49:04 +08:00
|
|
|
// We are not inside any quotes, we just found a space after an argument.
|
|
|
|
// We are done.
|
2015-03-02 20:46:22 +08:00
|
|
|
arg_complete = true;
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
case '`':
|
|
|
|
// We found the start of a quote scope.
|
|
|
|
if (first_quote_char == '\0')
|
|
|
|
first_quote_char = special;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
if (special == '"')
|
|
|
|
command = ParseDoubleQuotes(command, arg);
|
2016-09-07 04:57:50 +08:00
|
|
|
else {
|
2015-03-02 20:46:22 +08:00
|
|
|
// For single quotes, we simply skip ahead to the matching quote
|
2018-05-01 00:49:04 +08:00
|
|
|
// character (or the end of the string).
|
2015-03-02 20:46:22 +08:00
|
|
|
size_t quoted = command.find(special);
|
|
|
|
arg += command.substr(0, quoted);
|
|
|
|
command = command.substr(quoted);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
// If we found a closing quote, skip it.
|
|
|
|
if (!command.empty())
|
|
|
|
command = command.drop_front();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!arg_complete);
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
return std::make_tuple(arg, first_quote_char, command);
|
2015-03-02 20:46:22 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
Args::ArgEntry::ArgEntry(llvm::StringRef str, char quote) : quote(quote) {
|
|
|
|
size_t size = str.size();
|
|
|
|
ptr.reset(new char[size + 1]);
|
|
|
|
|
|
|
|
::memcpy(data(), str.data() ? str.data() : "", size);
|
|
|
|
ptr[size] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Args constructor
|
|
|
|
Args::Args(llvm::StringRef command) { SetCommandString(command); }
|
|
|
|
|
|
|
|
Args::Args(const Args &rhs) { *this = rhs; }
|
|
|
|
|
2017-12-11 22:22:30 +08:00
|
|
|
Args::Args(const StringList &list) : Args() {
|
2019-08-16 22:27:35 +08:00
|
|
|
for (const std::string &arg : list)
|
|
|
|
AppendArgument(arg);
|
2017-12-11 22:22:30 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
Args &Args::operator=(const Args &rhs) {
|
|
|
|
Clear();
|
|
|
|
|
2015-03-02 20:46:22 +08:00
|
|
|
m_argv.clear();
|
2016-10-04 06:51:09 +08:00
|
|
|
m_entries.clear();
|
|
|
|
for (auto &entry : rhs.m_entries) {
|
2019-09-13 19:26:48 +08:00
|
|
|
m_entries.emplace_back(entry.ref(), entry.quote);
|
2016-10-04 06:51:09 +08:00
|
|
|
m_argv.push_back(m_entries.back().data());
|
|
|
|
}
|
|
|
|
m_argv.push_back(nullptr);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-03-02 20:46:22 +08:00
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
// Destructor
|
|
|
|
Args::~Args() {}
|
|
|
|
|
|
|
|
void Args::Dump(Stream &s, const char *label_name) const {
|
|
|
|
if (!label_name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (auto &entry : m_entries) {
|
|
|
|
s.Indent();
|
2019-09-13 19:26:48 +08:00
|
|
|
s.Format("{0}[{1}]=\"{2}\"\n", label_name, i++, entry.ref());
|
2016-10-04 06:51:09 +08:00
|
|
|
}
|
2016-12-23 03:15:07 +08:00
|
|
|
s.Format("{0}[{1}]=NULL\n", label_name, i);
|
2016-10-04 06:51:09 +08:00
|
|
|
s.EOL();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Args::GetCommandString(std::string &command) const {
|
|
|
|
command.clear();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_entries.size(); ++i) {
|
|
|
|
if (i > 0)
|
|
|
|
command += ' ';
|
2019-09-13 19:26:48 +08:00
|
|
|
command += m_entries[i].ref();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2015-03-02 20:46:22 +08:00
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
return !m_entries.empty();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
bool Args::GetQuotedCommandString(std::string &command) const {
|
|
|
|
command.clear();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
for (size_t i = 0; i < m_entries.size(); ++i) {
|
|
|
|
if (i > 0)
|
|
|
|
command += ' ';
|
|
|
|
|
|
|
|
if (m_entries[i].quote) {
|
|
|
|
command += m_entries[i].quote;
|
2019-09-13 19:26:48 +08:00
|
|
|
command += m_entries[i].ref();
|
2016-10-04 06:51:09 +08:00
|
|
|
command += m_entries[i].quote;
|
|
|
|
} else {
|
2019-09-13 19:26:48 +08:00
|
|
|
command += m_entries[i].ref();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
return !m_entries.empty();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
void Args::SetCommandString(llvm::StringRef command) {
|
|
|
|
Clear();
|
2010-06-09 00:52:24 +08:00
|
|
|
m_argv.clear();
|
2016-10-04 06:51:09 +08:00
|
|
|
|
2018-07-11 04:17:38 +08:00
|
|
|
command = ltrimForArgs(command);
|
2016-10-04 06:51:09 +08:00
|
|
|
std::string arg;
|
|
|
|
char quote;
|
|
|
|
while (!command.empty()) {
|
|
|
|
std::tie(arg, quote, command) = ParseSingleArgument(command);
|
|
|
|
m_entries.emplace_back(arg, quote);
|
|
|
|
m_argv.push_back(m_entries.back().data());
|
2018-07-11 04:17:38 +08:00
|
|
|
command = ltrimForArgs(command);
|
2016-10-04 06:51:09 +08:00
|
|
|
}
|
2014-04-20 08:31:37 +08:00
|
|
|
m_argv.push_back(nullptr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 06:51:09 +08:00
|
|
|
size_t Args::GetArgumentCount() const { return m_entries.size(); }
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *Args::GetArgumentAtIndex(size_t idx) const {
|
|
|
|
if (idx < m_argv.size())
|
|
|
|
return m_argv[idx];
|
2014-04-20 08:31:37 +08:00
|
|
|
return nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char **Args::GetArgumentVector() {
|
2016-10-04 06:51:09 +08:00
|
|
|
assert(!m_argv.empty());
|
|
|
|
// TODO: functions like execve and posix_spawnp exhibit undefined behavior
|
|
|
|
// when argv or envp is null. So the code below is actually wrong. However,
|
2018-05-01 00:49:04 +08:00
|
|
|
// other code in LLDB depends on it being null. The code has been acting
|
|
|
|
// this way for some time, so it makes sense to leave it this way until
|
|
|
|
// someone has the time to come along and fix it.
|
2016-10-04 06:51:09 +08:00
|
|
|
return (m_argv.size() > 1) ? m_argv.data() : nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char **Args::GetConstArgumentVector() const {
|
2016-10-04 06:51:09 +08:00
|
|
|
assert(!m_argv.empty());
|
|
|
|
return (m_argv.size() > 1) ? const_cast<const char **>(m_argv.data())
|
|
|
|
: nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Args::Shift() {
|
|
|
|
// Don't pop the last NULL terminator from the argv array
|
2016-10-04 06:51:09 +08:00
|
|
|
if (m_entries.empty())
|
|
|
|
return;
|
|
|
|
m_argv.erase(m_argv.begin());
|
|
|
|
m_entries.erase(m_entries.begin());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:17:56 +08:00
|
|
|
void Args::Unshift(llvm::StringRef arg_str, char quote_char) {
|
|
|
|
InsertArgumentAtIndex(0, arg_str, quote_char);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Args::AppendArguments(const Args &rhs) {
|
2016-10-04 06:51:09 +08:00
|
|
|
assert(m_argv.size() == m_entries.size() + 1);
|
|
|
|
assert(m_argv.back() == nullptr);
|
|
|
|
m_argv.pop_back();
|
|
|
|
for (auto &entry : rhs.m_entries) {
|
2019-09-13 19:26:48 +08:00
|
|
|
m_entries.emplace_back(entry.ref(), entry.quote);
|
2016-10-04 06:51:09 +08:00
|
|
|
m_argv.push_back(m_entries.back().data());
|
|
|
|
}
|
|
|
|
m_argv.push_back(nullptr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-11-04 05:22:33 +08:00
|
|
|
void Args::AppendArguments(const char **argv) {
|
2016-10-04 06:51:09 +08:00
|
|
|
size_t argc = ArgvToArgc(argv);
|
|
|
|
|
|
|
|
assert(m_argv.size() == m_entries.size() + 1);
|
|
|
|
assert(m_argv.back() == nullptr);
|
|
|
|
m_argv.pop_back();
|
2016-10-06 01:07:34 +08:00
|
|
|
for (auto arg : llvm::makeArrayRef(argv, argc)) {
|
|
|
|
m_entries.emplace_back(arg, '\0');
|
2016-10-04 06:51:09 +08:00
|
|
|
m_argv.push_back(m_entries.back().data());
|
2011-11-04 05:22:33 +08:00
|
|
|
}
|
2016-10-04 06:51:09 +08:00
|
|
|
|
|
|
|
m_argv.push_back(nullptr);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:17:56 +08:00
|
|
|
void Args::AppendArgument(llvm::StringRef arg_str, char quote_char) {
|
|
|
|
InsertArgumentAtIndex(GetArgumentCount(), arg_str, quote_char);
|
2011-11-04 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:17:56 +08:00
|
|
|
void Args::InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
|
|
|
|
char quote_char) {
|
2016-10-04 06:51:09 +08:00
|
|
|
assert(m_argv.size() == m_entries.size() + 1);
|
|
|
|
assert(m_argv.back() == nullptr);
|
|
|
|
|
|
|
|
if (idx > m_entries.size())
|
2016-10-17 14:17:56 +08:00
|
|
|
return;
|
2016-10-04 06:51:09 +08:00
|
|
|
m_entries.emplace(m_entries.begin() + idx, arg_str, quote_char);
|
|
|
|
m_argv.insert(m_argv.begin() + idx, m_entries[idx].data());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:17:56 +08:00
|
|
|
void Args::ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
|
|
|
|
char quote_char) {
|
2016-10-04 06:51:09 +08:00
|
|
|
assert(m_argv.size() == m_entries.size() + 1);
|
|
|
|
assert(m_argv.back() == nullptr);
|
|
|
|
|
|
|
|
if (idx >= m_entries.size())
|
2016-10-17 14:17:56 +08:00
|
|
|
return;
|
2016-10-04 06:51:09 +08:00
|
|
|
|
2019-09-13 18:41:29 +08:00
|
|
|
m_entries[idx] = ArgEntry(arg_str, quote_char);
|
|
|
|
m_argv[idx] = m_entries[idx].data();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-08-31 16:43:37 +08:00
|
|
|
void Args::DeleteArgumentAtIndex(size_t idx) {
|
2016-10-04 06:51:09 +08:00
|
|
|
if (idx >= m_entries.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_argv.erase(m_argv.begin() + idx);
|
|
|
|
m_entries.erase(m_entries.begin() + idx);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Args::SetArguments(size_t argc, const char **argv) {
|
2016-10-04 06:51:09 +08:00
|
|
|
Clear();
|
|
|
|
|
|
|
|
auto args = llvm::makeArrayRef(argv, argc);
|
|
|
|
m_entries.resize(argc);
|
|
|
|
m_argv.resize(argc + 1);
|
2016-10-06 01:07:34 +08:00
|
|
|
for (size_t i = 0; i < args.size(); ++i) {
|
2016-10-04 06:51:09 +08:00
|
|
|
char quote =
|
|
|
|
((args[i][0] == '\'') || (args[i][0] == '"') || (args[i][0] == '`'))
|
|
|
|
? args[i][0]
|
|
|
|
: '\0';
|
|
|
|
|
|
|
|
m_entries[i] = ArgEntry(args[i], quote);
|
|
|
|
m_argv[i] = m_entries[i].data();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 02:06:21 +08:00
|
|
|
void Args::SetArguments(const char **argv) {
|
2016-10-04 06:51:09 +08:00
|
|
|
SetArguments(ArgvToArgc(argv), argv);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Args::Clear() {
|
2016-10-04 06:51:09 +08:00
|
|
|
m_entries.clear();
|
2010-06-09 00:52:24 +08:00
|
|
|
m_argv.clear();
|
2016-10-04 06:51:09 +08:00
|
|
|
m_argv.push_back(nullptr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 11:53:30 +08:00
|
|
|
const char *Args::GetShellSafeArgument(const FileSpec &shell,
|
|
|
|
const char *unsafe_arg,
|
|
|
|
std::string &safe_arg) {
|
|
|
|
struct ShellDescriptor {
|
2010-06-25 04:31:04 +08:00
|
|
|
ConstString m_basename;
|
2011-11-15 11:53:30 +08:00
|
|
|
const char *m_escapables;
|
2016-09-07 04:57:50 +08:00
|
|
|
};
|
|
|
|
|
2011-11-15 11:53:30 +08:00
|
|
|
static ShellDescriptor g_Shells[] = {{ConstString("bash"), " '\"<>()&"},
|
2016-04-05 06:46:38 +08:00
|
|
|
{ConstString("tcsh"), " '\"<>()&$"},
|
|
|
|
{ConstString("sh"), " '\"<>()&"}};
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-04-05 06:46:38 +08:00
|
|
|
// safe minimal set
|
|
|
|
const char *escapables = " '\"";
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-11-15 11:53:30 +08:00
|
|
|
if (auto basename = shell.GetFilename()) {
|
|
|
|
for (const auto &Shell : g_Shells) {
|
|
|
|
if (Shell.m_basename == basename) {
|
|
|
|
escapables = Shell.m_escapables;
|
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2011-11-15 11:53:30 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 11:53:30 +08:00
|
|
|
safe_arg.assign(unsafe_arg);
|
|
|
|
size_t prev_pos = 0;
|
|
|
|
while (prev_pos < safe_arg.size()) {
|
|
|
|
// Escape spaces and quotes
|
2016-04-05 06:46:38 +08:00
|
|
|
size_t pos = safe_arg.find_first_of(escapables, prev_pos);
|
2010-12-10 06:52:49 +08:00
|
|
|
if (pos != std::string::npos) {
|
2011-11-15 11:53:30 +08:00
|
|
|
safe_arg.insert(pos, 1, '\\');
|
|
|
|
prev_pos = pos + 2;
|
2016-09-07 04:57:50 +08:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2011-11-15 11:53:30 +08:00
|
|
|
return safe_arg.c_str();
|
|
|
|
}
|
|
|
|
|
2012-08-24 09:42:50 +08:00
|
|
|
lldb::Encoding Args::StringToEncoding(llvm::StringRef s,
|
2016-08-30 03:58:14 +08:00
|
|
|
lldb::Encoding fail_value) {
|
|
|
|
return llvm::StringSwitch<lldb::Encoding>(s)
|
|
|
|
.Case("uint", eEncodingUint)
|
|
|
|
.Case("sint", eEncodingSint)
|
|
|
|
.Case("ieee754", eEncodingIEEE754)
|
|
|
|
.Case("vector", eEncodingVector)
|
|
|
|
.Default(fail_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Args::StringToGenericRegister(llvm::StringRef s) {
|
2013-03-06 07:52:49 +08:00
|
|
|
if (s.empty())
|
2016-08-30 03:58:14 +08:00
|
|
|
return LLDB_INVALID_REGNUM;
|
|
|
|
uint32_t result = llvm::StringSwitch<uint32_t>(s)
|
|
|
|
.Case("pc", LLDB_REGNUM_GENERIC_PC)
|
|
|
|
.Case("sp", LLDB_REGNUM_GENERIC_SP)
|
|
|
|
.Case("fp", LLDB_REGNUM_GENERIC_FP)
|
|
|
|
.Cases("ra", "lr", LLDB_REGNUM_GENERIC_RA)
|
|
|
|
.Case("flags", LLDB_REGNUM_GENERIC_FLAGS)
|
2016-08-30 19:17:00 +08:00
|
|
|
.Case("arg1", LLDB_REGNUM_GENERIC_ARG1)
|
|
|
|
.Case("arg2", LLDB_REGNUM_GENERIC_ARG2)
|
|
|
|
.Case("arg3", LLDB_REGNUM_GENERIC_ARG3)
|
|
|
|
.Case("arg4", LLDB_REGNUM_GENERIC_ARG4)
|
|
|
|
.Case("arg5", LLDB_REGNUM_GENERIC_ARG5)
|
|
|
|
.Case("arg6", LLDB_REGNUM_GENERIC_ARG6)
|
|
|
|
.Case("arg7", LLDB_REGNUM_GENERIC_ARG7)
|
|
|
|
.Case("arg8", LLDB_REGNUM_GENERIC_ARG8)
|
2016-08-30 03:58:14 +08:00
|
|
|
.Default(LLDB_INVALID_REGNUM);
|
|
|
|
return result;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2012-08-24 09:42:50 +08:00
|
|
|
|
2012-09-01 08:38:36 +08:00
|
|
|
void Args::EncodeEscapeSequences(const char *src, std::string &dst) {
|
|
|
|
dst.clear();
|
|
|
|
if (src) {
|
|
|
|
for (const char *p = src; *p != '\0'; ++p) {
|
|
|
|
size_t non_special_chars = ::strcspn(p, "\\");
|
|
|
|
if (non_special_chars > 0) {
|
|
|
|
dst.append(p, non_special_chars);
|
2015-03-02 20:46:22 +08:00
|
|
|
p += non_special_chars;
|
2012-09-01 08:38:36 +08:00
|
|
|
if (*p == '\0')
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-01 08:38:36 +08:00
|
|
|
if (*p == '\\') {
|
|
|
|
++p; // skip the slash
|
|
|
|
switch (*p) {
|
2016-09-07 04:57:50 +08:00
|
|
|
case 'a':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\a');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case 'b':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\b');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case 'f':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\f');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\n');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case 'r':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\r');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case 't':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\t');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case 'v':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, '\v');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\\':
|
|
|
|
dst.append(1, '\\');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\'':
|
|
|
|
dst.append(1, '\'');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '"':
|
|
|
|
dst.append(1, '"');
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
case '0':
|
2012-09-01 08:38:36 +08:00
|
|
|
// 1 to 3 octal chars
|
2016-09-07 04:57:50 +08:00
|
|
|
{
|
2018-05-01 00:49:04 +08:00
|
|
|
// Make a string that can hold onto the initial zero char, up to 3
|
|
|
|
// octal digits, and a terminating NULL.
|
2012-09-01 08:38:36 +08:00
|
|
|
char oct_str[5] = {'\0', '\0', '\0', '\0', '\0'};
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
int i;
|
2012-09-01 08:38:36 +08:00
|
|
|
for (i = 0; (p[i] >= '0' && p[i] <= '7') && i < 4; ++i)
|
|
|
|
oct_str[i] = p[i];
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// We don't want to consume the last octal character since the main
|
|
|
|
// for loop will do this for us, so we advance p by one less than i
|
|
|
|
// (even if i is zero)
|
2012-09-01 08:38:36 +08:00
|
|
|
p += i - 1;
|
2014-04-20 08:31:37 +08:00
|
|
|
unsigned long octal_value = ::strtoul(oct_str, nullptr, 8);
|
2012-09-01 08:38:36 +08:00
|
|
|
if (octal_value <= UINT8_MAX) {
|
2019-05-23 13:12:11 +08:00
|
|
|
dst.append(1, static_cast<char>(octal_value));
|
2012-09-01 08:38:36 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2012-09-01 08:38:36 +08:00
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-09-01 08:38:36 +08:00
|
|
|
case 'x':
|
|
|
|
// hex number in the format
|
|
|
|
if (isxdigit(p[1])) {
|
|
|
|
++p; // Skip the 'x'
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-09-01 08:38:36 +08:00
|
|
|
// Make a string that can hold onto two hex chars plus a
|
|
|
|
// NULL terminator
|
|
|
|
char hex_str[3] = {*p, '\0', '\0'};
|
|
|
|
if (isxdigit(p[1])) {
|
|
|
|
++p; // Skip the first of the two hex chars
|
|
|
|
hex_str[1] = *p;
|
|
|
|
}
|
|
|
|
|
2014-04-20 08:31:37 +08:00
|
|
|
unsigned long hex_value = strtoul(hex_str, nullptr, 16);
|
2012-09-01 08:38:36 +08:00
|
|
|
if (hex_value <= UINT8_MAX)
|
2019-05-23 13:12:11 +08:00
|
|
|
dst.append(1, static_cast<char>(hex_value));
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, 'x');
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
default:
|
2018-05-01 00:49:04 +08:00
|
|
|
// Just desensitize any other character by just printing what came
|
|
|
|
// after the '\'
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append(1, *p);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2012-09-01 08:38:36 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2012-09-01 08:38:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:35:27 +08:00
|
|
|
void Args::ExpandEscapedCharacters(const char *src, std::string &dst) {
|
|
|
|
dst.clear();
|
|
|
|
if (src) {
|
|
|
|
for (const char *p = src; *p != '\0'; ++p) {
|
2018-04-10 18:07:22 +08:00
|
|
|
if (isprint(*p))
|
2015-09-02 18:35:27 +08:00
|
|
|
dst.append(1, *p);
|
|
|
|
else {
|
|
|
|
switch (*p) {
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\a':
|
|
|
|
dst.append("\\a");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\b':
|
|
|
|
dst.append("\\b");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\f':
|
|
|
|
dst.append("\\f");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\n':
|
|
|
|
dst.append("\\n");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2015-09-02 18:35:27 +08:00
|
|
|
case '\r':
|
|
|
|
dst.append("\\r");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2015-09-02 18:35:27 +08:00
|
|
|
case '\t':
|
2012-09-01 08:38:36 +08:00
|
|
|
dst.append("\\t");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\v':
|
|
|
|
dst.append("\\v");
|
2015-09-02 18:35:27 +08:00
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
dst.append("\\'");
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
dst.append("\\\"");
|
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '\\':
|
|
|
|
dst.append("\\\\");
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2015-09-02 18:35:27 +08:00
|
|
|
default: {
|
|
|
|
// Just encode as octal
|
|
|
|
dst.append("\\0");
|
|
|
|
char octal_str[32];
|
|
|
|
snprintf(octal_str, sizeof(octal_str), "%o", *p);
|
|
|
|
dst.append(octal_str);
|
|
|
|
} break;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 18:35:27 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-09-02 18:35:27 +08:00
|
|
|
}
|
|
|
|
|
2012-08-24 09:42:50 +08:00
|
|
|
std::string Args::EscapeLLDBCommandArgument(const std::string &arg,
|
2016-08-31 16:43:37 +08:00
|
|
|
char quote_char) {
|
2015-09-02 18:35:27 +08:00
|
|
|
const char *chars_to_escape = nullptr;
|
2010-12-19 11:41:24 +08:00
|
|
|
switch (quote_char) {
|
2016-09-07 04:57:50 +08:00
|
|
|
case '\0':
|
2015-09-02 18:35:27 +08:00
|
|
|
chars_to_escape = " \t\\'\"`";
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2012-09-01 08:38:36 +08:00
|
|
|
case '"':
|
2015-09-02 18:35:27 +08:00
|
|
|
chars_to_escape = "$\"`\\";
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2019-03-26 01:27:14 +08:00
|
|
|
case '`':
|
|
|
|
case '\'':
|
|
|
|
return arg;
|
2016-09-07 04:57:50 +08:00
|
|
|
default:
|
2015-09-02 18:35:27 +08:00
|
|
|
assert(false && "Unhandled quote character");
|
2019-03-26 01:27:14 +08:00
|
|
|
return arg;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 00:42:23 +08:00
|
|
|
std::string res;
|
2015-09-02 18:35:27 +08:00
|
|
|
res.reserve(arg.size());
|
|
|
|
for (char c : arg) {
|
|
|
|
if (::strchr(chars_to_escape, c))
|
|
|
|
res.push_back('\\');
|
|
|
|
res.push_back(c);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-09-02 18:35:27 +08:00
|
|
|
return res;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2018-07-11 04:17:38 +08:00
|
|
|
|
|
|
|
OptionsWithRaw::OptionsWithRaw(llvm::StringRef arg_string) {
|
|
|
|
SetFromString(arg_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionsWithRaw::SetFromString(llvm::StringRef arg_string) {
|
|
|
|
const llvm::StringRef original_args = arg_string;
|
|
|
|
|
|
|
|
arg_string = ltrimForArgs(arg_string);
|
|
|
|
std::string arg;
|
|
|
|
char quote;
|
|
|
|
|
|
|
|
// If the string doesn't start with a dash, we just have no options and just
|
|
|
|
// a raw part.
|
|
|
|
if (!arg_string.startswith("-")) {
|
2020-01-29 03:23:46 +08:00
|
|
|
m_suffix = std::string(original_args);
|
2018-07-11 04:17:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found_suffix = false;
|
|
|
|
|
|
|
|
while (!arg_string.empty()) {
|
|
|
|
// The length of the prefix before parsing.
|
|
|
|
std::size_t prev_prefix_length = original_args.size() - arg_string.size();
|
|
|
|
|
|
|
|
// Parse the next argument from the remaining string.
|
|
|
|
std::tie(arg, quote, arg_string) = ParseSingleArgument(arg_string);
|
|
|
|
|
|
|
|
// If we get an unquoted '--' argument, then we reached the suffix part
|
|
|
|
// of the command.
|
|
|
|
Args::ArgEntry entry(arg, quote);
|
|
|
|
if (!entry.IsQuoted() && arg == "--") {
|
|
|
|
// The remaining line is the raw suffix, and the line we parsed so far
|
|
|
|
// needs to be interpreted as arguments.
|
|
|
|
m_has_args = true;
|
2020-01-29 03:23:46 +08:00
|
|
|
m_suffix = std::string(arg_string);
|
2018-07-11 04:17:38 +08:00
|
|
|
found_suffix = true;
|
|
|
|
|
|
|
|
// The length of the prefix after parsing.
|
|
|
|
std::size_t prefix_length = original_args.size() - arg_string.size();
|
|
|
|
|
|
|
|
// Take the string we know contains all the arguments and actually parse
|
|
|
|
// it as proper arguments.
|
|
|
|
llvm::StringRef prefix = original_args.take_front(prev_prefix_length);
|
|
|
|
m_args = Args(prefix);
|
|
|
|
m_arg_string = prefix;
|
|
|
|
|
|
|
|
// We also record the part of the string that contains the arguments plus
|
|
|
|
// the delimiter.
|
|
|
|
m_arg_string_with_delimiter = original_args.take_front(prefix_length);
|
|
|
|
|
|
|
|
// As the rest of the string became the raw suffix, we are done here.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_string = ltrimForArgs(arg_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find a suffix delimiter, the whole string is the raw suffix.
|
|
|
|
if (!found_suffix) {
|
|
|
|
found_suffix = true;
|
2020-01-29 03:23:46 +08:00
|
|
|
m_suffix = std::string(original_args);
|
2018-07-11 04:17:38 +08:00
|
|
|
}
|
|
|
|
}
|