REDUCE

10.10 Generalized Input/Output Streams

All input and output functions are implemented in terms of operations on ”channels”. A channel is just a small integer 1 1The range of channel numbers is from 0 to MaxChannels, where MaxChannels is a system-dependent constant, currently 31, defined in the module io-decls which has 3 functions and some other information associated with it. The three functions are:

  1. A reading function, which is called with the channel as its argument and returns the integer ASCII value of the next character of the input stream. If the channel is for writing only, this function is writeonlychannel. If the channel has not been opened, this function is channelnotopen. The reading function is responsible for echoing characters if the flag *echo is non-nil. It should use the function writechar to echo the character. It may not be appropriate for a read function to echo characters. For example, the ”disk” reading function does echoing, while the reader used to implement the compress function does not.

    The read function must also be concerned with the handling of ends of ”files” (actually, ends of channels) and ends of lines. It should return the ASCII code for an end of file character (system dependent) when reaching the end of a channel. It should return the ASCII code for a line feed character to indicate an end of line (or ”newline”). This may require that the ASCII code for carriage return be ignored when read, not returned.

  2. A writing function, which is called with the channel as its first argument and the integer ASCII value of the character to write as its second argument. If the channel is for reading only, this function is readonlychannel. If the channel has not been opened, this function is channelnotopen.
  3. A closing function, which is called with the channel as its argument and performs any action necessary for the graceful termination of input and/or output operations to that channel. If the channel is not open, this function is channelnotopen.

The other information associated with a channel includes the current position in the output line (used by posn), the maximum line length allowed (used by linelength and the printing functions), the single character input backup buffer (used by the token scanner), and other system-dependent information.

Ordinarily, the user need not be aware of the existence of this mechanism. However, because of its generality, it is possible to implement operations other than just reading from and writing to files using it. In particular, the LISP functions explode and compress are performed by writing to a list and reading from a list, respectively (on channels 3 and 4 respectively).

10.10.1 Using the ”Special” Form of Open

If Open is called with AccessType eq to SPECIAL and the global variables specialreadfunction*, specialwritefunction*, and specialclosefunction* are bound to ids, then a free channel is returned and its associated functions are set to the values of these variables. Other non system-dependent status is set to default conditions, which can later be overridden. The functions readonlychannel and writeonlychannel are available as error handlers. The parameter Filename is used only if an error occurs.

The following globals are used by the functions in this section.

specialclosefunction* = [Initially: nil] global
   
specialreadfunction* = [Initially: nil] global
   
specialwritefunction* = [Initially: nil] global