aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2022-05-28 17:27:29 +0000
committerDaniel Eklöf <daniel@ekloef.se>2022-06-13 09:55:23 +0000
commit755f96321ab47f931d11680027d47d1d772f5584 (patch)
treebf35e0c7ca5b15640513b2ad3f470c29a6127267
parentcommands: scroll up: simplify viewport clamping logic (diff)
config: add a new ‘environment’ section
This section allows the user to define custom environment variables to be set in the child process: [environment] name=value
-rw-r--r--config.c40
-rw-r--r--config.h8
-rw-r--r--foot.ini3
-rw-r--r--slave.c10
-rw-r--r--slave.h4
-rw-r--r--terminal.c2
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
@@ -2201,6 +2201,29 @@ err:
}
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)
{
struct config *conf = ctx->conf;
@@ -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=<number of logical CPUs>
+[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 <sys/types.h>
+#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)
{