From 755f96321ab47f931d11680027d47d1d772f5584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 28 May 2022 19:27:29 +0200 Subject: [PATCH] =?UTF-8?q?config:=20add=20a=20new=20=E2=80=98environment?= =?UTF-8?q?=E2=80=99=20section?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This section allows the user to define custom environment variables to be set in the child process: [environment] name=value --- config.c | 40 ++++++++++++++++++++++++++++++++++++++++ config.h | 8 ++++++++ foot.ini | 3 +++ slave.c | 10 ++++++++-- slave.h | 4 +++- terminal.c | 2 +- 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/config.c b/config.c index cb05319a..d2be087b 100644 --- a/config.c +++ b/config.c @@ -2200,6 +2200,29 @@ err: return false; } +static bool +parse_section_environment(struct context *ctx) +{ + struct config *conf = ctx->conf; + const char *key = ctx->key; + const char *value = ctx->value; + + tll_foreach(conf->env_vars, it) { + if (strcmp(it->item.name, key) == 0) { + free(it->item.value); + it->item.value = xstrdup(value); + return true; + } + } + + struct env_var var = { + .name = xstrdup(key), + .value = xstrdup(value), + }; + tll_push_back(conf->env_vars, var); + return true; +} + static bool parse_section_tweak(struct context *ctx) { @@ -2403,6 +2426,7 @@ enum section { SECTION_URL_BINDINGS, SECTION_MOUSE_BINDINGS, SECTION_TEXT_BINDINGS, + SECTION_ENVIRONMENT, SECTION_TWEAK, SECTION_COUNT, }; @@ -2427,6 +2451,7 @@ static const struct { [SECTION_URL_BINDINGS] = {&parse_section_url_bindings, "url-bindings"}, [SECTION_MOUSE_BINDINGS] = {&parse_section_mouse_bindings, "mouse-bindings"}, [SECTION_TEXT_BINDINGS] = {&parse_section_text_bindings, "text-bindings"}, + [SECTION_ENVIRONMENT] = {&parse_section_environment, "environment"}, [SECTION_TWEAK] = {&parse_section_tweak, "tweak"}, }; @@ -2867,6 +2892,7 @@ config_load(struct config *conf, const char *conf_path, .sixel = true, }, + .env_vars = tll_init(), .notifications = tll_init(), }; @@ -3147,6 +3173,14 @@ config_clone(const struct config *old) key_binding_list_clone(&conf->bindings.url, &old->bindings.url); key_binding_list_clone(&conf->bindings.mouse, &old->bindings.mouse); + tll_foreach(old->env_vars, it) { + struct env_var copy = { + .name = xstrdup(it->item.name), + .value = xstrdup(it->item.value), + }; + tll_push_back(conf->env_vars, copy); + } + conf->notifications.length = 0; conf->notifications.head = conf->notifications.tail = 0; tll_foreach(old->notifications, it) { @@ -3207,6 +3241,12 @@ config_free(struct config *conf) free_key_binding_list(&conf->bindings.url); free_key_binding_list(&conf->bindings.mouse); + tll_foreach(conf->env_vars, it) { + free(it->item.name); + free(it->item.value); + tll_remove(conf->env_vars, it); + } + user_notifications_free(&conf->notifications); } diff --git a/config.h b/config.h index 2061415e..de5d8a7b 100644 --- a/config.h +++ b/config.h @@ -104,6 +104,12 @@ struct config_spawn_template { struct argv argv; }; +struct env_var { + char *name; + char *value; +}; +typedef tll(struct env_var) env_var_list_t; + struct config { char *term; char *shell; @@ -296,6 +302,8 @@ struct config { struct config_spawn_template notify; bool notify_focus_inhibit; + env_var_list_t env_vars; + struct { enum fcft_scaling_filter fcft_filter; bool overflowing_glyphs; diff --git a/foot.ini b/foot.ini index 21c49174..f4dc4280 100644 --- a/foot.ini +++ b/foot.ini @@ -33,6 +33,9 @@ # selection-target=primary # workers= +[environment] +# name=value + [bell] # urgent=no # notify=no diff --git a/slave.c b/slave.c index db60d7b5..6473ac7c 100644 --- a/slave.c +++ b/slave.c @@ -305,8 +305,9 @@ err: pid_t slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, - char *const *envp, const char *term_env, const char *conf_shell, - bool login_shell, const user_notifications_t *notifications) + char *const *envp, const env_var_list_t *extra_env_vars, + const char *term_env, const char *conf_shell, bool login_shell, + const user_notifications_t *notifications) { int fork_pipe[2]; if (pipe2(fork_pipe, O_CLOEXEC) < 0) { @@ -356,6 +357,11 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, setenv("TERMINFO", FOOT_TERMINFO_PATH, 1); #endif + if (extra_env_vars != NULL) { + tll_foreach(*extra_env_vars, it) + setenv(it->item.name, it->item.value, 1); + } + char **_shell_argv = NULL; char **shell_argv = NULL; diff --git a/slave.h b/slave.h index 4b47bfde..b1c08f14 100644 --- a/slave.h +++ b/slave.h @@ -3,9 +3,11 @@ #include +#include "config.h" #include "user-notification.h" pid_t slave_spawn( int ptmx, int argc, const char *cwd, char *const *argv, char *const *envp, - const char *term_env, const char *conf_shell, bool login_shell, + const env_var_list_t *extra_env_vars, const char *term_env, + const char *conf_shell, bool login_shell, const user_notifications_t *notifications); diff --git a/terminal.c b/terminal.c index d8bf7516..4b1e5931 100644 --- a/terminal.c +++ b/terminal.c @@ -1248,7 +1248,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, /* Start the slave/client */ if ((term->slave = slave_spawn( - term->ptmx, argc, term->cwd, argv, envp, + term->ptmx, argc, term->cwd, argv, envp, &conf->env_vars, conf->term, conf->shell, conf->login_shell, &conf->notifications)) == -1) {