1 # Copyright (C) 2005-2008, The Perl Foundation.
6 lib/luaos.pir - Lua Operating System Library
10 This library is implemented through table C<os>.
12 See "Lua 5.1 Reference Manual", section 5.8 "Operating System Facilities",
13 L<http://www.lua.org/manual/5.1/manual.html#5.8>.
21 .HLL 'Lua', 'lua_group'
22 .namespace [ 'Lua::os' ]
25 # print "init Lua OS\n"
27 .local pmc _lua__GLOBAL
28 _lua__GLOBAL = get_hll_global '_G'
34 _lua__GLOBAL[$P1] = _os
36 lua_register($P1, _os)
38 .const .Sub _os_clock = 'clock'
39 _os_clock.'setfenv'(_lua__GLOBAL)
43 .const .Sub _os_date = 'date'
44 _os_date.'setfenv'(_lua__GLOBAL)
48 .const .Sub _os_difftime = 'difftime'
49 _os_difftime.'setfenv'(_lua__GLOBAL)
51 _os[$P1] = _os_difftime
53 .const .Sub _os_execute = 'execute'
54 _os_execute.'setfenv'(_lua__GLOBAL)
56 _os[$P1] = _os_execute
58 .const .Sub _os_exit = 'exit'
59 _os_exit.'setfenv'(_lua__GLOBAL)
63 .const .Sub _os_getenv = 'getenv'
64 _os_getenv.'setfenv'(_lua__GLOBAL)
68 .const .Sub _os_remove = 'remove'
69 _os_remove.'setfenv'(_lua__GLOBAL)
73 .const .Sub _os_rename = 'rename'
74 _os_rename.'setfenv'(_lua__GLOBAL)
78 .const .Sub _os_setlocale = 'setlocale'
79 _os_setlocale.'setfenv'(_lua__GLOBAL)
81 _os[$P1] = _os_setlocale
83 .const .Sub _os_time = 'time'
84 _os_time.'setfenv'(_lua__GLOBAL)
88 .const .Sub _os_tmpname = 'tmpname'
89 _os_tmpname.'setfenv'(_lua__GLOBAL)
91 _os[$P1] = _os_tmpname
98 Returns an approximation of the amount in seconds of CPU time used by the
104 .param pmc extra :slurpy
112 =item C<os.date ([format [, time]])>
114 Returns a string or a table containing date and time, formatted according to
115 the given string C<format>.
117 If the C<time> argument is present, this is the time to be formatted (see
118 the C<os.time> function for a description of this value). Otherwise, C<date>
119 formats the current time.
121 If C<format> starts with C<‘!’>, then the date is formatted in Coordinated
122 Universal Time. After that optional character, if C<format> is C<*t>, then
123 C<date> returns a table with the following fields: C<year> (four digits),
124 C<month> (1-12), C<day> (1-31), C<hour> (0-23), C<min> (0-59), C<sec> (0-61),
125 C<wday> (weekday, Sunday is 1), C<yday> (day of the year), and C<isdst>
126 (daylight saving flag, a boolean).
128 If C<format> is not C<*t>, then C<date> returns the date as a string,
129 formatted according with the same rules as the C function C<strftime>.
131 When called without arguments, C<date> returns a reasonable date and time
132 representation that depends on the host system and on the current locale
133 (that is, C<os.date()> is equivalent to C<os.date("%c")>).
140 .param pmc format :optional
141 .param pmc time_ :optional
142 .param pmc extra :slurpy
145 $S1 = lua_optstring(1, format, '%c')
147 t = lua_optint(2, time_, $I0)
148 $S0 = substr $S1, 0, 1
149 unless $S0 == '!' goto L1
154 $P0 = decodelocaltime t
156 unless null $P0 goto L3
160 unless $S1 == '*t' goto L4
198 new $P2, 'LuaBoolean'
212 unless idx < $I1 goto L6
213 $S0 = substr $S1, idx, 1
214 if $S0 != '%' goto L7
216 if idx == $I1 goto L7
217 $S0 = substr $S1, idx, 1
219 $S0 = $P1.'strftime'($S2, $P0)
231 =item C<os.difftime (t2, t1)>
233 Returns the number of seconds from time C<t1> to time C<t2>. In Posix,
234 Windows, and some other systems, this value is exactly C<t2-t1>.
238 .sub 'difftime' :anon
239 .param pmc t2 :optional
240 .param pmc t1 :optional
241 .param pmc extra :slurpy
243 $I2 = lua_checknumber(1, t2)
244 $I1 = lua_optint(2, t1, 0)
252 =item C<os.execute ([command])>
254 This function is equivalent to the C function C<system>. It passes C<command>
255 to be executed by an operating system shell. It returns a status code, which
258 This function is equivalent to the C function C<system>. It passes C<command>
259 to be executed by an operating system shell. It returns a status code, which
260 is system-dependent. If C<command> is absent, then it returns nonzero if a
261 shell is available and zero otherwise.
266 .param pmc command :optional
267 .param pmc extra :slurpy
269 $S1 = lua_optstring(1, command, '')
270 unless $S1 == '' goto L1
283 =item C<os.exit ([code])>
285 Calls the C function C<exit>, with an optional C<code>, to terminate the host
286 program. The default value for C<code> is the success code.
291 .param pmc code :optional
292 .param pmc extra :slurpy
293 $I1 = lua_optint(1, code, 0)
298 =item C<os.getenv (varname)>
300 Returns the value of the process environment variable C<varname>, or B<nil>
301 if the variable is not defined.
306 .param pmc varname :optional
307 .param pmc extra :slurpy
309 $S1 = lua_checkstring(1, varname)
322 =item C<os.remove (filename)>
324 Deletes the file or directory with the given name. Directories must be empty
325 to be removed. If this function fails, it returns B<nil>, plus a string
326 describing the error.
331 .param pmc filename :optional
332 .param pmc extra :slurpy
334 $S1 = lua_checkstring(1, filename)
339 new res, 'LuaBoolean'
357 =item C<os.rename (oldname, newname)>
359 Renames file or directory named C<oldname> to C<newname>. If this function
360 fails, it returns B<nil>, plus a string describing the error.
365 .param pmc oldname :optional
366 .param pmc newname :optional
367 .param pmc extra :slurpy
369 $S1 = lua_checkstring(1, oldname)
371 $S2 = lua_checkstring(2, newname)
374 $P0.'rename'($S1, $S2)
375 new res, 'LuaBoolean'
393 =item C<os.setlocale (locale [, category])>
395 Sets the current locale of the program. C<locale> is a string specifying a
396 locale; C<category> is an optional string describing which category to change:
397 C<"all">, C<"collate">, C<"ctype">, C<"monetary">, C<"numeric">, or C<"time">;
398 the default category is C<"all">. The function returns the name of the new
399 locale, or B<nil> if the request cannot be honored.
403 .sub 'setlocale' :anon
404 .param pmc locale :optional
405 .param pmc category :optional
406 .param pmc extra :slurpy
408 $S1 = lua_optstring(1, locale)
409 $S2 = lua_optstring(2, category, 'all')
410 $I2 = lua_checkoption(2, $S2, 'all collate ctype monetary numeric time')
412 res = $P0.'setlocale'($I2, $S1)
417 =item C<os.time ([table])>
419 Returns the current time when called without arguments, or a time representing
420 the date and time specified by the given table. This table must have fields
421 C<year>, C<month>, and C<day>, and may have fields C<hour>, C<min>, C<sec>,
422 and C<isdst> (for a description of these fields, see the C<os.date> function).
424 The returned value is a number, whose meaning depends on your system. In
425 Posix, Windows, and some other systems, this number counts the number of
426 seconds since some given start time (the "epoch"). In other systems, the
427 meaning is not specified, and the number returned by C<time> can be used only
428 as an argument to C<date> and C<difftime>.
433 .param pmc table :optional
434 .param pmc extra :slurpy
436 if null table goto L1
437 $I0 = isa table, 'LuaNil'
445 lua_checktype(1, table, 'table')
446 new $P0, 'FixedIntegerArray'
448 $I0 = getfield(table, 'sec', 0)
449 set $P0[.TM_SEC], $I0
450 $I0 = getfield(table, 'min', 0)
451 set $P0[.TM_MIN], $I0
452 $I0 = getfield(table, 'hour', 12)
453 set $P0[.TM_HOUR], $I0
454 $I0 = getfield(table, 'day', -1)
455 set $P0[.TM_MDAY], $I0
456 $I0 = getfield(table, 'month', -1)
458 set $P0[.TM_MON], $I0
459 $I0 = getfield(table, 'year', -1)
461 set $P0[.TM_YEAR], $I0
462 $I0 = getboolfield(table, 'isdst')
463 set $P0[.TM_ISDST], $I0
465 res = $P1.'mktime'($P0)
469 .sub 'getfield' :anon
477 $P0 = $P0.'tonumber'()
478 $I0 = isa $P0, 'LuaNumber'
484 lua_error("field '", key, "' missing in date table")
491 .sub 'getboolfield' :anon
498 $I0 = isa $P0, 'LuaNil'
509 =item C<os.tmpname ()>
511 Returns a string with a file name that can be used for a temporary file.
512 The file must be explicitly opened before its use and explicitly removed
513 when no longer needed.
518 .param pmc extra :slurpy
521 $S0 = $P0.'tmpname'()
540 # vim: expandtab shiftwidth=4 ft=pir: