[project @ 2005-03-03 05:11:41 by chak]
[ghc-base.git] / Text / Regex / Posix.hsc
index ee22088..50c7c00 100644 (file)
@@ -14,7 +14,7 @@
 
 -- ToDo: should have an interface using PackedStrings.
 #ifndef __NHC__
-#include "ghcconfig.h"
+#include "HsBaseConfig.h"
 #else
 #define HAVE_REGEX_H 1
 #define HAVE_REGCOMP 1
@@ -42,6 +42,18 @@ module Text.Regex.Posix (
 
   ) where
 
+import Prelude
+
+import Foreign
+import Foreign.C
+
+type CRegex    = ()
+
+-- | A compiled regular expression
+newtype Regex = Regex (ForeignPtr CRegex)
+
+
+-- The C-library backend
 #include <sys/types.h>
 
 #if HAVE_REGEX_H && HAVE_REGCOMP
@@ -57,16 +69,6 @@ module Text.Regex.Posix (
 {-# CFILES cbits/regex/regfree.c #-}
 #endif
 
-import Prelude
-
-import Foreign
-import Foreign.C
-
-type CRegex    = ()
-
--- | A compiled regular expression
-newtype Regex = Regex (ForeignPtr CRegex)
-
 -- -----------------------------------------------------------------------------
 -- regcomp
 
@@ -176,7 +178,14 @@ unpack string p_match = do
 
 type CRegMatch = ()
 
-#ifdef __GLASGOW_HASKELL__
+-- GHC and Hugs get the appropriate include file from the OPTIONS
+-- pragma generated by hsc2hs from the above #include.
+-- Implementations following the FFI spec have to specify it in the
+-- foreign import, which is awkward because some systems provide
+-- regex.h and the rest of the regex library, but otherwise we
+-- need to use our own copy, regex/regex.h.
+
+#if __GLASGOW_HASKELL__ || __HUGS__
 foreign import ccall unsafe "regcomp"
   c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
 
@@ -186,16 +195,7 @@ foreign import ccall  unsafe "&regfree"
 foreign import ccall unsafe "regexec"
   c_regexec :: Ptr CRegex -> CString -> CSize
            -> Ptr CRegMatch -> CInt -> IO CInt
-#else
--- For NHC and (we think) Hugs, we have to hackily put
--- the regex.h include in the name of the C function to
--- import.  (GHC does this by interpreting the
--- "-#include regex.h" OPTIONS pragma that hsc2hs generates.
--- The trouble with the hacky solution is that sometimes
--- we want regex.h and sometimes regex/regex.h.  I'm not
--- sure if the hack will work for NHC and Hugs on all 
--- platforms
-
+#elif HAVE_REGEX_H && HAVE_REGCOMP
 foreign import ccall unsafe "regex.h regcomp"
   c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
 
@@ -205,4 +205,14 @@ foreign import ccall  unsafe "regex.h &regfree"
 foreign import ccall unsafe "regex.h regexec"
   c_regexec :: Ptr CRegex -> CString -> CSize
            -> Ptr CRegMatch -> CInt -> IO CInt
+#else
+foreign import ccall unsafe "regex/regex.h regcomp"
+  c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
+
+foreign import ccall  unsafe "regex/regex.h &regfree"
+  ptr_regfree :: FunPtr (Ptr CRegex -> IO ())
+
+foreign import ccall unsafe "regex/regex.h regexec"
+  c_regexec :: Ptr CRegex -> CString -> CSize
+           -> Ptr CRegMatch -> CInt -> IO CInt
 #endif