summaryrefslogtreecommitdiff
path: root/Utility/CoProcess.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Utility/CoProcess.hs')
-rw-r--r--Utility/CoProcess.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/Utility/CoProcess.hs b/Utility/CoProcess.hs
new file mode 100644
index 000000000..9fa8d864f
--- /dev/null
+++ b/Utility/CoProcess.hs
@@ -0,0 +1,35 @@
+{- 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 System.Cmd.Utils
+
+import Common
+
+type CoProcessHandle = (PipeHandle, Handle, Handle)
+
+start :: FilePath -> [String] -> IO CoProcessHandle
+start command params = hPipeBoth command params
+
+stop :: CoProcessHandle -> IO ()
+stop (pid, from, to) = do
+ hClose to
+ hClose from
+ forceSuccess pid
+
+query :: CoProcessHandle -> (Handle -> IO a) -> (Handle -> IO b) -> IO b
+query (_, from, to) send receive = do
+ _ <- send to
+ hFlush to
+ receive from