3.13. Loading and Executing Lush Program Files |
3.13.0. (load in [out [prompt]]) |
[DX] |
This function is used for loading a Lush source or binary file.
This function starts a new toplevel with the file named in as input file, and the file named out as the output file. Strings in and out represent filenames according to the general filename conventions explained with functions open-read and open-write .
Function load searches its input file in along the current search path. It also tries various suffixes usually associated with Lush program files (ie. .lsh .lshc as well as .sn .tl .snc .tlc for backward compatibility with TL3 and SN).
Function load then enters a loop which reads a Lush expression on the input file, evaluates this expression, and prints the result on the output file. This loop terminates when an end-of-file condition is detected or when function exit is called. Function load returns the full filename of the input file.
When the output file argument is omitted out , function load does not display the evaluation results. This is useful for silently loading a Lush program file. This file however can produce an output because it may contain calls to some printing function (eg. print .)
The optional string prompt indicates the prompts strings displayed by Lush when reading from the standard input. String prompt contains up to three prompt strings separated by the vertical bar character "|" . The first prompt string is used when Lush waits for a new expression. The second prompt string is used when Lush waits for the continuation of an already started expression. The last prompt string is used when the execution of the Lush expression reads information from the console (for instance using function read ).
Example:
;; The toplevel loop is implemented as (load "$stdin" "$stdout" "? | |> ")
3.13.1. ^L filename |
This macro-character expands into a call to function
load . If a filename filename
is not provided, the last file name used with macro-character
^L is used. This file name is saved in variable
last-loaded-file .
3.13.2. file-being-loaded |
[VAR] |
This variable is set when you load a Lush program file using function
load . It contains the full name of the file being loaded. It
provides a way to know the full filename of the Lush program file in
which this variable is checked.
3.13.3. (libload libname [opt]) |
[DE] (sysenv.lsh) |
This is the primary function used to load a library or a Lush file into
another Lush file or library. A typical Lush program file will begin
with a bunch of libload . The
difference between load and
libload is that libload
remembers when a particular file was previously libloaded, and only
loads it once. Libload also keeps track of the dependencies between Lush
program files (which file libloads which other file). This is the basis
of the automatic and transparent on-demand compilation mechanism as
described in detail below.
Function libload first searches the file libname . When libload is called from an existing Lush file, it first searches file libname relative to the location of the existing Lush file. If no such file exists, it uses function filepath to locate the file along the lush search path.
Function libload then updates its internal database of dependencies between Lush files. When function libload is called from an existing Lush file, it records the fact that this Lush file depends on the Lush file filename .
Function libload then tests whether it is necessary to load the file. It is necessary to load the file if
When this is the case, libload calls load to load the file, and stores a timestamp for the file in its internal database. Function libload always returns the full name of libname .
3.13.4. (libload-dependencies [fname]) |
[DE] |
3.13.5. (libload-add-dependency fname) |
[DE] |
This function also claims that fname has been successfully loaded. Calling libload on fname will not do anything unless someone modifies file fname after calling libload-add-dependency .
3.13.6. (find-file dirlist filename extlist) |
[DE] (sysenv.lsh) |
Example:
(find-file '("local" "lsh") "stdenv" '(".lshc" ".lsh"))
Note: This is somewhat similar to filepath but allows searching through any directory tree.
3.13.7. (save f s1 ... sn) |
[DM] (sysenv.lsh) |
3.13.8. (autoload f s1 ... sn) |
[DM] (sysenv.lsh) |
The first time one of these functions is called, its definition is loaded from file f , and the execution continues. Such autoloading functions often are defined by "stdenv.lsh" . They avoid loading many files while allowing the user to call those functions at any time.
3.14. Lisp-Style Input/Output |
3.14.0. Input |
3.14.0.0. (read) |
[DX] |
3.14.0.1. (read-string [spec]) |
[DX] |
The default spec for function read-string is "~\n\r\f\e" and means ``read anything until the end of line''.
3.14.0.2. (skip-char [spec]) |
[DX] |
Example:
This function reads the input stream and builds a list with each line, until it reaches an end-of-file character
(de read-lines() (let ((ans ())) (while (<> (skip-char "\n\r\f") "\e") (setq ans (nconc1 ans (read-string))) ) ) )
This function reads the input stream and builds a list with each word, until it reaches an end-of-file character
(de read-words() (let ((ans ())) (while (<> (skip-char) "\e") (setq ans (nconc1 ans (read-string "~ \t\n\r\f\e"))) ) ) )
3.14.0.3. (ask s) |
[DX] |
Example:
? (ask "Your answer") Your answer [y/n] ?
3.14.1. Output |
3.14.1.0. (prin l1 ... ln) |
[DX] |
3.14.1.1. (print l1 ... ln) |
[DX] |
Example:
? (print "hello" (+ 3 4) '(a b c d)) "hello" 7 (a b c d) = (a b c d)
3.14.1.2. (pprin l1 ... ln) |
[DE] (sysenv.lsh) |
3.14.1.3. (pprint l1 ... ln) |
[DE] (sysenv.lsh) |
Although the current implementation of pretty is a more complicated, a simple implementation of pretty would be :
(df pretty(f) (pprint (funcdef f)))
3.14.1.4. (printf format ... args ... ) |
[DX] |
Function printf is the only way to print the real contents of a character string, without adding surrounding double quotes or escaping the special characters.
Example:
? (printf "%5s%3d is equal to %6.3f\n" "sqrt" 2 (sqrt 2)) sqrt 2 is equal to 1.414 = ()
3.14.1.5. (tab [n]) |
[DX] |
? (tab) = 0
3.14.2. File Names |
3.14.2.0. (basename path [suffix]) |
[DX] |
This function makes no reference to the actual contents of the filesystem. It just chops the filename according to the file naming conventions.
3.14.2.1. (dirname path) |
[DX] |
This function makes no reference to the actual contents of the filesystem. It just chops the filename according to the file naming conventions.
3.14.2.2. (concat-fname [filename1] filename2) |
[DX] |
This function makes no reference to the actual contents of the filesystem. It just concatenates the filenames according to the file naming conventions. Nevertheless, if the resulting filename refers to an actual directory, a directory separator / is appended to the returned filename.
Examples:
? (concat-fname "..") = "/home/leonb/lush"
? (concat-fname (getenv "HOME") "myfile.lsh") = "/home/leonb/myfile.lsh"
3.14.2.3. (relative-fname dirname filename) |
[DX] |
Examples:
? (relative-fname "/home" "/home/lush/zoo") = "lush/zoo"
? (relative-fname "/usr/share" "/home/lush/zoo") = ()The following idiom can be used to relocate a filename x from directory fromdir to directory todir .
(concat-fname <todir> (or (relative-fname <fromdir> <x>) <x>) )
3.14.2.4. (tmpname [[dir] suffix]) |
[DX] |
The optional string dir specifies the directory where the temporary file will be created. A value of () is interpreted as the default system directory for temporary files.
This function generates file names using the optional suffix suffix . If you do not specify this argument, the generated filename will have no suffix.
Examples:
(tmpname) ;; creates a temporary file. (tmpname () "sn") ;; creates a temporary file whose name ends with ".lsh". (tmpname ".") ;; creates a temporary file in the current directory.
3.14.3. Searched Path |
When Lush is looking for a file, it searches for it among a list of
directories stored in variable |*PATH|
. Several functions are provided to handle this feature.
3.14.3.0. (path s1...sn) |
[DE] (sysenv.lsh) |
The initial path is automatically set while Lush is starting up by retrieving the Lush executable file and scanning up the directories until it finds a suitable library directory.
? (progn (printf "--> The current path is:\n") (path) ) --> The current path is: = ("." "/home/leonb/lush/local" "/home/leonb/lush/packages" "/home/leonb/lush/contrib" "/home/leonb/lush/lsh/libogre" "/home/leonb/lush/lsh/compiler" "/home/leonb/lush/lsh/libidx" "/home/leonb/lush/lsh/libstd" "/home/leonb/lush/lsh" "/home/leonb/lush/sys" )
3.14.3.1. (addpath s) |
[DE] (sysenv.lsh) |
The function addpath adds the
directory s to the file search path.
Directory s is added at the head of
the search path. Other occurrences of this directory in the path are
removed, in order to keep the search path small and non redundant.
Example:
? (addpath (concat (getenv "HOME") "/mysnlib"))
3.14.3.2. (filepath filename [suffixes]) |
[DX] |
The function filepath searches file
filename along the current search path, using the suffixes
specified by argument suffixes .
Function filepath returns the absolute
path name of the file. The empty list is returned if no file matches the
argument along the current search path.
Argument suffixes is a string containing a sequence of possible suffixes (including the initial period) separated by vertical bars ``|''. The sequence represents the priority order. A suffix may have zero characters.
".dump" Try suffix <.dump>. ".lshc|.lsh|" Try suffixes <.lshc> and <.lsh>, then try without suffix. "|.lsh" Try without suffix, then try suffix <.lsh>. ".lsh|" Try suffix <sn>, then try without suffix.
Argument suffixes may specify a single suffix without the initial period. This is a shorthand for testing first the specified suffix and then testing without a suffix.
"sn" Equivalent to ".lsh|"
When argument suffixes is omitted,
filepath uses the same suffixes than function
load . This default provides a way to determine which file
would be loaded by a given call to function load
.
? (filepath "sysenv") = "/home/leonb/lush/sys/sysenv.lsh"
3.14.4. Files and Directories Management |
3.14.4.0. (files [directory]) |
[DX] |
? (files) = ("latex" "README" "html" "CVS" ".." ".")
3.14.4.1. (fileinfo filename) |
[DX] |
? (fileinfo ".") = ((type . dir) (size . 4096) (mode . 16877) (dev . 2055) (ino . 1049073) (nlink . 5) (uid . 1000) (gid . 1000) (atime . ::DATE:year-second:(2011-01-29 15:43:30)) (mtime . ::DATE:year-second:(2011-02-10 22:55:21)) (ctime . ::DATE:year-second:(2011-02-10 22:55:21)) )
3.14.4.2. (dirp path) |
[DX] |
? (dirp "..") = t
3.14.4.3. (filep path) |
[DX] |
? (filep "..") = ()
3.14.4.4. (lockfile path) |
[DX] |
This function is useful for implementing a simple locking scheme.
3.14.4.5. (mkdir dirname) |
[DX] |
3.14.4.6. (unlink path) |
[DX] |
3.14.4.7. (rename path1 path2) |
[DX] |
3.14.4.8. (copyfile file1 file2) |
[DX] |
3.14.5. Files and Descriptors |
3.14.5.0. (script [s]) |
[DX] |
3.14.5.1. (open-read s [suffixes]) |
[DX] |
This function returns a read file descriptor of the file named
s . The empty list is returned when an error occurs while
opening the file. The file descriptor is closed when the garbage
collector detects that the file is no longer necessary. File descriptors
however can be manually closed with the function
delete .
Filenames have the following form:
When string suffixes is specified, the suffixes specified by this string are tried while searching the file along the search path. The possible values of the string suffix are documented with function filepath .
3.14.5.2. (open-write s [suffix]) |
[DX] |
This function returns a write file descriptor of the file named
s . The empty list is returned when an error occurs while
opening the file. The file descriptor is closed when the garbage
collector detects that the file is no longer necessary. File descriptors
however can be manually closed with the delete
function.
Filenames have the following form:
An optional suffix can be specified as argument suffix . This suffix is appended to the filename unless the filename already contains a suffix.
3.14.5.3. (open-append s [suffix]) |
[DX] |
An optional suffix can be specified as argument suffix . This suffix is appended to the filename unless the filename already contains a suffix.
3.14.5.4. (writing f l1 ... ln) |
[DY] |
Calls progn on
l1 to ln , while
redirecting the current output to f .
Argument f may be a filename or a file
descriptor obtained by open-write or
open-append .
3.14.5.5. (reading f l1... ln) |
[DY] |
Calls progn on
l1 to ln , while
redirecting the current input to f .
Argument f may be a filename or a read
file descriptor obtained by open-read
.
Example: This function returns the list of the files in directory s
(de directory(s) (let ((ans ())) (reading (concat "|/bin/ls " s) (while (<> (skip-char) "\e") (setq ans (nconc1 ans (read-string))) ) ) ans ) )
3.14.5.6. (reading-string str l1... ln) |
[DY] |
? (reading-string "(1 2 3)" (read)) = (1 2 3)
3.14.5.7. (read8 f) |
[DX] |
3.14.5.8. (write8 f b) |
[DX] |
3.14.5.9. (fsize f) |
[DX] |
3.14.5.10. (flush [f]) |
[DX] |
When no arguments are provided, both the current input and current output are flushed: All characters pending on the current input are removed until a newline is encountered, all characters buffered on the current output are printed out.
Example:
;;; This function ask a question and returns a string. (de input(question) (printf "%s" question) (flush) (read-string) )
3.14.6. Binary Input/Output |
3.14.6.0. (bwrite l1 ... ln) |
[DX] |
3.14.6.1. (bwrite-exact l1 ... ln) |
[DX] |
3.14.6.2. (bread) |
[DX] |
3.14.6.3. (bread-exact) |
[DX] |
Note that both bread and bread-exact can read files produced by either bwrite or bwrite-exact . There is no difference between bread and bread-exact when reading a file produced by bwrite-exact .
3.14.6.4. (tokenize fin fout) |
[DE] (sysenv.lsh) |
This function is useful for converting Lush program files (SN files) into preparsed binary file (SNC files). Loading a preparsed file is much faster than loading a text file. Preparsed copies of certain system libraries are located in directory "lushdir/lib" .
3.14.6.5. (dump fname) |
[DX] |
Writes the status of the lisp interpreter into file
fname . If fname has no
suffix, suffix ".dump" is assummed.
This function works by creating a binary file containing a binary
description of the state of the current Lush session.
Binary dump files are more useful for lauching Lush with preloaded libraries and application. During startup indeed, Tlisp can load a binary dump file specified by the command line arguments. Moreover, if Tlisp does not find the system library "stdenv.lsh" , it attempts to undump a file names "stdenv.dump" .
3.14.7. Pipes and Sockets |
3.14.7.0. (filteropen cmd fin fout) |
[DX] |
This function runs the shell command cmd in the background, stores into symbol fin a file descriptor for reading its standard output , and stores into symbol fout a file descriptor for writing into its standard input.
This commands allows for launching another program, sending commands and receiving the results. Arguments fin and fout are evaluated and must probably be quoted symbols.
3.14.7.1. (socketopen host port fin fout) |
[DX] |
This function connects a server process listening on port number port on the remote computer named host . It stores into symbol fin a file descriptor for reading from the server, and stores into symbol fout a file descriptor for writing to the server.
Arguments fin and fout are evaluated and must be quoted symbols.
This function causes an error if the server is not listening for connections. Make the port number negative to prevent this. Function socketopen will then return () if the port cannot be opened.
3.14.7.2. (socketaccept port [fin fout]) |
[DX] |
This commands creates a server side socket listening on port number port . It then waits for a client connection (for instance with socketopen ). Then it stores file descriptors for reading from and writing to the client into symbols fin and fout respectively. Arguments fin and fout are evaluated and must be quoted symbols.
When arguments fin and fout are not provided, it returns a boolean indicating whether it was possible to wait for connections on the specified port.
3.14.7.3. (socketselect [...filedescriptors...] [timeout]) |
[DX] |