Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 152 additions & 23 deletions src/library/cmd_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace xlang::cmd
struct reader
{
template <typename C, typename V>
reader(C const argc, V argv, std::vector<option> const& options)
reader(C const argc, V const argv, std::vector<option> const& options)
{
#ifdef XLANG_DEBUG
{
Expand All @@ -38,28 +38,7 @@ namespace xlang::cmd

for (C i = 1; i < argc; ++i)
{
std::string_view arg{ argv[i] };

if (arg[0] == '-')
{
arg.remove_prefix(1);
last = find(options, arg);

if (last == options.end())
{
throw_invalid("Option '-", arg, "' is not supported");
}

m_options.try_emplace(last->name);
}
else if (last == options.end())
{
throw_invalid("Value '", arg, "' is not supported");
}
else
{
m_options[last->name].push_back(std::string{ arg });
}
extract_option(argv[i], options, last);
}

for (auto&& option : options)
Expand Down Expand Up @@ -240,5 +219,155 @@ namespace xlang::cmd
}

std::map<std::string_view, std::vector<std::string>> m_options;

template<typename O, typename L>
void extract_option(std::string_view arg, O const& options, L& last)
{
if (arg[0] == '-')
{
arg.remove_prefix(1);
last = find(options, arg);

if (last == options.end())
{
throw_invalid("Option '-", arg, "' is not supported");
}

m_options.try_emplace(last->name);
}
else if (arg[0] == '@')
{
arg.remove_prefix(1);
extract_response_file(arg, options);
}
else if (last == options.end())
{
throw_invalid("Value '", arg, "' is not supported");
}
else
{
m_options[last->name].push_back(std::string{ arg });
}
}

template<typename O>
void extract_response_file(std::string_view const& arg, O const& options)
{
std::experimental::filesystem::path response_path{ std::string{ arg } };
std::string extension = response_path.extension().generic_string();
std::transform(extension.begin(), extension.end(), extension.begin(),
[](auto c) { return static_cast<unsigned char>(::tolower(c)); });

// Check if misuse of @ prefix, so if directory or metadata file instead of response file.
if (is_directory(response_path) || extension == ".winmd")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comment that this line is intended to help diagnose misuse of the '@' prefix (e.g., when supplying metadata files)

{
throw_invalid("'@' is reserved for response files");
}
std::array<char, 8192> line_buf;
std::ifstream response_file(absolute(response_path));
while (response_file.getline(line_buf.data(), line_buf.size()))
{
size_t argc = 0;
std::vector<std::string> argv;

parse_command_line(line_buf.data(), argv, &argc);

auto last{ options.end() };
for (size_t i = 0; i < argc; i++)
{
extract_option(argv[i], options, last);
}
}
}

template <typename Character>
static void parse_command_line(Character* cmdstart, std::vector<std::string>& argv, size_t* argument_count)
{

std::string arg;
bool copy_character;
unsigned backslash_count;
bool in_quotes;
bool first_arg;

Character* p = cmdstart;
in_quotes = false;
first_arg = true;
*argument_count = 0;

for (;;)
{
if (*p)
{
while (*p == ' ' || *p == '\t')
++p;
}

if (!first_arg)
{
argv.emplace_back(arg);
arg.clear();
++*argument_count;
}

if (*p == '\0')
break;

for (;;)
{
copy_character = true;

// Rules:
// 2N backslashes + " ==> N backslashes and begin/end quote
// 2N + 1 backslashes + " ==> N backslashes + literal "
// N backslashes ==> N backslashes
backslash_count = 0;

while (*p == '\\')
{
++p;
++backslash_count;
}

if (*p == '"')
{
// if 2N backslashes before, start/end quote, otherwise
// copy literally:
if (backslash_count % 2 == 0)
{
if (in_quotes && p[1] == '"')
{
p++; // Double quote inside quoted string
}
else
{
// Skip first quote char and copy second:
copy_character = false;
in_quotes = !in_quotes;
}
}

backslash_count /= 2;
}

while (backslash_count--)
{
arg.push_back('\\');
}

if (*p == '\0' || (!in_quotes && (*p == ' ' || *p == '\t')))
break;

if (copy_character)
{
arg.push_back(*p);
}

++p;
}

first_arg = false;
}
}
};
}
7 changes: 4 additions & 3 deletions src/test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.9)

