win32: catch Ctrl+C on console.

This commit is contained in:
Daniel Beer 2011-07-27 03:46:29 +12:00
parent 8c4080b97f
commit df46811d2d
2 changed files with 35 additions and 1 deletions

34
util.c
View File

@ -35,6 +35,37 @@
static volatile int ctrlc_flag;
#ifdef WIN32
static HANDLE ctrlc_event;
static WINAPI BOOL ctrlc_handler(DWORD event)
{
if (event == CTRL_C_EVENT) {
ctrlc_flag = 1;
SetEvent(ctrlc_event);
return TRUE;
}
return FALSE;
}
void ctrlc_init(void)
{
ctrlc_event = CreateEvent(0, TRUE, FALSE, NULL);
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
}
void ctrlc_reset(void)
{
ctrlc_flag = 0;
ResetEvent(ctrlc_event);
}
HANDLE ctrlc_win32_event(void)
{
return ctrlc_event;
}
#else /* WIN32 */
static void sigint_handler(int signum)
{
ctrlc_flag = 1;
@ -42,7 +73,7 @@ static void sigint_handler(int signum)
void ctrlc_init(void)
{
#if defined(WIN32) || defined(__CYGWIN__)
#if defined(__CYGWIN__)
signal(SIGINT, sigint_handler);
#else
const static struct sigaction siga = {
@ -58,6 +89,7 @@ void ctrlc_reset(void)
{
ctrlc_flag = 0;
}
#endif
int ctrlc_check(void)
{

2
util.h
View File

@ -57,6 +57,8 @@ int hexval(int c);
#ifdef WIN32
char *strsep(char **strp, const char *delim);
HANDLE ctrlc_win32_event(void);
#endif
#endif