diff --git a/src/config.cpp b/src/config.cpp index 444a66e..216f924 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -7,6 +7,7 @@ #include using namespace Hyprlang; +extern "C" char** environ; static std::string removeBeginEndSpacesTabs(std::string str) { if (str.empty()) @@ -35,6 +36,16 @@ CConfig::CConfig(const char* path) { if (!std::filesystem::exists(impl->path)) throw "File does not exist"; + + impl->envVariables.clear(); + for (char** env = environ; *env; ++env) { + const std::string ENVVAR = *env; + const auto VARIABLE = ENVVAR.substr(0, ENVVAR.find_first_of('=')); + const auto VALUE = ENVVAR.substr(ENVVAR.find_first_of('=') + 1); + impl->envVariables.push_back({VARIABLE, VALUE}); + } + + std::sort(impl->envVariables.begin(), impl->envVariables.end(), [&](const auto& a, const auto& b) { return a.name.length() > b.name.length(); }); } CConfig::~CConfig() { @@ -301,7 +312,7 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) { replaceAll(RHS, "$" + var.name, var.value); if (RHSIT == std::string::npos && LHSIT == std::string::npos) - break; + continue; else var.linesContainingVar.push_back(line); @@ -398,7 +409,11 @@ CParseResult CConfig::parse() { iffile.close(); - clearState(); + if (!impl->categories.empty()) { + fileParseResult.setError("Unclosed category at EOF"); + impl->categories.clear(); + return fileParseResult; + } return fileParseResult; } @@ -414,6 +429,7 @@ CParseResult CConfig::parseDynamic(const char* command, const char* value) { void CConfig::clearState() { impl->categories.clear(); impl->parseError = ""; + impl->variables = impl->envVariables; } CConfigValue* CConfig::getConfigValuePtr(const char* name) { diff --git a/src/config.hpp b/src/config.hpp index d1005b9..4026dc5 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -24,6 +24,7 @@ class CConfigImpl { std::unordered_map defaultValues; std::vector handlers; std::vector variables; + std::vector envVariables; std::vector categories; diff --git a/tests/config/config.conf b/tests/config/config.conf index d2b7448..c0174ad 100644 --- a/tests/config/config.conf +++ b/tests/config/config.conf @@ -9,6 +9,8 @@ $MY_VAR = 1337 $MY_VAR_2 = $MY_VAR testVar = $MY_VAR$MY_VAR_2 +testEnv = $XDG_SESSION_TYPE + testCategory { testValueInt = 123456 testValueHex = 0xF diff --git a/tests/main.cpp b/tests/main.cpp index f69203e..59e0743 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -53,6 +53,7 @@ int main(int argc, char** argv, char** envp) { config.addConfigValue("testFloat", 0.F); config.addConfigValue("testVec", Hyprlang::SVector2D{69, 420}); config.addConfigValue("testString", ""); + config.addConfigValue("testEnv", ""); config.addConfigValue("testVar", 0L); config.addConfigValue("testStringQuotes", ""); config.addConfigValue("testCategory:testValueInt", 0L); @@ -116,6 +117,9 @@ int main(int argc, char** argv, char** envp) { EXPECT(config.parseDynamic("$MY_VAR_2 = 420").error, false); EXPECT(std::any_cast(config.getConfigValue("testVar")), 1337420); + // test env variables + EXPECT(std::any_cast(config.getConfigValue("testEnv")), std::string{"wayland"}); + } catch (const char* e) { std::cout << Colors::RED << "Error: " << Colors::RESET << e << "\n"; return 1;