X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Foreign%2FC%2FError.hs;h=33a7bfd263184041d8ecb1ca6eb75a7b8cd2e710;hb=d539a9457e2c79a9f13744d073d3f253ea2fb33e;hp=3bba4ed55a7ed17dddcebdabea939ae4f53c8a75;hpb=7f1f4e7a695c402ddd3a1dc2cc7114e649a78ebc;p=ghc-base.git diff --git a/Foreign/C/Error.hs b/Foreign/C/Error.hs index 3bba4ed..33a7bfd 100644 --- a/Foreign/C/Error.hs +++ b/Foreign/C/Error.hs @@ -1,17 +1,15 @@ -{-# OPTIONS -fno-implicit-prelude -#include "HsCore.h" #-} +{-# OPTIONS -fno-implicit-prelude -#include "HsBase.h" #-} ----------------------------------------------------------------------------- --- +-- | -- Module : Foreign.C.Error -- Copyright : (c) The FFI task force 2001 --- License : BSD-style (see the file libraries/core/LICENSE) +-- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : ffi@haskell.org --- Stability : experimental --- Portability : non-portable --- --- $Id: Error.hs,v 1.1 2001/06/28 14:15:03 simonmar Exp $ +-- Stability : provisional +-- Portability : portable -- --- C-specific Marshalling support: Handling of C "errno" error codes +-- C-specific Marshalling support: Handling of C \"errno\" error codes -- ----------------------------------------------------------------------------- @@ -84,7 +82,9 @@ module Foreign.C.Error ( -- this is were we get the CCONST_XXX definitions from that configure -- calculated for us -- +#ifndef __NHC__ #include "config.h" +#endif -- system dependent imports -- ------------------------ @@ -92,13 +92,14 @@ module Foreign.C.Error ( -- GHC allows us to get at the guts inside IO errors/exceptions -- #if __GLASGOW_HASKELL__ -import GHC.IOBase (Exception(..), IOException(..), IOErrorType(..)) +import GHC.IOBase (IOException(..), IOErrorType(..)) #endif /* __GLASGOW_HASKELL__ */ -- regular imports -- --------------- +import Foreign.Storable import Foreign.Ptr import Foreign.C.Types import Foreign.C.String @@ -106,20 +107,31 @@ import Foreign.Marshal.Error ( void ) import Data.Maybe #if __GLASGOW_HASKELL__ -import GHC.Storable import GHC.IOBase import GHC.Num import GHC.Base #else import System.IO ( IOError, Handle, ioError ) +import System.IO.Unsafe ( unsafePerformIO ) #endif +#ifdef __HUGS__ +{-# CBITS errno.c #-} +#endif + + -- "errno" type -- ------------ -- import of C function that gives address of errno --- -foreign import "ghcErrno" unsafe _errno :: Ptr CInt +-- This function exists because errno is a variable on some systems, but on +-- Windows it is a macro for a function... +-- [yes, global variables and thread safety don't really go hand-in-hand. -- sof] +#ifdef __NHC__ +foreign import ccall unsafe "errno.h &errno" _errno :: Ptr CInt +#else +foreign import ccall unsafe "HsBase.h ghcErrno" _errno :: Ptr CInt +#endif -- Haskell representation for "errno" values -- @@ -147,10 +159,13 @@ eOK, e2BIG, eACCES, eADDRINUSE, eADDRNOTAVAIL, eADV, eAFNOSUPPORT, eAGAIN, eSOCKTNOSUPPORT, eSPIPE, eSRCH, eSRMNT, eSTALE, eTIME, eTIMEDOUT, eTOOMANYREFS, eTXTBSY, eUSERS, eWOULDBLOCK, eXDEV :: Errno -- --- the CCONST_XXX identifiers are cpp symbols whose value is computed by +-- the cCONST_XXX identifiers are cpp symbols whose value is computed by -- configure -- eOK = Errno 0 +#ifdef __NHC__ +#include "Errno.hs" +#else e2BIG = Errno (CCONST_E2BIG) eACCES = Errno (CCONST_EACCES) eADDRINUSE = Errno (CCONST_EADDRINUSE) @@ -249,6 +264,7 @@ eTXTBSY = Errno (CCONST_ETXTBSY) eUSERS = Errno (CCONST_EUSERS) eWOULDBLOCK = Errno (CCONST_EWOULDBLOCK) eXDEV = Errno (CCONST_EXDEV) +#endif -- checks whether the given errno value is supported on the current -- architecture @@ -404,7 +420,7 @@ errnoToIOError :: String -> Errno -> Maybe Handle -> Maybe String -> IOError errnoToIOError loc errno maybeHdl maybeName = unsafePerformIO $ do str <- strerror errno >>= peekCString #if __GLASGOW_HASKELL__ - return (IOException (IOError maybeHdl errType loc str maybeName)) + return (IOError maybeHdl errType loc str maybeName) where errType | errno == eOK = OtherError @@ -456,7 +472,7 @@ errnoToIOError loc errno maybeHdl maybeName = unsafePerformIO $ do | errno == eNFILE = ResourceExhausted | errno == eNOBUFS = ResourceExhausted | errno == eNODATA = NoSuchThing - | errno == eNODEV = NoSuchThing + | errno == eNODEV = UnsupportedOperation | errno == eNOENT = NoSuchThing | errno == eNOEXEC = InvalidArgument | errno == eNOLCK = ResourceExhausted @@ -511,4 +527,4 @@ errnoToIOError loc errno maybeHdl maybeName = unsafePerformIO $ do return (userError (loc ++ ": " ++ str ++ maybe "" (": "++) maybeName)) #endif -foreign import unsafe strerror :: Errno -> IO (Ptr CChar) +foreign import ccall unsafe "string.h" strerror :: Errno -> IO (Ptr CChar)