Skip to content

Instantly share code, notes, and snippets.

@expipiplus1
Created February 20, 2017 18:28
Show Gist options
  • Save expipiplus1/eabcd32d05255d7878f5c0c47c2f778d to your computer and use it in GitHub Desktop.
Save expipiplus1/eabcd32d05255d7878f5c0c47c2f778d to your computer and use it in GitHub Desktop.
module Main
( main
) where
import GHC.IO.Exception
import System.IO
import System.IO.Error
import System.Process.Typed
main :: IO ()
main =
let pc = setStdin createPipe
$ proc "ghc" ["-e", "System.IO.hClose System.IO.stdin"]
in withProcess pc $ \p -> do
let inn = getStdin p
-- wait for the pipe to be closed
_ <- waitExitCode p
-- Use the pipe
hPutStrLn inn "hello"
-- Uncomment this to make things work
-- handleResourceVanished (hClose inn)
pure ()
handleResourceVanished :: IO a -> IO (Maybe a)
handleResourceVanished a =
(Just <$> a) `catchIOError`
(\e ->
if ioeGetErrorType e == ResourceVanished
then pure Nothing
else ioError e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment