mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-04-02 07:15:31 -04:00
notify: break out command template expansion to spawn_expand_template()
This commit is contained in:
parent
4233c806c3
commit
06aba59430
3 changed files with 97 additions and 60 deletions
71
notify.c
71
notify.c
|
|
@ -30,69 +30,20 @@ notify_notify(const struct terminal *term, const char *title, const char *body)
|
||||||
if (term->conf->notify.argv == NULL)
|
if (term->conf->notify.argv == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
size_t argv_size = 0;
|
char **argv = NULL;
|
||||||
for (; term->conf->notify.argv[argv_size] != NULL; argv_size++)
|
size_t argc = 0;
|
||||||
;
|
|
||||||
|
|
||||||
#define append(s, n) \
|
if (!spawn_expand_template(
|
||||||
do { \
|
&term->conf->notify, 2,
|
||||||
expanded = xrealloc(expanded, len + (n) + 1); \
|
(const char *[]){"title", "body"},
|
||||||
memcpy(&expanded[len], s, n); \
|
(const char *[]){title, body},
|
||||||
len += n; \
|
&argc, &argv))
|
||||||
expanded[len] = '\0'; \
|
{
|
||||||
} while (0)
|
return;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
argv[argv_size] = NULL;
|
|
||||||
|
|
||||||
#undef append
|
|
||||||
|
|
||||||
LOG_DBG("notify command:");
|
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]);
|
LOG_DBG(" argv[%zu] = \"%s\"", i, argv[i]);
|
||||||
|
|
||||||
/* Redirect stdin to /dev/null, but ignore failure to open */
|
/* 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)
|
if (devnull >= 0)
|
||||||
close(devnull);
|
close(devnull);
|
||||||
|
|
||||||
for (size_t i = 0; i < argv_size; i++)
|
for (size_t i = 0; i < argc; i++)
|
||||||
free(argv[i]);
|
free(argv[i]);
|
||||||
free(argv);
|
free(argv);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
80
spawn.c
80
spawn.c
|
|
@ -1,5 +1,6 @@
|
||||||
#include "spawn.h"
|
#include "spawn.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
@ -11,6 +12,7 @@
|
||||||
#define LOG_ENABLE_DBG 0
|
#define LOG_ENABLE_DBG 0
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
#include "xmalloc.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
spawn(struct reaper *reaper, const char *cwd, char *const argv[],
|
spawn(struct reaper *reaper, const char *cwd, char *const argv[],
|
||||||
|
|
@ -74,3 +76,81 @@ err:
|
||||||
close(pipe_fds[1]);
|
close(pipe_fds[1]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
spawn_expand_template(const struct config_spawn_template *template,
|
||||||
|
size_t key_count,
|
||||||
|
const char *key_names[static key_count],
|
||||||
|
const char *key_values[static key_count],
|
||||||
|
size_t *argc, char ***argv)
|
||||||
|
{
|
||||||
|
*argc = 0;
|
||||||
|
*argv = NULL;
|
||||||
|
|
||||||
|
for (; template->argv[*argc] != NULL; (*argc)++)
|
||||||
|
;
|
||||||
|
|
||||||
|
#define append(s, n) \
|
||||||
|
do { \
|
||||||
|
expanded = xrealloc(expanded, len + (n) + 1); \
|
||||||
|
memcpy(&expanded[len], s, n); \
|
||||||
|
len += n; \
|
||||||
|
expanded[len] = '\0'; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
*argv = malloc((*argc + 1) * sizeof((*argv)[0]));
|
||||||
|
|
||||||
|
/* Expand the provided keys */
|
||||||
|
for (size_t i = 0; i < *argc; i++) {
|
||||||
|
size_t len = 0;
|
||||||
|
char *expanded = NULL;
|
||||||
|
|
||||||
|
char *start = NULL;
|
||||||
|
char *last_end = template->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 */
|
||||||
|
bool valid_key = false;
|
||||||
|
for (size_t j = 0; j < key_count; j++) {
|
||||||
|
if (strncmp(start, key_names[j], end - start) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
append(key_values[j], strlen(key_values[j]));
|
||||||
|
valid_key = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!valid_key) {
|
||||||
|
/* 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, template->argv[i] + strlen(template->argv[i]) - last_end);
|
||||||
|
(*argv)[i] = expanded;
|
||||||
|
}
|
||||||
|
(*argv)[*argc] = NULL;
|
||||||
|
|
||||||
|
#undef append
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
||||||
6
spawn.h
6
spawn.h
|
|
@ -1,7 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "config.h"
|
||||||
#include "reaper.h"
|
#include "reaper.h"
|
||||||
|
|
||||||
bool spawn(struct reaper *reaper, const char *cwd, char *const argv[],
|
bool spawn(struct reaper *reaper, const char *cwd, char *const argv[],
|
||||||
int stdin_fd, int stdout_fd, int stderr_fd);
|
int stdin_fd, int stdout_fd, int stderr_fd);
|
||||||
|
|
||||||
|
bool spawn_expand_template(
|
||||||
|
const struct config_spawn_template *template,
|
||||||
|
size_t key_count, const char *key_names[static key_count],
|
||||||
|
const char *key_values[static key_count], size_t *argc, char ***argv);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue