POSIX Interface Conventions
***************************

These interfaces provide access to operating system facilities. They provide a simple wrapping around the underlying C interfaces to make usage from Scheme more convenient.  They are also used to implement the Guile port of The Scheme shell (scsh).

Generally there is a single procedure for each corresponding Unix facility.  There are some exceptions, such as procedures implemented for speed and convenience in Scheme with no primitive Unix equivalent, e.g., copy-file.

The interfaces are intended as far as possible to be portable across different versions of Unix.  In some cases procedures which can't be implemented on particular systems may become no-ops, or perform limited actions.  In other cases they may throw errors.

General naming conventions are as follows:

  * The Scheme name is often identical to the name of the underlying Unix facility.

  * Underscores in Unix procedure names are converted to hyphens.

  * Procedures which destructively modify Scheme data have exclaimation marks appended, e.g., recv!.

  * Predicates (returning only #t or #f) have question marks appended, e.g., access?.

  * Some names are changed to avoid conflict with dissimilar interfaces defined by scsh, e.g., primitive-fork.

  * Unix preprocessor names such as EPERM or R_OK are converted to Scheme variables of the same name (underscores are not replaced with hyphens).

Unexpected conditions are generally handled by raising exceptions. There are a few procedures which return a special value if they don't succeed, e.g., getenv returns #f if it the requested string is not found in the environment.  These cases are noted in the documentation.

For ways to deal with exceptions, Exceptions.

Errors which the C-library would report by returning a NULL pointer or through some other means are reported by raising a system-error exception. The value of the Unix errno variable is available in the data passed by the exception.

Ports and File Descriptors
**************************

Conventions generally follow those of scsh, The Scheme shell (scsh).

File ports are implemented using low-level operating system I/O facilities, with optional buffering to improve efficiency see File Ports

Note that some procedures (e.g., recv!) will accept ports as arguments, but will actually operate directly on the file descriptor underlying the port.  Any port buffering is ignored, including the buffer which implements peek-char and unread-char.

The force-output and drain-input procedures can be used to clear the buffers.

Each open file port has an associated operating system file descriptor. File descriptors are generally not useful in Scheme programs; however they may be needed when interfacing with foreign code and the Unix environment.

A file descriptor can be extracted from a port and a new port can be created from a file descriptor.  However a file descriptor is just an integer and the garbage collector doesn't recognise it as a reference to the port.  If all other references to the port were dropped, then it's likely that the garbage collector would free the port, with the side-effect of closing the file descriptor prematurely.

To assist the programmer in avoiding this problem, each port has an associated "revealed count" which can be used to keep track of how many times the underlying file descriptor has been stored in other places. If a port's revealed count is greater than zero, the file descriptor will not be closed when the port is gabage collected.  A programmer can therefore ensure that the revealed count will be greater than zero if the file descriptor is needed elsewhere.

For the simple case where a file descriptor is "imported" once to become a port, it does not matter if the file descriptor is closed when the port is garbage collected.  There is no need to maintain a revealed count.  Likewise when "exporting" a file descriptor to the external environment, setting the revealed count is not required provided the port is kept open (i.e., is pointed to by a live Scheme binding) while the file descriptor is in use.

To correspond with traditional Unix behaviour, the three file descriptors (0, 1 and 2) are automatically imported when a program starts up and assigned to the initial values of the current input, output and error ports.  The revealed count for each is initially set to one, so that dropping references to one of these ports will not result in its garbage collection: it could be retrieved with fdopen or fdes->ports.

 -- primitive: port-revealed port
     Returns the revealed count for PORT.

 -- primitive: set-port-revealed! port count
     Sets the revealed count for a port to a given value.  The return value is unspecified.

 -- primitive: fileno port
     Returns the integer file descriptor underlying PORT without changing its revealed count.

 -- procedure: port->fdes port
     Returns the integer file descriptor underlying PORT.  As a side effect the revealed count of PORT is incremented.

 -- primitive: fdopen fdes modes
     Returns a new port based on the file descriptor FDES with modes given by the string MODES.  The revealed count of the port is initialized to zero.  The modes string is the same as that accepted by open-file.

 -- primitive: fdes->ports fdes
     Returns a list of existing ports which have FDES as an underlying file descriptor, without changing their revealed counts.

 -- procedure: fdes->inport fdes
     Returns an existing input port which has FDES as its underlying file descriptor, if one exists, and increments its revealed count. Otherwise, returns a new input port with a revealed count of 1.

 -- procedure: fdes->outport fdes
     Returns an existing output port which has FDES as its underlying file descriptor, if one exists, and increments its revealed count. Otherwise, returns a new output port with a revealed count of 1.

 -- primitive: primitive-move->fdes port fdes
     Moves the underlying file descriptor for PORT to the integer value FDES without changing the revealed count of PORT. Any other ports already using this descriptor will be automatically shifted to new descriptors and their revealed counts reset to zero. The return value is #f if the file descriptor already had the required value or #t if it was moved.

 -- procedure: move->fdes port fdes
     Moves the underlying file descriptor for PORT to the integer value FDES and sets its revealed count to one.  Any other ports already using this descriptor will be automatically shifted to new descriptors and their revealed counts reset to zero. The return value is unspecified.

 -- procedure: release-port-handle port
     Decrements the revealed count for a port.

 -- primitive: fsync port/fd
     Copies any unwritten data for the specified output file descriptor to disk. If PORT/FD is a port, its buffer is flushed before the underlying file descriptor is fsync'd. The return value is unspecified.

 -- primitive: open path flags [mode]
     Open the file named by PATH for reading and/or writing. FLAGS is an integer specifying how the file should be opened. MODE is an integer specifying the permission bits of the file, if it needs to be created, before the umask is applied.  The default is 666 (Unix itself has no default).

     FLAGS can be constructed by combining variables using logior. Basic flags are:

      -- Variable: O_RDONLY
          Open the file read-only.

      -- Variable: O_WRONLY
          Open the file write-only.

      -- Variable: O_RDWR
          Open the file read/write.

      -- Variable: O_APPEND
          Append to the file instead of truncating.

      -- Variable: O_CREAT
          Create the file if it does not already exist.

     See the Unix documentation of the open system call for additional flags.

 -- primitive: open-fdes path flags [modes]
     Similar to open but returns a file descriptor instead of a port.

 -- primitive: close port/fd
     Similar to close-port (see close-port), but also works on file descriptors.  A side effect of closing a file descriptor is that any ports using that file descriptor are moved to a different file descriptor and have their revealed counts set to zero.

 -- primitive: unread-char char [port]
     Place CHAR in PORT so that it will be read by the next read operation.  If called multiple times, the unread characters will be read again in last-in first-out order.  If PORT is not supplied, the current input port is used.

 -- primitive: unread-string str [port]
     Place the string STR in PORT so that its characters will be read in subsequent read operations.  If called multiple times, the unread characters will be read again in last-in first-out order.  If PORT is not supplied, the current-input-port is used.

 -- primitive: pipe
     Creates a pipe which can be used for communication.  The return value is a pair in which the CAR contains an input port and the CDR an output port.  Data written to the output port can be read from the input port.  Note that both ports are buffered so it may be necessary to flush the output port before data will actually be sent across the pipe. Alternatively a new unbuffered output port can be created using duplicate-port with an appropriate mode string.

The next group of procedures perform a dup2 system call, if NEWFD (an integer) is supplied, otherwise a dup.  The file descriptor to be duplicated can be supplied as an integer or contained in a port.  The type of value returned varies depending on which procedure is used.

All procedures also have the side effect when performing dup2 that any ports using NEWFD are moved to a different file descriptor and have their revealed counts set to zero.

 -- primitive: dup->fdes port/fd [newfd]
     Returns an integer file descriptor.

 -- procedure: dup->inport port/fd [newfd]
     Returns a new input port using the new file descriptor.

 -- procedure: dup->outport port/fd [newfd]
     Returns a new output port using the new file descriptor.

 -- procedure: dup port/fd [newfd]
     Returns a new port if PORT/FD is a port, with the same mode as the supplied port, otherwise returns an integer file descriptor.

 -- procedure: dup->port port/fd mode [newfd]
     Returns a new port using the new file descriptor.  MODE supplies a mode string for the port (see open-file).

 -- procedure: duplicate-port port modes
     Returns a new port which is opened on a duplicate of the file descriptor underlying PORT, with mode string MODES as for open-file.  The two ports will share a file position and file status flags.

     Unexpected behaviour can result if both ports are subsequently used and the original and/or duplicate ports are buffered. The mode string can include 0 to obtain an unbuffered duplicate port.

     This procedure is equivalent to (dup->port PORT MODES).

 -- primitive: redirect-port old-port new-port
     This procedure takes two ports and duplicates the underlying file descriptor from OLD-PORT into NEW-PORT.  The current file descriptor in NEW-PORT will be closed. After the redirection the two ports will share a file position and file status flags.

     The return value is unspecified.

     Unexpected behaviour can result if both ports are subsequently used and the original and/or duplicate ports are buffered.

     This procedure does not have any side effects on other ports or revealed counts.

 -- primitive: port-mode port
     Returns the port modes associated with the open port PORT.  These will not necessarily be identical to the modes used when the port was opened, since modes such as "append" which are used only during port creation are not retained.

 -- primitive: close-all-ports-except port ...
     Close all open file ports used by the interpreter except for those supplied as arguments.  This procedure is intended to be used before an exec call to close file descriptors which are not needed in the new process.

 -- primitive: setvbuf port mode [size]
     Set the buffering mode for PORT.  MODE can be:

     _IONBF
          non-buffered

     _IOLBF
          line buffered

     _IOFBF
          block buffered, using a newly allocated buffer of SIZE bytes. If SIZE is omitted, a default size will be used.

 -- primitive: fcntl fd/port command [value]
     Apply COMMAND to the specified file descriptor or the underlying file descriptor of the specified port.  VALUE is an optional integer argument.

     Values for COMMAND are:

     F_DUPFD
          Duplicate a file descriptor

     F_GETFD
          Get flags associated with the file descriptor.

     F_SETFD
          Set flags associated with the file descriptor to VALUE.

     F_GETFL
          Get flags associated with the open file.

     F_SETFL
          Set flags associated with the open file to VALUE

     F_GETOWN
          Get the process ID of a socket's owner, for SIGIO signals.

     F_SETOWN
          Set the process that owns a socket to VALUE, for SIGIO signals.

     FD_CLOEXEC
          The value used to indicate the "close on exec" flag with F_GETFL or F_SETFL.

 -- primitive: select reads writes excepts [secs] [usecs]
     READS, WRITES and EXCEPTS can be lists or vectors: it doesn't matter which, but the corresponding object returned will be of the same type. Each element is a port or file descriptor on which to wait for readability, writeability or exceptional conditions respectively.  SECS and USECS optionally specify a timeout: SECS can be specified alone, as either an integer or a real number, or both SECS and USECS can be specified as integers, in which case USECS is an additional timeout expressed in microseconds.

     Buffered input or output data is (currently, but this may change) ignored: select uses the underlying file descriptor of a port (char-ready? will check input buffers, output buffers are problematic).

     The return value is a list of subsets of the input lists or vectors for which the requested condition has been met.

     It is not quite compatible with scsh's select: scsh checks port buffers, doesn't accept input lists or a microsecond timeout, returns multiple values instead of a list and has an additional select! interface.

File System
***********

These procedures allow querying and setting file system attributes (such as owner, permissions, sizes and types of files); deleting, copying, renaming and linking files; creating and removing directories and querying their contents; syncing the file system and creating special files.

 -- primitive: access? path how
     Returns #t if PATH corresponds to an existing file and the current process has the type of access specified by HOW, otherwise #f. HOW should be specified using the values of the variables listed below.  Multiple values can be combined using a bitwise or, in which case #t will only be returned if all accesses are granted.

     Permissions are checked using the real id of the current process, not the effective id, although it's the effective id which determines whether the access would actually be granted.

      -- Variable: R_OK
          test for read permission.

      -- Variable: W_OK
          test for write permission.

      -- Variable: X_OK
          test for execute permission.

      -- Variable: F_OK
          test for existence of the file.

 -- primitive: stat obj
     Returns an object containing various information about the file determined by OBJ. OBJ can be a string containing a file name or a port or integer file descriptor which is open on a file (in which case fstat is used as the underlying system call).

     The object returned by stat can be passed as a single parameter to the following procedures, all of which return integers:

     stat:dev
          The device containing the file.

     stat:ino
          The file serial number, which distinguishes this file from all other files on the same device.

     stat:mode
          The mode of the file.  This includes file type information and the file permission bits.  See stat:type and stat:perms below.

     stat:nlink
          The number of hard links to the file.

     stat:uid
          The user ID of the file's owner.

     stat:gid
          The group ID of the file.

     stat:rdev
          Device ID; this entry is defined only for character or block special files.

     stat:size
          The size of a regular file in bytes.

     stat:atime
          The last access time for the file.

     stat:mtime
          The last modification time for the file.

     stat:ctime
          The last modification time for the attributes of the file.

     stat:blksize
          The optimal block size for reading or writing the file, in bytes.

     stat:blocks
          The amount of disk space that the file occupies measured in units of 512 byte blocks.

     In addition, the following procedures return the information from stat:mode in a more convenient form:

     stat:type
          A symbol representing the type of file.  Possible values are regular, directory, symlink, block-special, char-special, fifo, socket and unknown

     stat:perms
          An integer representing the access permission bits.

 -- primitive: lstat path
     Similar to stat, but does not follow symbolic links, i.e., it will return information about a symbolic link itself, not the file it points to.  PATH must be a string.

 -- primitive: readlink path
     Returns the value of the symbolic link named by PATH (a string), i.e., the file that the link points to.

 -- primitive: chown obj owner group
     Change the ownership and group of the file referred to by OBJ to the integer userid values OWNER and GROUP.  OBJ can be a string containing a file name or a port or integer file descriptor which is open on the file (in which case fchown is used as the underlying system call).  The return value is unspecified.

     If OBJ is a symbolic link, either the ownership of the link or the ownership of the referenced file will be changed depending on the operating system (lchown is unsupported at present).  If OWNER or GROUP is specified as -1, then that ID is not changed.

 -- primitive: chmod obj mode
     Changes the permissions of the file referred to by OBJ. OBJ can be a string containing a file name or a port or integer file descriptor which is open on a file (in which case fchmod is used as the underlying system call). MODE specifies the new permissions as a decimal number, e.g., (chmod "foo" #o755). The return value is unspecified.

 -- primitive: utime path [actime] [modtime]
     utime sets the access and modification times for the file named by PATH.  If ACTIME or MODTIME is not supplied, then the current time is used. ACTIME and MODTIME must be integer time values as returned by the current-time procedure.

     E.g.,

          (utime "foo" (- (current-time) 3600))

     will set the access time to one hour in the past and the modification time to the current time.

 -- primitive: delete-file path
     Deletes (or "unlinks") the file specified by PATH.

 -- primitive: copy-file path-from path-to
     Copy the file specified by PATH-FROM to PATH-TO. The return value is unspecified.

 -- primitive: rename-file path-from path-to
     Renames the file specified by PATH-FROM to PATH-TO. The return value is unspecified.

 -- primitive: link path-from path-to
     Creates a new name PATH-TO in the file system for the file named by PATH-FROM.  If PATH-FROM is a symbolic link, the link may or may not be followed depending on the system.

 -- primitive: symlink path-from path-to
     Create a symbolic link named PATH-TO with the value (i.e., pointing to) PATH-FROM.  The return value is unspecified.

 -- primitive: mkdir path [mode]
     Create a new directory named by PATH.  If MODE is omitted then the permissions of the directory file are set using the current umask.  Otherwise they are set to the decimal value specified with MODE.  The return value is unspecified.

 -- primitive: rmdir path
     Remove the existing directory named by PATH.  The directory must be empty for this to succeed.  The return value is unspecified.

 -- primitive: opendir path
     Open the directory specified by PATH and return a directory stream.

 -- primitive: directory-stream? object
     Returns a boolean indicating whether OBJECT is a directory stream as returned by opendir.

 -- primitive: readdir stream
     Return (as a string) the next directory entry from the directory stream STREAM.  If there is no remaining entry to be read then the end of file object is returned.

 -- primitive: rewinddir stream
     Reset the directory port STREAM so that the next call to readdir will return the first directory entry.

 -- primitive: closedir stream
     Close the directory stream STREAM. The return value is unspecified.

 -- primitive: sync
     Flush the operating system disk buffers. The return value is unspecified.

 -- primitive: mknod path type perms dev
     Creates a new special file, such as a file corresponding to a device. PATH specifies the name of the file.  TYPE should be one of the following symbols: regular, directory, symlink, block-special, char-special, fifo, or socket.  PERMS (an integer) specifies the file permissions. DEV (an integer) specifies which device the special file refers to.  Its exact interpretation depends on the kind of special file being created.

     E.g.,

          (mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2))

     The return value is unspecified.

 -- primitive: tmpnam
     Create a new file in the file system with a unique name.  The return value is the name of the new file.  This function is implemented with the tmpnam function in the system libraries.

User Information
****************

The facilities in this section provide an interface to the user and group database. They should be used with care since they are not reentrant.

The following functions accept an object representing user information and return a selected component:

passwd:name
     The name of the userid.

passwd:passwd
     The encrypted passwd.

passwd:uid
     The user id number.

passwd:gid
     The group id number.

passwd:gecos
     The full name.

passwd:dir
     The home directory.

passwd:shell
     The login shell.

 -- procedure: getpwuid uid
     Look up an integer userid in the user database.

 -- procedure: getpwnam name
     Look up a user name string in the user database.

 -- procedure: setpwent
     Initializes a stream used by getpwent to read from the user database. The next use of getpwent will return the first entry.  The return value is unspecified.

 -- procedure: getpwent
     Return the next entry in the user database, using the stream set by setpwent.

 -- procedure: endpwent
     Closes the stream used by getpwent.  The return value is unspecified.

 -- primitive: setpw [reset?]
     If called with a true argument, initialize or reset the password data stream.  Otherwise, close the stream.  The setpwent and endpwent procedures are implemented on top of this.

 -- primitive: getpw [obj]
     Look up an entry in the user database.  OBJ can be an integer, a string, or omitted, giving the behaviour of getpwuid, getpwnam or getpwent respectively.

The following functions accept an object representing group information and return a selected component:

group:name
     The group name.

group:passwd
     The encrypted group password.

group:gid
     The group id number.

group:mem
     A list of userids which have this group as a supplimentary group.

 -- procedure: getgrgid gid
     Look up an integer groupid in the group database.

 -- procedure: getgrnam name
     Look up a group name in the group database.

 -- procedure: setgrent
     Initializes a stream used by getgrent to read from the group database. The next use of getgrent will return the first entry. The return value is unspecified.

 -- procedure: getgrent
     Return the next entry in the group database, using the stream set by setgrent.

 -- procedure: endgrent
     Closes the stream used by getgrent. The return value is unspecified.

 -- primitive: setgr [reset?]
     If called with a true argument, initialize or reset the group data stream.  Otherwise, close the stream.  The setgrent and endgrent procedures are implemented on top of this.

 -- primitive: getgr [obj]
     Look up an entry in the group database.  OBJ can be an integer, a string, or omitted, giving the behaviour of getgrgid, getgrnam or getgrent respectively.

Time
****

 -- primitive: current-time
     Returns the number of seconds since 1970-01-01 00:00:00 UTC, excluding leap seconds.

 -- primitive: gettimeofday
     Returns a pair containing the number of seconds and microseconds since 1970-01-01 00:00:00 UTC, excluding leap seconds.  Note: whether true microsecond resolution is available depends on the operating system.

The following procedures either accept an object representing a broken down time and return a selected component, or accept an object representing a broken down time and a value and set the component to the value. The numbers in parentheses give the usual range.

tm:sec, set-tm:sec
     Seconds (0-59).

tm:min, set-tm:min
     Minutes (0-59).

tm:hour, set-tm:hour
     Hours (0-23).

tm:mday, set-tm:mday
     Day of the month (1-31).

tm:mon, set-tm:mon
     Month (0-11).

tm:year, set-tm:year
     Year (70-), the year minus 1900.

tm:wday, set-tm:wday
     Day of the week (0-6) with Sunday represented as 0.

tm:yday, set-tm:yday
     Day of the year (0-364, 365 in leap years).

tm:isdst, set-tm:isdst
     Daylight saving indicator (0 for "no", greater than 0 for "yes", less than 0 for "unknown").

tm:gmtoff, set-tm:gmtoff
     Time zone offset in seconds west of UTC (-46800 to 43200).

tm:zone, set-tm:zone
     Time zone label (a string), not necessarily unique.

 -- primitive: localtime time [zone]
     Returns an object representing the broken down components of TIME, an integer like the one returned by current-time.  The time zone for the calculation is optionally specified by ZONE (a string), otherwise the TZ environment variable or the system default is used.

 -- primitive: gmtime time
     Returns an object representing the broken down components of TIME, an integer like the one returned by current-time.  The values are calculated for UTC.

 -- primitive: mktime bd-time [zone]
     BD-TIME is an object representing broken down time and zone is an optional time zone specifier (otherwise the TZ environment variable or the system default is used).

     Returns a pair: the CAR is a corresponding integer time value like that returned by current-time; the CDR is a broken down time object, similar to as BD-TIME but with normalized values.

 -- primitive: tzset
     Initialize the timezone from the TZ environment variable or the system default.  Usually this is done automatically by other procedures which use the time zone, but this procedure may need to be used if TZ is changed.

 -- primitive: strftime template time
     Formats a time specification TIME using TEMPLATE.  TIME is an object with time components in the form returned by localtime or gmtime.  TEMPLATE is a string which can include formatting specifications introduced by a % character.  The formatting of month and day names is dependent on the current locale.  The value returned is the formatted string. See Formatting Date and Time (The GNU C Library Reference Manual).)

 -- primitive: strptime template string
     Performs the reverse action to strftime, parsing STRING according to the specification supplied in TEMPLATE.  The interpretation of month and day names is dependent on the current locale.  The value returned is a pair.  The CAR has an object with time components in the form returned by localtime or gmtime, but the time zone components are not usefully set. The CDR reports the number of characters from STRING which were used for the conversion.

 -- Variable: internal-time-units-per-second
     The value of this variable is the number of time units per second reported by the following procedures.

 -- primitive: times
     Returns an object with information about real and processor time. The following procedures accept such an object as an argument and return a selected component:

     tms:clock
          The current real time, expressed as time units relative to an arbitrary base.

     tms:utime
          The CPU time units used by the calling process.

     tms:stime
          The CPU time units used by the system on behalf of the calling process.

     tms:cutime
          The CPU time units used by terminated child processes of the calling process, whose status has been collected (e.g., using waitpid).

     tms:cstime
          Similarly, the CPU times units used by the system on behalf of terminated child processes.

 -- primitive: get-internal-real-time
     Returns the number of time units since the interpreter was started.

 -- primitive: get-internal-run-time
     Returns the number of time units of processor time used by the interpreter. Both "system" and "user" time are included but subprocesses are not.

