Run hooks quietly

Hooks should not be run like external programs, but like load and save
operations that do not take possession of the terminal.

Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen 2020-07-12 23:30:45 +02:00 committed by Lukas Fleischer
parent f743eab5ac
commit 2230525f4a

View File

@ -35,36 +35,41 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include <sys/wait.h>
#include "calcurse.h" #include "calcurse.h"
int run_hook(const char *name) int run_hook(const char *name)
{ {
char *hook_path = NULL; char *hook_cmd = NULL, *mesg;
char const *arg[2]; char const *arg[2];
int pid, ret = -127; int pid, ret = -127;
int prepare_wins = (ui_mode == UI_CURSES);
asprintf(&hook_path, "%s/%s", path_hooks, name); asprintf(&hook_cmd, "%s/%s", path_hooks, name);
arg[0] = hook_path; if (!io_file_exists(hook_cmd))
arg[1] = NULL;
if (!io_file_exists(hook_path))
goto cleanup; goto cleanup;
if (prepare_wins) asprintf(&hook_cmd, "%s <&- >&- 2>&-", hook_cmd);
wins_prepare_external(); arg[0] = hook_cmd;
arg[1] = NULL;
if ((pid = shell_exec(NULL, NULL, *arg, arg))) { if ((pid = shell_exec(NULL, NULL, *arg, arg))) {
ret = child_wait(NULL, NULL, pid); ret = child_wait(NULL, NULL, pid);
if (ret) if (ret > 0 && WIFEXITED(ret)) {
press_any_key(); asprintf(&mesg, "%s hook: exit status %d",
name,
WEXITSTATUS(ret));
que_ins(mesg, now(), 3);
mem_free(mesg);
} else if (ret != 0) {
asprintf(&mesg, "%s hook: abnormal termination",
name);
que_ins(mesg, now(), 4);
mem_free(mesg);
}
} }
if (prepare_wins)
wins_unprepare_external();
cleanup: cleanup:
mem_free(hook_path); mem_free(hook_cmd);
return ret; return ret;
} }