Skip to content

Commit 895c40f

Browse files
committed
Finished adding CLI!
1 parent 5176fd9 commit 895c40f

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

Herpaderping/Herpaderping/Source.cpp

+55-20
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
constexpr auto HELP_NUMBER_OF_ARGS = 2;
66
constexpr auto NORMAL_NUMBER_OF_ARGS = 4;
7-
constexpr auto SOURCE_EXECUTABLE_ARGS_POSITION = 1;
8-
constexpr auto TARGET_EXECUTABLE_ARGS_POSITION = 2;
9-
constexpr auto COVER_EXECUTABLE_ARGS_POSITION = 3;
7+
constexpr auto ARGS_POSITION_SOURCE_EXECUTABLE = 1;
8+
constexpr auto ARGS_POSITION_TARGET_EXECUTABLE = 2;
9+
constexpr auto ARGS_POSITION_COVER_EXECUTABLE = 3;
1010

1111
struct CmdlineArguments {
1212
std::wstring source_executable;
@@ -19,53 +19,88 @@ std::wstring string_to_wstring(const std::string& string)
1919
return std::wstring(string.begin(), string.end());
2020
}
2121

22+
bool is_file_exists(const std::string& path)
23+
{
24+
auto dwAttrib = GetFileAttributesA(path.c_str());
25+
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
26+
}
27+
2228
void print_help()
2329
{
24-
return;
30+
std::cout << std::endl << "##### Herpaderping v1.0.0 #####" << std::endl;
31+
std::cout << "Usage: herpaderping.exe source_executable target_executable cover_executable" << std::endl;
32+
std::cout << std::endl << "source_executable: path to the executable you wish to run." << std::endl;
33+
std::cout << "target_executable: path to where the source executable will be copied to and run from." << std::endl;
34+
std::cout << "cover_executable: path to the executable that will be used as a \"cover\" for our process." << std::endl;
35+
std::cout << std::endl << "Example:" << std::endl;
36+
std::cout << "Running cmd.exe with the cover of chrome.exe:" << std::endl;
37+
std::cout << " herpaderping.exe \"C:\\Windows\\System32\\cmd.exe\" \".\\target.exe\" \"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"" << std::endl;
2538
}
2639

27-
// constexpr auto PATH_TO_COVER = L"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
2840
std::unique_ptr<CmdlineArguments> parse_program_arguments(int argc, char** argv)
2941
{
3042
std::unique_ptr<CmdlineArguments> cmdline_args = nullptr;
3143

32-
switch (argc) {
33-
case HELP_NUMBER_OF_ARGS:
34-
// TODO: check if it is -h.
35-
print_help();
36-
break;
44+
std::string source_exe_path = "";
45+
std::string target_exe_path = "";
46+
std::string cover_exe_path = "";
3747

48+
switch (argc) {
3849
case NORMAL_NUMBER_OF_ARGS:
50+
source_exe_path = std::string(argv[ARGS_POSITION_SOURCE_EXECUTABLE]);
51+
target_exe_path = std::string(argv[ARGS_POSITION_TARGET_EXECUTABLE]);
52+
cover_exe_path = std::string(argv[ARGS_POSITION_COVER_EXECUTABLE]);
53+
54+
if (!is_file_exists(source_exe_path)) {
55+
throw std::invalid_argument(source_exe_path + ": No such file or directory.");
56+
}
57+
58+
if (!is_file_exists(cover_exe_path)) {
59+
throw std::invalid_argument(source_exe_path + ": No such file or directory.");
60+
}
61+
3962
cmdline_args = std::make_unique<CmdlineArguments>();
40-
cmdline_args->source_executable = string_to_wstring(std::string(argv[SOURCE_EXECUTABLE_ARGS_POSITION]));
41-
cmdline_args->target_executable = string_to_wstring(std::string(argv[TARGET_EXECUTABLE_ARGS_POSITION]));
42-
cmdline_args->cover_exectuable = string_to_wstring(std::string(argv[COVER_EXECUTABLE_ARGS_POSITION]));
63+
cmdline_args->source_executable = string_to_wstring(source_exe_path);
64+
cmdline_args->target_executable = string_to_wstring(target_exe_path);
65+
cmdline_args->cover_exectuable = string_to_wstring(cover_exe_path);
66+
4367
break;
4468

4569
default:
46-
print_help();
47-
throw std::invalid_argument("Invalid number of arguments (" + std::to_string(argc)
48-
+ "), expected " + std::to_string(NORMAL_NUMBER_OF_ARGS));
70+
throw std::invalid_argument("invalid number of arguments (got "
71+
+ std::to_string(argc) + " while expecting " + std::to_string(NORMAL_NUMBER_OF_ARGS) + ").");
4972
}
5073

5174
return cmdline_args;
5275
}
5376

5477
int main(int argc, char **argv)
5578
{
79+
std::unique_ptr<CmdlineArguments> cmdline_args = nullptr;
80+
5681
try {
57-
auto cmdline_args = parse_program_arguments(argc, argv);
82+
cmdline_args = parse_program_arguments(argc, argv);
83+
}
84+
catch (const std::invalid_argument& exc) {
85+
std::cout << std::endl << "Error: " + std::string(exc.what()) << std::endl;
86+
print_help();
87+
goto failure;
88+
}
5889

90+
try {
5991
auto herpaderping = Herpaderping(cmdline_args->source_executable,
6092
cmdline_args->target_executable,
6193
cmdline_args->cover_exectuable);
6294

6395
herpaderping.run_process_with_cover();
6496
}
65-
catch (const std::exception& exc) {
66-
std::cout << "Exception: " + std::string(exc.what()) << std::endl;
67-
return EXIT_FAILURE;
97+
catch (const std::runtime_error& exc) {
98+
std::cout << std::endl << "Error: " + std::string(exc.what()) << std::endl;
99+
goto failure;
68100
}
69101

70102
return EXIT_SUCCESS;
103+
104+
failure:
105+
return EXIT_FAILURE;
71106
}

0 commit comments

Comments
 (0)