blob: 6e44b86092ff8da9de5dd10688c9c5a772f71871 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
{-|
Module : System.IO.Terminal.IO.Posix
Copyright : (c) UU, 2019
License : MIT
Maintainer : Tom Smeding
Stability : experimental
Portability : POSIX, macOS
Platform-specific submodule of "System.IO.Terminal.IO" that works on POSIX-like platforms.
-}
module System.IO.Terminal.IO.Posix (
withWinchHandler,
) where
import Control.Exception (bracket)
import Control.Monad.IO.Unlift (MonadUnliftIO(..))
import Foreign.C.Types (CInt)
import System.Exit (die)
import System.Posix.Signals (installHandler, Handler(..))
sigWINCH :: CInt
sigWINCH = 28
-- | Install a SIGWINCH handler, then run the body, then uninstall. Uses
-- 'bracket'.
withWinchHandler :: MonadUnliftIO m => IO () -> m a -> m a
withWinchHandler h act =
withRunInIO $ \run ->
bracket (do prevh <- installHandler sigWINCH (Catch h) Nothing
case prevh of
Default -> return ()
Ignore -> return ()
_ -> die "ERROR: A signal handler was already installed for the WINCH signal!"
return prevh)
(\prevh -> installHandler sigWINCH prevh Nothing)
(\_ -> run act)
|