module Language.Haskell.HBB.ApplyTo (
applyTo
) where
import System.Process
import System.IO
import Data.List (intercalate)
applyTo :: Bool -> String -> String -> IO (String,Maybe String)
applyTo suppressGHCWarning stringToStringFun subject = do
let exeName = "ghc"
exeArgQ = "'(" ++ stringToStringFun ++ ") \"" ++ subject ++ "\"\'"
exeArgQi = "'interact (\\stdin -> (unlines . map (" ++ stringToStringFun ++ ") . lines) stdin)'"
spec = let base = proc exeName ("-e":["(" ++ stringToStringFun ++ ") " ++ '"':subject ++ "\""])
in base { std_in = Inherit
, std_out = CreatePipe
, std_err = UseHandle stderr }
warnMsg = unlines ["> Note that 'apply-to' is a small wrapper around a call to 'ghc'."
,"> "
,"> The functionality provided here can also be reached by calling 'ghc' as follows:"
,"> "
,(intercalate " " (("> # " ++ exeName):"-e":exeArgQ:[]))
,"> "
,"> Moreover with GHC all request could be done in a simple session."
,"> In this case the subject strings are written to stdin and the results"
,"> are read from stdout (one per line):"
,"> "
,(intercalate " " (("> # " ++ exeName):"-e":exeArgQi:[]))]
(_,Just childStdOut,_,_) <- createProcess spec
res <- hGetContents childStdOut
case (res,suppressGHCWarning) of ([],_ ) -> return ([],Just "Empty result (is the 'ghc' executable accessible?)")
(xs,False) -> return (xs,Just warnMsg)
(xs,True ) -> return (xs,Nothing )