From 02f0e0d912838572ff23fb02547eb3a6ac635d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 30 Jul 2021 14:44:16 +0200 Subject: [PATCH 1/4] config: include: add support for ~-expansion Closes #659 --- CHANGELOG.md | 2 ++ config.c | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a97aa72f..d8c84365 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ * Window title in the CSDs (https://codeberg.org/dnkl/foot/issues/638). * `-Ddocs=disabled|enabled|auto` meson command line option. +* Support for `~`-expansion in the `include` directive + (https://codeberg.org/dnkl/foot/issues/659). ### Changed diff --git a/config.c b/config.c index f671e7e0..6b51c506 100644 --- a/config.c +++ b/config.c @@ -644,12 +644,24 @@ parse_section_main(const char *key, const char *value, struct config *conf, const char *path, unsigned lineno, bool errors_are_fatal) { if (strcmp(key, "include") == 0) { - const char *include_path = value; + char *_include_path = NULL; + const char *include_path = NULL; + + if (value[0] == '~' && value[1] == '/') { + const char *home_dir = get_user_home_dir(); + + int chars = snprintf(NULL, 0, "%s/%s", home_dir, &value[2]); + _include_path = malloc(chars + 1); + snprintf(_include_path, chars + 1, "%s/%s", home_dir, &value[2]); + include_path = _include_path; + } else + include_path = value; if (include_path[0] != '/') { LOG_AND_NOTIFY_ERR( "%s:%d: [default]: %s: not an absolute path", path, lineno, include_path); + free(_include_path); return false; } @@ -659,6 +671,7 @@ parse_section_main(const char *key, const char *value, struct config *conf, LOG_AND_NOTIFY_ERRNO( "%s:%d: [default]: %s: failed to open", path, lineno, include_path); + free(_include_path); return false; } @@ -667,6 +680,7 @@ parse_section_main(const char *key, const char *value, struct config *conf, fclose(include); LOG_INFO("imported sub-configuration from %s", include_path); + free(_include_path); return ret; } From 7cfa48f369403d86dda2944511df0c23ce3dabd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 30 Jul 2021 14:47:45 +0200 Subject: [PATCH 2/4] config: include: handle get_user_home_dir() returning NULL --- config.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/config.c b/config.c index 6b51c506..2c76ffa5 100644 --- a/config.c +++ b/config.c @@ -650,6 +650,13 @@ parse_section_main(const char *key, const char *value, struct config *conf, if (value[0] == '~' && value[1] == '/') { const char *home_dir = get_user_home_dir(); + if (home_dir == NULL) { + LOG_AND_NOTIFY_ERRNO( + "%s:%d: [default]: include: %s: failed to expand '~'", + path, lineno, value); + return false; + } + int chars = snprintf(NULL, 0, "%s/%s", home_dir, &value[2]); _include_path = malloc(chars + 1); snprintf(_include_path, chars + 1, "%s/%s", home_dir, &value[2]); @@ -659,7 +666,7 @@ parse_section_main(const char *key, const char *value, struct config *conf, if (include_path[0] != '/') { LOG_AND_NOTIFY_ERR( - "%s:%d: [default]: %s: not an absolute path", + "%s:%d: [default]: include: %s: not an absolute path", path, lineno, include_path); free(_include_path); return false; @@ -669,7 +676,7 @@ parse_section_main(const char *key, const char *value, struct config *conf, if (include == NULL) { LOG_AND_NOTIFY_ERRNO( - "%s:%d: [default]: %s: failed to open", + "%s:%d: [default]: include: %s: failed to open", path, lineno, include_path); free(_include_path); return false; From ed855c72dc4bd4e83afaa9b605fe8bb86d97fe67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 30 Jul 2021 14:53:12 +0200 Subject: [PATCH 3/4] doc: foot.ini: include paths are allowed to begin with ~/ --- doc/foot.ini.5.scd | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 8233ed48..5e854420 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -70,13 +70,11 @@ in this order: The import file has its own section scope. I.e. the including configuration is still in the default section after the include, regardless of which section the included file ends in. - - The path must be an absolute path. - Multiple include directives are allowed, but only one path per - directive. - - Nested imports are allowed. + - The path must be an absolute path, or start with *~/*. + - Multiple include directives are allowed, but only one path per + directive. + - Nested imports are allowed. Default: _not set_. From 8f307178df4e7fc4f78d34e9875b8d69b97ea220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 31 Jul 2021 00:39:51 +0200 Subject: [PATCH 4/4] config: include: use xasprintf() instead of two calls to snprintf() --- config.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config.c b/config.c index 2c76ffa5..f57129a7 100644 --- a/config.c +++ b/config.c @@ -657,9 +657,7 @@ parse_section_main(const char *key, const char *value, struct config *conf, return false; } - int chars = snprintf(NULL, 0, "%s/%s", home_dir, &value[2]); - _include_path = malloc(chars + 1); - snprintf(_include_path, chars + 1, "%s/%s", home_dir, &value[2]); + _include_path = xasprintf("%s/%s", home_dir, value + 2); include_path = _include_path; } else include_path = value;