3.13. Loading and Executing Lush Program Files




3.13.0. (load in [out [prompt]])
[DX]


See: (open-write s [ suffix ])
See: (open-read s [ suffix ])
See: ^L filename


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


See: (load in [ out [ prompt ]])


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]


See: (load in [ out [ prompt ]])


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)


See: file-being-loaded
See: (load in [ out [ prompt ]])


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]


Function returns the names of all the files on which fname depends. This information is collected whenever libload is called. Argument fname defaults to the currently loaded file.



3.13.5. (libload-add-dependency fname)
[DE]


This function modifies the dependency database managed by function libload . It records that the file currently loaded depends on file fname . This function is useful when it is not practical to call libload on function fname . For instance fname might not be a Lush file but a data file processed by the current file.

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)


Finds the first file that exists fo the form dir / file . ext where dir is one of the directories in list dirlist and ext is one of the suffixes in extlist .

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)


This function just saves indented definitions of the symbols s1 to sn into the file named f . A suffix ".lsh" is automatically added to f if it has no suffix.



3.13.8. (autoload f s1 ... sn)
[DM] (sysenv.lsh)


Defines functions s1 to sn with a code that first loads file f , and then restart the evaluation.

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]


See: The Lush Reader.
Calls the Lisp reader, and returns the next Lush object available on the current input. Function read is able to read both text and binary Lush files.



3.14.0.1. (read-string [spec])
[DX]


Reads a string on the current input.

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]


Skips the characters matching string spec and returns the next available character. The default value of argument spec is "\t\n\r\f" and means ``skip any blank characters''.

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]


Prints the string s on the standard output and waits for a "yes" or "no" answer on the standard input. Function ask returns t or () according to the user's answer. Function ask is not affected by the current input or output file.

Example:

? (ask "Your answer")
Your answer [y/n] ?



3.14.1. Output




3.14.1.0. (prin l1 ... ln)
[DX]


Prints the Lush objects l1 to ln separated by spaces. Unlike print , the function prin does not terminates the line with a linefeed. Function prin returns the result of the last evaluation.



3.14.1.1. (print l1 ... ln)
[DX]


Prints the Lush objects l1 to ln separated by spaces and output a newline character. Function print returns the result of the last evaluation.

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)


Prints the Lush objects l1 to ln with a nice indentation. The indentation is defined according many heuristics suitable for printing nicely function definitions. Unlike function pprint , pprin does not terminates the line with a linefeed.



3.14.1.3. (pprint l1 ... ln)
[DE] (sysenv.lsh)


Prints the evaluations of l1 to ln with a nice indentation. The indentation is defined according many heuristics suitable for printing nicely function definitions. Function pprint terminates the line with a linefeed.

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]


This function is similar to the printf function in C language. String format will be printed verbatim, except for the following escape sequences, which refer to args .

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]


When a numeric argument n is given, tab outputs blanks until the cursor reaches the n -th column on the current line. The cursor position is unchanged if the cursor is located after the n -th column. Function tab always returns the current cursor position.

? (tab)
= 0



3.14.2. File Names




3.14.2.0. (basename path [suffix])
[DX]


Returns the terminal component of the pathname path by removing any prefix ending with / . If string suffix is provided, a possible suffix suffix is removed from the pathname.

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]


Returns the name of the directory containing the file named path .

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]


Returns an absolute filename equivalent to filename2 parsed from current directory filename1 . Argument filename1 defaults to the current directory.

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]


If filename represents a file located below directory dirname , this function returns a string representing the relative file name of filename with respect to directory dirname . Function concat-fname can then be reused to reconstruct the absolute file name. This function returns () otherwise.

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]


This function creates a temporary file and returns its name. When Lush exits, an automatic cleanup code removes these files.

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


See: Lush Startup.


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 function path allows you to get and set this list of directories searched by Lush. Without arguments, function path returns the current path. Otherwise, the strings s1 to sn become the current path.

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)


See: (path s1 ... sn )


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]


See: (path s1 ... sn )
See: (concat-fname [ filename1 ] filename2 )
See: (load in [ out [ prompt ]])


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]


Returns the list of the files located in directory directory . If argument directory is ommitted, the current directory is assummed.
? (files)
= ("latex" "README" "html" "CVS" ".." ".")



