2 | import public Data.So
5 | import public System.Escape
13 | supportC : (fn : String) -> String
14 | supportC fn = "C:\{fn}, libidris2_support, idris_support.h"
19 | supportNode : (fn : String) -> String
20 | supportNode fn = "node:support:\{fn},support_system"
25 | libc : (fn : String) -> String
26 | libc fn = "C:" ++ fn ++ ", libc 6"
33 | %foreign "scheme,racket:blodwen-sleep"
34 | "scheme,chez:blodwen-sleep"
35 | supportC "idris2_sleep"
36 | prim__sleep : Int -> PrimIO ()
38 | %foreign "scheme,racket:blodwen-usleep"
39 | "scheme,chez:blodwen-usleep"
40 | supportC "idris2_usleep"
41 | prim__usleep : Int -> PrimIO ()
50 | sleep : HasIO io => (sec : Int) -> io ()
51 | sleep sec = primIO (prim__sleep sec)
60 | usleep : HasIO io => (usec : Int) -> So (usec >= 0) => io ()
61 | usleep usec = primIO (prim__usleep usec)
70 | %foreign "scheme:blodwen-arg-count"
71 | supportC "idris2_getArgCount"
72 | "node:lambda:() => process.argv.length - 1"
73 | prim__getArgCount : PrimIO Int
77 | %foreign "scheme:blodwen-arg"
78 | supportC "idris2_getArg"
79 | "node:lambda:n => process.argv[n + 1]"
80 | prim__getArg : Int -> PrimIO String
84 | getArgs : HasIO io => io (List String)
86 | n <- primIO prim__getArgCount
88 | then for [0..n-1] $
primIO . prim__getArg
91 | %foreign libc "getenv"
92 | "node:lambda: n => process.env[n]"
93 | prim__getEnv : String -> PrimIO (Ptr String)
95 | %foreign supportC "idris2_getEnvPair"
96 | prim__getEnvPair : Int -> PrimIO (Ptr String)
97 | %foreign supportC "idris2_setenv"
98 | supportNode "setEnv"
99 | prim__setEnv : String -> String -> Int -> PrimIO Int
100 | %foreign supportC "idris2_unsetenv"
101 | supportNode "unsetEnv"
102 | prim__unsetEnv : String -> PrimIO Int
104 | %foreign "C:idris2_enableRawMode, libidris2_support, idris_support.h"
105 | prim__enableRawMode : (1 x : %World) -> IORes Int
107 | %foreign "C:idris2_resetRawMode, libidris2_support, idris_support.h"
108 | prim__resetRawMode : (1 x : %World) -> IORes ()
117 | enableRawMode : HasIO io => io (Either FileError ())
119 | case !(primIO prim__enableRawMode) of
120 | 0 => pure $
Right ()
126 | resetRawMode : HasIO io => io ()
127 | resetRawMode = primIO prim__resetRawMode
134 | withRawMode : HasIO io =>
135 | (onError : FileError -> io a) ->
136 | (onSuccess : () -> io a) ->
138 | withRawMode onError onSuccess = do
139 | Right () <- enableRawMode
140 | | Left err => onError err
141 | result <- onSuccess ()
150 | getEnv : HasIO io => (var : String) -> io (Maybe String)
152 | = do env <- primIO $
prim__getEnv var
153 | if prim__nullPtr env /= 0
155 | else pure (Just (prim__getString env))
161 | getEnvironment : HasIO io => io (List (String, String))
162 | getEnvironment = getAllPairs 0 []
164 | splitEq : String -> (String, String)
166 | = let (k, v) = break (== '=') str
167 | (_, v') = break (/= '=') v in
170 | getAllPairs : Int -> List String -> io (List (String, String))
171 | getAllPairs n acc = do
172 | envPair <- primIO $
prim__getEnvPair n
173 | if prim__nullPtr envPair /= 0
174 | then pure $
reverse $
map splitEq acc
175 | else getAllPairs (n + 1) (prim__getString envPair :: acc)
188 | setEnv : HasIO io => (var : String) -> (val : String) -> (overwrite : Bool) ->
190 | setEnv var val overwrite
191 | = do ok <- primIO $
prim__setEnv var val (if overwrite then 1 else 0)
198 | unsetEnv : HasIO io => (var : String) -> io Bool
200 | = do ok <- primIO $
prim__unsetEnv var
205 | %foreign "scheme,chez:blodwen-system"
206 | "C:idris2_system, libidris2_support, idris_system.h"
207 | supportNode "spawnSync"
208 | prim__system : String -> PrimIO Int
213 | system : HasIO io => String -> io Int
214 | system cmd = primIO (prim__system cmd)
218 | system : HasIO io => List String -> io Int
219 | system = system . escapeCmd
224 | run : HasIO io => (cmd : String) -> io (String, Int)
226 | Right f <- popen cmd Read
227 | | Left err => pure ("", 1)
228 | Right resp <- fRead f
229 | | Left err => pure ("", 1)
230 | exitCode <- pclose f
231 | pure (resp, exitCode)
236 | run : HasIO io => (cmd : List String) -> io (String, Int)
237 | run = run . escapeCmd
247 | runProcessingOutput : HasIO io => (String -> io ()) -> (cmd : String) -> io Int
248 | runProcessingOutput pr cmd = do
249 | Right f <- popen cmd Read
250 | | Left err => pure 1
256 | process : File -> io Bool
257 | process h = if !(fEOF h) then pure True else do
258 | Right line <- fGetLine h
259 | | Left err => pure False
266 | runProcessingOutput : HasIO io => (String -> io ()) -> (cmd : List String) -> io Int
267 | runProcessingOutput pr = runProcessingOutput pr . escapeCmd
269 | %foreign supportC "idris2_time"
270 | "javascript:lambda:() => Math.floor(new Date().getTime() / 1000)"
271 | prim__time : PrimIO Int
275 | time : HasIO io => io Integer
276 | time = pure $
cast !(primIO prim__time)
278 | %foreign supportC "idris2_getPID"
279 | "node:lambda:() => process.pid"
280 | prim__getPID : PrimIO Int
284 | getPID : HasIO io => io Int
285 | getPID = primIO prim__getPID
287 | %foreign libc "exit"
288 | "node:lambda:c => process.exit(c)"
289 | prim__exit : Int -> PrimIO ()
294 | data ExitCode : Type where
296 | ExitSuccess : ExitCode
301 | ExitFailure : (errNo : Int) -> (So (not $
errNo == 0)) => ExitCode
304 | Cast Int ExitCode where
305 | cast 0 = ExitSuccess
306 | cast code = ExitFailure code @{believe_me Oh}
309 | Cast ExitCode Int where
310 | cast ExitSuccess = 0
311 | cast (ExitFailure code) = code
315 | exitWith : HasIO io => ExitCode -> io a
316 | exitWith = primIO . believe_me . prim__exit . cast
321 | exitFailure : HasIO io => io a
322 | exitFailure = exitWith (ExitFailure 1)
326 | exitSuccess : HasIO io => io a
327 | exitSuccess = exitWith ExitSuccess
331 | die : HasIO io => String -> io a
333 | = do ignore $
fPutStrLn stderr str