From ccfb953bb020a1d229490b8a3e7790ed5c7e1752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 1 Dec 2022 19:43:38 +0100 Subject: [PATCH] slave: unsetenv() env vars that have been set to the empty string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That is, users can now *clear* environment variables by doing: [environment] VAR=”” Note that the quotes are required. Closes #1225 --- CHANGELOG.md | 3 +++ config.c | 23 ++++++++++++----------- slave.c | 11 +++++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bffe4e11..ead014cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,8 +51,11 @@ * `name` capability to `XTGETTCAP`. * String values in `foot.ini` may now be quoted. This can be used to set a value to the empty string, for example. +* Environment variables can now be **unset**, by setting + `[environment].=""` (quotes are required) ([#1225][1225]) [1136]: https://codeberg.org/dnkl/foot/issues/1136 +[1225]: https://codeberg.org/dnkl/foot/issues/1225 ### Changed diff --git a/config.c b/config.c index cbf64abc..68641647 100644 --- a/config.c +++ b/config.c @@ -2295,21 +2295,22 @@ parse_section_environment(struct context *ctx) { struct config *conf = ctx->conf; const char *key = ctx->key; - const char *value = ctx->value; + /* Check for pre-existing env variable */ tll_foreach(conf->env_vars, it) { - if (strcmp(it->item.name, key) == 0) { - free(it->item.value); - it->item.value = xstrdup(value); - return true; - } + if (strcmp(it->item.name, key) == 0) + return value_to_str(ctx, &it->item.value); } - struct env_var var = { - .name = xstrdup(key), - .value = xstrdup(value), - }; - tll_push_back(conf->env_vars, var); + /* + * No pre-existing variable - allocate a new one + */ + + char *value = NULL; + if (!value_to_str(ctx, &value)) + return false; + + tll_push_back(conf->env_vars, ((struct env_var){xstrdup(key), value})); return true; } diff --git a/slave.c b/slave.c index 4dd80e6f..d4861ad0 100644 --- a/slave.c +++ b/slave.c @@ -359,8 +359,15 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, #endif if (extra_env_vars != NULL) { - tll_foreach(*extra_env_vars, it) - setenv(it->item.name, it->item.value, 1); + tll_foreach(*extra_env_vars, it) { + const char *name = it->item.name; + const char *value = it->item.value; + + if (strlen(value) == 0) + unsetenv(name); + else + setenv(name, value, 1); + } } char **_shell_argv = NULL;