lua5 : IndexToc

TEKlib / Lua5 module reference manual

By Timm S. Müller - Copyright © 2005 TEK neoscientists. All rights reserved.


lua5 : IntroductionToc

Introduction

The Lua5 module implements the Lua (v5) scripting language interpreter as a standalone TEKlib module, and it extends the regular set of Lua's inbuilt libraries with a 'tek' library. The 'tek' library operates in TEKlib's virtual filesystem namespace, adding much-needed functionality (such as file and directory examination and argument parsing), while it retains the semantics of the 'io' and 'os' libraries to a large extent. On many platforms the 'io' and 'os' libraries are also available (for compatibility with existing scripts), but the use of the 'tek' library is encouraged for the sake of a powerful, yet fully portable OS and filesystem abstraction.
This document covers the pecularities of the TEKlib version and the additions that were made in the 'tek' library. For a general description and language reference see http://www.lua.org/ . For an introduction to the TEKlib filesystem naming conventions, see the introduction to the IO module reference manual.

lua5 : Module openToc

SYNOPSIS
L = TOpenModule("lua5", version, tags)
lua_State*      TSTRPTR TUINT16  TTAGITEM*

TAGS
None currently defined

FUNCTION
Create a new Lua interpreter instance.
When the Lua interpreter instance is no longer needed, it must be passed to exec:TCloseModule for freeing all associated memory. Quite untypically, the interpreter instance returned by exec:TOpenModule is different from the module base, and the macro LUABASE must be applied to the instance pointer before passing it to exec:TCloseModule, as shown below:
lua_State *L;

/* open Lua interpreter */
L = TOpenModule("lua5", 0, TNULL);
if (L)
{
    /* use interpreter here */

    /* close Lua intepreter */
    TCloseModule(LUABASE(L))
}

SEE ALSO


lua5 : File I/O functionsToc

tek.close ([file])

Equivalent to file:close. Without a file, closes the default output file.

tek.flush ()

Equivalent to file:flush over the default output file.

tek.input ([file])

When called with a file name, it opens the named file, and sets its handle as the default input file. When called with a file handle, it simply sets that file handle as the default input file. When called without parameters, it returns the current default input file.
In case of errors this function raises the error, instead of returning an error code.

tek.lines ([filename])

Opens the given file name in read mode and returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction
for line in tek.lines(filename) do ... end
will iterate over all lines of the file. When the iterator function detects the end of file, it returns nil (to finish the loop) and automatically closes the file.
The call tek.lines() (without a file name) is equivalent to tek.input():lines(), that is, it iterates over the lines of the default input file.

tek.open (filename [, mode])

This function opens a file, in the mode specified in the string mode. It returns a new file handle, or, in case of errors, nil plus an error message.
The mode string can be any of the following:
The mode string may also have a b at the end, which is provided for compatibility with standard C libraries.

tek.output ([file])

Similar to tek.input, but operates over the default output file.

tek.read (format1, ...)

Equivalent to tek.input():read.

tek.type (obj)

Checks whether obj is a valid file handle. Returns the string "file" if obj is an open file handle, "closed file" if obj is a closed file handle, and nil if obj is not a file handle.

tek.write (value1, ...)

Equivalent to tek.output():write.

lua5 : File class methodsToc

file:close ()

Closes file.

file:examine ()

Examines a file's attributes and returns its type, size and modification date to the caller. This is similar to lock:examine, only that type is always 1 (a regular file). The size returned is in bytes, the date is a continuous number representing a day in the Julian system; it can be used in arithmetics and converted using tek.date().

file:flush ()

Saves any written data to file.

file:lines ()

Returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction
for line in file:lines() do ... end
will iterate over all lines of the file. (Unlike tek.lines, this function does not close the file when the loop ends.)

file:read (format1, ...)

Reads the file file, according to the given formats, which specify what to read. For each format, the function returns a string (or a number) with the characters read, or nil if it cannot read data with the specified format. When called without formats, it uses a default format that reads the entire next line (see below).
The available formats are

file:seek ([whence] [, offset])

Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence, as follows:
In case of success, function seek returns the final file position, measured in bytes from the beginning of the file. If this function fails, it returns nil, plus a string describing the error.
The default value for whence is "cur", and for offset is 0. Therefore, the call file:seek returns the current file position, without changing it; the call file:seek("set") sets the position to the beginning of the file (and returns 0); and the call file:seek("end") sets the position to the end of the file, and returns its size.

file:write (value1, ...)

Writes the value of each of its arguments to the filehandle file. The arguments must be strings or numbers. To write other values, use tostring or string.format before write.

lua5 : Filesystem manipulation and naming functionsToc

tek.addpart (part1, part2)

