2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
4 \section[PosixUtil]{Haskell 1.3 POSIX utilities}
10 import PrelST -- ST representation
11 import PrelIOBase -- IOError representation
18 import PackedString ( packCBytesST, psToByteArrayST, unpackPS )
20 import PrelArr (StateAndMutableByteArray#(..), StateAndByteArray#(..))
23 First, all of the major Posix data types, to avoid any recursive dependencies
34 type ProcessGroupID = ProcessID
38 instance CReturnable Fd
41 (FD# x#) == (FD# y#) = x# ==# y#
45 intToFd (I# fd#) = FD# fd#
48 fdToInt (FD# x#) = I# x#
51 Now some local functions that shouldn't go outside this library.
53 Fail with a SystemError. Normally, we do not try to re-interpret
54 POSIX error numbers, so most routines in this file will only fail
55 with SystemError. The only exceptions are (1) those routines where
56 failure of some kind may be considered ``normal''...e.g. getpwnam()
57 for a non-existent user, or (2) those routines which do not set
61 syserr :: String -> IO a
62 syserr str = fail (IOError Nothing -- ToDo: better
66 -- Allocate a mutable array of characters with no indices.
68 allocChars :: Int -> IO (MutableByteArray RealWorld ())
69 allocChars (I# size#) = IO $ \ s# ->
70 case newCharArray# size# s# of
71 StateAndMutableByteArray# s2# barr# ->
72 IOok s2# (MutableByteArray bot barr#)
74 bot = error "PosixUtil.allocChars"
76 -- Allocate a mutable array of words with no indices
78 allocWords :: Int -> IO (MutableByteArray RealWorld ())
79 allocWords (I# size#) = IO $ \ s# ->
80 case newIntArray# size# s# of
81 StateAndMutableByteArray# s2# barr# ->
82 IOok s2# (MutableByteArray bot barr#)
84 bot = error "PosixUtil.allocWords"
86 -- Freeze these index-free mutable arrays
88 freeze :: MutableByteArray RealWorld () -> IO (ByteArray ())
89 freeze (MutableByteArray ixs arr#) = IO $ \ s# ->
90 case unsafeFreezeByteArray# arr# s# of
91 StateAndByteArray# s2# frozen# ->
92 IOok s2# (ByteArray ixs frozen#)
94 -- Copy a null-terminated string from outside the heap to
95 -- Haskellized nonsense inside the heap
97 strcpy :: Addr -> IO String
99 | str == ``NULL'' = return ""
101 _ccall_ strlen str >>= \ len ->
102 stToIO (packCBytesST len str) >>= \ ps ->
105 -- Turn a string list into a NULL-terminated vector of null-terminated
106 -- strings No indices...I hate indices. Death to Ix.
108 vectorize :: [String] -> IO (ByteArray ())
110 arr <- allocWords (len + 1)
117 fill :: MutableByteArray RealWorld () -> Int -> [String] -> IO ()
119 _casm_ ``((PP_)%0)[%1] = NULL;'' arr n
121 stToIO (psToByteArrayST x) >>= \ barr ->
122 _casm_ ``((PP_)%0)[%1] = (P_)%2;'' arr n barr
126 -- Turn a NULL-terminated vector of null-terminated strings into a string list
128 unvectorize :: Addr -> Int -> IO [String]
130 | str == ``NULL'' = return []
132 strcpy str >>= \ x ->
133 unvectorize ptr (n+1) >>= \ xs ->
136 str = indexAddrOffAddr ptr n
138 -- common templates for system calls
140 nonzero_error :: IO Int -> String -> IO ()
141 nonzero_error io err = do
147 minusone_error :: IO Int -> String -> IO ()
148 minusone_error io err = do
154 -- IO versions of a few ST functions.
156 psToByteArrayIO = stToIO . psToByteArrayST