summaryrefslogtreecommitdiff
path: root/Utility/CoProcess.hs
blob: 9fa8d864feae73b056d8051e79ec756d2fb9811c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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