0 | module System.Posix.Errno
2 | import Data.C.Integer
6 | import Data.SortedMap
7 | import public System.Posix.Errno.Type
8 | import public Data.Linear.ELift1
16 | Interpolation Errno where
17 | interpolate x = "\{errorText x} (\{errorName x})"
20 | 0 EPrim : Type -> Type
21 | EPrim a = {0 es : List Type} -> Has Errno es => E1 World es a
28 | public export %inline
29 | toBool : Bits8 -> Bool
35 | fromNeg : Neg n => Cast n Bits32 => n -> Errno
36 | fromNeg = EN . cast . negate
39 | primMap : (a -> b) -> PrimIO a -> PrimIO b
41 | let MkIORes v w := act w
45 | elinMap : (a -> b) -> E1 s es a -> E1 s es b
47 | let R v w := act w | E x w => E x w
51 | toVal : (CInt -> a) -> PrimIO CInt -> EPrim a
53 | let r # t := toF1 act t
54 | in if r < 0 then E (inject $
fromNeg r) t else R (f r) t
57 | toSize : PrimIO SsizeT -> EPrim Bits32
59 | let r # t := toF1 act t
60 | in if r < 0 then E (inject $
fromNeg r) t else R (cast r) t
63 | toUnit : PrimIO CInt -> EPrim ()
65 | let r # t := toF1 act t
66 | in if r < 0 then E (inject $
fromNeg r) t else R () t
69 | toPidT : PrimIO PidT -> EPrim PidT
71 | let r # t := toF1 act t
72 | in if r < 0 then E (inject $
fromNeg r) t else R r t
75 | posToUnit : PrimIO Bits32 -> EPrim ()
79 | x # t => E (inject $
EN x) t
82 | posNotErr : Errno -> PrimIO Bits32 -> EPrim Bool
83 | posNotErr err act t =
86 | x # t => case EN x == err of
88 | False => E (inject $
EN x) t
91 | toRes : PrimIO a -> PrimIO CInt -> EPrim a
93 | let R _ t := toUnit act t | E x t => E x t
94 | r # t := toF1 wrap t
98 | ignore : E1 World es a -> PrimIO ()
100 | primRun $
\t => case act t of
109 | freeFail : Struct f => f World -> Errno -> EPrim b
111 | let _ # t := freeStruct1 s t
112 | in E (inject err) t
115 | finally : PrimIO () -> EPrim a -> EPrim a
116 | finally cleanup act t =
118 | R r t => let _ # t := toF1 cleanup t in R r t
119 | E x t => let _ # t := toF1 cleanup t in E x t
122 | primStruct : (0 f : _) -> Struct f => SizeOf (f World) => F1 World (f World)
123 | primStruct f = allocStruct1 f
126 | withStruct : (0 f : _) -> Struct f => SizeOf (f World) => (f World -> EPrim b) -> EPrim b
128 | let str # t := primStruct a t
129 | in finally (prim__free (sunwrap str)) (f str) t
132 | withBox : (0 a : Type) -> SizeOf a => Deref a => (IOBox a -> EPrim b) -> EPrim b
134 | let box # t := ioToF1 (malloc a 1) t
135 | in finally (toPrim $
free box) (f box) t
138 | withPtr : Bits32 -> (AnyPtr -> EPrim b) -> EPrim b
140 | let ptr := prim__malloc sz
141 | in finally (prim__free ptr) (f ptr) t
146 | -> {auto sz : SizeOf a}
148 | -> (CArrayIO n a -> EPrim b)
150 | withCArray a n f t =
151 | let arr # t := malloc1 a n t
152 | in finally (prim__free (unsafeUnwrap arr)) (f arr) t
155 | primTraverse_ : (a -> PrimIO ()) -> List a -> PrimIO ()
156 | primTraverse_ f [] w = MkIORes () w
157 | primTraverse_ f (x :: xs) w =
158 | let MkIORes _ w := f x w
159 | in primTraverse_ f xs w
162 | filterM : SnocList a -> (a -> F1 rs Bool) -> List a -> F1 rs (List a)
163 | filterM sa f [] t = (sa <>> []) # t
164 | filterM sa f (v::vs) t =
165 | let True # t := f v t | _ # t => filterM sa f vs t
166 | in filterM (sa :< v) f vs t
170 | {auto der : Deref b}
171 | -> {auto sof : SizeOf b}
176 | -> {auto 0 prf : LTE k n}
178 | values cs arr f 0 t = cs # t
179 | values cs arr f (S k) t =
180 | let vb # t := getNat arr k t
182 | in values (vc::cs) arr f k t
186 | {auto str : Struct f}
187 | -> {auto sof : SizeOf (f s)}
189 | -> CArray s n (f s)
192 | -> {auto 0 prf : LTE k n}
194 | structs cs arr f 0 t = cs # t
195 | structs cs arr f (S k) t =
196 | let vb # t := getStructNat arr k t
198 | in structs (vc::cs) arr f k t