Std.hs 900 B

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