diff --git a/hyprtester/src/main.cpp b/hyprtester/src/main.cpp index 2fa27fa8c..f7f2c1981 100644 --- a/hyprtester/src/main.cpp +++ b/hyprtester/src/main.cpp @@ -22,11 +22,16 @@ #include #include #include +#include +using namespace Hyprutils::Memory; + #include #include #include #include #include +#include +#include #include "Log.hpp" @@ -87,97 +92,101 @@ static void help() { int main(int argc, char** argv, char** envp) { - std::string configPath = ""; - std::string binaryPath = ""; - std::string pluginPath = std::filesystem::current_path().string(); + std::string configPath = ""; + std::string binaryPath = ""; + std::string pluginPath = std::filesystem::current_path().string(); - std::vector args{argv + 1, argv + argc}; + if (argc > 1) { + std::span args{argv + 1, sc(argc - 1)}; - for (auto it = args.begin(); it != args.end(); it++) { - if (*it == "--config" || *it == "-c") { - if (std::next(it) == args.end()) { - help(); + for (auto it = args.begin(); it != args.end(); it++) { + std::string_view value = *it; - return 1; - } + if (value == "--config" || value == "-c") { + if (std::next(it) == args.end()) { + help(); - configPath = *std::next(it); - - try { - configPath = std::filesystem::canonical(configPath); - - if (!std::filesystem::is_regular_file(configPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); - help(); - return 1; - } + configPath = *std::next(it); - it++; + try { + configPath = std::filesystem::canonical(configPath); - continue; - } else if (*it == "--binary" || *it == "-b") { - if (std::next(it) == args.end()) { - help(); + if (!std::filesystem::is_regular_file(configPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); + help(); - return 1; - } - - binaryPath = *std::next(it); - - try { - binaryPath = std::filesystem::canonical(binaryPath); - - if (!std::filesystem::is_regular_file(binaryPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] Binary '{}' doesn't exist!", binaryPath); - help(); - return 1; - } + it++; - it++; + continue; + } else if (value == "--binary" || value == "-b") { + if (std::next(it) == args.end()) { + help(); - continue; - } else if (*it == "--plugin" || *it == "-p") { - if (std::next(it) == args.end()) { - help(); - - return 1; - } - - pluginPath = *std::next(it); - - try { - pluginPath = std::filesystem::canonical(pluginPath); - - if (!std::filesystem::is_regular_file(pluginPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] plugin '{}' doesn't exist!", pluginPath); + + binaryPath = *std::next(it); + + try { + binaryPath = std::filesystem::canonical(binaryPath); + + if (!std::filesystem::is_regular_file(binaryPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] Binary '{}' doesn't exist!", binaryPath); + help(); + + return 1; + } + + it++; + + continue; + } else if (value == "--plugin" || value == "-p") { + if (std::next(it) == args.end()) { + help(); + + return 1; + } + + pluginPath = *std::next(it); + + try { + pluginPath = std::filesystem::canonical(pluginPath); + + if (!std::filesystem::is_regular_file(pluginPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] plugin '{}' doesn't exist!", pluginPath); + help(); + + return 1; + } + + it++; + + continue; + } else if (value == "--help" || value == "-h") { + help(); + + return 0; + } else { + std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); help(); return 1; } - - it++; - - continue; - } else if (*it == "--help" || *it == "-h") { - help(); - - return 0; - } else { - std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); - help(); - - return 1; } } diff --git a/src/main.cpp b/src/main.cpp index d7624c4ec..8467ea6e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,10 @@ #include #include +#include #include using namespace Hyprutils::String; +using namespace Hyprutils::Memory; #include #include @@ -16,6 +18,8 @@ using namespace Hyprutils::String; #include #include #include +#include +#include #include static void help() { @@ -48,92 +52,96 @@ int main(int argc, char** argv) { setenv("MOZ_ENABLE_WAYLAND", "1", 1); // parse some args - std::string configPath; - std::string socketName; - int socketFd = -1; - bool ignoreSudo = false, verifyConfig = false; + std::string configPath; + std::string socketName; + int socketFd = -1; + bool ignoreSudo = false, verifyConfig = false; - std::vector args{argv + 1, argv + argc}; + if (argc > 1) { + std::span args{argv + 1, sc(argc - 1)}; - for (auto it = args.begin(); it != args.end(); it++) { - if (*it == "--i-am-really-stupid" && !ignoreSudo) { - std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system"); + for (auto it = args.begin(); it != args.end(); it++) { + std::string_view value = *it; - ignoreSudo = true; - } else if (*it == "--socket") { - if (std::next(it) == args.end()) { - help(); + if (value == "--i-am-really-stupid" && !ignoreSudo) { + std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system"); - return 1; - } + ignoreSudo = true; + } else if (value == "--socket") { + if (std::next(it) == args.end()) { + help(); - socketName = *std::next(it); - it++; - } else if (*it == "--wayland-fd") { - if (std::next(it) == args.end()) { - help(); - - return 1; - } - - try { - socketFd = std::stoi(*std::next(it)); - - // check if socketFd is a valid file descriptor - if (fcntl(socketFd, F_GETFD) == -1) - throw std::exception(); - } catch (...) { - std::println(stderr, "[ ERROR ] Invalid Wayland FD!"); - help(); - - return 1; - } - - it++; - } else if (*it == "-c" || *it == "--config") { - if (std::next(it) == args.end()) { - help(); - - return 1; - } - configPath = *std::next(it); - - try { - configPath = std::filesystem::canonical(configPath); - - if (!std::filesystem::is_regular_file(configPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); + + socketName = *std::next(it); + it++; + } else if (value == "--wayland-fd") { + if (std::next(it) == args.end()) { + help(); + + return 1; + } + + try { + socketFd = std::stoi(*std::next(it)); + + // check if socketFd is a valid file descriptor + if (fcntl(socketFd, F_GETFD) == -1) + throw std::exception(); + } catch (...) { + std::println(stderr, "[ ERROR ] Invalid Wayland FD!"); + help(); + + return 1; + } + + it++; + } else if (value == "-c" || value == "--config") { + if (std::next(it) == args.end()) { + help(); + + return 1; + } + configPath = *std::next(it); + + try { + configPath = std::filesystem::canonical(configPath); + + if (!std::filesystem::is_regular_file(configPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); + help(); + + return 1; + } + + Debug::log(LOG, "User-specified config location: '{}'", configPath); + + it++; + + continue; + } else if (value == "-h" || value == "--help") { + help(); + + return 0; + } else if (value == "-v" || value == "--version") { + std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); + return 0; + } else if (value == "--systeminfo") { + std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); + return 0; + } else if (value == "--verify-config") { + verifyConfig = true; + continue; + } else { + std::println(stderr, "[ ERROR ] Unknown option '{}' !", value); help(); return 1; } - - Debug::log(LOG, "User-specified config location: '{}'", configPath); - - it++; - - continue; - } else if (*it == "-h" || *it == "--help") { - help(); - - return 0; - } else if (*it == "-v" || *it == "--version") { - std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); - return 0; - } else if (*it == "--systeminfo") { - std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); - return 0; - } else if (*it == "--verify-config") { - verifyConfig = true; - continue; - } else { - std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); - help(); - - return 1; } }