[Haskell-cafe] catting to cat gets stuck at > 135K

Jason Dusek jason.dusek at gmail.com
Mon Nov 10 20:49:21 EST 2008


  This does not work either. It should cover all the bases,
  right? Fork off input, pull things from ouput as they are
  ready, stop when we reach end of file. If you remove the line
  `print partial`, the program loops forever; if you keep it,
  the program stops right there.

--
_jsn




import Data.ByteString.Lazy hiding (putStrLn)
import System.Process
import System.Environment
import System.IO (putStrLn, hClose, hWaitForInput)
import System.IO.Error
import Control.Concurrent
import Prelude hiding (writeFile, readFile)


main                         =  do
  exe:file:_                <-  getArgs
  bytes                     <-  readFile file
  foo                       <-  simple exe bytes []
  writeFile (file ++ ".foo") foo


 -- Manufactures a simple stream handler from a command line utility.
simple
 :: String -> ByteString -> [String] -> IO ByteString
simple exe bytes args        =  do
  (i, o, e, p)              <-  runInteractiveProcess exe args Nothing Nothing
  pushAndPull i o bytes


pushAndPull i o bytes        =  do
  putStrLn "Working with:"
  print bytes
  forkIO $ hPut i bytes
  putStrLn "forked"
  readUntilDone empty
 where
  readUntilDone soFar        =  do
    (const $ return soFar) `hctac` do
      putStrLn "hctac"
      hWaitForInput o 0     --  Wait as long as it takes.
      putStrLn "waited"
      partial               <-  hGetContents o
      putStrLn "contents:"
      print partial
      readUntilDone $ append soFar partial
   where
    hctac                    =  flip catch


More information about the Haskell-Cafe mailing list