on cf47310 help sign in
Idris-dev/libs/base/System.idr
69 lines 2.0 kB history raw
1module System
3%include C "unistd.h"
4%default partial
5%access public
7||| Retrieves a value from the environment if the given key is present,
8||| otherwise it returns Nothing.
9getEnv : String -> IO (Maybe String)
10getEnv key = do
11 str_ptr <- getEnv'
12 is_nil <- nullStr str_ptr
13 if is_nil
14 then pure Nothing
15 else pure (Just str_ptr)
16 where
17 getEnv' : IO String
18 getEnv' = foreign FFI_C "getenv" (String -> IO String) key
20||| Sets an environment variable with a given value.
21||| Returns true if the operation was successful.
22setEnv : String -> String -> IO Bool
23setEnv key value = do
24 ok <- foreign FFI_C "setenv" (String -> String -> Int -> IO Int) key value 1
25 return (ok == 0)
27||| Unsets an environment variable.
28||| Returns true if the variable was able to be unset.
29unsetEnv : String -> IO Bool
30unsetEnv key = do
31 ok <- foreign FFI_C "unsetenv" (String -> IO Int) key
32 return (ok == 0)
34getEnvironment : IO (List (String, String))
35getEnvironment = getAllPairs 0 []
36 where
37 getEnvPair : Int -> IO String
38 getEnvPair i = foreign FFI_C "getEnvPair" (Int -> IO String) i
40 splitEq : String -> (String, String)
41 splitEq str =
42 -- FIXME: There has to be a better way to split this up
43 let (k, v) = break (== '=') str in
44 let (_, v') = break (/= '=') v in
45 (k, v')
47 getAllPairs : Int -> List String -> IO (List (String, String))
48 getAllPairs n acc = do
49 envPair <- getEnvPair n
50 is_nil <- nullStr envPair
51 if is_nil
52 then return $ reverse $ map splitEq acc
53 else getAllPairs (n + 1) (envPair :: acc)
55||| Quit with a particular exit code
56exit : Int -> IO ()
57exit code = foreign FFI_C "exit" (Int -> IO ()) code
59||| Get the Unix time
60time : IO Integer
61time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
62 return t
64usleep : Int -> IO ()
65usleep i = foreign FFI_C "usleep" (Int -> IO ()) i
67system : String -> IO Int
68system cmd = foreign FFI_C "system" (String -> IO Int) cmd