Go to the first, previous, next, last section, table of contents.


Session Support

Repl

(require 'repl)

Here is a read-eval-print-loop which, given an eval, evaluates forms.

Procedure: repl:top-level repl:eval
reads, repl:evals and writes expressions from (current-input-port) to (current-output-port) until an end-of-file is encountered. load, slib:eval, slib:error, and repl:quit dynamically bound during repl:top-level.

Procedure: repl:quit
Exits from the invocation of repl:top-level.

The repl: procedures establish, as much as is possible to do portably, a top level environment supporting macros. repl:top-level uses dynamic-wind to catch error conditions and interrupts. If your implementation supports this you are all set.

Otherwise, if there is some way your implementation can catch error conditions and interrupts, then have them call slib:error. It will display its arguments and reenter repl:top-level. slib:error dynamically bound by repl:top-level.

To have your top level loop always use macros, add any interrupt catching lines and the following lines to your Scheme init file:

(require 'macro)
(require 'repl)
(repl:top-level macro:eval)

Quick Print

(require 'qp)

When displaying error messages and warnings, it is paramount that the output generated for circular lists and large data structures be limited. This section supplies a procedure to do this. It could be much improved.

Notice that the neccessity for truncating output eliminates Common-Lisp's See section Format from consideration; even when variables *print-level* and *print-level* are set, huge strings and bit-vectors are not limited.

Procedure: qp arg1 ...
Procedure: qpn arg1 ...
Procedure: qpr arg1 ...
qp writes its arguments, separated by spaces, to (current-output-port). qp compresses printing by substituting `...' for substructure it does not have sufficient room to print. qpn is like qp but outputs a newline before returning. qpr is like qpn except that it returns its last argument.

Variable: *qp-width*
*qp-width* is the largest number of characters that qp should use.

Debug

(require 'debug)

Requiring debug automatically requires trace and break.

An application with its own datatypes may want to substitute its own printer for qp. This example shows how to do this:

(define qpn (lambda args) ...)
(provide 'qp)
(require 'debug)

Procedure: trace-all file
Traces (see section Tracing) all procedures defined at top-level in file `file'.

Procedure: break-all file
Breakpoints (see section Breakpoints) all procedures defined at top-level in file `file'.

Breakpoints

(require 'break)

Function: init-debug
If your Scheme implementation does not support break or abort, a message will appear when you (require 'break) or (require 'debug) telling you to type (init-debug). This is in order to establish a top-level continuation. Typing (init-debug) at top level sets up a continuation for break.

Function: breakpoint arg1 ...
Returns from the top level continuation and pushes the continuation from which it was called on a continuation stack.

Function: continue
Pops the topmost continuation off of the continuation stack and returns an unspecified value to it.
Function: continue arg1 ...
Pops the topmost continuation off of the continuation stack and returns arg1 ... to it.

Macro: break proc1 ...
Redefines the top-level named procedures given as arguments so that breakpoint is called before calling proc1 ....
Macro: break
With no arguments, makes sure that all the currently broken identifiers are broken (even if those identifiers have been redefined) and returns a list of the broken identifiers.

Macro: unbreak proc1 ...
Turns breakpoints off for its arguments.
Macro: unbreak
With no arguments, unbreaks all currently broken identifiers and returns a list of these formerly broken identifiers.

The following routines are the procedures which actually do the tracing when this module is supplied by SLIB, rather than natively. If defmacros are not natively supported by your implementation, these might be more convenient to use.

Function: breakf proc
Function: breakf proc name
Function: debug:breakf proc
Function: debug:breakf proc name
To break, type
(set! symbol (breakf symbol))

or

(set! symbol (breakf symbol 'symbol))

or

(define symbol (breakf function))

or

(define symbol (breakf function 'symbol))

Function: unbreakf proc
Function: debug:unbreakf proc
To unbreak, type
(set! symbol (unbreakf symbol))

Tracing

(require 'trace)

Macro: trace proc1 ...
Traces the top-level named procedures given as arguments.
Macro: trace
With no arguments, makes sure that all the currently traced identifiers are traced (even if those identifiers have been redefined) and returns a list of the traced identifiers.

Macro: untrace proc1 ...
Turns tracing off for its arguments.
Macro: untrace
With no arguments, untraces all currently traced identifiers and returns a list of these formerly traced identifiers.

The following routines are the procedures which actually do the tracing when this module is supplied by SLIB, rather than natively. If defmacros are not natively supported by your implementation, these might be more convenient to use.

Function: tracef proc
Function: tracef proc name
Function: debug:tracef proc
Function: debug:tracef proc name
To trace, type
(set! symbol (tracef symbol))

or

(set! symbol (tracef symbol 'symbol))

or

(define symbol (tracef function))

or

(define symbol (tracef function 'symbol))

Function: untracef proc
Function: debug:untracef proc
To untrace, type
(set! symbol (untracef symbol))

Getopt

(require 'getopt)

This routine implements Posix command line argument parsing. Notice that returning values through global variables means that getopt is not reentrant.

Variable: *optind*
Is the index of the current element of the command line. It is initially one. In order to parse a new command line or reparse an old one, *opting* must be reset.

Variable: *optarg*
Is set by getopt to the (string) option-argument of the current option.

Procedure: getopt argc argv optstring
Returns the next option letter in argv (starting from (vector-ref argv *optind*)) that matches a letter in optstring. argv is a vector or list of strings, the 0th of which getopt usually ignores. argc is the argument count, usually the length of argv. optstring is a string of recognized option characters; if a character is followed by a colon, the option takes an argument which may be immediately following it in the string or in the next element of argv.

*optind* is the index of the next element of the argv vector to be processed. It is initialized to 1 by `getopt.scm', and getopt updates it when it finishes with each element of argv.

getopt returns the next option character from argv that matches a character in optstring, if there is one that matches. If the option takes an argument, getopt sets the variable *optarg* to the option-argument as follows:

If, when getopt is called, the string (vector-ref argv *optind*) either does not begin with the character #\- or is just "-", getopt returns #f without changing *optind*. If (vector-ref argv *optind*) is the string "--", getopt returns #f after incrementing *optind*.

If getopt encounters an option character that is not contained in optstring, it returns the question-mark #\? character. If it detects a missing option argument, it returns the colon character #\: if the first character of optstring was a colon, or a question-mark character otherwise. In either case, getopt sets the variable getopt:opt to the option character that caused the error.

The special option "--" can be used to delimit the end of the options; #f is returned, and "--" is skipped.

RETURN VALUE

getopt returns the next option character specified on the command line. A colon #\: is returned if getopt detects a missing argument and the first character of optstring was a colon #\:.

A question-mark #\? is returned if getopt encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon #\:.

Otherwise, getopt returns #f when all command line options have been parsed.

Example:

#! /usr/local/bin/scm
;;;This code is SCM specific.
(define argv (program-arguments))
(require 'getopt)

(define opts ":a:b:cd")
(let loop ((opt (getopt (length argv) argv opts)))
  (case opt
    ((#\a) (print "option a: " *optarg*))
    ((#\b) (print "option b: " *optarg*))
    ((#\c) (print "option c"))
    ((#\d) (print "option d"))
    ((#\?) (print "error" getopt:opt))
    ((#\:) (print "missing arg" getopt:opt))
    ((#f) (if (< *optind* (length argv))
              (print "argv[" *optind* "]="
                     (list-ref argv *optind*)))
          (set! *optind* (+ *optind* 1))))
  (if (< *optind* (length argv))
      (loop (getopt (length argv) argv opts))))

(slib:exit)

Getopt--

Function: getopt-- argc argv optstring
The procedure getopt-- is an extended version of getopt which parses long option names of the form `--hold-the-onions' and `--verbosity-level=extreme'. Getopt-- behaves as getopt except for non-empty options beginning with `--'.

Options beginning with `--' are returned as strings rather than characters. If a value is assigned (using `=') to a long option, *optarg* is set to the value. The `=' and value are not returned as part of the option string.

No information is passed to getopt-- concerning which long options should be accepted or whether such options can take arguments. If a long option did not have an argument, *optarg will be set to #f. The caller is responsible for detecting and reporting errors.

(define opts ":-:b:")
(define argc 5)
(define argv '("foo" "-b9" "--f1" "--2=" "--g3=35234.342" "--"))
(define *optind* 1)
(define *optarg* #f)
(require 'qp)
(do ((i 5 (+ -1 i)))
    ((zero? i))
  (define opt (getopt-- argc argv opts))
  (print *optind* opt *optarg*)))
-|
2 #\b "9" 
3 "f1" #f 
4 "2" "" 
5 "g3" "35234.342" 
5 #f "35234.342" 

Command Line

(require 'read-command)

Function: read-command port
Function: read-command
read-command converts a command line into a list of strings suitable for parsing by getopt. The syntax of command lines supported resembles that of popular shells. read-command updates port to point to the first character past the command delimiter.

If an end of file is encountered in the input before any characters are found that can begin an object or comment, then an end of file object is returned.

The port argument may be omitted, in which case it defaults to the value returned by current-input-port.

The fields into which the command line is split are delimited by whitespace as defined by char-whitespace?. The end of a command is delimited by end-of-file or unescaped semicolon (;) or newline. Any character can be literally included in a field by escaping it with a backslach (\).

The initial character and types of fields recognized are:

`\'
The next character has is taken literally and not interpreted as a field delimiter. If \ is the last character before a newline, that newline is just ignored. Processing continues from the characters after the newline as though the backslash and newline were not there.
`"'
The characters up to the next unescaped " are taken literally, according to [R4RS] rules for literal strings (see section `Strings' in Revised(4) Scheme).
`(', `%''
One scheme expression is read starting with this character. The read expression is evaluated, converted to a string (using display), and replaces the expression in the returned field.
`;'
Semicolon delimits a command. Using semicolons more than one command can appear on a line. Escaped semicolons and semicolons inside strings do not delimit commands.

The comment field differs from the previous fields in that it must be the first character of a command or appear after whitespace in order to be recognized. # can be part of fields if these conditions are not met. For instance, ab#c is just the field ab#c.

`#'
Introduces a comment. The comment continues to the end of the line on which the semicolon appears. Comments are treated as whitespace by read-dommand-line and backslashes before newlines in comments are also ignored.

System Interface

If (provided? 'getenv):

Function: getenv name
Looks up name, a string, in the program environment. If name is found a string of its value is returned. Otherwise, #f is returned.

If (provided? 'system):

Function: system command-string
Executes the command-string on the computer and returns the integer status code.

Require

These variables and procedures are provided by all implementations.

Variable: *features*
Is a list of symbols denoting features supported in this implementation.

Variable: *modules*
Is a list of pathnames denoting files which have been loaded.

Variable: *catalog*
Is an association list of features (symbols) and pathnames which will supply those features. The pathname can be either a string or a pair. If pathname is a pair then the first element should be a macro feature symbol, source, or compiled. The cdr of the pathname should be either a string or a list.

In the following three functions if feature is not a symbol it is assumed to be a pathname.

Function: provided? feature
Returns #t if feature is a member of *features* or *modules* or if feature is supported by a file already loaded and #f otherwise.

Procedure: require feature
If (not (provided? feature)) it is loaded if feature is a pathname or if (assq feature *catalog*). Otherwise an error is signaled.

Procedure: provide feature
Assures that feature is contained in *features* if feature is a symbol and *modules* otherwise.

Function: require:feature->path feature
Returns #t if feature is a member of *features* or *modules* or if feature is supported by a file already loaded. Returns a path if one was found in *catalog* under the feature name, and #f otherwise. The path can either be a string suitable as an argument to load or a pair as described above for *catalog*.

Below is a list of features that are automatically determined by require. For each item, (provided? 'feature) will return #t if that feature is available, and #f if not.

Vicinity

A vicinity is a descriptor for a place in the file system. Vicinities hide from the programmer the concepts of host, volume, directory, and version. Vicinities express only the concept of a file environment where a file name can be resolved to a file in a system independent manner. Vicinities can even be used on flat file systems (which have no directory structure) by having the vicinity express constraints on the file name. On most systems a vicinity would be a string. All of these procedures are file system dependent.

These procedures are provided by all implementations.

Function: make-vicinity filename
Returns the vicinity of filename for use by in-vicinity.

Function: program-vicinity
Returns the vicinity of the currently loading Scheme code. For an interpreter this would be the directory containing source code. For a compiled system (with multiple files) this would be the directory where the object or executable files are. If no file is currently loading it the result is undefined. Warning: program-vicinity can return incorrectl values if your program escapes back into a load.

Function: library-vicinity
Returns the vicinity of the shared Scheme library.

Function: implementation-vicinity
Returns the vicinity of the underlying Scheme implementation. This vicinity will likely contain startup code and messages and a compiler.

Function: user-vicinity
Returns the vicinity of the current directory of the user. On most systems this is `""' (the empty string).

Function: in-vicinity vicinity filename
Returns a filename suitable for use by slib:load, slib:load-source, slib:load-compiled, open-input-file, open-output-file, etc. The returned filename is filename in vicinity. in-vicinity should allow filename to override vicinity when filename is an absolute pathname and vicinity is equal to the value of (user-vicinity). The behavior of in-vicinity when filename is absolute and vicinity is not equal to the value of (user-vicinity) is unspecified. For most systems in-vicinity can be string-append.

Function: sub-vicinity vicinity name
Returns the vicinity of vicinity restricted to name. This is used for large systems where names of files in subsystems could conflict. On systems with directory structure sub-vicinity will return a pathname of the subdirectory name of vicinity.

Configuration

These constants and procedures describe characteristics of the Scheme and underlying operating system. They are provided by all implementations.

Constant: char-code-limit
An integer 1 larger that the largest value which can be returned by char->integer.

Constant: most-positive-fixnum
The immediate integer closest to positive infinity.

Constant: slib:tab
The tab character.

Constant: slib:form-feed
The form-feed character.

Function: software-type
Returns a symbol denoting the generic operating system type. For instance, unix, vms, macos, amiga, or ms-dos.

Function: slib:report-version
Displays the versions of SLIB and the underlying Scheme implementation and the name of the operating system. An unspecified value is returned.

(slib:report-version) => slib "2a3" on scm "4e1" on unix 

Function: slib:report
Displays the information of (slib:report-version) followed by almost all the information neccessary for submitting a problem report. An unspecified value is returned.

Function: slib:report #t
provides a more verbose listing.

Function: slib:report filename
Writes the report to file `filename'.

(slib:report)
=>
slib "2a3" on scm "4e1" on unix 
(implementation-vicinity) is "/usr/local/src/scm/" 
(library-vicinity) is "/usr/local/lib/slib/" 
(scheme-file-suffix) is ".scm" 
implementation *features* : 
        bignum complex real rational
        inexact vicinity ed getenv
        tmpnam system abort transcript
        with-file ieee-p1178 rev4-report rev4-optional-procedures
        hash object-hash delay eval
        dynamic-wind multiarg-apply multiarg/and- logical
        defmacro string-port source array-for-each
        array full-continuation char-ready? line-i/o
        i/o-extensions pipe
implementation *catalog* : 
        (rev4-optional-procedures . "/usr/local/lib/slib/sc4opt")
        ... 

Input/Output

These procedures are provided by all implementations.

Procedure: file-exists? filename
Returns #t if the specified file exists. Otherwise, returns #f. If the underlying implementation does not support this feature then #f is always returned.

Procedure: delete-file filename
Deletes the file specified by filename. If filename can not be deleted, #f is returned. Otherwise, #t is returned.

Procedure: tmpnam
Returns a pathname for a file which will likely not be used by any other process. Successive calls to (tmpnam) will return different pathnames.

Procedure: current-error-port
Returns the current port to which diagnostic and error output is directed.

Procedure: force-output
Procedure: force-output port
Forces any pending output on port to be delivered to the output device and returns an unspecified value. The port argument may be omitted, in which case it defaults to the value returned by (current-output-port).

Procedure: output-port-width
Procedure: output-port-width port

Returns the width of port, which defaults to (current-output-port) if absent. If the width cannot be determined 79 is returned.

Procedure: output-port-height
Procedure: output-port-height port

Returns the height of port, which defaults to (current-output-port) if absent. If the height cannot be determined 24 is returned.

Legacy

Function: identity x
identity returns its argument.

Example:

(identity 3)
   => 3
(identity '(foo bar))
   => (foo bar)
(map identity lst)
   == (copy-list lst)

These were present in Scheme until R4RS (see section `Language changes' in Revised(4) Scheme).

Constant: t
Derfined as #t.

Constant: nil
Defined as #f.

Function: last-pair l
Returns the last pair in the list l. Example:
(last-pair (cons 1 2))
   => (1 . 2)
(last-pair '(1 2))
   => (2)
    == (cons 2 '())

System

These procedures are provided by all implementations.

Procedure: slib:load-source name
Loads a file of Scheme source code from name with the default filename extension used in SLIB. For instance if the filename extension used in SLIB is `.scm' then (slib:load-source "foo") will load from file `foo.scm'.

Procedure: slib:load-compiled name
On implementations which support separtely loadable compiled modules, loads a file of compiled code from name with the implementation's filename extension for compiled code appended.

Procedure: slib:load name
Loads a file of Scheme source or compiled code from name with the appropriate suffixes appended. If both source and compiled code are present with the appropriate names then the implementation will load just one. It is up to the implementation to choose which one will be loaded.

If an implementation does not support compiled code then slib:load will be identical to slib:load-source.

Procedure: slib:eval obj
eval returns the value of obj evaluated in the current top level environment.

Procedure: slib:eval-load filename eval
filename should be a string. If filename names an existing file, the Scheme source code expressions and definitions are read from the file and eval called with them sequentially. The slib:eval-load procedure does not affect the values returned by current-input-port and current-output-port.

Procedure: slib:error arg1 arg2 ...
Outputs an error message containing the arguments, aborts evaluation of the current form and responds in a system dependent way to the error. Typical responses are to abort the program or to enter a read-eval-print loop.

Procedure: slib:exit n
Procedure: slib:exit
Exits from the Scheme session returning status n to the system. If n is omitted or #t, a success status is returned to the system (if possible). If n is #f a failure is returned to the system (if possible). If n is an integer, then n is returned to the system (if possible). If the Scheme session cannot exit an unspecified value is returned from slib:exit.


Go to the first, previous, next, last section, table of contents.