on 4e34dfd help sign in
Idris-dev/libs/base/System.idr
84 lines 2.5 kB history raw
1module System
3%include C "unistd.h"
4%default partial
5%access public
7||| Get the command-line arguments that the program was called with.
8getArgs : IO (List String)
9getArgs = do n <- numArgs
10 ga' [] 0 n
11 where
12 numArgs : IO Int
13 numArgs = foreign FFI_C "idris_numArgs" (IO Int)
15 getArg : Int -> IO String
16 getArg x = foreign FFI_C "idris_getArg" (Int -> IO String) x
18 ga' : List String -> Int -> Int -> IO (List String)
19 ga' acc i n = if (i == n) then (return $ reverse acc) else
20 do arg <- getArg i
21 ga' (arg :: acc) (i+1) n
23||| Retrieves an value from the environment, if the given key is present,
24||| otherwise it returns Nothing.
25getEnv : String -> IO (Maybe String)
26getEnv key = do
27 str_ptr <- getEnv'
28 is_nil <- nullStr str_ptr
29 if is_nil
30 then pure Nothing
31 else pure (Just str_ptr)
32 where
33 getEnv' : IO String
34 getEnv' = foreign FFI_C "getenv" (String -> IO String) key
36||| Sets an environment variable with a given value.
37||| Returns true if the operation was successful.
38setEnv : String -> String -> IO Bool
39setEnv key value = do
40 ok <- foreign FFI_C "setenv" (String -> String -> Int -> IO Int) key value 1
41 return (ok == 0)
43||| Unsets an environment variable.
44||| Returns true if the variable was able to be unset.
45unsetEnv : String -> IO Bool
46unsetEnv key = do
47 ok <- foreign FFI_C "unsetenv" (String -> IO Int) key
48 return (ok == 0)
50getEnvironment : IO (List (String, String))
51getEnvironment = getAllPairs 0 []
52 where
53 getEnvPair : Int -> IO String
54 getEnvPair i = foreign FFI_C "getEnvPair" (Int -> IO String) i
56 splitEq : String -> (String, String)
57 splitEq str =
58 -- FIXME: There has to be a better way to split this up
59 let (k, v) = break (== '=') str in
60 let (_, v') = break (/= '=') v in
61 (k, v')
63 getAllPairs : Int -> List String -> IO (List (String, String))
64 getAllPairs n acc = do
65 envPair <- getEnvPair n
66 is_nil <- nullStr envPair
67 if is_nil
68 then return $ reverse $ map splitEq acc
69 else getAllPairs (n + 1) (envPair :: acc)
71||| Quit with a particular exit code
72exit : Int -> IO ()
73exit code = foreign FFI_C "exit" (Int -> IO ()) code
75||| Get the Unix time
76time : IO Int
77time = foreign FFI_C "idris_time" (IO Int)
79usleep : Int -> IO ()
80usleep i = foreign FFI_C "usleep" (Int -> IO ()) i
82system : String -> IO Int
83system cmd = foreign FFI_C "system" (String -> IO Int) cmd