diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2021-01-31 13:40:27 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2021-02-07 15:33:32 +0000 |
| commit | 06aba59430bdb1ad5058ef513eeb91416057d1db (patch) | |
| tree | 01696d3c280233bd1aae7d44c590dd344f1792aa /notify.c | |
| parent | config: add ‘url-launch’ option, defaulting to “xdg-open ${url}” (diff) | |
notify: break out command template expansion to spawn_expand_template()
Diffstat (limited to 'notify.c')
| -rw-r--r-- | notify.c | 71 |
1 files changed, 11 insertions, 60 deletions
@@ -30,69 +30,20 @@ notify_notify(const struct terminal *term, const char *title, const char *body) if (term->conf->notify.argv == NULL) return; - size_t argv_size = 0; - for (; term->conf->notify.argv[argv_size] != NULL; argv_size++) - ; + char **argv = NULL; + size_t argc = 0; -#define append(s, n) \ - do { \ - expanded = xrealloc(expanded, len + (n) + 1); \ - memcpy(&expanded[len], s, n); \ - len += n; \ - expanded[len] = '\0'; \ - } while (0) - - char **argv = malloc((argv_size + 1) * sizeof(argv[0])); - - /* Expand ${title} and ${body} */ - for (size_t i = 0; i < argv_size; i++) { - size_t len = 0; - char *expanded = NULL; - - char *start = NULL; - char *last_end = term->conf->notify.argv[i]; - - while ((start = strstr(last_end, "${")) != NULL) { - /* Append everything from the last template's end to this - * one's beginning */ - append(last_end, start - last_end); - - /* Find end of template */ - start += 2; - char *end = strstr(start, "}"); - - if (end == NULL) { - /* Ensure final append() copies the unclosed '${' */ - last_end = start - 2; - LOG_WARN("notify: unclosed template: %s", last_end); - break; - } - - /* Expand template */ - if (strncmp(start, "title", end - start) == 0) - append(title, strlen(title)); - else if (strncmp(start, "body", end - start) == 0) - append(body, strlen(body)); - else { - /* Unrecognized template - append it as-is */ - start -= 2; - append(start, end + 1 - start); - LOG_WARN("notify: unrecognized template: %.*s", - (int)(end + 1 - start), start); - } - - last_end = end + 1;; - } - - append(last_end, term->conf->notify.argv[i] + strlen(term->conf->notify.argv[i]) - last_end); - argv[i] = expanded; + if (!spawn_expand_template( + &term->conf->notify, 2, + (const char *[]){"title", "body"}, + (const char *[]){title, body}, + &argc, &argv)) + { + return; } - argv[argv_size] = NULL; - -#undef append LOG_DBG("notify command:"); - for (size_t i = 0; i < argv_size; i++) + for (size_t i = 0; i < argc; i++) LOG_DBG(" argv[%zu] = \"%s\"", i, argv[i]); /* Redirect stdin to /dev/null, but ignore failure to open */ @@ -102,7 +53,7 @@ notify_notify(const struct terminal *term, const char *title, const char *body) if (devnull >= 0) close(devnull); - for (size_t i = 0; i < argv_size; i++) + for (size_t i = 0; i < argc; i++) free(argv[i]); free(argv); } |
