1
2
3
4
5
6
7
8
9
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
module System
( getCPUTime,getElapsedTime
, getArgs, getEnviron, setEnviron, unsetEnviron, getProgName
, getHostname, getPID, system, exitWith, sleep
, isPosix, isWindows
) where
import Global ( Global, GlobalSpec(..), global, readGlobal, writeGlobal )
getCPUTime :: IO Int
getCPUTime external
getElapsedTime :: IO Int
getElapsedTime external
getArgs :: IO [String]
getArgs external
getEnviron :: String -> IO String
getEnviron evar = do
envs <- readGlobal environ
maybe (prim_getEnviron $## evar) return (lookup evar envs)
prim_getEnviron :: String -> IO String
prim_getEnviron external
environ :: Global [(String,String)]
environ = global [] Temporary
setEnviron :: String -> String -> IO ()
setEnviron evar val = do
envs <- readGlobal environ
writeGlobal environ ((evar,val) : filter ((/=evar) . fst) envs)
unsetEnviron :: String -> IO ()
unsetEnviron evar = do
envs <- readGlobal environ
writeGlobal environ (filter ((/=evar) . fst) envs)
getHostname :: IO String
getHostname external
getPID :: IO Int
getPID external
getProgName :: IO String
getProgName external
system :: String -> IO Int
system cmd = do
envs <- readGlobal environ
prim_system $## (concatMap envToExport envs ++ escapedCmd)
where
win = isWindows
escapedCmd = if win then '"' : cmd ++ "\""
else cmd
envToExport (var, val) =
if win
then "set " ++ var ++ "=" ++ concatMap escapeWinSpecials val ++ " && "
else var ++ "='" ++ concatMap encodeShellSpecials val
++ "' ; export " ++ var ++ " ; "
escapeWinSpecials c = if c `elem` "<>|&^" then ['^', c]
else [c]
encodeShellSpecials c = if c == '\'' then map chr [39,34,39,34,39]
else [c]
prim_system :: String -> IO Int
prim_system external
exitWith :: Int -> IO _
exitWith exitcode = prim_exitWith $# exitcode
prim_exitWith :: Int -> IO _
prim_exitWith external
sleep :: Int -> IO ()
sleep n = prim_sleep $# n
prim_sleep :: Int -> IO ()
prim_sleep external
isPosix :: Bool
isPosix = not isWindows
isWindows :: Bool
isWindows external
|