3.14.4.1. (fileinfo filename)
[DX]


Returns an a-list containing lots of information about the file named filename in the form of an association list. Information includes type, dates, permissions, size,.... This function returns nil if the named file does not exist.
? (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]


Returns t if string path is the filename of a directory. This function also returns t if string path is the filename of a symbolic link referring to a directory.
? (dirp "..")
= t



3.14.4.3. (filep path)
[DX]


Returns t if string path is the filename of a regular file (not a directory). This function also returns t if string path is the filename of a symbolic link referring to a regular file.
? (filep "..")
= ()



3.14.4.4. (lockfile path)
[DX]


Attemps to create file path .

This function is useful for implementing a simple locking scheme.



3.14.4.5. (mkdir dirname)
[DX]


This function creates a new directory dirname .
See: (mkdir-on-need [ s ])
See: (chdir [ s ])




3.14.4.6. (unlink path)
[DX]


This function removes the file or directory path . It does not complain if the file or directory path does not exist.



3.14.4.7. (rename path1 path2)
[DX]


This function renames file path1 as path2 .



3.14.4.8. (copyfile file1 file2)
[DX]


Copy file file1 onto file file2 . Arguments file1 and file2 may be file descriptors or file names (as described in open-read and open-write .



3.14.5. Files and Descriptors




3.14.5.0. (script [s])
[DX]


Creates a file named s and records all the input/output operations on that file. The suffix ".script" is added to the name s . This function is handy for keeping a trace of the Lush session. If function script is called without arguments, the current script file is closed.



3.14.5.1. (open-read s [suffixes])
[DX]


See: (sys shellcmd )
See: (filepath filename [ suffixes ])


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]


See: (sys shellcmd )


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:



3.14.5.3. (open-append s [suffix])
[DX]


This function returns a ``append'' file descriptor of the file named s . All output on this file descriptor shall be appended to the file. 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.

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]


See: (open-write s [ suffix ])
See: (open-append s [ suffix ])


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]


See: (open-read s [ suffix ])


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]


Calls progn on l1 to ln , while redirecting the current input to read the contents of string str .
? (reading-string "(1 2 3)" (read))
= (1 2 3)



3.14.5.7. (read8 f)
[DX]


Reads a byte on file descriptor f . This function returns a number in the range 0 to 255.



3.14.5.8. (write8 f b)
[DX]


Writes a byte b on file descriptor f . Argument b must be a number in the range 0 to 255. This function does not flush the file f . The function flush must be used in order to write out all remaining data.



3.14.5.9. (fsize f)
[DX]


Returns the number of bytes remaining in the file. When the file is not exhaustable or not rewindable (such as pipes and terminals) it causes an error.



3.14.5.10. (flush [f])
[DX]


Flushes the file descriptor f .

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]


See: (bread)
Function bwrite writes the lisp objects l1 to ln on the current output stream as a sequence of binary tokens. This function is able to write correctly lisp objects with circular references. It cannot however write lisp objects useful for their side effects like open file descriptors or windows.



3.14.6.1. (bwrite-exact l1 ... ln)
[DX]


See: (bwrite l1 ... ln )
See: (bread)
Function bwrite-exact is similar to bwrite with a few twists that makes it more accurate but less frequently useful.

3.14.6.2. (bread)
[DX]


See: (read)
Function bread reads on the current input stream a binary sequence of tokens and builds the corresponding lisp object. Functions read and load are also able to recognize binary tokens in the middle of a text file. This allows for reading a file containing a mixture of lisp objects representing as binary sequence of tokens or as text.



3.14.6.3. (bread-exact)
[DX]


See: (bread)
Function bread-exact is similar to bwrite with one twists that makes it more accurate but less frequently useful.

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)


Function tokenize reads all lisp expressions from file fin and writes their binary equivalent into file fout .

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]


See: Lush Startup.


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 command is available under Unix. Check function winsys under Windows.

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 command is available under Unix.

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 command is available under Unix.

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]


This command waits until data becomes available on the specified file descriptors. The optional argument timeout is a number specifying how many milliseconds the function should wait for data. Providing a timeout of zero simply checks for the availability of data. Providing no timeout potentially waits forever.