mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-30 03:06:00 +01:00
Better and more secure argument parsing, and code reformatting (#1976)
* Better and more secure argument parsing, and code reformatting * Changes to resolve PR conversation * Formatted via clang-format, fixed typos * More typos
This commit is contained in:
parent
d8645cd148
commit
a35ea4d242
1 changed files with 51 additions and 20 deletions
71
src/main.cpp
71
src/main.cpp
|
@ -3,11 +3,26 @@
|
|||
#include "Compositor.hpp"
|
||||
#include "config/ConfigManager.hpp"
|
||||
#include "init/initHelpers.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#ifdef USES_SYSTEMD
|
||||
#include <systemd/sd-daemon.h> // for sd_notify
|
||||
#endif
|
||||
|
||||
void help() {
|
||||
std::cout << "usage: Hyprland [arg [...]].\n";
|
||||
std::cout << "\nArguments:\n";
|
||||
std::cout << " --help -h - Show this message again\n";
|
||||
std::cout << " --config FILE -c FILE - Specify config file to use\n";
|
||||
std::cout << " --i-am-really-stupid - Omits root user privileges check (why would you do that?)\n";
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
if (!getenv("XDG_RUNTIME_DIR"))
|
||||
|
@ -17,6 +32,7 @@ int main(int argc, char** argv) {
|
|||
std::string cmd = "";
|
||||
for (auto i = 0; i < argc; ++i)
|
||||
cmd += std::string(i == 0 ? "" : " ") + argv[i];
|
||||
|
||||
setenv("HYPRLAND_CMD", cmd.c_str(), 1);
|
||||
setenv("XDG_BACKEND", "wayland", 1);
|
||||
setenv("_JAVA_AWT_WM_NONREPARENTING", "1", 1);
|
||||
|
@ -24,30 +40,45 @@ int main(int argc, char** argv) {
|
|||
setenv("XDG_CURRENT_DESKTOP", "Hyprland", 1);
|
||||
|
||||
// parse some args
|
||||
std::string configPath;
|
||||
bool ignoreSudo = false;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (!strcmp(argv[i], "--i-am-really-stupid"))
|
||||
std::string configPath;
|
||||
bool ignoreSudo = false;
|
||||
|
||||
std::vector<std::string> args{argv + 1, argv + argc};
|
||||
|
||||
for (auto it = args.begin(); it != args.end(); it++) {
|
||||
if (it->compare("--i-am-really-stupid") == 0 && !ignoreSudo) {
|
||||
std::cout << "[ WARNING ] Running Hyprland with superuser privileges might damage your system\n";
|
||||
|
||||
ignoreSudo = true;
|
||||
else if ((!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config")) && argc >= i + 2) {
|
||||
configPath = std::string(argv[++i]);
|
||||
Debug::log(LOG, "Using config location %s.", configPath.c_str());
|
||||
} else {
|
||||
std::cout << "Hyprland usage: Hyprland [arg [...]].\n\nArguments:\n"
|
||||
<< "--help -h | Show this help message\n"
|
||||
<< "--config -c | Specify config file to use\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
else if (it->compare("-c") == 0 || it->compare("--config") == 0) {
|
||||
if (std::next(it)->c_str() == nullptr) {
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
std::string next_arg = std::next(it)->c_str();
|
||||
|
||||
if (!std::filesystem::exists(next_arg)) {
|
||||
std::cerr << "[ ERROR ] Config path '" << next_arg << "' doesn't exist!\n";
|
||||
help();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
configPath = next_arg;
|
||||
Debug::log(LOG, "User-specified config location: '%s'", configPath.c_str());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ignoreSudo) {
|
||||
if (Init::isSudo()) {
|
||||
std::cout << "Hyprland shall not be run as the root user. If you really want to, use the --i-am-really-stupid flag.\n";
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
std::cout << "Running with ignored root checks, I surely hope you know what you're doing.\n";
|
||||
sleep(1);
|
||||
if (!ignoreSudo && Init::isSudo()) {
|
||||
std::cerr << "[ ERROR ] Hyprland was launched with superuser priveleges, but the privileges check is not omitted.\n";
|
||||
std::cerr << " Hint: Use the --i-am-really-stupid flag to omit that check.\n";
|
||||
|
||||
return 1;
|
||||
} else if (ignoreSudo && Init::isSudo()) {
|
||||
std::cout << "Superuser privileges check is omitted. I hope you know what you're doing.\n";
|
||||
}
|
||||
|
||||
std::cout << "Welcome to Hyprland!\n";
|
||||
|
|
Loading…
Reference in a new issue