1 {-# OPTIONS_GHC -fffi #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
4 module CtrlC
(withCtrlCHandler
) where
6 import Data
.Word
( Word32
)
7 import Foreign
.Ptr
( FunPtr
)
8 import Control
.Exception
( bracket )
10 type Handler
= Word32
-> IO Int
12 foreign import ccall
"wrapper" wrap
:: Handler
-> IO (FunPtr Handler
)
13 foreign import stdcall
"SetConsoleCtrlHandler" setConsoleCtrlHandler
:: FunPtr Handler
-> Int -> IO ()
16 withCtrlCHandler
:: IO () -> IO a
-> IO a
17 withCtrlCHandler handler m
= do
18 fp
<- wrap
(\_ctrlType
-> handler
>> return 1)
19 bracket (setConsoleCtrlHandler fp
1) (const $ setConsoleCtrlHandler fp
0) (const m
)