mirror of
https://github.com/DreamMaoMao/maomaowm.git
synced 2026-05-06 06:46:54 -04:00
feat: add support for variable in config
This commit is contained in:
parent
4280f8e0bf
commit
e8bb29edee
1 changed files with 45 additions and 1 deletions
|
|
@ -324,6 +324,8 @@ typedef struct {
|
||||||
ConfigEnv **env;
|
ConfigEnv **env;
|
||||||
int env_count;
|
int env_count;
|
||||||
|
|
||||||
|
ConfigVariables *variables;
|
||||||
|
|
||||||
char **exec;
|
char **exec;
|
||||||
int exec_count;
|
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) {
|
void parse_config_line(Config *config, const char *line) {
|
||||||
char key[256], value[256];
|
char key[256], value[256];
|
||||||
if (sscanf(line, "%255[^=]=%255[^\n]", key, value) != 2) {
|
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(key);
|
||||||
trim_whitespace(value);
|
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) {
|
void parse_config_file(Config *config, const char *file_path) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue