2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \section[LibPosixUtil]{Haskell 1.3 POSIX utilities}
22 First, all of the major Posix data types, to avoid any recursive dependencies
35 type ProcessGroupID = ProcessID
40 Now some local fucntions that shouldn't go outside this library.
44 -- Fail with a SystemError. Normally, we do not try to re-interpret POSIX
45 -- error numbers, so most routines in this file will only fail with SystemError.
46 -- The only exceptions are (1) those routines where failure of some kind may be
47 -- considered ``normal''...e.g. getpwnam() for a non-existent user, or (2) those
48 -- routines which do not set errno.
50 syserr :: String -> IO a
51 syserr = failWith . SystemError
53 -- Allocate a mutable array of characters with no indices.
55 allocChars :: Int -> _ST s (_MutableByteArray s ())
56 allocChars (I# size#) (S# s#) =
57 case newCharArray# size# s# of
58 StateAndMutableByteArray# s2# barr# -> (_MutableByteArray bot barr#, S# s2#)
60 bot = error "allocChars{LibPosix}"
62 -- Allocate a mutable array of words with no indices
64 allocWords :: Int -> _ST s (_MutableByteArray s ())
65 allocWords (I# size#) (S# s#) =
66 case newIntArray# size# s# of
67 StateAndMutableByteArray# s2# barr# -> (_MutableByteArray bot barr#, S# s2#)
69 bot = error "allocWords{LibPosix}"
71 -- Freeze these index-free mutable arrays
73 freeze :: _MutableByteArray s () -> _ST s (_ByteArray ())
74 freeze (_MutableByteArray ixs arr#) (S# s#) =
75 case unsafeFreezeByteArray# arr# s# of
76 StateAndByteArray# s2# frozen# -> (_ByteArray ixs frozen#, S# s2#)
78 -- Copy a null-terminated string from outside the heap to
79 -- Haskellized nonsense inside the heap
81 strcpy :: _Addr -> PrimIO String
83 | str == ``NULL'' = returnPrimIO ""
85 _ccall_ strlen str `thenPrimIO` \ len ->
86 _packCBytesST len str `thenStrictlyST` \ ps ->
87 returnPrimIO (_unpackPS ps)
89 -- Turn a string list into a NULL-terminated vector of null-terminated strings
90 -- No indices...I hate indices. Death to Ix.
92 vectorize :: [String] -> PrimIO (_ByteArray ())
94 allocWords (len+1) `thenStrictlyST` \ arr ->
95 fill arr 0 xs `thenPrimIO` \ () ->
96 freeze arr `thenStrictlyST` \ frozen ->
103 fill :: _MutableByteArray _RealWorld () -> Int -> [String] -> PrimIO ()
105 _casm_ ``((PP_)%0)[%1] = NULL;'' arr n
107 _packBytesForCST x `thenStrictlyST` \ barr ->
108 _casm_ ``((PP_)%0)[%1] = (P_)%2;'' arr n barr
112 -- Turn a NULL-terminated vector of null-terminated strings into a string list
114 unvectorize :: _Addr -> Int -> PrimIO [String]
116 | str == ``NULL'' = returnPrimIO []
118 strcpy str `thenPrimIO` \ x ->
119 unvectorize ptr (n+1) `thenPrimIO` \ xs ->
120 returnPrimIO (x : xs)
121 where str = indexAddrOffAddr ptr n