Skip to content

Commit

Permalink
config: add source parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricekraus committed Feb 20, 2024
1 parent 7b15d34 commit 66af704
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
55 changes: 54 additions & 1 deletion src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#include "ConfigManager.hpp"
#include "../helpers/MiscFunctions.hpp"
#include <filesystem>
#include <glob.h>
#include <cstring>

static Hyprlang::CParseResult handleSource(const char* c, const char* v) {
const std::string VALUE = v;
const std::string COMMAND = c;

const auto RESULT = g_pConfigManager->handleSource(COMMAND, VALUE);

Hyprlang::CParseResult result;
if (RESULT.has_value())
result.setError(RESULT.value().c_str());
return result;
}


static std::string getConfigDir() {
static const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
Expand All @@ -15,7 +31,7 @@ static std::string getMainConfigPath() {
}

CConfigManager::CConfigManager() : m_config(getMainConfigPath().c_str(), Hyprlang::SConfigOptions{.throwAllErrors = true, .allowMissingConfig = true}) {
;
configCurrentPath = getMainConfigPath();
}

void CConfigManager::init() {
Expand Down Expand Up @@ -53,6 +69,8 @@ void CConfigManager::init() {
m_config.addSpecialConfigValue("label", "halign", Hyprlang::STRING{"none"});
m_config.addSpecialConfigValue("label", "valign", Hyprlang::STRING{"none"});

m_config.registerHandler(&::handleSource, "source", {false});

m_config.commence();

auto result = m_config.parse();
Expand Down Expand Up @@ -132,3 +150,38 @@ std::vector<CConfigManager::SWidgetConfig> CConfigManager::getWidgetConfigs() {

return result;
}


std::optional<std::string> CConfigManager::handleSource(const std::string& command, const std::string& rawpath) {
if (rawpath.length() < 2) {
Debug::log(ERR, "source= path garbage");
return "source path " + rawpath + " bogus!";
}
std::unique_ptr<glob_t, void (*)(glob_t*)> glob_buf{new glob_t, [](glob_t* g) { globfree(g); }};
memset(glob_buf.get(), 0, sizeof(glob_t));

if (auto r = glob(absolutePath(rawpath, configCurrentPath).c_str(), GLOB_TILDE, nullptr, glob_buf.get()); r != 0) {
std::string err = std::format("source= globbing error: {}", r == GLOB_NOMATCH ? "found no match" : GLOB_ABORTED ? "read error" : "out of memory");
Debug::log(ERR, "{}", err);
return err;
}

for (size_t i = 0; i < glob_buf->gl_pathc; i++) {
auto value = absolutePath(glob_buf->gl_pathv[i], configCurrentPath);

if (!std::filesystem::is_regular_file(value)) {
if (std::filesystem::exists(value)) {
Debug::log(WARN, "source= skipping non-file {}", value);
continue;
}

Debug::log(ERR, "source= file doesnt exist");
return "source file " + value + " doesn't exist!";
}

m_config.parseFile(value.c_str());

}

return {};
}
6 changes: 4 additions & 2 deletions src/config/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <unordered_map>

class CConfigManager {
public:
public:
CConfigManager();
void init();
void* const* getValuePtr(const std::string& name);
Expand All @@ -22,8 +22,10 @@ class CConfigManager {
};

std::vector<SWidgetConfig> getWidgetConfigs();
std::optional<std::string> handleSource(const std::string&, const std::string&);

private:
std::string configCurrentPath;
private:
Hyprlang::CConfig m_config;
};

Expand Down
25 changes: 25 additions & 0 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "MiscFunctions.hpp"

std::string absolutePath(const std::string& rawpath, const std::string& currentPath) {
auto value = rawpath;

if (value[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
value.replace(0, 1, std::string(ENVHOME));
} else if (value[0] != '/') {
auto currentDir = currentPath.substr(0, currentPath.find_last_of('/'));

if (value[0] == '.') {
if (value[1] == '.' && value[2] == '/') {
auto parentDir = currentDir.substr(0, currentDir.find_last_of('/'));
value.replace(0, 2 + currentPath.empty(), parentDir);
} else if (value[1] == '/')
value.replace(0, 1 + currentPath.empty(), currentDir);
else
value = currentDir + '/' + value;
} else
value = currentDir + '/' + value;
}

return value;
}
6 changes: 6 additions & 0 deletions src/helpers/MiscFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <string>

std::string absolutePath(const std::string&, const std::string&);

0 comments on commit 66af704

Please sign in to comment.