Processes
*********

 -- primitive: chdir path
     Change the current working directory to PATH. The return value is unspecified.

 -- primitive: getcwd
     Returns the name of the current working directory.

 -- primitive: umask [mode]
     If MODE is omitted, retuns a decimal number representing the current file creation mask.  Otherwise the file creation mask is set to MODE and the previous value is returned.

     E.g., (umask #o022) sets the mask to octal 22, decimal 18.

 -- primitive: getpid
     Returns an integer representing the current process ID.

 -- primitive: getgroups
     Returns a vector of integers representing the current supplimentary group IDs.

 -- primitive: getppid
     Returns an integer representing the process ID of the parent process.

 -- primitive: getuid
     Returns an integer representing the current real user ID.

 -- primitive: getgid
     Returns an integer representing the current real group ID.

 -- primitive: geteuid
     Returns an integer representing the current effective user ID. If the system does not support effective IDs, then the real ID is returned.  (feature? 'EIDs) reports whether the system supports effective IDs.

 -- primitive: getegid
     Returns an integer representing the current effective group ID. If the system does not support effective IDs, then the real ID is returned.  (feature? 'EIDs) reports whether the system supports effective IDs.

 -- primitive: setuid id
     Sets both the real and effective user IDs to the integer ID, provided the process has appropriate privileges. The return value is unspecified.

 -- primitive: setgid id
     Sets both the real and effective group IDs to the integer ID, provided the process has appropriate privileges. The return value is unspecified.

 -- primitive: seteuid id
     Sets the effective user ID to the integer ID, provided the process has appropriate privileges.  If effective IDs are not supported, the real ID is set instead -- (feature? 'EIDs) reports whether the system supports effective IDs. The return value is unspecified.

 -- primitive: setegid id
     Sets the effective group ID to the integer ID, provided the process has appropriate privileges.  If effective IDs are not supported, the real ID is set instead -- (feature? 'EIDs) reports whether the system supports effective IDs. The return value is unspecified.

 -- primitive: getpgrp
     Returns an integer representing the current process group ID. This is the POSIX definition, not BSD.

 -- primitive: setpgid pid pgid
     Move the process PID into the process group PGID.  PID or PGID must be integers: they can be zero to indicate the ID of the current process. Fails on systems that do not support job control. The return value is unspecified.

 -- primitive: setsid
     Creates a new session.  The current process becomes the session leader and is put in a new process group.  The process will be detached from its controlling terminal if it has one. The return value is an integer representing the new process group ID.

 -- primitive: waitpid pid [options]
     This procedure collects status information from a child process which has terminated or (optionally) stopped.  Normally it will suspend the calling process until this can be done.  If more than one child process is eligible then one will be chosen by the operating system.

     The value of PID determines the behaviour:

     PID greater than 0
          Request status information from the specified child process.

     PID equal to -1 or WAIT_ANY
          Request status information for any child process.

     PID equal to 0 or WAIT_MYPGRP
          Request status information for any child process in the current process group.

     PID less than -1
          Request status information for any child process whose process group ID is -PID.

     The OPTIONS argument, if supplied, should be the bitwise OR of the values of zero or more of the following variables:

      -- Variable: WNOHANG
          Return immediately even if there are no child processes to be collected.

      -- Variable: WUNTRACED
          Report status information for stopped processes as well as terminated processes.

     The return value is a pair containing:

      1. The process ID of the child process, or 0 if WNOHANG was specified and no process was collected.

      2. The integer status value.

The following three functions can be used to decode the process status code returned by waitpid.

 -- primitive: status:exit-val status
     Returns the exit status value, as would be set if a process ended normally through a call to exit or _exit, if any, otherwise #f.

 -- primitive: status:term-sig status
     Returns the signal number which terminated the process, if any, otherwise #f.

 -- primitive: status:stop-sig status
     Returns the signal number which stopped the process, if any, otherwise #f.

 -- primitive: system [cmd]
     Executes CMD using the operating system's "command processor". Under Unix this is usually the default shell sh.  The value returned is CMD's exit status as returned by waitpid, which can be interpreted using the functions above.

     If system is called without arguments, it returns a boolean indicating whether the command processor is available.

 -- primitive: primitive-exit [status]
     Terminate the current process without unwinding the Scheme stack. This is would typically be useful after a fork.  The exit status is STATUS if supplied, otherwise zero.

 -- primitive: execl path [arg] ...
     Executes the file named by PATH as a new process image. The remaining arguments are supplied to the process; from a C program they are accessable as the argv argument to main. Conventionally the first ARG is the same as PATH. All arguments must be strings.

     If ARG is missing, PATH is executed with a null argument list, which may have system-dependent side-effects.

     This procedure is currently implemented using the execv system call, but we call it execl because of its Scheme calling interface.

 -- primitive: execlp path [arg] ...
     Similar to execl, however if FILENAME does not contain a slash then the file to execute will be located by searching the directories listed in the PATH environment variable.

     This procedure is currently implemented using the execlv system call, but we call it execlp because of its Scheme calling interface.

 -- primitive: execle path env [arg] ...
     Similar to execl, but the environment of the new process is specified by ENV, which must be a list of strings as returned by the environ procedure.

     This procedure is currently implemented using the execve system call, but we call it execle because of its Scheme calling interface.

 -- primitive: primitive-fork
     Creates a new "child" process by duplicating the current "parent" process. In the child the return value is 0.  In the parent the return value is the integer process ID of the child.

     This procedure has been renamed from fork to avoid a naming conflict with the scsh fork.

 -- primitive: getenv name
     Looks up the string NAME in the current environment.  The return value is #f unless a string of the form NAME=VALUE is found, in which case the string VALUE is returned.

 -- primitive: putenv string
     Modifies the environment of the current process, which is also the default environment inherited by child processes.

     If STRING is of the form NAME=VALUE then it will be written directly into the environment, replacing any existing environment string with name matching NAME.  If STRING does not contain an equal sign, then any existing string with name matching STRING will be removed.

     The return value is unspecified.

 -- procedure: setenv name value
     Modifies the environment of the current process, which is also the default environment inherited by child processes.

     If VALUE is #f, then NAME is removed from the environment.  Otherwise, the string NAME=VALUE is added to the environment, replacing any existing string with name matching NAME.

     The return value is unspecified.

 -- primitive: environ [env]
     If ENV is omitted, returns the current environment as a list of strings. Otherwise it sets the current environment, which is also the default environment for child processes, to the supplied list of strings. Each member of ENV should be of the form NAME=VALUE and values of NAME should not be duplicated. If ENV is supplied then the return value is unspecified.

 -- primitive: nice incr
     Increment the priority of the current process by INCR.  A higher priority value means that the process runs less often. The return value is unspecified.

Signals
*******

Procedures to raise, handle and wait for signals.

 -- primitive: kill pid sig

     Sends a signal to the specified process or group of processes.

     PID specifies the processes to which the signal is sent:

     PID greater than 0
          The process whose identifier is PID.

     PID equal to 0
          All processes in the current process group.

     PID less than -1
          The process group whose identifier is -PID

     PID equal to -1
          If the process is privileged, all processes except for some special system processes.  Otherwise, all processes with the current effective user ID.

     SIG should be specified using a variable corresponding to the Unix symbolic name, e.g.,

      -- Variable: SIGHUP
          Hang-up signal.

      -- Variable: SIGINT
          Interrupt signal.

 -- primitive: raise sig

     Sends a specified signal SIG to the current process, where SIG is as described for the kill procedure.

 -- primitive: sigaction signum [action] [flags]

     Install or report the signal hander for a specified signal.

     SIGNUM is the signal number, which can be specified using the value of variables such as SIGINT.

     If ACTION is omitted, sigaction returns a pair: the CAR is the current signal hander, which will be either an integer with the value SIG_DFL (default action) or SIG_IGN (ignore), or the Scheme procedure which handles the signal, or #f if a non-Scheme procedure handles the signal.  The CDR contains the current sigaction flags for the handler.

     If ACTION is provided, it is installed as the new handler for SIGNUM.  ACTION can be a Scheme procedure taking one argument, or the value of SIG_DFL (default action) or SIG_IGN (ignore), or #f to restore whatever signal handler was installed before sigaction was first used.  Flags can optionally be specified for the new handler (SA_RESTART will always be added if it's available and the system is using rstartable system calls.)  The return value is a pair with information about the old handler as described above.

     This interface does not provide access to the "signal blocking" facility.  Maybe this is not needed, since the thread support may provide solutions to the problem of consistent access to data structures.

 -- primitive: restore-signals
     Return all signal handlers to the values they had before any call to sigaction was made.  The return value is unspecified.

 -- primitive: alarm secs
     Set a timer to raise a SIGALRM signal after the specified number of seconds (an integer).  It's advisable to install a signal handler for SIGALRM beforehand, since the default action is to terminate the process.

     The return value indicates the time remaining for the previous alarm, if any.  The new value replaces the previous alarm.  If there was no previous alarm, the return value is zero.

 -- primitive: pause
     Pause the current process (thread?) until a signal arrives whose action is to either terminate the current process or invoke a handler procedure.  The return value is unspecified.

 -- primitive: sleep secs
     Wait for the given number of seconds (an integer) or until a signal arrives.  The return value is zero if the time elapses or the number of seconds remaining otherwise.

Terminals and Ptys
******************

 -- primitive: isatty? port
     Returns #t if PORT is using a serial non-file device, otherwise #f.

 -- primitive: ttyname port
     Returns a string with the name of the serial terminal device underlying PORT.

 -- primitive: ctermid
     Returns a string containing the file name of the controlling terminal for the current process.

 -- primitive: tcgetpgrp port
     Returns the process group ID of the foreground process group associated with the terminal open on the file descriptor underlying PORT.

     If there is no foreground process group, the return value is a number greater than 1 that does not match the process group ID of any existing process group.  This can happen if all of the processes in the job that was formerly the foreground job have terminated, and no other job has yet been moved into the foreground.

 -- primitive: tcsetpgrp port pgid
     Set the foreground process group ID for the terminal used by the file descriptor underlying PORT to the integer PGID. The calling process must be a member of the same session as PGID and must have the same controlling terminal.  The return value is unspecified.

Pipes
*****

The following procedures provide an interface to the popen and pclose system routines.  The code is in a separate "popen" module:

     (use-modules (ice-9 popen))

 -- primitive: open-pipe command modes
     Executes the shell command COMMAND (a string) in a subprocess. A pipe to the process is created and returned.  MODES specifies whether an input or output pipe to the process is created: it should be the value of OPEN_READ or OPEN_WRITE.

 -- procedure: open-input-pipe command
     Equivalent to (open-pipe command OPEN_READ).

 -- procedure: open-output-pipe command
     Equivalent to (open-pipe command OPEN_WRITE).

 -- primitive: close-pipe port
     Closes the pipe created by open-pipe, then waits for the process to terminate and returns its status value, see waitpid, for information on how to interpret this value.

     close-port (see close-port) can also be used to close a pipe, but doesn't return the status.

Networking
**********

Networking Databases
====================

This section describes procedures which convert internet addresses and query various network databases.  Care should be taken when using the database routines since they are not reentrant.

 -- primitive: inet-aton address
     Converts a string containing an Internet host address in the traditional dotted decimal notation into an integer.

          (inet-aton "127.0.0.1") => 2130706433

 -- primitive: inet-ntoa number
     Converts an integer Internet host address into a string with the traditional dotted decimal representation.

          (inet-ntoa 2130706433) => "127.0.0.1"

 -- primitive: inet-netof address
     Returns the network number part of the given integer Internet address.

          (inet-netof 2130706433) => 127

 -- primitive: inet-lnaof address
     Returns the local-address-with-network part of the given Internet address.

          (inet-lnaof 2130706433) => 1

 -- primitive: inet-makeaddr net lna
     Makes an Internet host address by combining the network number NET with the local-address-within-network number LNA.

          (inet-makeaddr 127 1) => 2130706433

A host object is a structure that represents what is known about a network host, and is the usual way of representing a system's network identity inside software.  The hostent functions accept a host object and return some information about that host's presence on the network.

 -- procedure: hostent:name host
     The "official" hostname for HOST.

 -- procedure: hostent:aliases host
     A list of aliases for HOST.

 -- procedure: hostent:addrtype host
     The host address type.  For hosts with Internet addresses, this will return AF_INET.

 -- procedure: hostent:length host
     The length of each address for HOST, in bytes.

 -- procedure: hostent:addr-list host
     The list of network addresses associated with HOST.

The gethost functions are used to find a particular host's entry in the host database.

 -- primitive: gethost [host]
 -- procedure: gethostbyname hostname
 -- procedure: gethostbyaddr address
     Look up a host by name or address, and return a host object.  The gethost procedure will accept either a string name or an integer address; if given no arguments, it behaves like gethostent (see below).

The following three procedures may be used to step through the host database from beginning to end.  [FIXME: document the `sethost' variant of `sethostent'.]

 -- procedure: sethostent
     Initialize an internal stream from which host objects may be read.  This procedure must be called before any calls to gethostent, and may also be called afterward to reset the host entry stream.

 -- procedure: gethostent
     Return the next host object from the host database, or #f if there are no more hosts to be found (or an error has been encountered). This procedure may not be used before sethostent has been called.

 -- procedure: endhostent
     Close the stream used by gethostent.  The return value is unspecified.

The netent functions accept an object representing a network and return a selected component.

 -- procedure: netent:name net
     The "official" network name.

 -- procedure: netent:aliases net
     A list of aliases for the network.

 -- procedure: netent:addrtype net
     The type of the network number.  Currently, this returns only AF_INET.

 -- procedure: netent:net net
     The network number.

The getnet procedures look up a particular network in the network database.  Each returns a network object that describes the network's technical nature.

 -- primitive: getnet [net]
 -- procedure: getnetbyname net-name
 -- procedure: getnetbyaddr net-number
     Look up a network by name or net number in the network database.  The NET-NAME argument must be a string, and the NET-NUMBER argument must be an integer.  getnet will accept either type of argument, behaving like getnetent (see below) if no arguments are given.

[FIXME: document the `setnet' variant of `setnetent'.]

 -- procedure: setnetent
     Initializes a stream used by getnetent to read from the network database. The next use of getnetent will return the first entry.  The return value is unspecified.

 -- procedure: getnetent
     Return the next entry from the network database.

 -- procedure: endnetent
     Close the stream used by getnetent.  The return value is unspecified.

The protoent procedures accept an object representing a protocol and return a selected component.

 -- procedure: protoent:name protocol
     The "official" protocol name.

 -- procedure: protoent:aliases protocol
     A list of aliases for the protocol.

 -- procedure: protoent:proto protocol
     The protocol number.

The getproto procedures look up a particular network protocol and return a protocol object.

 -- primitive: getproto [protocol]
 -- procedure: getprotobyname name
 -- procedure: getprotobynumber number
     Look up a network protocol by name or by number.  getprotobyname takes a string argument, and getprotobynumber takes an integer argument.  getproto will accept either type, behaving like getprotoent (see below) if no arguments are supplied.

[FIXME: document the `setproto' variant of `setprotoent'.]

 -- procedure: setprotoent
     Initializes a stream used by getprotoent to read from the protocol database. The next use of getprotoent will return the first entry.  The return value is unspecified.

 -- procedure: getprotoent
     Return the next entry from the protocol database.

 -- procedure: endprotoent
     Close the stream used by getprotoent.  The return value is unspecified.

The servent procedures accept a service object and return some information about that service.

 -- procedure: servent:name serv
     The "official" name of the network service.

 -- procedure: servent:aliases serv
     A list of aliases for the network service.

 -- procedure: servent:port serv
     The Internet port used by the service.

 -- procedure: servent:proto serv
     The protocol used by the service.  A service may be listed many times in the database under different protocol names.

The getserv procedures look up a network service and return a service object which describes the names and network ports conventionally assigned to the service.

 -- primitive: getserv [name protocol]
 -- procedure: getservbyname name protocol
 -- procedure: getservbyport port protocol
     Look up a network service by name or by service number, and return a network service object.  The PROTOCOL argument specifies the name of the desired protocol; if the protocol found in the network service database does not match this name, a system error is signalled.

     The getserv procedure will take either a service name or number as its first argument; if given no arguments, it behaves like getservent (see below).

[FIXME: document the `setserv' variant of `setservent'.]

 -- procedure: setservent
     Initializes a stream used by getservent to read from the services database. The next use of getservent will return the first entry.  The return value is unspecified.

 -- procedure: getservent
     Return the next entry from the services database.

 -- procedure: endservent
     Close the stream used by getservent.  The return value is unspecified.

Network sockets
===============

Socket ports can be created using socket and socketpair. The ports are initially unbuffered, to makes reading and writing to the same port more reliable.  Buffered ports can be obtained with duplicate-port, see Ports and File Descriptors.

These procedures convert Internet addresses and port values between "host" order and "network" order as required.  The arguments and return values should be in "host" order.

 -- primitive: socket family style protocol
     Returns a new socket port of the type specified by FAMILY, STYLE and PROTOCOL.  All three parameters are integers.  Typical values for FAMILY are the values of AF_UNIX and AF_INET.  Typical values for STYLE are the values of SOCK_STREAM, SOCK_DGRAM and SOCK_RAW.

     PROTOCOL can be obtained from a protocol name using getprotobyname.  A value of zero specifies the default protocol, which is usually right.

     A single socket port cannot by used for communication until it has been connected to another socket.

 -- primitive: socketpair family style protocol
     Returns a pair of connected (but unnamed) socket ports of the type specified by FAMILY, STYLE and PROTOCOL. Many systems support only socket pairs of the AF_UNIX family.  Zero is likely to be the only meaningful value for PROTOCOL.

 -- primitive: getsockopt socket level optname
     Returns the value of a particular socket option for the socket port SOCKET.  LEVEL is an integer code for type of option being requested, e.g., SOL_SOCKET for socket-level options. OPTNAME is an integer code for the option required and should be specified using one of the symbols SO_DEBUG, SO_REUSEADDR etc.

     The returned value is typically an integer but SO_LINGER returns a pair of integers.

 -- primitive: setsockopt socket level optname value
     Sets the value of a particular socket option for the socket port SOCKET.  LEVEL is an integer code for type of option being set, e.g., SOL_SOCKET for socket-level options. OPTNAME is an integer code for the option to set and should be specified using one of the symbols SO_DEBUG, SO_REUSEADDR etc. VALUE is the value to which the option should be set.  For most options this must be an integer, but for SO_LINGER it must be a pair.

     The return value is unspecified.

 -- primitive: shutdown socket how
     Sockets can be closed simply by using close-port. The shutdown procedure allows reception or tranmission on a connection to be shut down individually, according to the parameter HOW:

     0
          Stop receiving data for this socket.  If further data arrives,  reject it.

     1
          Stop trying to transmit data from this socket.  Discard any data waiting to be sent.  Stop looking for acknowledgement of data already sent; don't retransmit it if it is lost.

     2
          Stop both reception and transmission.

     The return value is unspecified.

 -- primitive: connect socket family address [arg ...]
     Initiates a connection from SOCKET to the address specified by ADDRESS and possibly ARG ....  The format required for ADDRESS and ARG ... depends on the family of the socket.

     For a socket of family AF_UNIX, only address is specified and must be a string with the filename where the socket is to be created.

     For a socket of family AF_INET, address must be an integer Internet host address and ARG ... must be a single integer port number.

     The return value is unspecified.

 -- primitive: bind socket family address arg ...
     Assigns an address to the socket port SOCKET. Generally this only needs to be done for server sockets, so they know where to look for incoming connections.  A socket without an address will be assigned one automatically when it starts communicating.

     The format of ADDRESS and ARG ... depends on the family of the socket.

     For a socket of family AF_UNIX, only ADDRESS is specified and must be a string with the filename where the socket is to be created.

     For a socket of family AF_INET, ADDRESS must be an integer Internet host address and ARG ... must be a single integer port number.

     The values of the following variables can also be used for ADDRESS:

      -- Variable: INADDR_ANY
          Allow connections from any address.

      -- Variable: INADDR_LOOPBACK
          The address of the local host using the loopback device.

      -- Variable: INADDR_BROADCAST
          The broadcast address on the local network.

      -- Variable: INADDR_NONE
          No address.

     The return value is unspecified.

 -- primitive: listen socket backlog
     This procedure enables SOCKET to accept connection requests.  BACKLOG is an integer specifying the maximum length of the queue for pending connections. If the queue fills, new clients will fail to connect until the server calls accept to accept a connection from the queue.

     The return value is unspecified.

 -- primitive: accept socket
     Accepts a connection on a bound, listening socket SOCKET.  If there are no pending connections in the queue, it waits until one is available unless the non-blocking option has been set on the socket.

     The return value is a pair in which the CAR is a new socket port for the connection and the CDR is an object with address information about the client which initiated the connection.

     If the address is not available then the CDR will be an empty vector.

     SOCKET does not become part of the connection and will continue to accept new requests.

The following functions take a socket address object, as returned by accept and other procedures, and return a selected component.

sockaddr:fam
     The socket family, typically equal to the value of AF_UNIX or AF_INET.

sockaddr:path
     If the socket family is AF_UNIX, returns the path of the filename the socket is based on.

sockaddr:addr
     If the socket family is AF_INET, returns the Internet host address.

sockaddr:port
     If the socket family is AF_INET, returns the Internet port number.

 -- primitive: getsockname socket
     Returns the address of SOCKET, in the same form as the object returned by accept.  On many systems the address of a socket in the AF_FILE namespace cannot be read.

 -- primitive: getpeername socket
     Returns the address of the socket that the socket SOCKET is connected to, in the same form as the object returned by accept.  On many systems the address of a socket in the AF_FILE namespace cannot be read.

 -- primitive: recv! socket buf [flags]
     Receives data from the socket port SOCKET.  SOCKET must already be bound to the address from which data is to be received. BUF is a string into which the data will be written.  The size of BUF limits the amount of data which can be received: in the case of packet protocols, if a packet larger than this limit is encountered then some data will be irrevocably lost.

     The optional FLAGS argument is a value or bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.

     The value returned is the number of bytes read from the socket.

     Note that the data is read directly from the socket file descriptor: any unread buffered port data is ignored.

 -- primitive: send socket message [flags]
     Transmits the string MESSAGE on the socket port SOCKET. SOCKET must already be bound to a destination address.  The value returned is the number of bytes transmitted -- it's possible for this to be less than the length of MESSAGE if the socket is set to be non-blocking.  The optional FLAGS argument is a value or bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.

     Note that the data is written directly to the socket file descriptor: any unflushed buffered port data is ignored.

 -- primitive: recvfrom! socket buf [flags] [start] [end]
     Returns data from the socket port SOCKET and also information about where the data was received from.  SOCKET must already be bound to the address from which data is to be received. buf, is a string into which the data will be written.  The size of BUF limits the amount of data which can be received: in the case of packet protocols, if a packet larger than this limit is encountered then some data will be irrevocably lost.

     The optional FLAGS argument is a value or bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.

     The value returned is a pair: the CAR is the number of bytes read from the socket and the CDR an address object in the same form as returned by accept.

     The START and END arguments specify a substring of BUF to which the data should be written.

     Note that the data is read directly from the socket file descriptor: any unread buffered port data is ignored.

 -- primitive: sendto socket message family address arg ... [flags]
     Transmits the string MESSAGE on the socket port SOCKET.  The destination address is specified using the FAMILY, ADDRESS and ARG arguments, in a similar way to the connect procedure.  The value returned is the number of bytes transmitted -- it's possible for this to be less than the length of MESSAGE if the socket is set to be non-blocking.  The optional FLAGS argument is a value or bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.

     Note that the data is written directly to the socket file descriptor: any unflushed buffered port data is ignored.

System Identification
*********************

 -- primitive: uname
     Returns an object with some information about the computer system the program is running on.

The following procedures accept an object as returned by uname and return a selected component.

utsname:sysname
     The name of the operating system.

utsname:nodename
     The network name of the computer.

utsname:release
     The current release level of the operating system implementation.

utsname:version
     The current version level within the release of the operating system.

utsname:machine
     A description of the hardware.

 -- primitive: software-type
     Return a symbol describing the current platform's operating system. This may be one of AIX, VMS, UNIX, COHERENT, WINDOWS, MS-DOS, OS/2, THINKC, AMIGA, ATARIST, MACH, or ACORN.

     Note that most varieties of Unix are considered to be simply "UNIX". That is because when a program depends on features that are not present on every operating system, it is usually better to test for the presence or absence of that specific feature.  The return value of software-type should only be used for this purpose when there is no other easy or unambiguous way of detecting such features.

Locales
*******

 -- primitive: setlocale category [locale]
     If LOCALE is omitted, returns the current value of the specified locale category as a system-dependent string. CATEGORY should be specified using the values LC_COLLATE, LC_ALL etc.

     Otherwise the specified locale category is set to the string LOCALE and the new value is returned as a system-dependent string.  If LOCALE is an empty string, the locale will be set using envirionment variables.

Expect
******

The macros in this section are made available with:

     (use-modules (ice-9 expect))

expect is a macro for selecting actions based on the output from a port.  The name comes from a tool of similar functionality by Don Libes. Actions can be taken when a particular string is matched, when a timeout occurs, or when end-of-file is seen on the port.  The expect macro is described below; expect-strings is a front-end to expect based on regexec (see the regular expression documentation).

 -- Macro: expect-strings clause ...
     By default, expect-strings will read from the current input port. The first term in each clause consists of an expression evaluating to a string pattern (regular expression).  As characters are read one-by-one from the port, they are accumulated in a buffer string which is matched against each of the patterns.  When a pattern matches, the remaining expression(s) in the clause are evaluated and the value of the last is returned.  For example:

          (with-input-from-file "/etc/passwd"
            (lambda ()
              (expect-strings
                ("^nobody" (display "Got a nobody user.\n")
                           (display "That's no problem.\n"))
                ("^daemon" (display "Got a daemon user.\n")))))

     The regular expression is compiled with the REG_NEWLINE flag, so that the ^ and $ anchors will match at any newline, not just at the start and end of the string.

     There are two other ways to write a clause:

     The expression(s) to evaluate can be omitted, in which case the result of the regular expression match (converted to strings, as obtained from regexec with match-pick set to "") will be returned if the pattern matches.

     The symbol => can be used to indicate that the expression is a procedure which will accept the result of a successful regular expression match.  E.g.,

          ("^daemon" => write)
          ("^d\\(aemon\\)" => (lambda args (for-each write args)))
          ("^da\\(em\\)on" => (lambda (all sub)
                                   (write all) (newline)
                                   (write sub) (newline)))

     The order of the substrings corresponds to the order in which the opening brackets occur.

     A number of variables can be used to control the behaviour of expect (and expect-strings). Most have default top-level bindings to the value #f, which produces the default behaviour. They can be redefined at the top level or locally bound in a form enclosing the expect expression.

     expect-port
          A port to read characters from, instead of the current input port.

     expect-timeout
          expect will terminate after this number of seconds, returning #f or the value returned by expect-timeout-proc.

     expect-timeout-proc
          A procedure called if timeout occurs.  The procedure takes a single argument: the accumulated string.

     expect-eof-proc
          A procedure called if end-of-file is detected on the input port.  The procedure takes a single argument: the accumulated string.

     expect-char-proc
          A procedure to be called every time a character is read from the port.  The procedure takes a single argument: the character which was read.

     expect-strings-compile-flags
          Flags to be used when compiling a regular expression, which are passed to make-regexp see Regexp Functions.  The default value is regexp/newline.

     expect-strings-exec-flags
          Flags to be used when executing a regular expression, which are passed to regexp-exec see Regexp Functions. The default value is regexp/noteol, which prevents $ from matching the end of the string while it is still accumulating, but still allows it to match after a line break or at the end of file.

     Here's an example using all of the variables:

          (let ((expect-port (open-input-file "/etc/passwd"))
                (expect-timeout 1)
                (expect-timeout-proc
                  (lambda (s) (display "Times up!\n")))
                (expect-eof-proc
                  (lambda (s) (display "Reached the end of the file!\n")))
                (expect-char-proc display)
                (expect-strings-compile-flags (logior regexp/newline regexp/icase))
                (expect-strings-exec-flags 0))
             (expect-strings
               ("^nobody"  (display "Got a nobody user\n"))))

 -- Macro: expect clause ...
     expect is used in the same way as expect-strings, but tests are specified not as patterns, but as procedures.  The procedures are called in turn after each character is read from the port, with two arguments: the value of the accumulated string and a flag to indicate whether end-of-file has been reached.  The flag will usually be #f, but if end-of-file is reached, the procedures are called an additional time with the final accumulated string and #t.

     The test is successful if the procedure returns a non-false value.

     If the => syntax is used, then if the test succeeds it must return a list containing the arguments to be provided to the corresponding expression.

     In the following example, a string will only be matched at the beginning of the file:

          (let ((expect-port (open-input-file "/etc/passwd")))
            (expect
               ((lambda (s) (string=? s "fnord!"))
                  (display "Got a nobody user!\n"))))

     The control variables described for expect-strings also influence the behaviour of expect, with the exception of variables whose names begin with expect-strings-.

The Scheme shell (scsh)
***********************

An incomplete port of the Scheme shell (scsh) 0.5.1 is available for Guile.  The idea is to allow Scheme code using scsh interfaces to be run inside the Guile interpreter.

For information about scsh on the Web see http://www-swiss.ai.mit.edu/scsh/scsh.html. The original scsh is available by ftp from ftp://swiss-ftp.ai.mit.edu:/pub/su.

The scsh code is distributed as a separate module, guile-scsh, which must be installed somewhere in Guile's load path before it can be used.  This is similar to the installation of slib (you may want to install that first, since it's needed before scsh can run in Guile: see SLIB for details).

This port of scsh does not currently use the Guile module system, but can be initialized with:

     (load-from-path "scsh/init")

Tcl/Tk Interface
****************