git-annex/Utility/CoProcess.hs

51 lines
1.1 KiB
Haskell
Raw Normal View History

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.
-}
2013-05-12 03:11:56 +00:00
{-# LANGUAGE CPP #-}
2012-02-20 19:20:36 +00:00
module Utility.CoProcess (
CoProcessHandle,
start,
stop,
2013-05-12 03:11:56 +00:00
query,
rawMode
2012-02-20 19:20:36 +00:00
) where
import Common
type CoProcessHandle = (ProcessHandle, Handle, Handle, CreateProcess)
2012-02-20 19:20:36 +00:00
2012-09-15 21:25:05 +00:00
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)
2012-02-20 19:20:36 +00:00
stop :: CoProcessHandle -> IO ()
stop (pid, from, to, p) = do
2012-02-20 19:20:36 +00:00
hClose to
hClose from
forceSuccessProcess p pid
2012-02-20 19:20:36 +00:00
query :: CoProcessHandle -> (Handle -> IO a) -> (Handle -> IO b) -> IO b
query (_, from, to, _) send receive = do
2012-02-20 19:20:36 +00:00
_ <- send to
hFlush to
receive from
2013-05-12 03:11:56 +00:00
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