/[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 92166 - (show annotations)
Sun Feb 3 22:18:37 2013 UTC (6 years, 11 months ago) by manus_eiffel
File size: 9602 byte(s)
Made it compile by adding inheritance to NATIVE_STRING_HANDLER.

1 note
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 inherit
16 NATIVE_STRING_HANDLER
17
18 feature -- Access
19
20 command_line: ARGUMENTS
21 -- Command line that was used to start current execution
22 once
23 create Result
24 end
25
26 current_working_directory: STRING
27 -- Directory of current execution.
28 -- Execution of this query on concurrent threads will result in
29 -- an unspecified behavior.
30 local
31 l_count, l_nbytes: INTEGER
32 l_managed: MANAGED_POINTER
33 do
34 l_count := 50
35 create l_managed.make (l_count)
36 l_nbytes := eif_dir_current (l_managed.item, l_count)
37 if l_nbytes = -1 then
38 -- The underlying OS could not retrieve the current working directory. Most likely
39 -- a case where it has been deleted under our feet. We simply return that the current
40 -- directory is `.' the symbol for the current working directory.
41 Result := "."
42 else
43 if l_nbytes > l_count then
44 -- We need more space.
45 l_count := l_nbytes
46 l_managed.resize (l_count)
47 l_nbytes := eif_dir_current (l_managed.item, l_count)
48 end
49 if l_nbytes > 0 and l_nbytes <= l_count then
50 Result := file_info.pointer_to_file_name_8 (l_managed.item)
51 else
52 -- Something went wrong.
53 Result := "."
54 check False end
55 end
56 end
57 ensure
58 result_not_void: Result /= Void
59 end
60
61 default_shell: STRING
62 -- Default shell
63 local
64 s: detachable STRING
65 once
66 s := get ("SHELL")
67 if s = Void then
68 Result := ""
69 else
70 Result := s
71 end
72 end
73
74 get (s: STRING): detachable STRING
75 -- Value of `s' if it is an environment variable and has been set;
76 -- void otherwise.
77 require
78 s_exists: s /= Void
79 not_has_null_character: not s.has ('%U')
80 local
81 ext: ANY
82 c_string: POINTER
83 void_pointer: POINTER
84 do
85 ext := s.to_c
86 c_string := eif_getenv ($ext)
87 if c_string /= void_pointer then
88 create Result.make_from_c (c_string)
89 end
90 end
91
92 home_directory_name: detachable STRING
93 -- Directory name corresponding to the home directory.
94 require
95 home_directory_supported: Operating_environment.home_directory_supported
96 local
97 l_count, l_nbytes: INTEGER
98 l_managed: MANAGED_POINTER
99 once
100 l_count := 50
101 create l_managed.make (l_count)
102 l_nbytes := eif_home_directory_name_ptr (l_managed.item, l_count)
103 if l_nbytes > l_count then
104 l_count := l_nbytes
105 l_managed.resize (l_count)
106 l_nbytes := eif_home_directory_name_ptr (l_managed.item, l_count)
107 end
108 if l_nbytes > 0 and l_nbytes <= l_count then
109 Result := file_info.pointer_to_file_name_8 (l_managed.item)
110 end
111 end
112
113 root_directory_name: STRING
114 -- Directory name corresponding to the root directory.
115 require
116 root_directory_supported: Operating_environment.root_directory_supported
117 once
118 if {PLATFORM}.is_windows then
119 Result := "\"
120 elseif {PLATFORM}.is_vms then
121 Result := "[000000]"
122 else
123 Result := "/"
124 end
125 ensure
126 result_not_void: Result /= Void
127 end
128
129 starting_environment_variables: HASH_TABLE [STRING, STRING]
130 -- Table of environment variables when current process starts,
131 -- indexed by variable name
132 local
133 l_ptr: POINTER
134 i: INTEGER
135 l_curr_var: detachable TUPLE [value: STRING; key: STRING]
136 do
137 create Result.make (40)
138 from
139 i := 0
140 l_ptr := i_th_environ (i)
141 until
142 l_ptr = default_pointer
143 loop
144 l_curr_var := separated_variables (create {STRING}.make_from_c (l_ptr))
145 if l_curr_var /= Void then
146 Result.force (l_curr_var.value, l_curr_var.key)
147 end
148 i := i + 1
149 l_ptr := i_th_environ (i)
150 end
151 ensure
152 result_attached: Result /= Void
153 end
154
155 feature -- Status
156
157 return_code: INTEGER
158 -- Status code set by last call to `system' or `put'
159
160 feature -- Status setting
161
162 change_working_directory (path: STRING)
163 -- Set the current directory to `path'
164 local
165 l_ptr: MANAGED_POINTER
166 do
167 l_ptr := file_info.file_name_to_pointer (path, Void)
168 return_code := eif_chdir (l_ptr.item)
169 end
170
171 put (value, key: STRING)
172 -- Set the environment variable `key' to `value'.
173 require
174 key_exists: key /= Void
175 key_meaningful: not key.is_empty
176 not_key_has_null_character: not key.has ('%U')
177 value_exists: value /= Void
178 not_value_has_null_character: not value.has ('%U')
179 local
180 l_env: STRING
181 l_c_env: C_STRING
182 do
183 create l_env.make (value.count + key.count + 1)
184 l_env.append (key)
185 l_env.append_character ('=')
186 l_env.append (value)
187 create l_c_env.make (l_env)
188 environ.force (l_c_env, key)
189 return_code := eif_putenv (l_c_env.item)
190 ensure
191 variable_set: (return_code = 0) implies
192 ((get (key) ~ value.string) or else (value.is_empty and then (get (key) = Void)))
193 end
194
195 system (s: STRING)
196 -- Pass to the operating system a request to execute `s'.
197 -- If `s' is empty, use the default shell as command.
198 require
199 s_exists: s /= Void
200 local
201 l_cstr: C_STRING
202 do
203 if s.is_empty then
204 create l_cstr.make (default_shell)
205 else
206 create l_cstr.make (s)
207 end
208 return_code := system_call (l_cstr.item)
209 end
210
211 launch (s: STRING)
212 -- Pass to the operating system an asynchronous request to
213 -- execute `s'.
214 -- If `s' is empty, use the default shell as command.
215 require
216 s_not_void: s /= Void
217 local
218 l_cstr: C_STRING
219 do
220 if s.is_empty then
221 create l_cstr.make (default_shell)
222 else
223 create l_cstr.make (s)
224 end
225 asynchronous_system_call (l_cstr.item)
226 end
227
228 sleep (nanoseconds: INTEGER_64)
229 -- Suspend thread execution for interval specified in
230 -- `nanoseconds' (1 nanosecond = 10^(-9) second).
231 require
232 non_negative_nanoseconds: nanoseconds >= 0
233 do
234 eif_sleep (nanoseconds)
235 end
236
237 feature {NONE} -- Implementation
238
239 environ: HASH_TABLE [C_STRING, STRING]
240 -- Environment variable memory set by current execution,
241 -- indexed by environment variable name. Needed otherwise
242 -- we would corrupt memory after freeing memory used by
243 -- C_STRING instance since not referenced anywhere else.
244 once
245 create Result.make (10)
246 end
247
248 i_th_environ (i: INTEGER): POINTER
249 -- Environment variable at `i'-th position of `eif_environ'.
250 require
251 i_valid: i >=0
252 external
253 "C inline use <string.h>"
254 alias
255 "[
256 if (eif_environ) {
257 #ifdef EIF_WINDOWS
258 LPSTR vars = (LPSTR) eif_environ;
259 int cnt = 0;
260 for (; *vars; vars++) {
261 if ($i==cnt) return vars;
262 while (*vars) { vars++; }
263 cnt++;
264 }
265 return NULL;
266 #else
267 return ((char **)eif_environ)[$i];
268 #endif
269 } else {
270 return NULL;
271 }
272 ]"
273 end
274
275 separated_variables (a_var: STRING): detachable TUPLE [value: STRING; key: STRING]
276 -- Given an environment variable `a_var' in form of "key=value",
277 -- return separated key and value.
278 -- Return Void if `a_var' is in incorrect format.
279 require
280 a_var_attached: a_var /= Void
281 local
282 i, j: INTEGER
283 done: BOOLEAN
284 do
285 j := a_var.count
286 from
287 i := 1
288 until
289 i > j or done
290 loop
291 if a_var.item (i) = '=' then
292 done := True
293 else
294 i := i + 1
295 end
296 end
297 if i > 1 and then i < j then
298 Result := [a_var.substring (i + 1, j), a_var.substring (1, i - 1)]
299 end
300 end
301
302 file_info: FILE_INFO
303 -- Platform specific helper of filenames.
304 once
305 create Result.make
306 end
307
308 feature {NONE} -- External
309
310 eif_dir_current (a_ptr: POINTER; a_count: INTEGER): INTEGER
311 -- Store platform specific path of current working directory in `a_ptr' with `a_count' bytes. If
312 -- there is a need for more bytes than `a_count', or if `a_ptr' is the default_pointer, nothing is done with `a_ptr'.
313 -- We always return the number of bytes required including the null-terminating character, or -1 on error.
314 external
315 "C signature (EIF_FILENAME, EIF_INTEGER): EIF_INTEGER use %"eif_dir.h%""
316 end
317
318 eif_getenv (s: POINTER): POINTER
319 -- Value of environment variable `s'
320 external
321 "C use <stdlib.h>"
322 alias
323 "getenv"
324 end
325
326 eif_putenv (v: POINTER): INTEGER
327 -- Set `v' in environment.
328 external
329 "C use <stdlib.h>"
330 alias
331 "putenv"
332 end
333
334 eif_chdir (path: POINTER): INTEGER
335 -- Set the current directory to `path'
336 external
337 "C signature (EIF_FILENAME): EIF_INTEGER use %"eif_dir.h%""
338 end
339
340 system_call (s: POINTER): INTEGER
341 -- Pass to the operating system a request to execute `s'.
342 external
343 "C blocking use %"eif_misc.h%""
344 alias
345 "eif_system"
346 end
347
348 asynchronous_system_call (s: POINTER)
349 -- Pass to the operating system an asynchronous request to execute `s'.
350 external
351 "C blocking use %"eif_misc.h%""
352 alias
353 "eif_system_asynchronous"
354 end
355
356 eif_home_directory_name_ptr (a_ptr: POINTER; a_count: INTEGER): INTEGER
357 -- Stored directory name corresponding to the home directory in `a_ptr' with `a_count' bytes. If
358 -- there is a need for more bytes than `a_count', or if `a_ptr' is the default_pointer, nothing is done with `a_ptr'.
359 -- We always return the number of bytes required including the null-terminating character.
360 external
361 "C signature (EIF_FILENAME, EIF_INTEGER): EIF_INTEGER use %"eif_path_name.h%""
362 end
363
364 eif_sleep (nanoseconds: INTEGER_64)
365 -- Suspend thread execution for interval specified in
366 -- `nanoseconds' (1 nanosecond = 10^(-9) second).
367 require
368 non_negative_nanoseconds: nanoseconds >= 0
369 external
370 "C blocking use %"eif_misc.h%""
371 end
372
373 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23