[project @ 2002-08-30 14:54:58 by simonpj]
[ghc-base.git] / GHC / Posix.hs
index f46b6cf..d492e29 100644 (file)
@@ -1,12 +1,22 @@
 {-# OPTIONS -fno-implicit-prelude #-}
 
--- ---------------------------------------------------------------------------
--- $Id: Posix.hs,v 1.6 2002/03/26 10:53:03 simonmar Exp $
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  GHC.Posix
+-- Copyright   :  (c) The University of Glasgow, 1992-2002
+-- License     :  see libraries/base/LICENSE
+-- 
+-- Maintainer  :  cvs-ghc@haskell.org
+-- Stability   :  internal
+-- Portability :  non-portable
 --
--- POSIX support layer for the standard libraries
+-- POSIX support layer for the standard libraries.
+-- This library is built on *every* platform, including Win32.
 --
 -- Non-posix compliant in order to support the following features:
 --     * S_ISSOCK (no sockets in POSIX)
+--
+-----------------------------------------------------------------------------
 
 module GHC.Posix where
 
@@ -126,10 +136,28 @@ closeFd isStream fd
   | isStream  = c_closesocket fd
   | otherwise = c_close fd
 
-foreign import ccall unsafe "closesocket"
+foreign import stdcall unsafe "closesocket"
    c_closesocket :: CInt -> IO CInt
 #endif
 
+fdGetMode :: Int -> IO IOMode
+fdGetMode fd = do
+    flags <- throwErrnoIfMinus1Retry "fdGetMode" 
+               (c_fcntl_read (fromIntegral fd) const_f_getfl)
+    
+    let
+       wH  = (flags .&. o_WRONLY) /= 0
+       aH  = (flags .&. o_APPEND) /= 0
+       rwH = (flags .&. o_RDWR) /= 0
+
+       mode
+        | wH && aH  = AppendMode
+        | wH        = WriteMode
+        | rwH       = ReadWriteMode
+        | otherwise = ReadMode
+         
+    return mode
+
 -- ---------------------------------------------------------------------------
 -- Terminal-related stuff
 
@@ -201,13 +229,37 @@ tcSetAttr fd options p_tios = do
 
 -- bogus defns for win32
 setCooked :: Int -> Bool -> IO ()
-setCooked fd cooked = return ()
+setCooked fd cooked = do
+  x <- set_console_buffering (fromIntegral fd) (if cooked then 1 else 0)
+  if (x /= 0)
+   then ioException (ioe_unk_error "setCooked" "failed to set buffering")
+   else return ()
+
+ioe_unk_error loc msg 
+ = IOError Nothing OtherError loc msg Nothing
 
 setEcho :: Int -> Bool -> IO ()
-setEcho fd on = return ()
+setEcho fd on = do
+  x <- set_console_echo (fromIntegral fd) (if on then 1 else 0)
+  if (x /= 0)
+   then ioException (ioe_unk_error "setEcho" "failed to set echoing")
+   else return ()
 
 getEcho :: Int -> IO Bool
-getEcho fd = return False
+getEcho fd = do
+  r <- get_console_echo (fromIntegral fd)
+  if (r == (-1))
+   then ioException (ioe_unk_error "getEcho" "failed to get echoing")
+   else return (r == 1)
+
+foreign import ccall unsafe "consUtils.h set_console_buffering__"
+   set_console_buffering :: CInt -> CInt -> IO CInt
+
+foreign import ccall unsafe "consUtils.h set_console_echo__"
+   set_console_echo :: CInt -> CInt -> IO CInt
+
+foreign import ccall unsafe "consUtils.h get_console_echo__"
+   get_console_echo :: CInt -> IO CInt
 
 #endif