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
16 import PrelBase ( Int(..), Int#, (==#)
17 , newIntArray#, unsafeFreezeByteArray#, newCharArray#
24 import PackedString ( unpackCStringIO, packCBytesST, psToByteArrayST )
26 import PrelArr (StateAndMutableByteArray#(..), StateAndByteArray#(..))
27 import Util ( unvectorize )
31 First, all of the major Posix data types, to avoid any recursive dependencies
42 type ProcessGroupID = ProcessID
46 instance CReturnable Fd
49 (FD# x#) == (FD# y#) = x# ==# y#
53 intToFd (I# fd#) = FD# fd#
56 fdToInt (FD# x#) = I# x#
59 Now some local functions that shouldn't go outside this library.
61 Fail with a SystemError. Normally, we do not try to re-interpret
62 POSIX error numbers, so most routines in this file will only fail
63 with SystemError. The only exceptions are (1) those routines where
64 failure of some kind may be considered ``normal''...e.g. getpwnam()
65 for a non-existent user, or (2) those routines which do not set
69 syserr :: String -> IO a
70 syserr str = fail (IOError Nothing -- ToDo: better
75 -- Allocate a mutable array of characters with no indices.
77 allocChars :: Int -> IO (MutableByteArray RealWorld ())
78 allocChars (I# size#) = IO $ \ s# ->
79 case newCharArray# size# s# of
80 StateAndMutableByteArray# s2# barr# ->
81 IOok s2# (MutableByteArray bot barr#)
83 bot = error "PosixUtil.allocChars"
85 -- Allocate a mutable array of words with no indices
87 allocWords :: Int -> IO (MutableByteArray RealWorld ())
88 allocWords (I# size#) = IO $ \ s# ->
89 case newIntArray# size# s# of
90 StateAndMutableByteArray# s2# barr# ->
91 IOok s2# (MutableByteArray bot barr#)
93 bot = error "PosixUtil.allocWords"
95 -- Freeze these index-free mutable arrays
97 freeze :: MutableByteArray RealWorld () -> IO (ByteArray ())
98 freeze (MutableByteArray ixs arr#) = IO $ \ s# ->
99 case unsafeFreezeByteArray# arr# s# of
100 StateAndByteArray# s2# frozen# ->
101 IOok s2# (ByteArray ixs frozen#)
103 -- Copy a null-terminated string from outside the heap to
104 -- Haskellized nonsense inside the heap
106 strcpy :: Addr -> IO String
107 strcpy str = unpackCStringIO str
109 -- Turn a string list into a NULL-terminated vector of null-terminated
110 -- strings No indices...I hate indices. Death to Ix.
112 vectorize :: [String] -> IO (ByteArray ())
114 arr <- allocWords (len + 1)
121 fill :: MutableByteArray RealWorld () -> Int -> [String] -> IO ()
123 _casm_ ``((PP_)%0)[%1] = NULL;'' arr n
125 stToIO (psToByteArrayST x) >>= \ barr ->
126 _casm_ ``((PP_)%0)[%1] = (P_)%2;'' arr n barr
130 -- Turn a NULL-terminated vector of null-terminated strings into a string list
131 -- unvectorize ... (now in misc/Util.lhs)
133 -- common templates for system calls
135 nonzero_error :: IO Int -> String -> IO ()
136 nonzero_error io err = do
142 minusone_error :: IO Int -> String -> IO ()
143 minusone_error io err = do
149 -- IO versions of a few ST functions.
151 psToByteArrayIO = stToIO . psToByteArrayST