\begin{code}
module PosixUtil where
-import ST
-import PrelST -- ST representation
+import GlaExts
import PrelIOBase -- IOError representation
-import Addr
-import Foreign
-import CCall
-import PrelBase
-import MutableArray
-import ByteArray
-import Array
-import PackedString ( packCBytesST, psToByteArrayST, unpackPS )
-import Ix
-import PrelArr (StateAndMutableByteArray#(..), StateAndByteArray#(..))
\end{code}
First, all of the major Posix data types, to avoid any recursive dependencies
\begin{code}
syserr :: String -> IO a
-syserr str = fail (IOError Nothing -- ToDo: better
- SystemError
- str)
-
--- Allocate a mutable array of characters with no indices.
-
-allocChars :: Int -> IO (MutableByteArray RealWorld ())
-allocChars (I# size#) = IO $ \ s# ->
- case newCharArray# size# s# of
- StateAndMutableByteArray# s2# barr# ->
- IOok s2# (MutableByteArray bot barr#)
- where
- bot = error "PosixUtil.allocChars"
-
--- Allocate a mutable array of words with no indices
-
-allocWords :: Int -> IO (MutableByteArray RealWorld ())
-allocWords (I# size#) = IO $ \ s# ->
- case newIntArray# size# s# of
- StateAndMutableByteArray# s2# barr# ->
- IOok s2# (MutableByteArray bot barr#)
- where
- bot = error "PosixUtil.allocWords"
-
--- Freeze these index-free mutable arrays
-
-freeze :: MutableByteArray RealWorld () -> IO (ByteArray ())
-freeze (MutableByteArray ixs arr#) = IO $ \ s# ->
- case unsafeFreezeByteArray# arr# s# of
- StateAndByteArray# s2# frozen# ->
- IOok s2# (ByteArray ixs frozen#)
-
--- Copy a null-terminated string from outside the heap to
--- Haskellized nonsense inside the heap
-
-strcpy :: Addr -> IO String
-strcpy str
- | str == ``NULL'' = return ""
- | otherwise =
- _ccall_ strlen str >>= \ len ->
- stToIO (packCBytesST len str) >>= \ ps ->
- return (unpackPS ps)
-
--- Turn a string list into a NULL-terminated vector of null-terminated
--- strings No indices...I hate indices. Death to Ix.
-
-vectorize :: [String] -> IO (ByteArray ())
-vectorize xs = do
- arr <- allocWords (len + 1)
- fill arr 0 xs
- freeze arr
- where
- len :: Int
- len = length xs
-
- fill :: MutableByteArray RealWorld () -> Int -> [String] -> IO ()
- fill arr n [] =
- _casm_ ``((PP_)%0)[%1] = NULL;'' arr n
- fill arr n (x:xs) =
- stToIO (psToByteArrayST x) >>= \ barr ->
- _casm_ ``((PP_)%0)[%1] = (P_)%2;'' arr n barr
- >>= \ () ->
- fill arr (n+1) xs
-
--- Turn a NULL-terminated vector of null-terminated strings into a string list
-
-unvectorize :: Addr -> Int -> IO [String]
-unvectorize ptr n
- | str == ``NULL'' = return []
- | otherwise =
- strcpy str >>= \ x ->
- unvectorize ptr (n+1) >>= \ xs ->
- return (x : xs)
- where
- str = indexAddrOffAddr ptr n
+syserr str = ioError (IOError Nothing -- ToDo: better
+ SystemError
+ str
+ "")
-- common templates for system calls
then return ()
else syserr err
--- IO versions of a few ST functions.
-
-psToByteArrayIO = stToIO . psToByteArrayST
-
\end{code}