X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Text%2FRegex%2FPosix.hsc;h=50c7c00f0b0011d22af6742a65da5efc745e760f;hb=3457025838609e26918bb0cf5c16393d3bb5b69b;hp=9b4d7062dced2c9a98c30112ff051f0d1c1077b9;hpb=b23d75aa7253aee1bb3bfc685cbae352bf5fbe0d;p=ghc-base.git diff --git a/Text/Regex/Posix.hsc b/Text/Regex/Posix.hsc index 9b4d706..50c7c00 100644 --- a/Text/Regex/Posix.hsc +++ b/Text/Regex/Posix.hsc @@ -6,20 +6,24 @@ -- -- Maintainer : libraries@haskell.org -- Stability : experimental --- Portability : non-portable (needs POSIX regexps) +-- Portability : portable -- -- Interface to the POSIX regular expression library. -- ----------------------------------------------------------------------------- -- ToDo: should have an interface using PackedStrings. -#include "config.h" +#ifndef __NHC__ +#include "HsBaseConfig.h" +#else +#define HAVE_REGEX_H 1 +#define HAVE_REGCOMP 1 +#endif module Text.Regex.Posix ( -- * The @Regex@ type Regex, -- abstract -#if defined(HAVE_REGEX_H) -- * Compiling a regular expression regcomp, -- :: String -> Int -> IO Regex @@ -36,14 +40,8 @@ module Text.Regex.Posix ( -- String, -- everything after match -- [String])) -- subexpression matches -#endif ) where -#if defined(HAVE_REGEX_H) -#include -#include "regex.h" -#endif - import Prelude import Foreign @@ -54,8 +52,23 @@ type CRegex = () -- | A compiled regular expression newtype Regex = Regex (ForeignPtr CRegex) -#if defined(HAVE_REGEX_H) --- to the end + +-- The C-library backend +#include + +#if HAVE_REGEX_H && HAVE_REGCOMP +#include "regex.h" +#else +#include "regex/regex.h" + +-- CFILES stuff is Hugs only +{-# CFILES cbits/regex/reallocf.c #-} +{-# CFILES cbits/regex/regcomp.c #-} +{-# CFILES cbits/regex/regerror.c #-} +{-# CFILES cbits/regex/regexec.c #-} +{-# CFILES cbits/regex/regfree.c #-} +#endif + -- ----------------------------------------------------------------------------- -- regcomp @@ -102,13 +115,13 @@ regexec (Regex regex_fptr) str = do if (r /= 0) then return Nothing else do - (before,match,after) <- matched_parts str p_match + (before,match,after) <- matched_parts str p_match - sub_strs <- - mapM (unpack str) $ take nsub_int $ tail $ - iterate (`plusPtr` (#const sizeof(regmatch_t))) p_match + sub_strs <- + mapM (unpack str) $ take nsub_int $ tail $ + iterate (`plusPtr` (#const sizeof(regmatch_t))) p_match - return (Just (before, match, after, sub_strs)) + return (Just (before, match, after, sub_strs)) matched_parts :: String -> Ptr CRegMatch -> IO (String, String, String) matched_parts string p_match = do @@ -126,7 +139,7 @@ unpack string p_match = do -- the subexpression may not have matched at all, perhaps because it -- was optional. In this case, the offsets are set to -1. if (start == -1) then return "" else do - return (take (fromIntegral (end-start)) (drop (fromIntegral start) string)) + return (take (fromIntegral (end-start)) (drop (fromIntegral start) string)) -- ----------------------------------------------------------------------------- -- The POSIX regex C interface @@ -165,6 +178,14 @@ unpack string p_match = do type CRegMatch = () +-- 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 @@ -174,5 +195,24 @@ foreign import ccall unsafe "®free" foreign import ccall unsafe "regexec" c_regexec :: Ptr CRegex -> CString -> CSize -> Ptr CRegMatch -> CInt -> IO CInt +#elif HAVE_REGEX_H && HAVE_REGCOMP +foreign import ccall unsafe "regex.h regcomp" + c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt + +foreign import ccall unsafe "regex.h ®free" + ptr_regfree :: FunPtr (Ptr CRegex -> IO ()) + +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 ®free" + ptr_regfree :: FunPtr (Ptr CRegex -> IO ()) -#endif /* HAVE_REGEX_H */ +foreign import ccall unsafe "regex/regex.h regexec" + c_regexec :: Ptr CRegex -> CString -> CSize + -> Ptr CRegMatch -> CInt -> IO CInt +#endif