git-annex/Utility/CoProcess.hs

36 lines
846 B
Haskell
Raw Normal View History

2012-02-20 19:20:36 +00:00
{- Interface for running a shell command as a coprocess,
- sending it queries and getting back results.
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.CoProcess (
CoProcessHandle,
start,
stop,
query
) where
import Common
type CoProcessHandle = (ProcessHandle, Handle, Handle, CreateProcess)
2012-02-20 19:20:36 +00:00
start :: FilePath -> [String] -> IO CoProcessHandle
start command params = do
(from, to, _err, pid) <- runInteractiveProcess command params Nothing Nothing
return (pid, to, from, proc command params)
2012-02-20 19:20:36 +00:00
stop :: CoProcessHandle -> IO ()
stop (pid, from, to, p) = do
2012-02-20 19:20:36 +00:00
hClose to
hClose from
forceSuccessProcess p pid
2012-02-20 19:20:36 +00:00
query :: CoProcessHandle -> (Handle -> IO a) -> (Handle -> IO b) -> IO b
query (_, from, to, _) send receive = do
2012-02-20 19:20:36 +00:00
_ <- send to
hFlush to
receive from