0 | module System.File.Process
2 | import public System.Escape
3 | import public System.File.Error
4 | import public System.File.Mode
6 | import System.File.Support
7 | import public System.File.Types
9 | %foreign "C:fflush,libc 6"
11 | prim__flush : FilePtr -> PrimIO Int
12 | %foreign supportC "idris2_popen"
14 | prim__popen : String -> String -> PrimIO FilePtr
17 | %foreign supportCSafe "idris2_pclose"
18 | supportC "idris2_pclose"
19 | supportNode "pclose"
20 | prim__pclose : FilePtr -> PrimIO Int
22 | data Popen2Result : Type where [external]
24 | %foreign supportC "idris2_popen2"
25 | prim__popen2 : String -> PrimIO (Ptr Popen2Result)
27 | %foreign supportCSafe "idris2_popen2WaitByPid"
28 | supportC "idris2_popen2WaitByPid"
30 | prim__popen2WaitByPid : Int -> PrimIO Int
31 | %foreign supportCSafe "idris2_popen2WaitByHandler"
32 | supportC "idris2_popen2WaitByHandler"
34 | prim__popen2WaitByHandler : AnyPtr -> PrimIO Int
36 | %foreign supportC "idris2_popen2ChildPid"
37 | prim__popen2ChildPid : Ptr Popen2Result -> PrimIO Int
39 | %foreign supportC "idris2_popen2ChildHandler"
40 | prim__popen2ChildHandler : Ptr Popen2Result -> PrimIO AnyPtr
42 | %foreign supportC "idris2_popen2FileIn"
43 | prim__popen2FileIn : Ptr Popen2Result -> PrimIO FilePtr
45 | %foreign supportC "idris2_popen2FileOut"
46 | prim__popen2FileOut : Ptr Popen2Result -> PrimIO FilePtr
52 | fflush : HasIO io => (h : File) -> io ()
54 | = ignore $
primIO (prim__flush f)
68 | popen : HasIO io => (cmd : String) -> (m : Mode) -> io (Either FileError File)
70 | ptr <- primIO (prim__popen cmd (modeStr m))
71 | if prim__nullAnyPtr ptr /= 0
73 | else pure (Right (FHandle ptr))
80 | pclose : HasIO io => (fh : File) -> io Int
81 | pclose (FHandle h) = primIO (prim__pclose h)
85 | record SubProcess where
86 | constructor MkSubProcess
108 | popen2 : HasIO io => (cmd : String) -> io (Either FileError SubProcess)
110 | ptr <- primIO (prim__popen2 cmd)
111 | if prim__nullPtr ptr /= 0
114 | pid <- primIO (prim__popen2ChildPid ptr)
115 | handle <- primIO (prim__popen2ChildHandler ptr)
116 | input <- primIO (prim__popen2FileIn ptr)
117 | output <- primIO (prim__popen2FileOut ptr)
118 | free (prim__forgetPtr ptr)
119 | pure $
Right (MkSubProcess pid handle (FHandle input) (FHandle output))
130 | popen2Wait : HasIO io => SubProcess -> io Int
131 | popen2Wait sp = primIO $
132 | if prim__nullAnyPtr sp.handler /= 0
133 | then prim__popen2WaitByPid sp.pid
134 | else prim__popen2WaitByHandler sp.handler
138 | popen : HasIO io => (cmd : List String) -> (m : Mode) -> io (Either FileError File)
139 | popen = popen . escapeCmd
142 | popen2 : HasIO io => (cmd : List String) -> io (Either FileError SubProcess)
143 | popen2 = popen2 . escapeCmd