Adds part2, which can be a path part or a file, to the path part1. The result (which will be in accordance to TEKlib's filesystem naming conventions) is returned to the caller, or nil if the either of the parts (or the combination thereof) is invalid.

tek.delete (filename)

Deletes the file of the given name. If the addressed object is a directory, it must be empty for this function to succeed. Returns a boolean to the caller, indicating whether the operation was successful.

tek.makename (path)

Tries to convert a path or path component in host-style naming conventions to a fully-qualified path/name in TEKlib's path naming conventions. The resulting name is returned to the caller, or nil in case of an error.
The name resolution request will be sent to the handler that is responsible for the current task's current directory. If the current directory is on a device that does not implement this kind of action, nil is returned.

tek.mount (devname, handlername [, initstring])

This function tries to mount the named device using the specified handler. Optionally, an initstring is passed to the handler for initialization. Returns a boolean to the caller, which indicates success.

tek.rename (oldname, newname)

Renames the file or directory specified by oldname to newname. If an object of the new name already exists then an error is returned.
Both oldname and newname can contain path parts, in which case the object will be moved across directories on the same filesystem. Attempts to rename an object across different filesystems will fail.
Returns a boolean to the caller, indicating whether the operation was successful.

lua5 : Locking functionsToc

tek.cdlock (lock)

Change the current task's current directory to the lock specified. If successful, the lock on the previous current directory will be returned to the caller. If the current task has no current directory, or if an error occured, nil is returned.

tek.lock (filename [, exclusive])

Obtains a lock on a filesystem object (file or directory). The lock, if successful, is returned to the caller, otherwise nil.
By default, the locking mode is shared, which means that mutiple accessors can access the object at the same time. If the second argument is true, then an exclusive (or read/write) lock is attempted, which locks out other accessors from simultaneous use of the object.
Warning: Not all operating systems actually implement all locking modes at this time. Locks aren't currently enforced under POSIX.

tek.makedir (name)

Create a new directory of the given name. If successful, a shared lock to the newly created directory will be returned to the caller. Otherwise, the return value is nil.

tek.unlock (lock)

Releases a lock which was previously obtained with tek.lock.

lua5 : Locking class methodsToc

lock:unlock ()

Releases the lock.

lock:assign (devicename)

Assigns a logical device name to the specified lock. devicename must be without a trailing colon. A boolean is returned to the caller, indicating whether the assign succeeded. If successful, the lock has been relinquished and is no longer valid.

lock:examine ()

Examines a lock's attributes and returns its type, size and modification date to the caller. This is similar to file:examine, only that type can be 1 (a regular file), 2 (a directory), or 0 (unknown). The size returned is in bytes, the date is a continuous number representing a day in the Julian system; it can be used in arithmetics and converted using tek.date().

lock:exnext ()

If the lock refers to a directory, then this method examines the directory content's next entry and returns its name, type, size and modification date to the caller. Returns nil if the end of the directory is reached.

lock:nameof ()

Returns the name of the object which the lock refers to.

lua5 : OS functionsToc

tek.clock ()

Returns the number of seconds that has passed since some starting point in time, which is specific to the underlying operating system; it can be the beginning of the operating system's "epoch", since the application was started, or some other entity which is defined outside the scope of this library.

tek.time ([table])

Returns the current date/time when called without arguments, or a date/time representing the date and time specified by the given table. This table must have fields year, month, and day, and may have fields hour, min, sec (for a description of these fields, see the tek.date function).
The returned value represents a day in the Julian date system; it can be used in arithmetics and converted using tek.date().

tek.date ([format [, time]])

Returns a string or a table containing date and time, formatted according to the given string format.
If the time argument is present, this is the date/time to be formatted (see the tek.time function for a description of this value). Otherwise, date formats the current date/time.
If format starts with !, then the date/time is formatted in Coordinated Universal Time. After that optional character, if format is *t, then date returns a table with the following fields: year (four digits), month (1--12), day (1--31), hour (0--23), min (0--59), sec (0--61), wday (weekday, Sunday is 1), yday (day of the year).
If format is not *t, then date returns the date/time as a string, formatted according to the same rules as the C function strftime. (Note, however, that only the "%c" format specifier is currently supported.)
When called without arguments, date returns a reasonable date and time representation that depends on the host system and on the current locale (that is, tek.date() is equivalent to tek.date("%c")).

tek.readargs (template, ...)

Parse an argument list according to a template, and if the arguments match the given template, return them to the caller in the order specified by the template. If the argument list do not match the template, the return value is nil.
Arguments in the template are separated with commas. Each argument consists of a name, an optional alias, and an optional set of qualifiers. Example:
-s=SOURCE/A/M,-d=DEST/A/K
Unless qualified with /A, an argument is optional and does not necessarily have to appear in the argument list. If an argument is present, it will be in the list of return values at the same position as specified in the template, otherwise the return value at its position is nil. An argument without qualifiers represents a optional string argument. Qualifiers:
Example:
source, dest = tek.readargs("-s=SOURCE/A/M,-d=DEST/A/K", unpack(arg))
And this is how it would work:
SOURCE one two three DEST foo valid; returns { "one", "two", "three" }, "foo"
DEST foo -s one valid; returns { "one" }, "foo"
DEST foo rejected; source missing
one two three foo rejected; keyword missing
one two dest foo valid; returns { "one", "two" }, "foo"
-s one two -d three four valid; returns { "one", "two", "four" }, "three"

lua5 : Table of contents


Generated Sat Oct 8 02:07:13 2005 from lua5.doc