System.hs.include 1.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
[ForFunction "getCPUTime"
,ForFunction "getElapsedTime"
,ForFunction "getArgs"
,ForFunction "prim_getEnviron"
,ForFunction "getHostname"
,ForFunction "getPID"
,ForFunction "getProgName"
,ForFunction "prim_system"
,ForFunction "prim_sleep"
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
,ForFunction "prim_exitWith"
]

import qualified System.Environment as SE
import qualified System.CPUTime as SC
import System.Cmd
import System.Exit
import qualified Network.BSD as NB
import System.Posix.Process

instance ConvertCH C_Int ExitCode where
   toCurry ExitSuccess = toCurry (0::Integer)
   toCurry (ExitFailure i) = toCurry i

   fromCurry i = if hi Prelude.== 0 then ExitSuccess else ExitFailure hi
     where hi = fromCurry i

getCPUTime :: Result (C_IO C_Int)
getCPUTime = ioFunc0 (SC.getCPUTime Prelude.>>= Prelude.return . (`div` 1000000000))

getElapsedTime :: Result (C_IO C_Int)
getElapsedTime = error "getElapsedTime not provided"

getArgs :: Result (C_IO (List (List C_Char)))
getArgs = ioFunc0 SE.getArgs

prim_getEnviron :: (List C_Char) -> Result (C_IO (List C_Char))
prim_getEnviron = 
  ioFunc1 (\s -> do
             env <- SE.getEnvironment
             case Prelude.lookup s env of
              Nothing -> Prelude.return ""
              Just v  -> Prelude.return v)

getHostname :: Result (C_IO (List C_Char))
getHostname = ioFunc0 NB.getHostName

getPID :: Result (C_IO C_Int)
getPID = ioFunc0 (do
                    pid <- getProcessID 
                    Prelude.return (toInteger pid))

getProgName :: Result (C_IO (List C_Char))
53
getProgName = ioFunc0 (Curry.RunTimeSystem.getProgName)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  -- conform with haskell would be: SE.getProgName

prim_system :: (List C_Char) -> Result (C_IO C_Int)
prim_system = ioFunc1 system

prim_sleep :: C_Int -> Result (C_IO T0)
prim_sleep = ioFunc1 (\ t -> do
                          system ("sleep "++Prelude.show (t::Integer)) 
                          Prelude.return ())

prim_exitWith :: Curry a => C_Int -> Result (C_IO a)
prim_exitWith e _ = C_IO (\ _ -> do
  ex <- exitWith (fromCurry e) 
  Prelude.return (IOVal ex))