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
|
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
type CoProcessHandle = (ProcessHandle, Handle, Handle, CreateProcess)
|
2012-02-20 19:20:36 +00:00
|
|
|
|
|
|
|
start :: FilePath -> [String] -> IO CoProcessHandle
|
2012-07-18 19:30:26 +00:00
|
|
|
start command params = do
|
|
|
|
(from, to, _err, pid) <- runInteractiveProcess command params Nothing Nothing
|
2012-07-19 04:43:36 +00:00
|
|
|
return (pid, to, from, proc command params)
|
2012-02-20 19:20:36 +00:00
|
|
|
|
|
|
|
stop :: CoProcessHandle -> IO ()
|
2012-07-19 04:43:36 +00:00
|
|
|
stop (pid, from, to, p) = do
|
2012-02-20 19:20:36 +00:00
|
|
|
hClose to
|
|
|
|
hClose from
|
2012-07-19 04:43:36 +00:00
|
|
|
forceSuccessProcess p pid
|
2012-02-20 19:20:36 +00:00
|
|
|
|
|
|
|
query :: CoProcessHandle -> (Handle -> IO a) -> (Handle -> IO b) -> IO b
|
2012-07-19 04:43:36 +00:00
|
|
|
query (_, from, to, _) send receive = do
|
2012-02-20 19:20:36 +00:00
|
|
|
_ <- send to
|
|
|
|
hFlush to
|
|
|
|
receive from
|