From 8e4d7f04e4c0353020151b6a7a721374c619878a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 11 Jul 2023 08:27:41 +0200 Subject: [PATCH] =?UTF-8?q?module/script:=20path:=20expand=20=E2=80=98~?= =?UTF-8?q?=E2=80=99=20to=20the=20user=E2=80=99s=20$HOME=20directory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #307 --- CHANGELOG.md | 1 + doc/yambar-modules-script.5.scd | 3 ++- modules/script.c | 35 +++++++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff897d..2b7eca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * modules/dwl: handle the appid status ([#284][284]) * battery: also show estimation for time to full ([#303][303]). * on-click: tilde expansion ([#307][307]) +* script: tilde expansion of `path` ([#307][307]). [246]: https://codeberg.org/dnkl/yambar/issues/246 [256]: https://codeberg.org/dnkl/yambar/pulls/256 diff --git a/doc/yambar-modules-script.5.scd b/doc/yambar-modules-script.5.scd index ec91c7e..d27a006 100644 --- a/doc/yambar-modules-script.5.scd +++ b/doc/yambar-modules-script.5.scd @@ -70,7 +70,8 @@ User defined. | path : string : yes -: Path to script/binary to execute. Must be an absolute path. +: Path to script/binary to execute. Must either be an absolute path, + or start with *~/*. | args : list of strings : no diff --git a/modules/script.c b/modules/script.c index f0cc9a7..63928a6 100644 --- a/modules/script.c +++ b/modules/script.c @@ -631,11 +631,11 @@ run(struct module *mod) } static struct module * -script_new(const char *path, size_t argc, const char *const argv[static argc], +script_new(char *path, size_t argc, const char *const argv[static argc], int poll_interval, struct particle *_content) { struct private *m = calloc(1, sizeof(*m)); - m->path = strdup(path); + m->path = path; m->content = _content; m->argc = argc; m->argv = malloc(argc * sizeof(m->argv[0])); @@ -655,7 +655,7 @@ script_new(const char *path, size_t argc, const char *const argv[static argc], static struct module * from_conf(const struct yml_node *node, struct conf_inherit inherited) { - const struct yml_node *path = yml_get_value(node, "path"); + const struct yml_node *path_node = yml_get_value(node, "path"); const struct yml_node *args = yml_get_value(node, "args"); const struct yml_node *c = yml_get_value(node, "content"); const struct yml_node *poll_interval = yml_get_value(node, "poll-interval"); @@ -673,8 +673,26 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) } } + const char *yml_path = yml_value_as_string(path_node); + char *path = NULL; + + if (yml_path[0] == '~' && yml_path[1] == '/') { + const char *home_dir = getenv("HOME"); + + if (home_dir == NULL) { + LOG_ERRNO("failed to expand '~"); + return NULL; + } + + if (asprintf(&path, "%s/%s", home_dir, yml_path + 2) < 0) { + LOG_ERRNO("failed to expand '~"); + return NULL; + } + } else + path = strdup(yml_path); + return script_new( - yml_value_as_string(path), argc, argv, + path, argc, argv, poll_interval != NULL ? yml_value_as_int(poll_interval) : 0, conf_to_particle(c, inherited)); } @@ -686,8 +704,13 @@ conf_verify_path(keychain_t *chain, const struct yml_node *node) return false; const char *path = yml_value_as_string(node); - if (strlen(path) < 1 || path[0] != '/') { - LOG_ERR("%s: path must be absolute", conf_err_prefix(chain, node)); + + const bool is_tilde = path[0] == '~' && path[1] == '/'; + const bool is_absolute = path[0] == '/'; + + if (!is_tilde && !is_absolute) { + LOG_ERR("%s: path must either be absolute, or begin with '~/'", + conf_err_prefix(chain, node)); return false; }