From 28d87592945acf85e03ffeb948728773b9ea2d6d Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 28 Dec 2015 08:21:34 +0100 Subject: [PATCH] Setup the cancel signal handler. --- examples/dctool.c | 23 +++++++++++++++++++++++ examples/dctool.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/examples/dctool.c b/examples/dctool.c index 48f3687..0bc3c55 100644 --- a/examples/dctool.c +++ b/examples/dctool.c @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef HAVE_GETOPT_H #include #endif @@ -50,6 +51,8 @@ static const dctool_command_t *g_commands[] = { NULL }; +static volatile sig_atomic_t g_cancel = 0; + const dctool_command_t * dctool_command_find (const char *name) { @@ -67,6 +70,23 @@ dctool_command_find (const char *name) return g_commands[i]; } +int +dctool_cancel_cb (void *userdata) +{ + return g_cancel; +} + +static void +sighandler (int signum) +{ +#ifndef _WIN32 + // Restore the default signal handler. + signal (signum, SIG_DFL); +#endif + + g_cancel = 1; +} + static void logfunc (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *msg, void *userdata) { @@ -182,6 +202,9 @@ main (int argc, char *argv[]) return EXIT_FAILURE; } + // Setup the cancel signal handler. + signal (SIGINT, sighandler); + // Initialize the logfile. message_set_logfile (logfile); diff --git a/examples/dctool.h b/examples/dctool.h index b091c83..f8a08ac 100644 --- a/examples/dctool.h +++ b/examples/dctool.h @@ -42,6 +42,9 @@ typedef struct dctool_command_t { const char *usage; } dctool_command_t; +int +dctool_cancel_cb (void *userdata); + #ifdef __cplusplus } #endif /* __cplusplus */