[Haskell-cafe] Re: `Expect'-like lazy reading/Parsec matching on
igloo at earth.li
Mon Apr 16 15:04:55 EDT 2007
On Mon, Apr 09, 2007 at 10:03:55AM -0600, Scott Bell wrote:
> >Have you got a complete (but preferably small) program showing the
Great example, thanks!
Sorry for the delay in tracking it down.
> main :: IO ()
> main = do (_, h, _, p) <- runInteractiveCommand "telnet nyx.nyx.net"
> t <- hGetContentsTimeout h 15000
> print t >> terminateProcess p
The input handle is being garbage collected and closed, so telnet is
main :: IO ()
main = do (hin, h, _, p) <- runInteractiveCommand "telnet nyx.nyx.net"
t <- hGetContentsTimeout h 15000
Note that you can't do either the hClose or terminateProcess before you
have forced the whole string (which print does here). You might prefer
to pass hin and p to hGetContentsTimeout, and have it close/terminate
them just before the return "".
> hGetContentsTimeout :: Handle -> Int -> IO String
> hGetContentsTimeout h t = do
> hSetBuffering stdin NoBuffering
> ready <- hWaitForInput h t; eof <- hIsEOF h
You'll also need to remove the hIsEOF call from your code, or having
decided that nothing is ready it will then block, waiting to see if
there is an end of file or not.
More information about the Haskell-Cafe