I am writing a program that launches an external subprocess interactively, and I need the contents of the output descriptor to be output to stdout as soon as it is available. I tried something like this:
main = do processInfo <- createProcess (proc "ghci" []){std_out = CreatePipe,
std_in = CreatePipe }
case processInfo of
(Just hIn, Just hOut, _, _) -> do mainloop hIn hOut
hClose hIn
hClose hOut
_ -> do error "Unable to start process"
mainloop :: Handle -> Handle -> IO ()
mainloop inh outh =
do ineof <- hIsEOF outh
if ineof
then return ()
else do inpStr <- hGetLine outh
putStrLn inpStr
mainloop inh outh
But this will not work, since it only recognizes the output line by line, therefore, no output on the process output descriptor that does not end with a new line is displayed. I tried the same with hGetContents, but it gives the same result. I read the documentation of both System.Process and System.IO and actually did not find anything convincing.
source
share