add_executable(cpp_test)
target_include_directories(cpp_test BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/implementation "${CMAKE_CURRENT_SOURCE_DIR}/generated")
target_sources(cpp_test PUBLIC
target_include_directories(cpp_test BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/implementation "${CMAKE_CURRENT_SOURCE_DIR}/generated" ${XLANG_LIBRARY_PATH})
target_sources(cpp_test PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/generated/module.g.cpp"
pch.cpp
activation.cpp
Expand All @@ -11,8 +11,10 @@ target_sources(cpp_test PUBLIC
test/Async.cpp
test/AsyncNoSuspend.cpp
test/AsyncReturn.cpp
test/CmdReader.cpp
test/Collections.cpp
test/Composable.cpp
test/CustomError.cpp
test/Edge.cpp
test/Enum.cpp
test/Events.cpp
Expand All @@ -21,7 +23,6 @@ target_sources(cpp_test PUBLIC
test/FinalRelease.cpp
test/Names.cpp
test/Structs.cpp
test/CustomError.cpp
test/VariadicDelegate.cpp
implementation/Component.Async.Class.cpp
implementation/Component.Collections.Class.cpp
Expand Down
154 changes: 154 additions & 0 deletions src/test/cpp/test/CmdReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include "pch.h"
#include "winrt/Windows.Foundation.h"
#include "cmd_reader.h"
#include <fstream>

using namespace xlang::cmd;

class response_file
{
const char* resp_file_name = "respfile.txt";
void write_response_file(const char* input)
{
std::ofstream resp_file(resp_file_name);
if (!resp_file.is_open())
FAIL("Response file could not be created");
resp_file << input;
resp_file.close();
}

void remove_response_file()
{
std::remove(resp_file_name);
}

public:
response_file(const char* input)
{
write_response_file(input);
}

reader create_reader(size_t const argc, const char* argv[], std::vector<option> const& options)
{
return reader{ argc, argv, options };
}

~response_file()
{
remove_response_file();
}
};

TEST_CASE("CmdReader")
Comment thread
devhawk marked this conversation as resolved.
{
static const std::vector<option> options
{
{ "input", 1 },
{ "reference", 0 },
{ "output", 0, 1 },
{ "component", 0, 1 },
{ "filter", 0 },
{ "name", 0, 1 },
{ "verbose", 0, 0 },
};

// input and output
{
const char* argv[] = { "progname", "-in", "example_file.in", "-out", "example_file.out" };
const size_t argc = 5;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all argc values can be = _countof(argv)

reader args{ argc, argv, options };

REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == "example_file.in");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == "example_file.out");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}

// response file #1: filename no quotes
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);

response_file rf{ R"(-in example_file.in -out example_file.out)" };
reader args = rf.create_reader(argc, argv, options);

REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == "example_file.in");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == "example_file.out");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}

// response file #2: filename with quotes
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);

response_file rf{ R"(-in "example file.in" -out "example file.out")" };
reader args = rf.create_reader(argc, argv, options);

REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == "example file.in");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == "example file.out");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}

// response file #3: filename with quote within name
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);

response_file rf{ R"(-in example\"file.in -out example\"file.out)" };
reader args = rf.create_reader(argc, argv, options);

REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == R"(example"file.in)");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == R"(example"file.out)");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}

// response file #4: really really long path
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);
std::string file_name_in(R"(C:\)");
std::string file_name_out(R"(C:\)");
std::string input_str("-in ");

for (int i = 0; i < 500; i++) {
file_name_in.append(R"(dirname\)");
file_name_out.append(R"(dirname\)");
}

file_name_in.append("example_file.in");
file_name_out.append("example_file.out");
input_str.append(file_name_in).append(" -out ").append(file_name_out);

response_file rf{ input_str.data() };
reader args = rf.create_reader(argc, argv, options);

REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == file_name_in);
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == file_name_out);
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}
}