/[eiffelstudio]/FreeELKS/trunk/library/support/execution_environment.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/support/execution_environment.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91599 - (show annotations)
Mon Jun 16 17:03:22 2008 UTC (11 years, 7 months ago) by manus_eiffel
File size: 7177 byte(s)
Added `sleep' routine (formerly in the ISE EiffelThread library only) and corresponding `eif_sleep' C
  external for platform specific support.

1 indexing
2
3 description:
4 "The objects available from the environment at time of execution"
5
6 library: "Free implementation of ELKS library"
7 copyright: "Copyright (c) 2005-2008, Eiffel Software and others"
8 license: "Eiffel Forum License v2 (see forum.txt)"
9 date: "$Date$"
10 revision: "$Revision$"
11
12
13 class EXECUTION_ENVIRONMENT
14
15 feature -- Access
16
17 command_line: ARGUMENTS is
18 -- Command line that was used to start current execution
19 once
20 create Result
21 end
22
23 current_working_directory: STRING is
24 -- Directory of current execution
25 external
26 "C use %"eif_dir.h%""
27 alias
28 "dir_current"
29 ensure
30 result_not_void: Result /= Void
31 end
32
33 default_shell: STRING is
34 -- Default shell
35 local
36 s: ?STRING
37 once
38 s := get ("SHELL")
39 if s = Void then
40 Result := ""
41 else
42 Result := s
43 end
44 end
45
46 get (s: STRING): ?STRING is
47 -- Value of `s' if it is an environment variable and has been set;
48 -- void otherwise.
49 require
50 s_exists: s /= Void
51 not_has_null_character: not s.has ('%U')
52 local
53 ext: ANY
54 c_string: POINTER
55 void_pointer: POINTER
56 do
57 ext := s.to_c
58 c_string := eif_getenv ($ext)
59 if c_string /= void_pointer then
60 create Result.make_from_c (c_string)
61 end
62 end
63
64 home_directory_name: ?STRING is
65 -- Directory name corresponding to the home directory.
66 require
67 home_directory_supported: Operating_environment.home_directory_supported
68 do
69 Result := eif_home_directory_name
70 end
71
72 root_directory_name: STRING is
73 -- Directory name corresponding to the root directory.
74 require
75 root_directory_supported: Operating_environment.root_directory_supported
76 do
77 Result := eif_root_directory_name
78 ensure
79 result_not_void: Result /= Void
80 end
81
82 starting_environment_variables: HASH_TABLE [STRING, STRING] is
83 -- Table of environment variables when current process starts,
84 -- indexed by variable name
85 local
86 l_ptr: POINTER
87 i: INTEGER
88 l_curr_var: ?TUPLE [value: STRING; key: STRING]
89 do
90 create Result.make (40)
91 from
92 i := 0
93 l_ptr := i_th_environ (i)
94 until
95 l_ptr = default_pointer
96 loop
97 l_curr_var := separated_variables (create {STRING}.make_from_c (l_ptr))
98 if l_curr_var /= Void then
99 Result.force (l_curr_var.value, l_curr_var.key)
100 end
101 i := i + 1
102 l_ptr := i_th_environ (i)
103 end
104 ensure
105 result_attached: Result /= Void
106 end
107
108 feature -- Status
109
110 return_code: INTEGER
111 -- Status code set by last call to `system' or `put'
112
113 feature -- Status setting
114
115 change_working_directory (path: STRING) is
116 -- Set the current directory to `path'
117 local
118 ext: ANY
119 do
120 ext := path.to_c
121 return_code := eif_chdir ($ext)
122 end
123
124 put (value, key: STRING) is
125 -- Set the environment variable `key' to `value'.
126 require
127 key_exists: key /= Void
128 key_meaningful: not key.is_empty
129 not_key_has_null_character: not key.has ('%U')
130 value_exists: value /= Void
131 not_value_has_null_character: not value.has ('%U')
132 local
133 l_env: STRING
134 l_c_env: C_STRING
135 do
136 create l_env.make (value.count + key.count + 1)
137 l_env.append (key)
138 l_env.append_character ('=')
139 l_env.append (value)
140 create l_c_env.make (l_env)
141 environ.force (l_c_env, key)
142 return_code := eif_putenv (l_c_env.item)
143 ensure
144 variable_set: (return_code = 0) implies
145 (equal (get (key), value) or else (value.is_empty and then (get (key) = Void)))
146 end
147
148 system (s: STRING) is
149 -- Pass to the operating system a request to execute `s'.
150 -- If `s' is empty, use the default shell as command.
151 require
152 s_exists: s /= Void
153 local
154 ext: ANY
155 do
156 if s.is_empty then
157 ext := default_shell.to_c
158 else
159 ext := s.to_c
160 end
161 return_code := system_call ($ext)
162 end
163
164 launch (s: STRING) is
165 -- Pass to the operating system an asynchronous request to
166 -- execute `s'.
167 -- If `s' is empty, use the default shell as command.
168 require
169 s_not_void: s /= Void
170 local
171 ext: ANY
172 do
173 if s.is_empty then
174 ext := default_shell.to_c
175 else
176 ext := s.to_c
177 end
178 asynchronous_system_call ($ext)
179 end
180
181 sleep (nanoseconds: INTEGER_64) is
182 -- Suspend thread execution for interval specified in
183 -- `nanoseconds' (1 nanosecond = 10^(-9) second).
184 require
185 non_negative_nanoseconds: nanoseconds >= 0
186 do
187 eif_sleep (nanoseconds)
188 end
189
190 feature {NONE} -- Implementation
191
192 environ: HASH_TABLE [C_STRING, STRING] is
193 -- Environment variable memory set by current execution,
194 -- indexed by environment variable name. Needed otherwise
195 -- we would corrupt memory after freeing memory used by
196 -- C_STRING instance since not referenced anywhere else.
197 once
198 create Result.make (10)
199 end
200
201 i_th_environ (i: INTEGER): POINTER is
202 -- Environment variable at `i'-th position of `eif_environ'.
203 require
204 i_valid: i >=0
205 external
206 "C inline use <string.h>"
207 alias
208 "[
209 if (eif_environ) {
210 #ifdef EIF_WINDOWS
211 LPSTR vars = (LPSTR) eif_environ;
212 int cnt = 0;
213 for (; *vars; vars++) {
214 if ($i==cnt) return vars;
215 while (*vars) { vars++; }
216 cnt++;
217 }
218 return NULL;
219 #else
220 return ((char **)eif_environ)[$i];
221 #endif
222 } else {
223 return NULL;
224 }
225 ]"
226 end
227
228 separated_variables (a_var: STRING): ?TUPLE [value: STRING; key: STRING] is
229 -- Given an environment variable `a_var' in form of "key=value",
230 -- return separated key and value.
231 -- Return Void if `a_var' is in incorrect format.
232 require
233 a_var_attached: a_var /= Void
234 local
235 i, j: INTEGER
236 done: BOOLEAN
237 do
238 j := a_var.count
239 from
240 i := 1
241 until
242 i > j or done
243 loop
244 if a_var.item (i) = '=' then
245 done := True
246 else
247 i := i + 1
248 end
249 end
250 if i > 1 and then i < j then
251 Result := [a_var.substring (i + 1, j), a_var.substring (1, i - 1)]
252 end
253 end
254
255 feature {NONE} -- External
256
257 eif_getenv (s: POINTER): POINTER is
258 -- Value of environment variable `s'
259 external
260 "C use <stdlib.h>"
261 alias
262 "getenv"
263 end
264
265 eif_putenv (v: POINTER): INTEGER is
266 -- Set `v' in environment.
267 external
268 "C use <stdlib.h>"
269 alias
270 "putenv"
271 end
272
273 eif_chdir (path: POINTER): INTEGER is
274 -- Set the current directory to `path'
275 external
276 "C use %"eif_dir.h%""
277 end
278
279 system_call (s: POINTER): INTEGER is
280 -- Pass to the operating system a request to execute `s'.
281 external
282 "C blocking use %"eif_misc.h%""
283 alias
284 "eif_system"
285 end
286
287 asynchronous_system_call (s: POINTER) is
288 -- Pass to the operating system an asynchronous request to execute `s'.
289 external
290 "C blocking use %"eif_misc.h%""
291 alias
292 "eif_system_asynchronous"
293 end
294
295 eif_home_directory_name: ?STRING is
296 -- Directory name corresponding to the home directory
297 external
298 "C use %"eif_path_name.h%""
299 end
300
301 eif_root_directory_name: STRING is
302 -- Directory name corresponding to the root directory
303 external
304 "C use %"eif_path_name.h%""
305 end
306
307 eif_sleep (nanoseconds: INTEGER_64) is
308 -- Suspend thread execution for interval specified in
309 -- `nanoseconds' (1 nanosecond = 10^(-9) second).
310 require
311 non_negative_nanoseconds: nanoseconds >= 0
312 external
313 "C blocking use %"eif_misc.h%""
314 end
315
316 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23