From 6717a5f62ec9b2f83f7761f492abcd3ce6d5b336 Mon Sep 17 00:00:00 2001 From: Nikita Ivanov Date: Tue, 31 May 2022 22:53:07 +0500 Subject: [PATCH] Set $fifo variable when ctpv is started --- clear.sh | 2 +- end.sh | 2 +- helpers.sh | 12 ++++++++++-- preview.c | 4 ++++ server.c | 24 ++++++++++++++++++++++-- server.h | 1 + 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/clear.sh b/clear.sh index 3229e1e..1db0456 100644 --- a/clear.sh +++ b/clear.sh @@ -1,3 +1,3 @@ -setup_fifo "$1" 1 +setup_fifo 1 printf '{"action": "remove", "identifier": "preview"}\n' > "$fifo" diff --git a/end.sh b/end.sh index 38cb31f..1f893d3 100644 --- a/end.sh +++ b/end.sh @@ -1,4 +1,4 @@ -setup_fifo "$1" 1 +setup_fifo 1 # tell ctpv server to exit printf '\0' > "$fifo" diff --git a/helpers.sh b/helpers.sh index d14375d..0f52c85 100644 --- a/helpers.sh +++ b/helpers.sh @@ -1,3 +1,7 @@ +echo_err() { + echo "$@" >&2 +} + fifo_open() { # https://unix.stackexchange.com/a/522940/183147 dd oflag=nonblock conv=notrunc,nocreat count=0 of="$1" \ @@ -5,8 +9,12 @@ fifo_open() { } setup_fifo() { - fifo="$(printf '/tmp/ctpvfifo.%s' "${1:-$id}")" - exit_code="${2:-127}" + if [ -z "$fifo" ]; then + echo_err '$fifo is empty!' + exit 1 + fi + + exit_code="${1:-127}" [ -e "$fifo" ] || exit "$exit_code" fifo_open "$fifo" || exit "$exit_code" } diff --git a/preview.c b/preview.c index a6d0cc6..6a56623 100644 --- a/preview.c +++ b/preview.c @@ -4,6 +4,7 @@ #include "utils.h" #include "error.h" #include "shell.h" +#include "server.h" #include "preview.h" #define FAILED_PREVIEW_EC NOTEXIST_EC @@ -157,6 +158,9 @@ static int run(Preview *p, int *exitcode) int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa) { + if (pa->id || (pa->id = getenv("id"))) + ERRCHK_RET_OK(server_set_fifo_var(pa->id)); + SET_PENV("ctpv", pa->ctpv); SET_PENV("f", pa->f); SET_PENV("w", pa->w); diff --git a/server.c b/server.c index 10fd49b..60ea268 100644 --- a/server.c +++ b/server.c @@ -11,6 +11,8 @@ #include "shell.h" #include "gen/server.h" +#define FIFO_FILENAME_SIZE 256 + static pid_t ueberzug_pid; static void kill_ueberzug(void) @@ -99,6 +101,11 @@ static int check_ueberzug(int *exitcode) return spawn(args, NULL, exitcode, NULL, NULL); } +static void get_fifo_name(char *buf, size_t len, const char *id_s) +{ + snprintf(buf, len-1, "/tmp/ctpvfifo.%s", id_s); +} + int server_listen(char const *id_s) { int ret = OK; @@ -111,8 +118,8 @@ int server_listen(char const *id_s) goto exit; } - char fifo[256]; - snprintf(fifo, LEN(fifo)-1, "/tmp/ctpvfifo.%s", id_s); + char fifo[FIFO_FILENAME_SIZE]; + get_fifo_name(fifo, LEN(fifo), id_s); ERRCHK_GOTO(mkfifo(fifo, 0600) == -1 && errno != EEXIST, ret, exit, FUNCFAILED("mkfifo"), ERRNOS); @@ -148,12 +155,25 @@ cleanup: return ret; } +int server_set_fifo_var(const char *id_s) +{ + char fifo[FIFO_FILENAME_SIZE]; + get_fifo_name(fifo, LEN(fifo), id_s); + ERRCHK_RET(setenv("fifo", fifo, 1) != 0, FUNCFAILED("setenv"), ERRNOS); + + return OK; +} + int server_clear(const char *id_s) { + ERRCHK_RET_OK(server_set_fifo_var(id_s)); + return run_script(scr_clear_sh, LEN(scr_clear_sh)-1, (char *)id_s); } int server_end(const char *id_s) { + ERRCHK_RET_OK(server_set_fifo_var(id_s)); + return run_script(scr_end_sh, LEN(scr_end_sh)-1, (char *)id_s); } diff --git a/server.h b/server.h index ee3e964..c589162 100644 --- a/server.h +++ b/server.h @@ -2,6 +2,7 @@ #define SERVER_H int server_listen(char const *id_s); +int server_set_fifo_var(const char *id_s); int server_clear(const char *id_s); int server_end(const char *id_s);