Set environment variables as configuration variables

Allows to refer env vars inside the configuration, as follows:

   background = "$XDG_CONFIG_HOME/wallpapers/file.png"
This commit is contained in:
andy 2020-09-14 19:13:42 +00:00
parent 74f5249c33
commit c0c15f3f93
2 changed files with 41 additions and 0 deletions

View File

@ -160,6 +160,17 @@ terminal application.
On startup **hikari** attempts to execute _~/.config/hikari/autostart_ to
autostart applications.
Environment Variables
---------------------
**hikari** supports the use of environment variables in its string values.
Occurrences of `${VARIABLE}` or `$VARIABLE` inside a string will be substituted
with the value of an environment variable named `VARIABLE`. If no such
environment variable exists, no substituation takes place.
You can use double dollar signs to escape variables: `$${VARIABLE}` will result
in `${VARIABLE}`, `$$VARIABLE` will result in `$VARIABLE`.
ACTIONS
=======

View File

@ -36,6 +36,8 @@
#include <hikari/view_config.h>
#include <hikari/workspace.h>
extern char **environ;
struct hikari_configuration *hikari_configuration = NULL;
static bool
@ -1563,11 +1565,39 @@ done:
return success;
}
bool
set_env_vars(struct ucl_parser *parser)
{
for (char **current_var = environ; *current_var != NULL; ++current_var) {
const char *separator = strchr(*current_var, '=');
if (separator == NULL) {
continue;
}
const size_t name_length = separator - *current_var;
char *name = hikari_malloc(name_length + 1);
if (name == NULL) {
fprintf(stderr, "Could not allocate enough memory :(\n");
return false;
}
strncpy(name, *current_var, name_length);
name[name_length] = '\0';
ucl_parser_register_variable(parser, name, separator + 1);
hikari_free(name);
}
return true;
}
bool
hikari_configuration_load(
struct hikari_configuration *configuration, char *config_path)
{
struct ucl_parser *parser = ucl_parser_new(0);
if (!set_env_vars(parser)) {
ucl_parser_free(parser);
return false;
}
bool success = false;
const ucl_object_t *cur;