feat: add support for variable in config

This commit is contained in:
Victor Tennekes 2025-11-25 13:08:48 +01:00
parent 4280f8e0bf
commit e8bb29edee
No known key found for this signature in database

View file

@ -324,6 +324,8 @@ typedef struct {
ConfigEnv **env;
int env_count;
ConfigVariables *variables;
char **exec;
int exec_count;
@ -2258,6 +2260,42 @@ void parse_option(Config *config, char *key, char *value) {
}
}
void parse_config_store_variable(Config *config, const char *key, const char *value) {
ConfigVariable *var = config->variables;
while (var) {
if (strcmp(var->name, key) == 0) {
free(var->value);
var->value = strdup(value);
return ;
}
var = var->next;
}
ConfigVariable *new_var = malloc(sizeof(ConfigVariable));
new_var->name = strdup(key);
new_var->value = strdup(value);
new_var->next = config->variables;
config->variables = new_var;
}
char *parse_config_expand_variable(Config *config, const char *value) {
if (value[0] != '$') {
return strdup(value);
}
const char *var_name = value + 1;
ConfigVariable *var = config->variables;
while (var) {
if (strcmp(var->name, var_name) == 0) {
return strdup(var->value);
}
var = var->next;
}
return strdup(value);
}
void parse_config_line(Config *config, const char *line) {
char key[256], value[256];
if (sscanf(line, "%255[^=]=%255[^\n]", key, value) != 2) {
@ -2269,7 +2307,13 @@ void parse_config_line(Config *config, const char *line) {
trim_whitespace(key);
trim_whitespace(value);
parse_option(config, key, value);
if (key[0] == '$') {
parse_config_store_variable(config, key + 1, value);
} else {
char *expanded_value = parse_config_expand_variable(config, value);
parse_option(config, key, expanded_value);
free(expanded_value);
}
}
void parse_config_file(Config *config, const char *file_path) {