From 2230525f4a14ae9e8a5e7a76ae205101904a7f7f Mon Sep 17 00:00:00 2001
From: Lars Henriksen <LarsHenriksen@get2net.dk>
Date: Sun, 12 Jul 2020 23:30:45 +0200
Subject: 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>
---
 src/hooks.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

(limited to 'src')

diff --git a/src/hooks.c b/src/hooks.c
index 20aed69..8fda0da 100644
--- a/src/hooks.c
+++ b/src/hooks.c
@@ -35,36 +35,41 @@
  */
 
 #include <stddef.h>
+#include <sys/wait.h>
 
 #include "calcurse.h"
 
 int run_hook(const char *name)
 {
-	char *hook_path = NULL;
+	char *hook_cmd = NULL, *mesg;
 	char const *arg[2];
 	int pid, ret = -127;
-	int prepare_wins = (ui_mode == UI_CURSES);
 
-	asprintf(&hook_path, "%s/%s", path_hooks, name);
-	arg[0] = hook_path;
-	arg[1] = NULL;
-
-	if (!io_file_exists(hook_path))
+	asprintf(&hook_cmd, "%s/%s", path_hooks, name);
+	if (!io_file_exists(hook_cmd))
 		goto cleanup;
 
-	if (prepare_wins)
-		wins_prepare_external();
+	asprintf(&hook_cmd, "%s <&- >&- 2>&-", hook_cmd);
+	arg[0] = hook_cmd;
+	arg[1] = NULL;
 
 	if ((pid = shell_exec(NULL, NULL, *arg, arg))) {
 		ret = child_wait(NULL, NULL, pid);
-		if (ret)
-			press_any_key();
+		if (ret > 0 && WIFEXITED(ret)) {
+			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:
-	mem_free(hook_path);
+	mem_free(hook_cmd);
 	return ret;
 }
-- 
cgit v1.2.3-70-g09d2