Std.hs 778 B

123456789101112131415161718192021222324252627282930
  1. -- | UniformIO over stdin and stdout
  2. module System.IO.Uniform.Std (
  3. StdIO(StdIO)
  4. ) where
  5. import System.IO.Uniform
  6. import System.IO.Uniform.External
  7. import Foreign
  8. import Foreign.C.Error
  9. import qualified Data.ByteString as BS
  10. import Control.Monad
  11. -- | UniformIO that reads from stdin and writes to stdout.
  12. instance UniformIO StdIO where
  13. uRead _ n = allocaArray n (
  14. \b -> do
  15. count <- c_recvStd b (fromIntegral n)
  16. if count < 0
  17. then throwErrno "could not read"
  18. else BS.packCStringLen (b, fromIntegral count)
  19. )
  20. uPut _ t = BS.useAsCStringLen t (
  21. \(str, n) -> do
  22. count <- c_sendStd str $ fromIntegral n
  23. when (count < 0) $ throwErrno "could not write"
  24. )
  25. uClose _ = return ()
  26. startTls _ = return
  27. isSecure _ = True