aboutsummaryrefslogtreecommitdiff
path: root/Utility/CoProcess.hs
blob: f72850fc5236d3b33341b713084b8d434cc6a7bb (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{- 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.
 -}

{-# LANGUAGE CPP #-}

module Utility.CoProcess (
	CoProcessHandle,
	start,
	stop,
	query,
	rawMode
) where

import Common

type CoProcessHandle = (ProcessHandle, Handle, Handle, CreateProcess)

start :: FilePath -> [String] -> Maybe [(String, String)] -> IO CoProcessHandle
start command params env = do
	(from, to, _err, pid) <- runInteractiveProcess command params Nothing env
	return (pid, to, from, proc command params)

stop :: CoProcessHandle -> IO ()
stop (pid, from, to, p) = do
	hClose to
	hClose from
	forceSuccessProcess p pid

query :: CoProcessHandle -> (Handle -> IO a) -> (Handle -> IO b) -> IO b
query (_, from, to, _) send receive = do
	_ <- send to
	hFlush to
	receive from

rawMode :: CoProcessHandle -> IO CoProcessHandle
rawMode ch@(_, from, to, _) = do
	raw from
	raw to
	return ch
  where
  	raw h = do
		fileEncoding h
#ifdef __WINDOWS__
		hSetNewlineMode h noNewlineTranslation
#endif