on ddfdb2e 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 = mkForeign (FFun "idris_numArgs" [] FInt)
15 getArg : Int -> IO String
16 getArg x = mkForeign (FFun "idris_getArg" [FInt] FString) 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' = mkForeign (FFun "getenv" [FString] FString) 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 <- mkForeign (FFun "setenv" [FString, FString, FInt] FInt) 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 <- mkForeign (FFun "unsetenv" [FString] FInt) key
48 return (ok == 0)
50getEnvironment : IO (List (String, String))
51getEnvironment = getAllPairs 0 []
52 where
53 getEnvPair : Int -> IO String
54 getEnvPair i = mkForeign (FFun "getEnvPair" [FInt] FString) 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 = mkForeign (FFun "exit" [FInt] FUnit) code
75||| Get the Unix time
76time : IO Int
77time = mkForeign (FFun "idris_time" [] FInt)
79usleep : Int -> IO ()
80usleep i = mkForeign (FFun "usleep" [FInt] FUnit) i
82system : String -> IO Int
83system cmd = mkForeign (FFun "system" [FString] FInt) cmd