Name | Executed | Routines | % | Executed | Lines | % | Unexecuted |
/home/matt/eu/rds/include/std/os.e | 4 | 7 | 57.14% | 27 | 38 | 71.05% | 11 |
Routine | Executed | Lines | Unexecuted | |
uname() | 0 | 6 | 0.00% | 6 |
is_win_nt() | 0 | 3 | 0.00% | 3 |
instance() | 0 | 2 | 0.00% | 2 |
get_pid() | 3 | 3 | 100.00% | 0 |
setenv() | 2 | 2 | 100.00% | 0 |
sleep() | 4 | 4 | 100.00% | 0 |
unsetenv() | 2 | 2 | 100.00% | 0 |
# | Executed | |
1 | namespace os | |
2 | ||
3 | --**** | |
4 | -- == Operating System Helpers | |
5 | -- | |
6 | -- < | |
7 | -- | |
8 | ||
9 | include std/sequence.e | |
10 | include std/text.e | |
11 | include std/machine.e | |
12 | ||
13 | include std/dll.e | |
14 | ||
15 | 11 | ifdef UNIX then |
16 | ||
17 | 11 | public constant CMD_SWITCHES = "-" |
18 | ||
19 | elsifdef WINDOWS then | |
20 | ||
21 | public constant CMD_SWITCHES = "-/" | |
22 | ||
23 | end ifdef | |
24 | ||
25 | constant | |
26 | 11 | M_SLEEP = 64, |
27 | 11 | M_SET_ENV = 73, |
28 | 11 | M_UNSET_ENV = 74 |
29 | ||
30 | --**** | |
31 | -- === Operating System Constants | |
32 | -- | |
33 | ||
34 | public enum | |
35 | 11 | WIN32 = 2, |
36 | 11 | LINUX, |
37 | 11 | OSX, |
38 | 11 | SUNOS, |
39 | 11 | OPENBSD, |
40 | 11 | NETBSD, |
41 | 11 | FREEBSD |
42 | ||
43 | --**** | |
44 | -- These constants are returned by the [[:platform]] function. | |
45 | -- | |
46 | -- * ##WIN32## ~-- Host operating system is Windows | |
47 | -- * ##LINUX## ~-- Host operating system is Linux | |
48 | -- * ##FREEBSD## ~-- Host operating system is FreeBSD | |
49 | -- * ##OSX## ~-- Host operating system is Mac OS X | |
50 | -- * ##SUNOS## ~-- Host operating system is Sun's OpenSolaris | |
51 | -- * ##OPENBSD## ~-- Host operating system is OpenBSD | |
52 | -- * ##NETBSD## ~-- Host operating system is NetBSD | |
53 | -- | |
54 | -- Note: | |
55 | -- Via the [[:platform]] call, there is no way to determine if you are on Linux | |
56 | -- or FreeBSD. This was done to provide a generic UNIX return value for | |
57 | -- [[:platform]]. | |
58 | -- | |
59 | -- In most situations you are better off to test the host platform by using | |
60 | -- the [[:ifdef statement]]. It is both more precise and faster. | |
61 | -- | |
62 | ||
63 | --**** | |
64 | -- === Environment. | |
65 | ||
66 | 11 | constant M_INSTANCE = 55 |
67 | ||
68 | --** | |
69 | -- Return ##hInstance## on //Windows// and Process ID (pid) on //Unix//. | |
70 | -- | |
71 | -- Comments: | |
72 | -- On //Windows// the ##hInstance## can be passed around to various | |
73 | -- //Windows// routines. | |
74 | ||
75 | 0 | |
76 | 0 | return machine_func(M_INSTANCE, 0) |
77 | end function | |
78 | ||
79 | 11 | ifdef WINDOWS then |
80 | atom cur_pid = -1 | |
81 | end ifdef | |
82 | ||
83 | --** | |
84 | -- Return the ID of the current Process (pid) | |
85 | -- | |
86 | -- Returns: | |
87 | -- An atom: The current process' id. | |
88 | -- | |
89 | -- Example: | |
90 | -- | |
91 | -- mypid = get_pid() | |
92 | -- | |
93 | ||
94 | 1 | |
95 | 1 | ifdef UNIX then |
96 | 1 | return machine_func(M_INSTANCE, 0) |
97 | elsifdef WINDOWS then | |
98 | if cur_pid = -1 then | |
99 | cur_pid = define_c_func(open_dll("kernel32.dll"), "GetCurrentProcessId", {}, C_DWORD) | |
100 | if cur_pid >= 0 then | |
101 | cur_pid = c_func(cur_pid, {}) | |
102 | end if | |
103 | end if | |
104 | ||
105 | return cur_pid | |
106 | end ifdef | |
107 | end function | |
108 | ||
109 | 11 | ifdef WIN32 then |
110 | constant M_UNAME = define_c_func(open_dll("kernel32.dll"), "GetVersionExA", {C_POINTER}, C_INT) | |
111 | elsifdef UNIX then | |
112 | 11 | constant M_UNAME = 76 |
113 | end ifdef | |
114 | ||
115 | --** | |
116 | -- Retrieves the name of the host OS. | |
117 | -- | |
118 | -- Returns: | |
119 | -- A **sequence**, starting with the OS name. If identification fails, returns | |
120 | -- an OS name of UNKNOWN. Extra information depends on the OS. | |
121 | -- | |
122 | -- On Unix, returns the same information as the uname() syscall in the same | |
123 | -- order as the struct utsname. This information is: | |
124 | -- OS Name/Kernel Name | |
125 | -- Local Hostname | |
126 | -- Kernel Version/Kernel Release | |
127 | -- Kernel Specific Version information (This is usually the date that the | |
128 | -- kernel was compiled on and the name of the host that performed the compiling.) | |
129 | -- Architecture Name (Usually a string of i386 vs x86_64 vs ARM vs etc) | |
130 | -- | |
131 | -- On Windows, returns the following in order: | |
132 | -- Windows Platform (out of WinCE, Win9x, WinNT, Win32s, or Unknown Windows) | |
133 | -- Name of Windows OS (Windows 3.1, Win95, WinXP, etc) | |
134 | -- Platform Number | |
135 | -- Build Number | |
136 | -- Minor OS version number | |
137 | -- Major OS version number | |
138 | -- | |
139 | -- On UNKNOWN, returns an OS name of "UNKNOWN". No other information is returned. | |
140 | -- | |
141 | -- Returns a string of "" if an internal error has occured. | |
142 | -- | |
143 | -- Comments: | |
144 | -- On Unix, M_UNAME is defined as a machine_func() and this is passed to the C | |
145 | -- backend. If the M_UNAME call fails, the raw machine_func() returns -1. | |
146 | -- On non Unix platforms, calling the machine_func() directly returns 0. | |
147 | ||
148 | 0 | |
149 | 0 | ifdef WIN32 then |
150 | atom buf | |
151 | sequence sbuf | |
152 | integer maj, mine, build, plat | |
153 | buf = allocate(148) | |
154 | poke4(buf, 148) | |
155 | if c_func(M_UNAME, {buf}) then | |
156 | maj = peek4u(buf+4) | |
157 | mine = peek4u(buf+8) | |
158 | build = peek4u(buf+12) | |
159 | plat = peek4u(buf+16) | |
160 | sbuf = {} | |
161 | if plat = 0 then | |
162 | sbuf = append(sbuf, "Win32s") | |
163 | sbuf = append(sbuf, sprintf("Windows %d.%d", {maj,mine})) | |
164 | elsif plat = 1 then | |
165 | sbuf = append(sbuf, "Win9x") | |
166 | if mine = 0 then | |
167 | sbuf = append(sbuf, "Win95") | |
168 | elsif mine = 10 then | |
169 | sbuf = append(sbuf, "Win98") | |
170 | elsif mine = 90 then | |
171 | sbuf = append(sbuf, "WinME") | |
172 | else | |
173 | sbuf = append(sbuf, "Unknown") | |
174 | end if | |
175 | elsif plat = 2 then | |
176 | sbuf = append(sbuf, "WinNT") | |
177 | if maj = 6 and mine = 1 then | |
178 | sbuf = append(sbuf, "Windows7") | |
179 | elsif maj = 6 and mine = 0 then | |
180 | sbuf = append(sbuf, "Vista") | |
181 | elsif maj = 5 and mine = 1 or 2 then | |
182 | sbuf = append(sbuf, "WinXP") | |
183 | elsif maj = 5 and mine = 0 then | |
184 | sbuf = append(sbuf, "Win2K") | |
185 | elsif maj = 4 and mine = 0 then | |
186 | sbuf = append(sbuf, "WinNT 4.0") | |
187 | elsif maj = 3 and mine = 51 then | |
188 | sbuf = append(sbuf, "WinNT 3.51") | |
189 | elsif maj = 3 and mine = 50 then --is it 50 or 5? | |
190 | sbuf = append(sbuf, "WinNT 3.5") | |
191 | elsif maj = 3 and mine = 1 then | |
192 | sbuf = append(sbuf, "WinNT 3.1") | |
193 | else | |
194 | sbuf = append(sbuf, sprintf("WinNT %d.%d", {maj,mine})) | |
195 | end if | |
196 | elsif plat = 3 then | |
197 | sbuf = append(sbuf, "WinCE") | |
198 | sbuf = append(sbuf, sprintf("WinCE %d.%d", {maj,mine})) | |
199 | else | |
200 | sbuf = append(sbuf, "Unknown Windows") | |
201 | sbuf = append(sbuf, sprintf("Version %d.%d", {maj,mine})) | |
202 | end if | |
203 | sbuf = append(sbuf, peek_string(buf+20)) | |
204 | sbuf &= {plat, build, mine, maj} | |
205 | return sbuf | |
206 | else | |
207 | return {} | |
208 | end if | |
209 | elsifdef UNIX then | |
210 | 0 | object o = machine_func(M_UNAME, {}) |
211 | 0 | if atom(o) then |
212 | 0 | return {} |
213 | else | |
214 | 0 | return o |
215 | end if | |
216 | elsedef | |
217 | return {"UNKNOWN"} --TODO | |
218 | end ifdef | |
219 | end function | |
220 | ||
221 | --** | |
222 | -- Decides whether the host system is a newer Windows version (NT/2K/XP/Vista). | |
223 | -- | |
224 | -- Returns: | |
225 | -- An **integer**, 1 if host system is a newer Windows (NT/2K/XP/Vista), else 0. | |
226 | ||
227 | 0 | |
228 | 0 | ifdef WIN32 then |
229 | sequence s | |
230 | s = uname() | |
231 | return equal(s[1], "WinNT") | |
232 | elsedef | |
233 | 0 | return -1 |
234 | end ifdef | |
235 | end function | |
236 | ||
237 | --**** | |
238 | -- Signature: | |
239 | -- | |
240 | -- | |
241 | -- Description: | |
242 | -- Return the value of an environment variable. | |
243 | -- | |
244 | -- Parameters: | |
245 | -- # ##var_name## : a string, the name of the variable being queried. | |
246 | -- | |
247 | -- Returns: | |
248 | -- An **object**, -1 if the variable does not exist, else a sequence holding its value. | |
249 | -- | |
250 | -- Comments: | |
251 | -- | |
252 | -- Both the argument and the return value, may, or may not be, case sensitive. You might need to test this on your own system. | |
253 | -- | |
254 | -- Example: | |
255 | -- | |
256 | -- e = getenv("EUDIR") | |
257 | -- -- e will be "C:\EUPHORIA" -- or perhaps D:, E: etc. | |
258 | -- | |
259 | -- | |
260 | -- See Also: | |
261 | -- [[:setenv]], [[:command_line]] | |
262 | ||
263 | --** | |
264 | -- Set an environment variable. | |
265 | -- | |
266 | -- Parameters: | |
267 | -- | |
268 | -- # ##name## : a string, the environment variable name | |
269 | -- # ##val## : a string, the value to set to | |
270 | -- # ##overwrite## : an integer, nonzero to overwrite an existing variable, 0 to disallow this. | |
271 | -- | |
272 | -- Example 1: | |
273 | -- | |
274 | -- ? setenv("NAME", "John Doe") | |
275 | -- ? setenv("NAME", "Jane Doe") | |
276 | -- ? setenv("NAME", "Jim Doe", 0) | |
277 | -- | |
278 | -- | |
279 | -- See Also: | |
280 | -- [[:getenv]], [[:unsetenv]] | |
281 | ||
282 | 4 | |
283 | 4 | return machine_func(M_SET_ENV, {name, val, overwrite}) |
284 | end function | |
285 | ||
286 | --** | |
287 | -- Unset an environment variable | |
288 | -- | |
289 | -- Parameters: | |
290 | -- # ##name## : name of environment variable to unset | |
291 | -- | |
292 | -- Example 1: | |
293 | -- | |
294 | -- ? unsetenv("NAME") | |
295 | -- | |
296 | -- | |
297 | -- See Also: | |
298 | -- [[:setenv]], [[:getenv]] | |
299 | ||
300 | 1 | |
301 | 1 | return machine_func(M_UNSET_ENV, {env}) |
302 | end function | |
303 | ||
304 | --**** | |
305 | -- Signature: | |
306 | -- | |
307 | -- | |
308 | -- Description: | |
309 | -- Indicates the platform that the program is being executed on. | |
310 | -- | |
311 | -- Returns: | |
312 | -- An **integer**, | |
313 | -- | |
314 | -- public constant | |
315 | -- WIN32, | |
316 | -- LINUX, | |
317 | -- FREEBSD, | |
318 | -- OSX, | |
319 | -- SUNOS, | |
320 | -- OPENBSD, | |
321 | -- NETBSD, | |
322 | -- FREEBSD | |
323 | -- | |
324 | -- | |
325 | -- Comments: | |
326 | -- The [[:ifdef statement]] is much more versatile and in most cases supersedes ##platform##(). | |
327 | -- | |
328 | -- ##platform##() used to be the way to execute different code depending on which platform the program | |
329 | -- is running on. Additional platforms will be added as Euphoria is ported to new machines and | |
330 | -- operating environments. | |
331 | -- | |
332 | -- Example 1: | |
333 | -- | |
334 | -- ifdef WIN32 then | |
335 | -- -- call system Beep routine | |
336 | -- err = c_func(Beep, {0,0}) | |
337 | -- elsedef | |
338 | -- -- do nothing (Linux/FreeBSD) | |
339 | -- end if | |
340 | -- | |
341 | -- | |
342 | -- See Also: | |
343 | -- [[:Platform-Specific Issues]], [[:ifdef statement]] | |
344 | ||
345 | ||
346 | --**** | |
347 | -- === Interacting with the OS | |
348 | -- | |
349 | ||
350 | --**** | |
351 | -- Signature: | |
352 | -- | |
353 | -- | |
354 | -- Description: | |
355 | -- Pass a command string to the operating system command interpreter. | |
356 | -- | |
357 | -- Parameters: | |
358 | -- # ##command## : a string to be passed to the shell | |
359 | -- # ##mode## : an integer, indicating the manner in which to return from the call. | |
360 | -- | |
361 | -- Errors: | |
362 | -- ##command## should not exceed 1,024 characters. | |
363 | -- | |
364 | -- Comments: | |
365 | -- Allowable values for ##mode## are: | |
366 | -- * 0: the previous graphics mode is restored and the screen is cleared. | |
367 | -- * 1: a beep sound will be made and the program will wait for the user to press a key before the previous graphics mode is restored. | |
368 | -- * 2: the graphics mode is not restored and the screen is not cleared. | |
369 | -- | |
370 | -- ##mode## = 2 should only be used when it is known that the command executed by ##system##() will not change the graphics mode. | |
371 | -- | |
372 | -- You can use Euphoria as a sophisticated "batch" (.bat) language by making calls to ##system##() and ##system_exec##(). | |
373 | -- | |
374 | -- ##system##() will start a new command shell. | |
375 | -- | |
376 | -- ##system##() allows you to use command-line redirection of standard input and output in | |
377 | -- ##command##. | |
378 | -- | |
379 | -- Example 1: | |
380 | -- | |
381 | -- system("copy temp.txt a:\\temp.bak", 2) | |
382 | -- -- note use of double backslash in literal string to get | |
383 | -- -- single backslash | |
384 | -- | |
385 | -- | |
386 | -- Example 2: | |
387 | -- | |
388 | -- system("eui \\test\\myprog.ex < indata > outdata", 2) | |
389 | -- -- executes myprog by redirecting standard input and | |
390 | -- -- standard output | |
391 | -- | |
392 | -- | |
393 | -- See Also: | |
394 | -- [[:system_exec]], [[:command_line]], [[:current_dir]], [[:getenv]] | |
395 | -- | |
396 | ||
397 | --**** | |
398 | -- Signature: | |
399 | -- | |
400 | -- | |
401 | -- Description: | |
402 | -- Try to run the a shell executable command | |
403 | -- | |
404 | -- Parameters: | |
405 | -- # ##command## : a string to be passed to the shell, representing an executable command | |
406 | -- # ##mode## : an integer, indicating the manner in which to return from the call. | |
407 | -- | |
408 | -- Returns: | |
409 | -- An **integer**, basically the exit/return code from the called process. | |
410 | -- | |
411 | -- Errors: | |
412 | -- ##command## should not exceed 1,024 characters. | |
413 | -- | |
414 | -- Comments: | |
415 | -- | |
416 | -- Allowable values for ##mode## are: | |
417 | -- * 0 ~-- the previous graphics mode is restored and the screen is cleared. | |
418 | -- * 1 ~-- a beep sound will be made and the program will wait for the user to press a key before the previous graphics mode is restored. | |
419 | -- * 2 ~-- the graphics mode is not restored and the screen is not cleared. | |
420 | -- | |
421 | -- If it is not possible to run the program, ##system_exec##() will return -1. | |
422 | -- | |
423 | -- On //WIN32//, ##system_exec##() will only run .exe and .com programs. | |
424 | -- To run .bat files, or built-in shell commands, you need [[:system]](). Some commands, | |
425 | -- such as DEL, are not programs, they are actually built-in to the command interpreter. | |
426 | -- | |
427 | -- On //WIN32//, ##system_exec##() does not allow the use of command-line redirection in ##command##. | |
428 | -- Nor does it allow you to quote strings that contain blanks, such as file names. | |
429 | -- | |
430 | -- exit codes from Windows programs are normally in the range 0 to 255, with 0 indicating "success". | |
431 | -- | |
432 | -- You can run a Euphoria program using ##system_exec##(). A Euphoria program can return an exit code using [[:abort]](). | |
433 | -- | |
434 | -- ##system_exec##() does not start a new command shell. | |
435 | -- | |
436 | -- Example 1: | |
437 | -- | |
438 | -- integer exit_code | |
439 | -- exit_code = system_exec("xcopy temp1.dat temp2.dat", 2) | |
440 | -- | |
441 | -- if exit_code = -1 then | |
442 | -- puts(2, "\n couldn't run xcopy.exe\n") | |
443 | -- elsif exit_code = 0 then | |
444 | -- puts(2, "\n xcopy succeeded\n") | |
445 | -- else | |
446 | -- printf(2, "\n xcopy failed with code %d\n", exit_code) | |
447 | -- end if | |
448 | -- | |
449 | -- | |
450 | -- Example 2: | |
451 | -- | |
452 | -- -- executes myprog with two file names as arguments | |
453 | -- if system_exec("eui \\test\\myprog.ex indata outdata", 2) then | |
454 | -- puts(2, "failure!\n") | |
455 | -- end if | |
456 | -- | |
457 | -- | |
458 | -- See Also: | |
459 | -- [[:system]], [[:abort]] | |
460 | -- | |
461 | ||
462 | --**** | |
463 | -- === Miscellaneous | |
464 | ||
465 | --** | |
466 | -- Suspend thread execution. for ##t## seconds. | |
467 | -- | |
468 | -- Parameters: | |
469 | -- # ##t## : an atom, the number of seconds for which to sleep. | |
470 | -- | |
471 | -- Comments: | |
472 | -- The operating system will suspend your process and schedule other processes. | |
473 | -- | |
474 | -- With multiple tasks, the whole program sleeps, not just the current task. To make | |
475 | -- just the current task sleep, you can call ##[[:task_schedule]]([[:task_self]](), {i, i})## | |
476 | -- and then execute [[:task_yield]](). Another option is to call [[:task_delay]](). | |
477 | -- | |
478 | -- Example: | |
479 | -- | |
480 | -- puts(1, "Waiting 15 seconds and a quarter...\n") | |
481 | -- sleep(15.25) | |
482 | -- puts(1, "Done.\n") | |
483 | -- | |
484 | -- | |
485 | -- See Also: | |
486 | -- [[:task_schedule]], [[:task_yield]], [[:task_delay]] | |
487 | ||
488 | 1 | |
489 | -- go to sleep for t seconds | |
490 | -- allowing (on WIN32 and Linux) other processes to run | |
491 | 1 | if t >= 0 then |
492 | 1 | machine_proc(M_SLEEP, t) |
493 | end if | |
494 | 1 | end procedure |
495 | ||
496 | --**** | |
497 | -- Signature: | |
498 | -- | |
499 | -- | |
500 | -- Description: | |
501 | -- Returns the list of include paths, in the order in which they are searched | |
502 | -- | |
503 | -- Parameters: | |
504 | -- # ##convert## : an integer, nonzero to include converted path entries | |
505 | -- that were not validated yet. | |
506 | -- | |
507 | -- Returns: | |
508 | -- A **sequence**, of strings, each holding a fully qualified include path. | |
509 | -- | |
510 | -- Comments: | |
511 | -- | |
512 | -- ##convert## is checked only under //Windows//. If a path has accented characters in it, then | |
513 | -- it may or may not be valid to convert those to the OEM code page. Setting ##convert## to a nonzero value | |
514 | -- will force conversion for path entries that have accents and which have not been checked to be valid yet. | |
515 | -- The extra entries, if any, are returned at the end of the returned sequence. | |
516 | -- | |
517 | -- The paths are ordered in the order they are searched: | |
518 | -- # current directory | |
519 | -- # configuration file, | |
520 | -- # command line switches, | |
521 | -- # EUINC | |
522 | -- # a default based on EUDIR. | |
523 | -- | |
524 | -- Example 1: | |
525 | -- | |
526 | -- sequence s = include_paths(0) | |
527 | -- -- s might contain | |
528 | -- { | |
529 | -- "/usr/euphoria/tests", | |
530 | -- "/usr/euphoria/include", | |
531 | -- "./include", | |
532 | -- "../include" | |
533 | -- } | |
534 | -- | |
535 | -- | |
536 | -- See Also: | |
537 | -- [[:eu.cfg]], [[:include]], [[:option_switches]] | |
538 |