on c9883e6 help sign in
Idris-dev/libs/base/System.idr
72 lines 2.2 kB history raw
1module System
3import Data.So
5%include C "unistd.h"
6%default partial
7%access public export
9||| Retrieves a value from the environment if the given key is present,
10||| otherwise it returns Nothing.
11getEnv : String -> IO (Maybe String)
12getEnv key = do
13 str_ptr <- getEnv'
14 is_nil <- nullStr str_ptr
15 if is_nil
16 then pure Nothing
17 else pure (Just str_ptr)
18 where
19 getEnv' : IO String
20 getEnv' = foreign FFI_C "getenv" (String -> IO String) key
22||| Sets an environment variable with a given value.
23||| Returns true if the operation was successful.
24setEnv : String -> String -> IO Bool
25setEnv key value = do
26 ok <- foreign FFI_C "setenv" (String -> String -> Int -> IO Int) key value 1
27 pure (ok == 0)
29||| Unsets an environment variable.
30||| Returns true if the variable was able to be unset.
31unsetEnv : String -> IO Bool
32unsetEnv key = do
33 ok <- foreign FFI_C "unsetenv" (String -> IO Int) key
34 pure (ok == 0)
36getEnvironment : IO (List (String, String))
37getEnvironment = getAllPairs 0 []
38 where
39 getEnvPair : Int -> IO String
40 getEnvPair i = foreign FFI_C "getEnvPair" (Int -> IO String) i
42 splitEq : String -> (String, String)
43 splitEq str =
44 -- FIXME: There has to be a better way to split this up
45 let (k, v) = break (== '=') str in
46 let (_, v') = break (/= '=') v in
47 (k, v')
49 getAllPairs : Int -> List String -> IO (List (String, String))
50 getAllPairs n acc = do
51 envPair <- getEnvPair n
52 is_nil <- nullStr envPair
53 if is_nil
54 then pure $ reverse $ map splitEq acc
55 else getAllPairs (n + 1) (envPair :: acc)
57||| Quit with a particular exit code
58exit : Int -> IO ()
59exit code = foreign FFI_C "exit" (Int -> IO ()) code
61||| Get the numbers of seconds since 1st January 1970, 00:00 UTC
62time : IO Integer
63time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
64 pure t
66||| Specify interval to sleep for, must be in range [0, 1000000]
67usleep : (i : Int) -> { auto prf : So (i > 0 && i <= 1000000) } -> IO ()
68usleep i = foreign FFI_C "usleep" (Int -> IO ()) i
70system : String -> IO Int
71system cmd = foreign FFI_C "system" (String -> IO Int) cmd