summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-02-20 15:20:36 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-02-20 15:22:21 -0400
commit6c0155efb7fd1ff7259095655093e0eab0e37e51 (patch)
treec3765e2dd4a7ba4b040468fc302a56279b7f886f /Utility
parentac5cff3668ad1fc529d32058c31b30dd341c2547 (diff)
refactor
Diffstat (limited to 'Utility')
-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