Commit 7babe76f97822c701050c6ee8d3984462f73c27f

Edward Thomson 2020-05-12T08:56:55

cli: introduce signal handler Provide a mechanism to add a signal handler for Unix or Win32.

diff --git a/src/cli/cli.h b/src/cli/cli.h
index 222d53a..7dede67 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -15,5 +15,6 @@
 #include "error.h"
 #include "opt.h"
 #include "opt_usage.h"
+#include "sighandler.h"
 
 #endif /* CLI_cli_h__ */
diff --git a/src/cli/sighandler.h b/src/cli/sighandler.h
new file mode 100644
index 0000000..877223e
--- /dev/null
+++ b/src/cli/sighandler.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef CLI_sighandler_h__
+#define CLI_sighandler_h__
+
+/**
+ * Sets up a signal handler that will run when the process is interrupted
+ * (via SIGINT on POSIX or Control-C or Control-Break on Windows).
+ *
+ * @param handler The function to run on interrupt
+ * @return 0 on success, -1 on failure
+ */
+int cli_sighandler_set_interrupt(void (*handler)(void));
+
+#endif /* CLI_sighandler_h__ */
diff --git a/src/cli/unix/sighandler.c b/src/cli/unix/sighandler.c
new file mode 100644
index 0000000..6b4982d
--- /dev/null
+++ b/src/cli/unix/sighandler.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include <stdint.h>
+#include <signal.h>
+#include "git2_util.h"
+#include "cli.h"
+
+static void (*interrupt_handler)(void) = NULL;
+
+static void interrupt_proxy(int signal)
+{
+	GIT_UNUSED(signal);
+	interrupt_handler();
+}
+
+int cli_sighandler_set_interrupt(void (*handler)(void))
+{
+	void (*result)(int);
+
+	if ((interrupt_handler = handler) != NULL)
+		result = signal(SIGINT, interrupt_proxy);
+	else
+		result = signal(SIGINT, SIG_DFL);
+
+	if (result == SIG_ERR) {
+		git_error_set(GIT_ERROR_OS, "could not set signal handler");
+		return -1;
+	}
+
+	return 0;
+}
diff --git a/src/cli/win32/sighandler.c b/src/cli/win32/sighandler.c
new file mode 100644
index 0000000..cc0b646
--- /dev/null
+++ b/src/cli/win32/sighandler.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "git2_util.h"
+#include <windows.h>
+
+#include "cli.h"
+
+static void (*interrupt_handler)(void) = NULL;
+
+static BOOL WINAPI interrupt_proxy(DWORD signal)
+{
+	GIT_UNUSED(signal);
+	interrupt_handler();
+	return TRUE;
+}
+
+int cli_sighandler_set_interrupt(void (*handler)(void))
+{
+	BOOL result;
+
+	if ((interrupt_handler = handler) != NULL)
+		result = SetConsoleCtrlHandler(interrupt_proxy, FALSE);
+	else
+		result = SetConsoleCtrlHandler(NULL, FALSE);
+
+	if (!result) {
+		git_error_set(GIT_ERROR_OS, "could not set control control handler");
+		return -1;
+	}
+
+	return 0;
+}