

@node Streams, Printer, Files, Top
@chapter Streams

@menu
* Stream Concepts::		
* Streams Dictionary::		
@end menu

@node Stream Concepts, Streams Dictionary, Streams, Streams
@section Stream Concepts

@c including concept-streams

@menu
* Introduction to Streams::	
* Stream Variables::		
* Stream Arguments to Standardized Functions::	
* Restrictions on Composite Streams::  
@end menu

@node Introduction to Streams, Stream Variables, Stream Concepts, Stream Concepts
@subsection Introduction to Streams

A @i{stream}
@IGindex{stream}
 is an @i{object} that can be used with an input or output
function to identify an appropriate source or sink of @i{characters} or 
@i{bytes} for that operation.
A @i{character}
@IGindex{character}
 @i{stream}
@IGindex{stream}
 is a source or sink of @i{characters}.
A @i{binary}
@IGindex{binary}
 @i{stream}
@IGindex{stream}
 is a source or sink of @i{bytes}.

Some operations may be performed on any kind of @i{stream};
Figure 21--1 provides a list of @i{standardized} operations
that are potentially useful with any kind of @i{stream}.

@group
@noindent
@w{  close                 stream-element-type  }
@w{  input-stream-p        streamp              }
@w{  interactive-stream-p  with-open-stream     }
@w{  output-stream-p                            }

@noindent
@w{  Figure 21--1: Some General-Purpose Stream Operations}

@end group

Other operations are only meaningful on certain @i{stream} @i{types}.
For example, @b{read-char} is only defined for @i{character} @i{streams}
and @b{read-byte} is only defined for @i{binary} @i{streams}.

@menu
* Abstract Classifications of Streams (Introduction to Streams)::  
* Input::			
* Open and Closed Streams::	
* Interactive Streams::		
* Abstract Classifications of Streams::	 
* File Streams::		
* Other Subclasses of Stream::	
@end menu

@node Abstract Classifications of Streams (Introduction to Streams), Input, Introduction to Streams, Introduction to Streams
@subsubsection Abstract Classifications of Streams

@node Input, Open and Closed Streams, Abstract Classifications of Streams (Introduction to Streams), Introduction to Streams
@subsubsection Input, Output, and Bidirectional Streams

A @i{stream}, whether a @i{character} @i{stream} or a @i{binary} @i{stream},
can be an @i{input}
@IGindex{input}
 @i{stream}
@IGindex{stream}
 (source of data),
       an @i{output}
@IGindex{output}
 @i{stream}
@IGindex{stream}
 (sink for data),
       both, 
    or (@i{e.g.}, when ``@t{:direction :probe}'' is given to @b{open}) neither.

Figure 21--2 shows @i{operators} relating to
@i{input} @i{streams}.

@group
@noindent
@w{  clear-input  read-byte            read-from-string            }
@w{  listen       read-char            read-line                   }
@w{  peek-char    read-char-no-hang    read-preserving-whitespace  }
@w{  read         read-delimited-list  unread-char                 }

@noindent
@w{        Figure 21--2: Operators relating to Input Streams.      }

@end group

Figure 21--3 shows @i{operators} relating to
@i{output} @i{streams}.

@group
@noindent
@w{  clear-output   prin1            write            }
@w{  finish-output  prin1-to-string  write-byte       }
@w{  force-output   princ            write-char       }
@w{  format         princ-to-string  write-line       }
@w{  fresh-line     print            write-string     }
@w{  pprint         terpri           write-to-string  }

@noindent
@w{  Figure 21--3: Operators relating to Output Streams.}

@end group

A @i{stream} that is both an @i{input} @i{stream} and an @i{output} @i{stream}
is called a @i{bidirectional}
@IGindex{bidirectional}
 @i{stream}
@IGindex{stream}
.
See the @i{functions} @b{input-stream-p} and @b{output-stream-p}.

Any of the @i{operators} listed in @i{Figure~21--2} or @i{Figure~21--3}
can be used with @i{bidirectional} @i{streams}.  In addition, Figure 21--4
shows a list of @i{operators} that relate specificaly to 
@i{bidirectional} @i{streams}.

@group
@noindent
@w{  y-or-n-p  yes-or-no-p    }

@noindent
@w{  Figure 21--4: Operators relating to Bidirectional Streams.}

@end group

@node Open and Closed Streams, Interactive Streams, Input, Introduction to Streams
@subsubsection Open and Closed Streams

@i{Streams} are either @i{open}
@IGindex{open}
 or @i{closed}
@IGindex{closed}
.  

Except as explicitly specified otherwise,
operations that create and return @i{streams} return @i{open} @i{streams}.

The action of @i{closing} a @i{stream} marks the end of its use as a source
or sink of data, permitting the @i{implementation} to reclaim its internal data
structures, and to free any external resources which might have been locked by the
@i{stream} when it was opened.

Except as explicitly specified otherwise,
the consequences are undefined when a @i{closed} @i{stream} 
is used where a @i{stream} is called for.

Coercion of @i{streams} to @i{pathnames} 
is permissible for @i{closed} @i{streams};
in some situations, such as for a @i{truename} computation, 
the result might be different for an @i{open} @i{stream}
and for that same @i{stream} once it has been @i{closed}.

@node Interactive Streams, Abstract Classifications of Streams, Open and Closed Streams, Introduction to Streams
@subsubsection Interactive Streams

An @i{interactive stream}
@IGindex{interactive stream}
 is one on which it makes sense to perform
interactive querying.

The precise meaning of an @i{interactive stream} is
@i{implementation-defined}, and may depend on the underlying
operating system.  Some examples of the things that an
@i{implementation} might choose to use as identifying characteristics
of an @i{interactive stream} include:

@table @asis

@item @t{*}  
The @i{stream} is connected to a person (or equivalent) in such a way
  that the program can prompt for information and expect to receive different
  input depending on the prompt.

@item @t{*}  
The program is expected to prompt for input and support ``normal input editing''.

@item @t{*}  
@b{read-char} might wait for the user to type something before returning
  instead of immediately returning a character or end-of-file. 

@end table

The general intent of having some @i{streams} be classified as
@i{interactive streams} is to allow them to be distinguished from
streams containing batch (or background or command-file) input.
Output to batch streams is typically discarded or saved for later viewing, 
so interactive queries to such streams might not have the expected effect.

@i{Terminal I/O} might or might not be an @i{interactive stream}.

@node Abstract Classifications of Streams, File Streams, Interactive Streams, Introduction to Streams
@subsubsection Abstract Classifications of Streams

@node File Streams, Other Subclasses of Stream, Abstract Classifications of Streams, Introduction to Streams
@subsubsection File Streams

Some @i{streams}, called @i{file streams}
@IGindex{file stream}
, provide access to @i{files}.
An @i{object} of @i{class} @b{file-stream} is used to represent a @i{file stream}.

The basic operation for opening a @i{file} is @b{open},
which typically returns a @i{file stream} 
(see its dictionary entry for details).
The basic operation for closing a @i{stream} is @b{close}.
The macro @b{with-open-file} is useful 
to express the common idiom of opening a @i{file} 
for the duration of a given body of @i{code}, 
and assuring that the resulting @i{stream} is closed upon exit from that body.

@node Other Subclasses of Stream,  , File Streams, Introduction to Streams
@subsubsection Other Subclasses of Stream

The @i{class} @b{stream} has a number of @i{subclasses} defined 
by this specification.  Figure 21--5 shows some information 
about these subclasses.

@group
@noindent
@w{  Class                Related Operators             }
@w{  @b{broadcast-stream}     @b{make-broadcast-stream}         }
@w{                       @b{broadcast-stream-streams}      }
@w{  @b{concatenated-stream}  @b{make-concatenated-stream}      }
@w{                       @b{concatenated-stream-streams}   }
@w{  @b{echo-stream}          @b{make-echo-stream}              }
@w{                       @b{echo-stream-input-stream}      }
@w{                       @b{echo-stream-output-stream}     }
@w{  @b{string-stream}        @b{make-string-input-stream}      }
@w{                       @b{with-input-from-string}        }
@w{                       @b{make-string-output-stream}     }
@w{                       @b{with-output-to-string}         }
@w{                       @b{get-output-stream-string}      }
@w{  @b{synonym-stream}       @b{make-synonym-stream}           }
@w{                       @b{synonym-stream-symbol}         }
@w{  @b{two-way-stream}       @b{make-two-way-stream}           }
@w{                       @b{two-way-stream-input-stream}   }
@w{                       @b{two-way-stream-output-stream}  }

@noindent
@w{  Figure 21--5: Defined Names related to Specialized Streams}

@end group

@node Stream Variables, Stream Arguments to Standardized Functions, Introduction to Streams, Stream Concepts
@subsection Stream Variables

@i{Variables} whose @i{values} must be @i{streams} are sometimes called 
@i{stream variables}
@IGindex{stream variable}
.

Certain @i{stream variables} are defined by this specification 
to be the proper source of input or output in various @i{situations} 
where no specific @i{stream} has been specified instead.
A complete list of such @i{standardized} @i{stream variables}
appears in Figure 21--6.  
The consequences are undefined if at any time
the @i{value} of any of these @i{variables} is not an @i{open} @i{stream}.

@group
@noindent
@w{  Glossary Term    Variable Name      }
@w{  @i{debug I/O}        @b{*debug-io*}         }
@w{  @i{error output}     @b{*error-output*}     }
@w{  @i{query I/O}        @b{*query-io*}         }
@w{  @i{standard input}   @b{*standard-input*}   }
@w{  @i{standard output}  @b{*standard-output*}  }
@w{  @i{terminal I/O}     @b{*terminal-io*}      }
@w{  @i{trace output}     @b{*trace-output*}     }

@noindent
@w{  Figure 21--6: Standardized Stream Variables}

@end group

Note that, by convention, @i{standardized} @i{stream variables} have names 
    ending in ``@t{-input*}''  if they must be @i{input} @i{streams},
    ending in ``@t{-output*}'' if they must be @i{output} @i{streams},
 or ending in ``@t{-io*}''     if they must be @i{bidirectional} @i{streams}.

User programs may @i{assign} or @i{bind} any @i{standardized} @i{stream variable}
except @b{*terminal-io*}.

@node Stream Arguments to Standardized Functions, Restrictions on Composite Streams, Stream Variables, Stream Concepts
@subsection Stream Arguments to Standardized Functions

The @i{operators} in Figure 21--7 accept @i{stream} @i{arguments} that
might be either @i{open} or @i{closed} @i{streams}.

@group
@noindent
@w{  broadcast-stream-streams     file-author       pathnamep                     }
@w{  close                        file-namestring   probe-file                    }
@w{  compile-file                 file-write-date   rename-file                   }
@w{  compile-file-pathname        host-namestring   streamp                       }
@w{  concatenated-stream-streams  load              synonym-stream-symbol         }
@w{  delete-file                  logical-pathname  translate-logical-pathname    }
@w{  directory                    merge-pathnames   translate-pathname            }
@w{  directory-namestring         namestring        truename                      }
@w{  dribble                      open              two-way-stream-input-stream   }
@w{  echo-stream-input-stream     open-stream-p     two-way-stream-output-stream  }
@w{  echo-stream-ouput-stream     parse-namestring  wild-pathname-p               }
@w{  ed                           pathname          with-open-file                }
@w{  enough-namestring            pathname-match-p                                }

@noindent
@w{        Figure 21--7: Operators that accept either Open or Closed Streams      }

@end group

The @i{operators} in Figure 21--8 accept @i{stream} @i{arguments} that
must be @i{open} @i{streams}.

@group
@noindent
@w{ clear-input              output-stream-p         read-char-no-hang          }
@w{ clear-output             peek-char               read-delimited-list        }
@w{ file-length              pprint                  read-line                  }
@w{ file-position            pprint-fill             read-preserving-whitespace }
@w{ file-string-length       pprint-indent           stream-element-type        }
@w{ finish-output            pprint-linear           stream-external-format     }
@w{ force-output             pprint-logical-block    terpri                     }
@w{ format                   pprint-newline          unread-char                }
@w{ fresh-line               pprint-tab              with-open-stream           }
@w{ get-output-stream-string pprint-tabular          write                      }
@w{ input-stream-p           prin1                   write-byte                 }
@w{ interactive-stream-p     princ                   write-char                 }
@w{ listen                   print                   write-line                 }
@w{ make-broadcast-stream    print-object            write-string               }
@w{ make-concatenated-stream print-unreadable-object y-or-n-p                   }
@w{ make-echo-stream         read                    yes-or-no-p                }
@w{ make-synonym-stream      read-byte                                          }
@w{ make-two-way-stream      read-char                                          }

@noindent
@w{             Figure 21--8: Operators that accept Open Streams only            }

@end group

@node Restrictions on Composite Streams,  , Stream Arguments to Standardized Functions, Stream Concepts
@subsection Restrictions on Composite Streams

The consequences are undefined if any @i{component} of a @i{composite stream}
is @i{closed} before the @i{composite stream} is @i{closed}.

The consequences are undefined if the @i{synonym stream symbol} is not @i{bound}
to an @i{open} @i{stream} from the time of the @i{synonym stream}'s creation
until the time it is @i{closed}.

@c end of including concept-streams

@node Streams Dictionary,  , Stream Concepts, Streams
@section Streams Dictionary

@c including dict-streams

@menu
* stream::			
* broadcast-stream::		
* concatenated-stream::		
* echo-stream::			
* file-stream::			
* string-stream::		
* synonym-stream::		
* two-way-stream::		
* input-stream-p::		
* interactive-stream-p::	
* open-stream-p::		
* stream-element-type::		
* streamp::			
* read-byte::			
* write-byte::			
* peek-char::			
* read-char::			
* read-char-no-hang::		
* terpri::			
* unread-char::			
* write-char::			
* read-line::			
* write-string::		
* read-sequence::		
* write-sequence::		
* file-length::			
* file-position::		
* file-string-length::		
* open::			
* stream-external-format::	
* with-open-file::		
* close::			
* with-open-stream::		
* listen::			
* clear-input::			
* finish-output::		
* y-or-n-p::			
* make-synonym-stream::		
* synonym-stream-symbol::	
* broadcast-stream-streams::	
* make-broadcast-stream::	
* make-two-way-stream::		
* two-way-stream-input-stream::	 
* echo-stream-input-stream::	
* make-echo-stream::		
* concatenated-stream-streams::	 
* make-concatenated-stream::	
* get-output-stream-string::	
* make-string-input-stream::	
* make-string-output-stream::	
* with-input-from-string::	
* with-output-to-string::	
* *debug-io*::			
* *terminal-io*::		
* stream-error::		
* stream-error-stream::		
* end-of-file::			
@end menu

@node stream, broadcast-stream, Streams Dictionary, Streams Dictionary
@subsection stream                                                       [System Class]

@subsubheading  Class Precedence List::
@b{stream},
@b{t}

@subsubheading  Description::

A @i{stream} is an @i{object} that can be used with an input or output
function to identify an appropriate source or sink of @i{characters} or 
@i{bytes} for that operation.

For more complete information, see @ref{Stream Concepts}.

@subsubheading  See Also::

@ref{Stream Concepts},
@ref{Printing Other Objects},
{@ref{Printer}},
{@ref{Reader}}

@node broadcast-stream, concatenated-stream, stream, Streams Dictionary
@subsection broadcast-stream                                             [System Class]

@subsubheading  Class Precedence List::

@b{broadcast-stream},
@b{stream},
@b{t}

@subsubheading  Description::

A @i{broadcast stream} is an @i{output} @i{stream} which 
has associated with it a set of zero or more @i{output} @i{streams} 
such that any output sent to the @i{broadcast stream} gets passed on
as output to each of the associated @i{output} @i{streams}.
(If a @i{broadcast stream} has no @i{component streams},
then all output to the @i{broadcast stream} is discarded.)

The set of operations that may be performed on a @i{broadcast stream} 
is the intersection of those for its associated @i{output} @i{streams}.

Some output operations (@i{e.g.}, @b{fresh-line}) return @i{values} based on the
state of the @i{stream} at the time of the operation.

Since these @i{values} might differ for each of the @i{component streams},
it is necessary to describe their return value specifically:

@table @asis

@item @t{*}  
@b{stream-element-type} returns
    the value from the last component stream, 
    or @b{t} if there are no component streams.

@item @t{*}  
@b{fresh-line} returns
     the value from the last component stream, 
     or @b{nil} if there are no component streams.

@item @t{*}  
The functions
          @b{file-length},
          @b{file-position},
          @b{file-string-length},
      and @b{stream-external-format}
   return the value from the last component stream;
   if there are no component streams,
       @b{file-length} and @b{file-position} return @t{0},
       @b{file-string-length} returns @t{1},
   and @b{stream-external-format} returns @t{:default}.

@item @t{*}  
The functions @b{streamp} and @b{output-stream-p} 
  always return @i{true} for @i{broadcast streams}.

@item @t{*}  
The functions @b{open-stream-p} tests whether the @i{broadcast stream}
  is @i{open}_2, not whether its component streams are @i{open}.

@item @t{*}  
The functions @b{input-stream-p} and @i{interactive-stream-p} 
  return an @i{implementation-defined}, @i{generalized boolean} value.

@item @t{*}  
For the input operations
        @b{clear-input}
        @b{listen},
        @b{peek-char},
        @b{read-byte},
        @b{read-char-no-hang},
        @b{read-char},
        @b{read-line},
    and @b{unread-char},
  the consequences are undefined if the indicated operation is performed.
  However, an @i{implementation} is permitted 
  to define such a behavior as an @i{implementation-dependent} extension.
@end table

For any output operations not having their return values explicitly specified above
or elsewhere in this document, it is defined that
the @i{values} returned by such an operation are 
the @i{values} resulting from performing the operation 
on the last of its @i{component streams};
the @i{values} resulting from performing the operation
on all preceding @i{streams} are discarded.                    
If there are no @i{component streams}, 
the value is @i{implementation-dependent}.

@subsubheading  See Also::

@ref{broadcast-stream-streams}
,
@ref{make-broadcast-stream}

@node concatenated-stream, echo-stream, broadcast-stream, Streams Dictionary
@subsection concatenated-stream                                          [System Class]

@subsubheading  Class Precedence List::

@b{concatenated-stream},
@b{stream},
@b{t}

@subsubheading  Description::

A @i{concatenated stream} is an @i{input} @i{stream} which 
is a @i{composite stream} of zero or more other @i{input} @i{streams}, 
such that the sequence of data which can be read from the
@i{concatenated stream} is the same as the concatenation of the 
sequences of data which could be read from each of the 
constituent @i{streams}.

Input from a @i{concatenated stream} is taken from the first
of the associated @i{input streams} until it reaches @i{end of file}_1; 
then that @i{stream} is discarded, and subsequent input is taken
from the next @i{input stream}, and so on.
An @i{end of file} on the associated @i{input streams} is always managed
invisibly by the @i{concatenated stream}---the only time a client of
a @i{concatenated stream} sees an @i{end of file} is when an attempt is
made to obtain data from the @i{concatenated stream} but it has no
remaining @i{input streams} from which to obtain such data.

@subsubheading  See Also::

@ref{concatenated-stream-streams}
,
@ref{make-concatenated-stream}

@node echo-stream, file-stream, concatenated-stream, Streams Dictionary
@subsection echo-stream                                                  [System Class]

@subsubheading  Class Precedence List::

@b{echo-stream},
@b{stream},
@b{t}

@subsubheading  Description::

An @i{echo stream} is a @i{bidirectional} @i{stream}
that gets  its input  from an associated @i{input}  @i{stream}
and  sends its output to   an associated @i{output} @i{stream}.

All input taken from the @i{input} @i{stream} 
is echoed to the @i{output} @i{stream}.
Whether the input is echoed immediately after it is encountered,
or after it has been read from the @i{input stream}
is @i{implementation-dependent}.

@subsubheading  See Also::

@ref{echo-stream-input-stream; echo-stream-output-stream}
,
@b{echo-stream-output-stream},
@ref{make-echo-stream}

@node file-stream, string-stream, echo-stream, Streams Dictionary
@subsection file-stream                                                  [System Class]

@subsubheading  Class Precedence List::

@b{file-stream},
@b{stream},
@b{t}

@subsubheading  Description::

An @i{object} of @i{type} @b{file-stream} is a @i{stream} the direct
source or sink of which is a @i{file}.  Such a @i{stream} is
created explicitly by @b{open} and @b{with-open-file}, and
implicitly by @i{functions} such as @b{load} that process @i{files}.

@subsubheading  See Also::

@ref{load}
,
@ref{open}
,
@ref{with-open-file}

@node string-stream, synonym-stream, file-stream, Streams Dictionary
@subsection string-stream                                                [System Class]

@subsubheading  Class Precedence List::

@b{string-stream},
@b{stream},
@b{t}

@subsubheading  Description::

A @i{string stream} is a @i{stream} 
which reads input from or writes output to an associated @i{string}.

The @i{stream element type} of a @i{string stream} is always
a @i{subtype} of @i{type} @b{character}.

@subsubheading  See Also::

@ref{make-string-input-stream}
,
@ref{make-string-output-stream}
,
@ref{with-input-from-string}
,
@ref{with-output-to-string}

@node synonym-stream, two-way-stream, string-stream, Streams Dictionary
@subsection synonym-stream                                               [System Class]

@subsubheading  Class Precedence List::

@b{synonym-stream},
@b{stream},
@b{t}

@subsubheading  Description::

A @i{stream} that is an alias for another @i{stream},
which is the @i{value} of a @i{dynamic variable}
whose @i{name} is the @i{synonym stream symbol} of the @i{synonym stream}.

Any operations on a @i{synonym stream} will be performed 
on the @i{stream} that is then the @i{value} of the
@i{dynamic variable} named by the @i{synonym stream symbol}.
If the @i{value} of the @i{variable} should change,
or if the @i{variable} should be @i{bound},
then the @i{stream} will operate on the new @i{value} of the @i{variable}.

@subsubheading  See Also::

@ref{make-synonym-stream}
,
@ref{synonym-stream-symbol}

@node two-way-stream, input-stream-p, synonym-stream, Streams Dictionary
@subsection two-way-stream                                               [System Class]

@subsubheading  Class Precedence List::

@b{two-way-stream},
@b{stream},
@b{t}

@subsubheading  Description::

A @i{bidirectional} @i{composite stream} that 
     receives its input  from an associated @i{input}  @i{stream} 
 and sends    its output to   an associated @i{output} @i{stream}.

@subsubheading  See Also::

@ref{make-two-way-stream}
,
@ref{two-way-stream-input-stream; two-way-stream-output-stream}
,
@b{two-way-stream-output-stream}

@node input-stream-p, interactive-stream-p, two-way-stream, Streams Dictionary
@subsection input-stream-p, output-stream-p                                  [Function]

@code{input-stream-p}  @i{stream} @result{}  @i{generalized-boolean}

@code{output-stream-p}  @i{stream} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

@b{input-stream-p} returns @i{true} if @i{stream} is an @i{input} @i{stream};
otherwise, returns @i{false}.

@b{output-stream-p} returns @i{true} if @i{stream} is an @i{output} @i{stream};
otherwise, returns @i{false}.

@subsubheading  Examples::

@example
 (input-stream-p *standard-input*) @result{}  @i{true}
 (input-stream-p *terminal-io*) @result{}  @i{true}
 (input-stream-p (make-string-output-stream)) @result{}  @i{false}

 (output-stream-p *standard-output*) @result{}  @i{true}
 (output-stream-p *terminal-io*) @result{}  @i{true}
 (output-stream-p (make-string-input-stream "jr")) @result{}  @i{false}
@end example

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream}.

@node interactive-stream-p, open-stream-p, input-stream-p, Streams Dictionary
@subsection interactive-stream-p                                             [Function]

@code{interactive-stream-p}  @i{stream} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

Returns @i{true} if @i{stream} is an @i{interactive stream};
otherwise, returns @i{false}.

@subsubheading  Examples::

@example
 (when (> measured limit)
   (let ((error (round (* (- measured limit) 100)
                       limit)))
     (unless (if (interactive-stream-p *query-io*)
                 (yes-or-no-p "The frammis is out of tolerance by ~D
                               Is it safe to proceed? " error)
                 (< error 15))  ;15
       (error "The frammis is out of tolerance by ~D
@end example

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream}.

@subsubheading  See Also::

@ref{Stream Concepts}

@node open-stream-p, stream-element-type, interactive-stream-p, Streams Dictionary
@subsection open-stream-p                                                    [Function]

@code{open-stream-p}  @i{stream} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

Returns @i{true} if @i{stream} is an @i{open} @i{stream};
otherwise, returns @i{false}.

@i{Streams} are open until they have been explicitly closed with @b{close},
or until they are implicitly closed due to exit from a
  @b{with-output-to-string}, 
  @b{with-open-file},
  @b{with-input-from-string},  or 
  @b{with-open-stream} @i{form}.

@subsubheading  Examples::

@example
 (open-stream-p *standard-input*) @result{}  @i{true}
@end example

@subsubheading  Affected By::

@b{close}.

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream}.

@node stream-element-type, streamp, open-stream-p, Streams Dictionary
@subsection stream-element-type                                              [Function]

@code{stream-element-type}  @i{stream} @result{}  @i{typespec}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream}.

@i{typespec}---a @i{type specifier}.

@subsubheading  Description::

@b{stream-element-type} returns a @i{type specifier} that
indicates the @i{types} of @i{objects} that may be read from 
or written to @i{stream}.

@i{Streams} created by @b{open} have an @i{element type}
restricted to @b{integer} or a @i{subtype} of @i{type} @b{character}.

@subsubheading  Examples::

@example
;; Note that the stream must accomodate at least the specified type,
;; but might accomodate other types.  Further note that even if it does
;; accomodate exactly the specified type, the type might be specified in
;; any of several ways.
 (with-open-file (s "test" :element-type '(integer 0 1)
                           :if-exists :error
                           :direction :output)
   (stream-element-type s))
@result{}  INTEGER
@i{OR}@result{} (UNSIGNED-BYTE 16)
@i{OR}@result{} (UNSIGNED-BYTE 8)
@i{OR}@result{} BIT
@i{OR}@result{} (UNSIGNED-BYTE 1)
@i{OR}@result{} (INTEGER 0 1)
@i{OR}@result{} (INTEGER 0 (2))
@end example

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream}.

@node streamp, read-byte, stream-element-type, Streams Dictionary
@subsection streamp                                                          [Function]

@code{streamp}  @i{object} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{object}---an @i{object}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

Returns @i{true} if @i{object} is of @i{type} @b{stream};
otherwise, returns @i{false}.

@b{streamp} is unaffected by whether @i{object},
if it is a @i{stream}, is @i{open} or closed.

@subsubheading  Examples::

@example
 (streamp *terminal-io*) @result{}  @i{true}
 (streamp 1) @result{}  @i{false}
@end example

@subsubheading  Notes::

@example
 (streamp @i{object}) @equiv{} (typep @i{object} 'stream)
@end example

@node read-byte, write-byte, streamp, Streams Dictionary
@subsection read-byte                                                        [Function]

@code{read-byte}  @i{stream {&optional} eof-error-p eof-value} @result{}  @i{byte}

@subsubheading  Arguments and Values::

@i{stream}---a @i{binary} @i{input} @i{stream}.

@i{eof-error-p}---a @i{generalized boolean}.
 The default is @i{true}.

@i{eof-value}---an @i{object}.
 The default is @b{nil}.

@i{byte}---an @i{integer},
 or the @i{eof-value}.

@subsubheading  Description::

@b{read-byte} reads and returns one byte from @i{stream}.

If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false}, 
the @i{eof-value} is returned.  

@subsubheading  Examples::
@example
 (with-open-file (s "temp-bytes" 
                     :direction :output
                     :element-type 'unsigned-byte)
    (write-byte 101 s)) @result{}  101
 (with-open-file (s "temp-bytes" :element-type 'unsigned-byte)
    (format t "~S ~S" (read-byte s) (read-byte s nil 'eof)))
@t{ |> } 101 EOF
@result{}  NIL
@end example

@subsubheading  Side Effects::

Modifies @i{stream}.

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream}.

Should signal an error of @i{type} @b{error} 
if @i{stream} is not  a @i{binary} @i{input} @i{stream}.

If there are no @i{bytes} remaining in the @i{stream} 
and @i{eof-error-p} is @i{true}, an error of @i{type} @b{end-of-file} is signaled.

@subsubheading  See Also::

@ref{read-char}
,

@ref{read-sequence}
,

@ref{write-byte}

@node write-byte, peek-char, read-byte, Streams Dictionary
@subsection write-byte                                                       [Function]

@code{write-byte}  @i{byte stream} @result{}  @i{byte}

@subsubheading  Arguments and Values::

@i{byte}---an @i{integer} of the @i{stream element type}
		  of @i{stream}.

@i{stream}---a @i{binary} @i{output} @i{stream}.

@subsubheading  Description::

@b{write-byte} writes one byte, @i{byte}, to @i{stream}.

@subsubheading  Examples::

@example
 (with-open-file (s "temp-bytes" 
                    :direction :output
                    :element-type 'unsigned-byte)
    (write-byte 101 s)) @result{}  101
@end example

@subsubheading  Side Effects::

@i{stream} is modified.

@subsubheading  Affected By::

The @i{element type} of the @i{stream}.

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream}.
Should signal an error of @i{type} @b{error} 
if @i{stream} is not  a @i{binary} @i{output} @i{stream}.

Might signal an error of @i{type} @b{type-error} if @i{byte} is not
an @i{integer} of the @i{stream element type} of @i{stream}.

@subsubheading  See Also::

@ref{read-byte}
,
@ref{write-char}
,

@ref{write-sequence}

@node peek-char, read-char, write-byte, Streams Dictionary
@subsection peek-char                                                        [Function]

@code{peek-char}  @i{{&optional} peek-type input-stream eof-error-p
			       eof-value recursive-p} @result{}  @i{char}

@subsubheading  Arguments and Values::

@i{peek-type}---a @i{character} or @b{t} or @b{nil}.

@i{input-stream}---@i{input} @i{stream designator}.
  The default is @i{standard input}.

@i{eof-error-p}---a @i{generalized boolean}.
 The default is @i{true}.

@i{eof-value}---an @i{object}.
 The default is @b{nil}.

@i{recursive-p}---a @i{generalized boolean}.
 The default is @i{false}.

@i{char}---a @i{character} or the @i{eof-value}.

@subsubheading  Description::

@b{peek-char} obtains the next character in @i{input-stream}
without actually reading it, thus leaving the character
to be read at a later time.  It can
also be used to skip over and discard intervening
characters in the @i{input-stream} 
until a particular character is found.

If @i{peek-type} is not supplied or @b{nil},
@b{peek-char} returns the next character to be read from 
@i{input-stream}, without actually removing it from 
@i{input-stream}.
The next time input is done from @i{input-stream}, the character will still
be there.  
If @i{peek-type} is @b{t},
then @b{peek-char} skips over @i{whitespace}_2 @i{characters},
but not comments,
and then performs the peeking operation on the next
character.
The last character examined, the one that starts an @i{object},
is not removed from @i{input-stream}.
If @i{peek-type} is a @i{character},
then @b{peek-char} skips
over input characters until a character that
is @b{char=} to that @i{character} is found;          
that character is left in @i{input-stream}.

If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false},
@i{eof-value} is returned.  

{ }{If @i{recursive-p} is @i{true},
this call is expected to be embedded in a higher-level call to @b{read} 
or a similar @i{function} used by the @i{Lisp reader}.}

When @i{input-stream} is an @i{echo stream},
characters that are only peeked at are not echoed. In the
case that @i{peek-type} is not @b{nil},
the characters that are passed by @b{peek-char} 
are treated as if by @b{read-char},
and so are echoed unless they have been marked otherwise by @b{unread-char}.

@subsubheading  Examples::
@example
 (with-input-from-string (input-stream "    1 2 3 4 5")
    (format t "~S ~S ~S" 
            (peek-char t input-stream)
            (peek-char #\4 input-stream)
            (peek-char nil input-stream)))
@t{ |> } #\1 #\4 #\4
@result{}  NIL
@end example

@subsubheading  Affected By::

@b{*readtable*},
@b{*standard-input*},
@b{*terminal-io*}.

@subsubheading  Exceptional Situations::

If @i{eof-error-p} is @i{true} and an @i{end of file}_2 occurs
an error of @i{type} @b{end-of-file} is signaled.

If     @i{peek-type} is a @i{character},
       an @i{end of file}_2 occurs,
   and @i{eof-error-p} is @i{true},
an error of @i{type} @b{end-of-file} is signaled.

If @i{recursive-p} is @i{true}
and an @i{end of file}_2 occurs,
an error of @i{type} @b{end-of-file} is signaled.

@node read-char, read-char-no-hang, peek-char, Streams Dictionary
@subsection read-char                                                        [Function]

@code{read-char}  @i{{&optional} input-stream eof-error-p eof-value recursive-p} @result{}  @i{char}

@subsubheading  Arguments and Values::

@i{input-stream}---an @i{input} @i{stream designator}.
 The default is @i{standard input}.

@i{eof-error-p}---a @i{generalized boolean}.
 The default is @i{true}.

@i{eof-value}---an @i{object}.
 The default is @b{nil}.

@i{recursive-p}---a @i{generalized boolean}.
 The default is @i{false}.

@i{char}---a @i{character} or the @i{eof-value}.

@subsubheading  Description::

@b{read-char} returns the next @i{character} from @i{input-stream}.

When @i{input-stream} is an @i{echo stream},
the character is echoed on @i{input-stream} the first time the character is 
seen.
  Characters that are not echoed by @b{read-char} 
are those that were
  put there by @b{unread-char} 
and hence are assumed to have been echoed
  already by a previous call to @b{read-char}.

{ }{If @i{recursive-p} is @i{true},
this call is expected to be embedded in a higher-level call to @b{read} 
or a similar @i{function} used by the @i{Lisp reader}.}

If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false}, 
@i{eof-value} is returned.  

@subsubheading  Examples::
@example
 (with-input-from-string (is "0123")
    (do ((c (read-char is) (read-char is nil 'the-end)))
        ((not (characterp c)))
     (format t "~S " c)))
@t{ |> } #\0 #\1 #\2 #\3
@result{}  NIL
@end example

@subsubheading  Affected By::

@b{*standard-input*},
@b{*terminal-io*}.

@subsubheading  Exceptional Situations::

If an @i{end of file}_2 occurs before a character can be read, and 
@i{eof-error-p} is @i{true}, 
an error of @i{type} @b{end-of-file} is signaled.

@subsubheading  See Also::

@ref{read-byte}
,

@ref{read-sequence}
,

@ref{write-char}
,
@ref{read; read-preserving-whitespace}

@subsubheading  Notes::
The corresponding output function is @b{write-char}.

@node read-char-no-hang, terpri, read-char, Streams Dictionary
@subsection read-char-no-hang                                                [Function]

@code{read-char-no-hang}  @i{{&optional} input-stream eof-error-p
			       eof-value recursive-p} @result{}  @i{char}

@subsubheading  Arguments and Values::

@i{input-stream} -- an @i{input} @i{stream designator}.
 The default is @i{standard input}.

@i{eof-error-p}---a @i{generalized boolean}.
 The default is @i{true}.

@i{eof-value}---an @i{object}.
 The default is @b{nil}.

@i{recursive-p}---a @i{generalized boolean}.
 The default is @i{false}.

@i{char}---a @i{character} or @b{nil} or the @i{eof-value}.

@subsubheading  Description::

@b{read-char-no-hang} returns a character
from @i{input-stream} if such a character is available.  If no character
is available, @b{read-char-no-hang} returns @b{nil}.

{ }{If @i{recursive-p} is @i{true},
this call is expected to be embedded in a higher-level call to @b{read} 
or a similar @i{function} used by the @i{Lisp reader}.}

If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false}, 
@i{eof-value} is returned.  

@subsubheading  Examples::

@example
;; This code assumes an implementation in which a newline is not
;; required to terminate input from the console.
 (defun test-it ()
   (unread-char (read-char))
   (list (read-char-no-hang) 
         (read-char-no-hang) 
         (read-char-no-hang)))
@result{}  TEST-IT
;; Implementation A, where a Newline is not required to terminate
;; interactive input on the console.
 (test-it)
@t{ |> } @b{|>>}@t{a}@b{<<|}
@result{}  (#\a NIL NIL)
;; Implementation B, where a Newline is required to terminate
;; interactive input on the console, and where that Newline remains
;; on the input stream.
 (test-it)
@t{ |> } @b{|>>}@t{a{@i{[<--}~]}}@b{<<|}
@result{}  (#\a #\Newline NIL)
@end example

@subsubheading  Affected By::

@b{*standard-input*},
@b{*terminal-io*}.

@subsubheading  Exceptional Situations::

If an @i{end of file}_2 occurs
when @i{eof-error-p} is @i{true},
an error of @i{type} @b{end-of-file} is signaled .

@subsubheading  See Also::

@ref{listen}

@subsubheading  Notes::

@b{read-char-no-hang} is exactly like @b{read-char}, except
that if it would be necessary to wait in order to get a character (as
from a keyboard), @b{nil} is immediately returned without waiting.  

@node terpri, unread-char, read-char-no-hang, Streams Dictionary
@subsection terpri, fresh-line                                               [Function]

@code{terpri}  @i{{&optional} output-stream} @result{}  @i{@b{nil}}

@code{fresh-line}  @i{{&optional} output-stream} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{output-stream} -- an @i{output} @i{stream designator}.
 The default is @i{standard output}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

@b{terpri} outputs a @i{newline} to @i{output-stream}.

@b{fresh-line} is similar to @b{terpri} but outputs a @i{newline}
only if the @i{output-stream} is not already at the start of a line.
If for some reason this cannot be determined, then a @i{newline} is output anyway.
@b{fresh-line} returns @i{true} if it outputs a @i{newline};
otherwise it returns @i{false}.

@subsubheading  Examples::

@example
 (with-output-to-string (s)
    (write-string "some text" s)
    (terpri s)
    (terpri s)
    (write-string "more text" s))
@result{}  "some text

more text"
 (with-output-to-string (s)
    (write-string "some text" s)
    (fresh-line s)
    (fresh-line s)
    (write-string "more text" s))
@result{}  "some text
more text"
@end example

@subsubheading  Side Effects::

The @i{output-stream} is modified.

@subsubheading  Affected By::

@b{*standard-output*},
@b{*terminal-io*}.

@subsubheading  Exceptional Situations::

None.

[Reviewer Note by Barmar: What if stream is closed?]

@subsubheading  Notes::

@b{terpri} is identical in effect to

@example
 (write-char #\Newline output-stream)
@end example

@node unread-char, write-char, terpri, Streams Dictionary
@subsection unread-char                                                      [Function]

@code{unread-char}  @i{character {&optional} input-stream} @result{}  @i{@b{nil}}

@subsubheading  Arguments and Values::

@i{character}---a @i{character};
  must be the last @i{character} that was read from @i{input-stream}.

@i{input-stream}---an @i{input} @i{stream designator}.
 The default is @i{standard input}.

@subsubheading  Description::

@b{unread-char} places @i{character} back onto the front of
@i{input-stream} so that it will again be the next character
in @i{input-stream}.

When @i{input-stream} is an @i{echo stream},
no attempt is made to undo any echoing of the character that might already
have been done on @i{input-stream}. However, characters placed on 
@i{input-stream} by @b{unread-char} are marked in such a way 
as to inhibit later re-echo by @b{read-char}.

It is an error to invoke @b{unread-char} 
twice consecutively on the same @i{stream}
without an intervening call to @b{read-char} 
(or some other input operation which implicitly reads characters)
on that @i{stream}.

Invoking @b{peek-char} or @b{read-char} commits all previous characters.
The consequences of invoking @b{unread-char}
on any character preceding that which is returned by 
@b{peek-char} (including those passed over by 
@b{peek-char} that has a @i{non-nil} @i{peek-type})
are unspecified.
In particular, the consequences of 
invoking @b{unread-char} after @b{peek-char}
are unspecified.

@subsubheading  Examples::

@example
 (with-input-from-string (is "0123")
    (dotimes (i 6)
      (let ((c (read-char is)))
        (if (evenp i) (format t "~&~S ~S~
@t{ |> } 0 #\0
@t{ |> } 2 #\1
@t{ |> } 4 #\2
@result{}  NIL
@end example

@subsubheading  Affected By::

@b{*standard-input*},
@b{*terminal-io*}.

@subsubheading  See Also::

@ref{peek-char}
,
@ref{read-char}
,
@ref{Stream Concepts}

@subsubheading  Notes::

@b{unread-char} is intended to be an efficient mechanism for allowing
the @i{Lisp reader} and other parsers to perform one-character lookahead
in @i{input-stream}.

@node write-char, read-line, unread-char, Streams Dictionary
@subsection write-char                                                       [Function]

@code{write-char}  @i{character {&optional} output-stream} @result{}  @i{character}

@subsubheading  Arguments and Values::

@i{character}---a @i{character}.

@i{output-stream} -- an @i{output} @i{stream designator}.
 The default is @i{standard output}.

@subsubheading  Description::

@b{write-char} outputs @i{character} to @i{output-stream}.

@subsubheading  Examples::
@example
 (write-char #\a)
@t{ |> } a
@result{}  #\a
 (with-output-to-string (s) 
   (write-char #\a s)
   (write-char #\Space s)
   (write-char #\b s))
@result{}  "a b"
@end example

@subsubheading  Side Effects::

The @i{output-stream} is modified.

@subsubheading  Affected By::

@b{*standard-output*},
@b{*terminal-io*}.

@subsubheading  See Also::

@ref{read-char}
,
@ref{write-byte}
,

@ref{write-sequence}

@node read-line, write-string, write-char, Streams Dictionary
@subsection read-line                                                        [Function]

@code{read-line}  @i{{&optional} input-stream eof-error-p eof-value recursive-p}@*
   @result{}  @i{line, missing-newline-p}

@subsubheading  Arguments and Values::

@i{input-stream}---an @i{input} @i{stream designator}.
 The default is @i{standard input}.

@i{eof-error-p}---a @i{generalized boolean}.
 The default is @i{true}.

@i{eof-value}---an @i{object}.
 The default is @b{nil}.

@i{recursive-p}---a @i{generalized boolean}.
 The default is @i{false}.

@i{line}---a @i{string} or the @i{eof-value}.

@i{missing-newline-p}---a @i{generalized boolean}.

@subsubheading  Description::

Reads from @i{input-stream} a line of text
that is terminated by a @i{newline} or @i{end of file}.

{ }{If @i{recursive-p} is @i{true},
this call is expected to be embedded in a higher-level call to @b{read} 
or a similar @i{function} used by the @i{Lisp reader}.}

The @i{primary value}, @i{line}, is the line that is read,
represented as a @i{string} (without the trailing @i{newline}, if any).
If  @i{eof-error-p} is @i{false} 
and the @i{end of file} for @i{input-stream} is reached
     before any @i{characters} are read,
@i{eof-value} is returned as the @i{line}.

The @i{secondary value}, @i{missing-newline-p}, 
is a @i{generalized boolean} that is
    @i{false} if the @i{line} was terminated by a @i{newline},
 or @i{true}  if the @i{line} was terminated by 
		   the @i{end of file} for @i{input-stream}
		   (or if the @i{line} is the @i{eof-value}).

@subsubheading  Examples::

@example
 (setq a "line 1
 line2")
@result{}  "line 1
 line2"
 (read-line (setq input-stream (make-string-input-stream a)))
@result{}  "line 1", @i{false}
 (read-line input-stream)
@result{}  "line2", @i{true}
 (read-line input-stream nil nil)
@result{}  NIL, @i{true}
@end example

@subsubheading  Affected By::

@b{*standard-input*},
@b{*terminal-io*}.

@subsubheading  Exceptional Situations::

If an @i{end of file}_2 occurs before any characters are read in the line,
an error is signaled if @i{eof-error-p} is @i{true}.  

@subsubheading  See Also::

@ref{read; read-preserving-whitespace}

@subsubheading  Notes::

The corresponding output function is @b{write-line}.

@node write-string, read-sequence, read-line, Streams Dictionary
@subsection write-string, write-line                                         [Function]

@code{write-string}  @i{string {&optional} output-stream {&key} start end} @result{}  @i{string}

@code{write-line}  @i{string {&optional} output-stream {&key} start end} @result{}  @i{string}

@subsubheading  Arguments and Values::

@i{string}---a @i{string}.

@i{output-stream} -- an @i{output} @i{stream designator}.
 The default is @i{standard output}.

@i{start}, @i{end}---@i{bounding index designators} of @i{string}.
 The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.

@subsubheading  Description::

@b{write-string} writes the @i{characters} of 
the subsequence of @i{string} @i{bounded} by @i{start} and @i{end}
to @i{output-stream}.
@b{write-line} does the same thing, 
but then outputs a newline afterwards.  

@subsubheading  Examples::

@example
 (prog1 (write-string "books" nil :end 4) (write-string "worms"))
@t{ |> } bookworms
@result{}  "books"
 (progn (write-char #\*)
        (write-line "test12" *standard-output* :end 5) 
        (write-line "*test2")
        (write-char #\*)
        nil)
@t{ |> } *test1
@t{ |> } *test2
@t{ |> } *
@result{}  NIL
@end example

@subsubheading  Affected By::

@b{*standard-output*},
@b{*terminal-io*}.

@subsubheading  See Also::

@ref{read-line}
, 
@ref{write-char}

@subsubheading  Notes::

@b{write-line} and @b{write-string} return @i{string},
not the substring @i{bounded} by @i{start} and @i{end}.

@example
 (write-string string)
@equiv{} (dotimes (i (length string)
      (write-char (char string i)))

 (write-line string)
@equiv{} (prog1 (write-string string) (terpri))
@end example

@node read-sequence, write-sequence, write-string, Streams Dictionary
@subsection read-sequence                                                    [Function]

@code{read-sequence}  @i{sequence stream {&key} start end} @result{}  @i{position}

@i{sequence}---a @i{sequence}.

@i{stream}---an @i{input} @i{stream}.

@i{start}, @i{end}---@i{bounding index designators} of
 @i{sequence}.  The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.

@i{position}---an @i{integer} greater than or equal to zero, and
 less than or equal to the @i{length} of the @i{sequence}.

@subsubheading  Description::

Destructively modifies @i{sequence} by replacing the @i{elements}
of @i{sequence} @i{bounded} by @i{start} and @i{end} with
@i{elements} read from @i{stream}.

@i{Sequence} is destructively modified by copying successive
@i{elements} into it from @i{stream}.  If the @i{end of file} for
@i{stream} is reached before copying all @i{elements} of the
subsequence, then the extra @i{elements} near the end of @i{sequence}
are not updated.

@i{Position} is the index of the first @i{element} of @i{sequence}
that was not updated, which might be less than @i{end} because the
@i{end of file} was reached.

@subsubheading  Examples::

@example
 (defvar *data* (make-array 15 :initial-element nil))
 (values (read-sequence *data* (make-string-input-stream "test string")) *data*)
 @result{}  11, #(#\t #\e #\s #\t #\Space #\s #\t #\r #\i #\n #\g NIL NIL NIL NIL)
@end example

@subsubheading  Side Effects::

Modifies @i{stream} and @i{sequence}.

@subsubheading  Exceptional Situations::

Should be prepared to signal an error of @i{type} @b{type-error}
			 if @i{sequence} is not a @i{proper sequence}.
Should signal an error of @i{type} @b{type-error}
			      if @i{start} is not a non-negative @i{integer}.
Should signal an error of @i{type} @b{type-error}
			      if @i{end} is not a non-negative @i{integer} or @b{nil}.

Might signal an error of @i{type} @b{type-error} if an @i{element} read from
the @i{stream} is not a member of the @i{element type} of the
@i{sequence}.

@subsubheading  See Also::

@ref{Compiler Terminology},
@ref{write-sequence}
,
@ref{read-line}

@subsubheading  Notes::

@b{read-sequence} is identical in effect to iterating over the indicated
subsequence and reading one @i{element} at a time from @i{stream} and
storing it into @i{sequence}, but may be more efficient than the
equivalent loop.  An efficient implementation is more likely to exist
for the case where the @i{sequence} is a @i{vector} with the same
@i{element type} as the @i{stream}.

@node write-sequence, file-length, read-sequence, Streams Dictionary
@subsection write-sequence                                                   [Function]

@code{write-sequence}  @i{sequence stream {&key} start end} @result{}  @i{sequence}

@i{sequence}---a @i{sequence}.

@i{stream}---an @i{output} @i{stream}.

@i{start}, @i{end}---@i{bounding index designators} of
 @i{sequence}.  The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.

@subsubheading  Description::

@b{write-sequence} writes the @i{elements} of the subsequence
of @i{sequence} @i{bounded} by @i{start} and @i{end} to
@i{stream}.

@subsubheading  Examples::

@example
 (write-sequence "bookworms" *standard-output* :end 4)
 @t{ |> } book
 @result{}  "bookworms"
@end example

@subsubheading  Side Effects::

Modifies @i{stream}.

@subsubheading  Exceptional Situations::

Should be prepared to signal an error of @i{type} @b{type-error}
			 if @i{sequence} is not a @i{proper sequence}.
Should signal an error of @i{type} @b{type-error}
			      if @i{start} is not a non-negative @i{integer}.
Should signal an error of @i{type} @b{type-error}
			      if @i{end} is not a non-negative @i{integer} or @b{nil}.

Might signal an error of @i{type} @b{type-error} if an @i{element} of the
@i{bounded} @i{sequence} is not a member of the
@i{stream element type} of the @i{stream}.

@subsubheading  See Also::

@ref{Compiler Terminology},
@ref{read-sequence}
,
@ref{write-string; write-line}
,
@b{write-line}

@subsubheading  Notes::

@b{write-sequence} is identical in effect to iterating over the indicated
subsequence and writing one @i{element} at a time to @i{stream}, but
may be more efficient than the equivalent loop.  An efficient implementation
is more likely to exist for the case where the @i{sequence} is a
@i{vector} with the same @i{element type} as the @i{stream}.

@node file-length, file-position, write-sequence, Streams Dictionary
@subsection file-length                                                      [Function]

@code{file-length}  @i{stream} @result{}  @i{length}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream associated with a file}.

@i{length}---a non-negative @i{integer} or @b{nil}.

@subsubheading  Description::

@b{file-length} returns the length of @i{stream},  
or @b{nil} if the length cannot be determined.

For a binary file, the length is measured in units of
the @i{element type} of the @i{stream}.

@subsubheading  Examples::

@example
 (with-open-file (s "decimal-digits.text" 
                    :direction :output :if-exists :error)
   (princ "0123456789" s)
   (truename s))
@result{}  #P"A:>Joe>decimal-digits.text.1"
 (with-open-file (s "decimal-digits.text")
   (file-length s))
@result{}  10
@end example

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{stream} is not a @i{stream associated with a file}.

@subsubheading  See Also::

@ref{open}

@node file-position, file-string-length, file-length, Streams Dictionary
@subsection file-position                                                    [Function]

@code{file-position}  @i{stream} @result{}  @i{position}

@code{file-position}  @i{stream position-spec} @result{}  @i{success-p}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream}.

@i{position-spec}---a @i{file position designator}.

@i{position}---a @i{file position} or @b{nil}.

@i{success-p}---a @i{generalized boolean}.

@subsubheading  Description::

Returns or changes the current position within a @i{stream}.

When @i{position-spec} is not supplied,
@b{file-position} returns the current @i{file position} in the @i{stream},
or @b{nil} if this cannot be determined.

When @i{position-spec} is supplied,
the @i{file position} in @i{stream} is set to that @i{file position} (if possible).
@b{file-position} returns @i{true} 
if the repositioning is performed successfully,
or @i{false} if it is not.

An @i{integer} returned by @b{file-position} of one argument 
should be acceptable as @i{position-spec} for use with the same file.

For a character file,
performing a single @b{read-char} or @b{write-char} operation
may cause the file position to be increased by more than 1 because of
character-set translations (such as translating between the @r{Common Lisp}
@t{#\Newline} character and an external ASCII
carriage-return/line-feed sequence) and other aspects of the
implementation.  For a binary file, every @b{read-byte} 
or @b{write-byte}
operation increases the file position by 1.

@subsubheading  Examples::

@example
 (defun tester ()
   (let ((noticed '()) file-written)
     (flet ((notice (x) (push x noticed) x))
       (with-open-file (s "test.bin" 
                          :element-type '(unsigned-byte 8)
                          :direction :output
                          :if-exists :error)
          (notice (file-position s)) ;1
          (write-byte 5 s) 
          (write-byte 6 s)
          (let ((p (file-position s)))
            (notice p) ;2
            (notice (when p (file-position s (1- p))))) ;3
          (write-byte 7 s)
          (notice (file-position s)) ;4
          (setq file-written (truename s)))
        (with-open-file (s file-written
                           :element-type '(unsigned-byte 8)
                           :direction :input)
          (notice (file-position s)) ;5
          (let ((length (file-length s)))
            (notice length) ;6
            (when length
              (dotimes (i length)
                (notice (read-byte s)))))) ;7,...
        (nreverse noticed))))
@result{}  tester
 (tester)
@result{}  (0 2 T 2 0 2 5 7)
@i{OR}@result{} (0 2 NIL 3 0 3 5 6 7)
@i{OR}@result{} (NIL NIL NIL NIL NIL NIL)
@end example

@subsubheading  Side Effects::

When the @i{position-spec} argument is supplied,
the @i{file position} in the @i{stream} might be moved.

@subsubheading  Affected By::

The value returned by @b{file-position} increases monotonically
as input or output operations are performed.  

@subsubheading  Exceptional Situations::

If @i{position-spec} is supplied, but is too large or otherwise inappropriate, 
an error is signaled.

@subsubheading  See Also::

@ref{file-length}
,
@ref{file-string-length}
,
@ref{open}

@subsubheading  Notes::

Implementations that have character files represented
as a sequence of records of bounded size might choose to encode the
file position as, for example,
<<@i{record-number}>>*<<@i{max-record-size}>>+<<@i{character-within-record}>>.
This is a valid encoding because it increases monotonically as
each character is read or written, though not necessarily by 1 at
each step.  An @i{integer} might then be considered ``inappropriate''
as @i{position-spec} to @b{file-position} if, when decoded into
record number and character number, it turned out that the
supplied record was too short for the specified character number.

@node file-string-length, open, file-position, Streams Dictionary
@subsection file-string-length                                               [Function]

@code{file-string-length}  @i{stream object} @result{}  @i{length}

@subsubheading  Arguments and Values::

@i{stream}---an @i{output} @i{character} @i{file stream}.

@i{object}---a @i{string} or a @i{character}.

@i{length}---a non-negative @i{integer}, or @b{nil}.

@subsubheading  Description::

@b{file-string-length} returns the difference between what 
@t{(file-position @i{stream})} would be after writing 
@i{object} and its current value, or @b{nil} if this cannot be determined.  

The returned value corresponds to the current state of @i{stream}
at the time of the call and might not be 
the same if it is called again
when the state of the @i{stream} has changed.

@node open, stream-external-format, file-string-length, Streams Dictionary
@subsection open                                                             [Function]

@code{open}  @i{filespec {&key} direction element-type
				               if-exists if-does-not-exist
						     external-format}@*
   @result{}  @i{stream}

@subsubheading  Arguments and Values::

@i{filespec}---a @i{pathname designator}.

@i{direction}---one of @t{:input}, @t{:output}, @t{:io}, or @t{:probe}.
 The default is @t{:input}.

@i{element-type}---a @i{type specifier} 
		       for @i{recognizable subtype} of @b{character};
		    or a @i{type specifier} 
		       for a @i{finite} @i{recognizable subtype} of @i{integer};
		    or one of the @i{symbols} 
                          @b{signed-byte},
                          @b{unsigned-byte},
		       or @t{:default}.
 The default is @b{character}.

@i{if-exists}---one of @t{:error}, @t{:new-version}, @t{:rename},
		  @t{:rename-and-delete}, @t{:overwrite}, @t{:append},
		  @t{:supersede}, or @b{nil}.
 The default is @t{:new-version} if the version component of @i{filespec} is @t{:newest},
          or @t{:error} otherwise.

@i{if-does-not-exist}---one of @t{:error}, @t{:create}, or @b{nil}.
 The default is @t{:error} if @i{direction} is @t{:input} 
		      or @i{if-exists} is @t{:overwrite} or @t{:append};
	  @t{:create} if @i{direction} is @t{:output} or @t{:io},
		      and @i{if-exists} is neither @t{:overwrite} nor @t{:append};
       or @b{nil} when @i{direction} is @t{:probe}.

@i{external-format}---an @i{external file format designator}.
  The default is @t{:default}.

@i{stream}---a @i{file stream} or @b{nil}.

@subsubheading  Description::

@b{open} creates, opens, and returns a @i{file stream} 
that is connected to the file specified by @i{filespec}.
@i{Filespec} is the name of the file to be opened.  
If the @i{filespec} @i{designator} is a @i{stream},
that @i{stream} is not closed first or otherwise affected.

The keyword arguments to @b{open} specify the characteristics 
of the @i{file stream} that is returned, and how to handle errors.

If @i{direction} is @t{:input}
or @t{:probe},
or if @i{if-exists} is not @t{:new-version}
and the version component of the @i{filespec} is @t{:newest},
then the file opened is that file already existing in the file system
that has a version greater than that of any other file in the file system
whose other pathname components are the same as those of @i{filespec}.

An implementation is required to recognize all of 
the @b{open} keyword options 
and to do something reasonable in the context of the host operating
system.  
For example, if a file system does not support distinct file
versions and does not distinguish the notions of deletion and expunging,
@t{:new-version} might be treated the same as
@t{:rename} or @t{:supersede}, and @t{:rename-and-delete} might
be treated the same as @t{:supersede}.

@table @asis

@item @t{:direction}  
These are the possible values for @i{direction}, 
and how they affect the nature of the @i{stream} that is created:

@table @asis

@item @t{:input}  
Causes the creation of an @i{input} @i{file stream}.

@item @t{:output}  
Causes the creation of an @i{output} @i{file stream}.

@item @t{:io}  
Causes the creation of a @i{bidirectional} @i{file stream}.

@item @t{:probe}  
Causes the creation of a ``no-directional'' @i{file stream}; 
in effect, the @i{file stream} is created 
and then closed prior to being returned by @b{open}.

@end table

@item @t{:element-type}  
The @i{element-type} specifies the unit of transaction for the @i{file stream}.
If it is @t{:default},
the unit is determined by @i{file system},
possibly based on the @i{file}.

@item @t{:if-exists}  
@i{if-exists} specifies the action to be taken if @i{direction} is
@t{:output} or @t{:io} and a file of the name @i{filespec} 
already exists.
If @i{direction} is @t{:input}, not supplied, or @t{:probe}, 
@i{if-exists} is ignored.
These are the results of @b{open} as modified by @i{if-exists}:

@table @asis

@item @t{:error}  
An error of @i{type} @b{file-error} is signaled.

@item @t{:new-version}  
A new file is created with a larger version number.

@item @t{:rename}  
The existing file is renamed to some other name and then a new file is created.

@item @t{:rename-and-delete}  
The existing file is renamed to some other name,
then it is deleted but not expunged, and then a new file is created.

@item @t{:overwrite}  
Output operations on the @i{stream} destructively modify the existing file.
If @i{direction} is @t{:io} the file is opened in a bidirectional mode
that allows both reading and writing.  The file pointer is initially 
positioned at the beginning of the file; however, the file is not truncated
back to length zero when it is opened.

@item @t{:append}  
Output operations on the @i{stream} destructively modify the existing file.
The file pointer is initially positioned at the end of the file.

If @i{direction} is @t{:io},
the file is opened in a bidirectional mode that allows both reading and writing.

@item @t{:supersede}  
The existing file is superseded;
that is, a new file with the same name as the old one is created.
If possible, the implementation should not destroy the old file until the new
@i{stream} is closed.

@item @b{nil}  
No file or @i{stream} is created;
instead, @b{nil} is returned to indicate failure.

@end table

@item @t{:if-does-not-exist}  
@i{if-does-not-exist}
specifies the action to be taken if
a file of name @i{filespec} does not already exist.
These are the results of @b{open} as modified by @i{if-does-not-exist}:

@table @asis

@item @t{:error}  
An error of @i{type} @b{file-error} is signaled.

@item @t{:create}  
An empty file is created.
Processing continues  as if the file 
had already existed but no processing as 
directed by @i{if-exists} is performed.

@item @b{nil}  
No file or @i{stream} is created;
instead, @b{nil} is returned to indicate failure.

@end table

@item @t{:external-format}  
This option selects an @i{external file format} for the @i{file}:
The only @i{standardized} value for this option is @t{:default},
although @i{implementations} are permitted to define additional 
@i{external file formats} and @i{implementation-dependent} values 
returned by @b{stream-external-format} can also be used by @i{conforming programs}.

The @i{external-format} is meaningful for
any kind of @i{file stream} whose @i{element type}
is a @i{subtype} of @i{character}.
This option is ignored for @i{streams} for which it is not meaningful;
however, @i{implementations} may define other @i{element types} 
for which it is meaningful.
The consequences are unspecified if a @i{character} is written 
that cannot be represented by the given @i{external file format}.

@end table

When a file is opened, a @i{file stream} is constructed to serve
as the file system's ambassador to the @r{Lisp} environment;
operations on the @i{file stream} are reflected by operations on the file
in the file system.  

A file can be deleted, renamed, or destructively modified by @b{open}.

For information about opening relative pathnames,
see @ref{Merging Pathnames}.

@subsubheading  Examples::

@example
 (open @i{filespec} :direction :probe)  @result{}  #<Closed Probe File Stream...>
 (setq q (merge-pathnames (user-homedir-pathname) "test"))
@result{}  #<PATHNAME :HOST NIL :DEVICE @i{device-name} :DIRECTORY @i{directory-name}
    :NAME "test" :TYPE NIL :VERSION :NEWEST>
 (open @i{filespec} :if-does-not-exist :create) @result{}  #<Input File Stream...>
 (setq s (open @i{filespec} :direction :probe)) @result{}  #<Closed Probe File Stream...>
 (truename s) @result{}  #<PATHNAME :HOST NIL :DEVICE @i{device-name} :DIRECTORY
    @i{directory-name} :NAME @i{filespec} :TYPE @i{extension} :VERSION 1>
 (open s :direction :output :if-exists nil) @result{}  NIL 
@end example

@subsubheading  Affected By::

The nature and state of the host computer's @i{file system}.

@subsubheading  Exceptional Situations::                        

If @i{if-exists} is @t{:error}, (subject to the
constraints on the meaning of @i{if-exists} listed above),
an error of @i{type} @b{file-error} is signaled.                        

If @i{if-does-not-exist} is @t{:error} (subject to the
constraints on the meaning of @i{if-does-not-exist} listed above),
an error of @i{type} @b{file-error} is signaled.

If it is impossible for an implementation to handle some option
in a manner close to what is specified here,
an error of @i{type} @b{error} might be signaled.

An error of @i{type} @b{file-error} is signaled if
@t{(wild-pathname-p @i{filespec})} returns true.

An error of @i{type} @b{error} is signaled if the @i{external-format} 
is not understood by the @i{implementation}.

The various @i{file systems} in existence today have widely differing capabilities,
and some aspects of the @i{file system} are beyond the scope of this specification
to define.  A given @i{implementation} might not be able to support all of these options 
in exactly the manner stated.  An @i{implementation} is required to recognize all of 
these option keywords and to try to do something ``reasonable'' in the context of the 
host @i{file system}.  Where necessary to accomodate the @i{file system},
an @i{implementation} deviate slightly from the semantics specified here without 
being disqualified for consideration as a @i{conforming implementation}.
If it is utterly impossible for an @i{implementation} to handle some option
in a manner similar to what is specified here, it may simply signal an error.

With regard to the @t{:element-type} option, if a @i{type} is
requested that is not supported by the @i{file system}, a substitution of types 
such as that which goes on in @i{upgrading} is permissible.  As a minimum 
requirement, it should be the case that opening an @i{output} @i{stream}
to a @i{file} in a given @i{element type} and later opening 
an @i{input} @i{stream} to the same @i{file} in the same @i{element type}
should work compatibly.

@subsubheading  See Also::

@ref{with-open-file}
,
@ref{close}
,
@b{pathname},
@b{logical-pathname},

@ref{Merging Pathnames},

@ref{Pathnames as Filenames}

@subsubheading  Notes::

@b{open} does not automatically close the file when an abnormal
exit occurs.

When @i{element-type} is a @i{subtype} of @b{character},
@b{read-char} and/or @b{write-char} can be
used on the resulting @i{file stream}. 

When @i{element-type} is a @i{subtype} of @i{integer},
@b{read-byte} and/or @b{write-byte} can be used on the resulting @i{file stream}.

When @i{element-type} is @t{:default},
the @i{type} can be determined by using @b{stream-element-type}.

@node stream-external-format, with-open-file, open, Streams Dictionary
@subsection stream-external-format                                           [Function]

@code{stream-external-format}  @i{stream} @result{}  @i{format}

@subsubheading  Arguments and Values::

@i{stream}---a @i{file stream}.

@i{format}---an @i{external file format}.

@subsubheading  Description::

Returns an @i{external file format designator} for the @i{stream}.

@subsubheading  Examples::

@example
 (with-open-file (stream "test" :direction :output)
   (stream-external-format stream))
@result{}  :DEFAULT
@i{OR}@result{} :ISO8859/1-1987
@i{OR}@result{} (:ASCII :SAIL)
@i{OR}@result{} ACME::PROPRIETARY-FILE-FORMAT-17
@i{OR}@result{} #<FILE-FORMAT :ISO646-1983 2343673>
@end example

@subsubheading  See Also::

the @t{:external-format} @i{argument} to the @i{function} 
@ref{open}
 and
the 
@ref{with-open-file}
 @i{macro}.

@subsubheading  Notes::

The @i{format} returned is not necessarily meaningful 
to other @i{implementations}.

@node with-open-file, close, stream-external-format, Streams Dictionary
@subsection with-open-file                                                      [macro]

@subsubheading  Syntax::

@code{with-open-file}  @i{@r{(}stream filespec @{@i{options}@}{*}@r{)}
		   @{@i{declaration}@}{*}
		   @{@i{form}@}{*}}@*
   @result{}  @i{results}

@subsubheading  Arguments and Values::

@i{stream} -- a variable.

@i{filespec}---a @i{pathname designator}.

@i{options} -- @i{forms}; evaluated.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{forms}---an @i{implicit progn}.

@i{results}---the @i{values} returned by the @i{forms}.

@subsubheading  Description::

@b{with-open-file} uses @b{open} to create a @i{file stream}

to @i{file} named by @i{filespec}. 
@i{Filespec} is the name of the file to be opened.
@i{Options} are used as keyword arguments to @b{open}.

The @i{stream} @i{object} to which the @i{stream} @i{variable} 
is @i{bound} has @i{dynamic extent};
its @i{extent} ends when the @i{form} is exited.

@b{with-open-file} evaluates the @i{forms} as an @i{implicit progn} 
with @i{stream} bound to 

the value returned by @b{open}.

When control leaves the body, either normally or abnormally (such as by
use of @b{throw}), the file is automatically closed.  If a new
output file is being written, and control leaves abnormally, the file is
aborted and the file system is left, so far as possible, as if the file
had never been opened.  

It is possible by the use of @t{:if-exists nil} 
or @t{:if-does-not-exist nil} for
@i{stream} to be bound to @b{nil}.

Users of @t{:if-does-not-exist nil} should check for a valid @i{stream}.

The consequences are undefined if an attempt is made to @i{assign} the 
@i{stream} @i{variable}.  The compiler may choose to issue a
warning if such an attempt is detected.

@subsubheading  Examples::

@example
 (setq p (merge-pathnames "test"))
@result{}  #<PATHNAME :HOST NIL :DEVICE @i{device-name} :DIRECTORY @i{directory-name}
    :NAME "test" :TYPE NIL :VERSION :NEWEST>
 (with-open-file (s p :direction :output :if-exists :supersede)
    (format s "Here are a couple~
 (with-open-file (s p)
    (do ((l (read-line s) (read-line s nil 'eof)))
        ((eq l 'eof) "Reached end of file.")
     (format t "~&*** ~A~
@t{ |> } *** Here are a couple
@t{ |> } *** of test data lines
@result{}  "Reached end of file."
@end example

@example
;; Normally one would not do this intentionally because it is
;; not perspicuous, but beware when using :IF-DOES-NOT-EXIST NIL
;; that this doesn't happen to you accidentally...
 (with-open-file (foo "no-such-file" :if-does-not-exist nil)
   (read foo))
@t{ |> } @b{|>>}@t{hello?}@b{<<|}
@result{}  HELLO? ;This value was read from the terminal, not a file!

;; Here's another bug to avoid...
 (with-open-file (foo "no-such-file" :direction :output :if-does-not-exist nil)
   (format foo "Hello"))
@result{}  "Hello" ;FORMAT got an argument of NIL!
@end example

@subsubheading  Side Effects::

Creates a @i{stream} to the @i{file} named by @i{filename} (upon entry),
and closes the @i{stream} (upon exit).
In some @i{implementations},
the @i{file} might be locked in some way while it is open.
If the @i{stream} is an @i{output} @i{stream},
a @i{file} might be created.

@subsubheading  Affected By::

The host computer's file system.

@subsubheading  Exceptional Situations::

See the @i{function} @b{open}.

@subsubheading  See Also::

@ref{open}
,
@ref{close}
,
@b{pathname},
@b{logical-pathname},

@ref{Pathnames as Filenames}

@node close, with-open-stream, with-open-file, Streams Dictionary
@subsection close                                                            [Function]

@code{close}  @i{stream {&key} abort} @result{}  @i{result}

@subsubheading  Arguments and Values::

@i{stream}---a @i{stream} (either @i{open} or @i{closed}).

@i{abort}---a @i{generalized boolean}.
 The default is @i{false}.

@i{result}---@b{t} if the @i{stream} was @i{open} at the time it was
		     received as an @i{argument},
		 or @i{implementation-dependent} otherwise.

@subsubheading  Description::

@b{close} closes @i{stream}. 
Closing a @i{stream} means
that it may no longer be used in input or output operations.
The act of @i{closing} a @i{file stream}
ends the association between the @i{stream} and its associated @i{file}; 
the transaction with the @i{file system} is terminated,
and input/output may no longer be performed on the @i{stream}. 

If @i{abort} is @i{true}, an attempt is made to clean up any side
effects of having created @i{stream}.
If @i{stream} performs output to a file
that was created when the @i{stream} was created, the
file is deleted and any previously existing file is not superseded.

It is permissible to close an already closed @i{stream},
but in that case the @i{result} is @i{implementation-dependent}.

After @i{stream} is closed, it is still possible to perform 
the following query operations upon it:

@b{streamp}, @b{pathname}, @b{truename}, 
@b{merge-pathnames}, @b{pathname-host}, @b{pathname-device},
@b{pathname-directory},@b{pathname-name}, 
@b{pathname-type}, @b{pathname-version}, @b{namestring}, 
@b{file-namestring}, @b{directory-namestring}, 
@b{host-namestring}, @b{enough-namestring}, @b{open},
@b{probe-file}, and @b{directory}. 

The effect of @b{close} on a @i{constructed stream} is 
 to close the argument @i{stream} only. 
There is no effect on the @i{constituents} of @i{composite streams}.

For a @i{stream} created with @b{make-string-output-stream},
the result of @b{get-output-stream-string} is unspecified after @b{close}.

@subsubheading  Examples::

@example
 (setq s (make-broadcast-stream)) @result{}  #<BROADCAST-STREAM>
 (close s) @result{}  T
 (output-stream-p s) @result{}  @i{true}
@end example

@subsubheading  Side Effects::

The @i{stream} is @i{closed} (if necessary).
If @i{abort} is @i{true} and the @i{stream} is
an @i{output} @i{file stream}, its associated @i{file}
might be deleted.

@subsubheading  See Also::

@ref{open}

@node with-open-stream, listen, close, Streams Dictionary
@subsection with-open-stream                                                    [Macro]

@code{with-open-stream}  @i{@r{(}var stream@r{)}
		   @{@i{declaration}@}{*} 
		   @{@i{form}@}{*}}@*
   @result{}  @i{@{@i{result}@}{*}}

@subsubheading  Arguments and Values::

@i{var}---a @i{variable} @i{name}.

@i{stream}---a @i{form}; evaluated to produce a @i{stream}.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{forms}---an @i{implicit progn}.

@i{results}---the @i{values} returned by the @i{forms}.

@subsubheading  Description::

@b{with-open-stream} performs a series of operations on 
@i{stream}, returns a value, and then closes the @i{stream}.

@i{Var} is bound to the value of @i{stream},
and then @i{forms} are executed
as an @i{implicit progn}. 
@i{stream}
is automatically closed on exit from @b{with-open-stream},
no matter whether the exit is normal or abnormal.

The @i{stream} has @i{dynamic extent};
its @i{extent} ends when the @i{form} is exited.

The consequences are undefined if an attempt is made to @i{assign} the 
the @i{variable} @i{var} with the @i{forms}.

@subsubheading  Examples::

@example
 (with-open-stream (s (make-string-input-stream "1 2 3 4"))
    (+ (read s) (read s) (read s))) @result{}  6
@end example

@subsubheading  Side Effects::

The @i{stream} is closed (upon exit).

@subsubheading  See Also::

@ref{close}

@node listen, clear-input, with-open-stream, Streams Dictionary
@subsection listen                                                           [Function]

@code{listen}  @i{{&optional} input-stream} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{input-stream}---an @i{input} @i{stream designator}.
 The default is @i{standard input}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

Returns @i{true} if 
there is a character immediately available from @i{input-stream};
otherwise, returns @i{false}.
On a non-interactive @i{input-stream},
@b{listen} returns @i{true} except when at @i{end of file}_1.
If an @i{end of file} is encountered, @b{listen} returns @i{false}.
@b{listen} is intended to be used 
when @i{input-stream} obtains characters
from an interactive device such as a keyboard.  

@subsubheading  Examples::

@example
 (progn (unread-char (read-char)) (list (listen) (read-char)))
@t{ |> } @b{|>>}@t{1}@b{<<|}
@result{}  (T #\1)
 (progn (clear-input) (listen))
@result{}  NIL ;Unless you're a very fast typist!
@end example

@subsubheading  Affected By::

@b{*standard-input*}

@subsubheading  See Also::

@ref{interactive-stream-p}
,
@ref{read-char-no-hang}

@node clear-input, finish-output, listen, Streams Dictionary
@subsection clear-input                                                      [Function]

@code{clear-input}  @i{{&optional} input-stream} @result{}  @i{@b{nil}}

@subsubheading  Arguments and Values::

@i{input-stream}---an @i{input} @i{stream designator}.
  The default is @i{standard input}.

@subsubheading  Description::

Clears any available input from @i{input-stream}.

If @b{clear-input} does not make sense for @i{input-stream}, 
then @b{clear-input} does nothing. 

@subsubheading  Examples::
@example
;; The exact I/O behavior of this example might vary from implementation
;; to implementation depending on the kind of interactive buffering that
;; occurs.  (The call to SLEEP here is intended to help even out the 
;; differences in implementations which do not do line-at-a-time buffering.)

(defun read-sleepily (&optional (clear-p nil) (zzz 0))
  (list (progn (print '>) (read))
        ;; Note that input typed within the first ZZZ seconds 
        ;; will be discarded.
        (progn (print '>) 
               (if zzz (sleep zzz))
               (print '>>)
               (if clear-p (clear-input))
               (read))))

(read-sleepily)
@t{ |> } > @b{|>>}@t{10}@b{<<|}
@t{ |> } >
@t{ |> } >> @b{|>>}@t{20}@b{<<|}
@result{}  (10 20)

(read-sleepily t)
@t{ |> } > @b{|>>}@t{10}@b{<<|}
@t{ |> } >
@t{ |> } >> @b{|>>}@t{20}@b{<<|}
@result{}  (10 20)

(read-sleepily t 10)
@t{ |> } > @b{|>>}@t{10}@b{<<|}
@t{ |> } > @b{|>>}@t{20}@b{<<|}  ; Some implementations won't echo typeahead here.
@t{ |> } >> @b{|>>}@t{30}@b{<<|}
@result{}  (10 30)
@end example

@subsubheading  Side Effects::

The @i{input-stream} is modified.

@subsubheading  Affected By::

@b{*standard-input*}

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{input-stream} is not a @i{stream designator}.

@subsubheading  See Also::

@b{clear-output}

@node finish-output, y-or-n-p, clear-input, Streams Dictionary
@subsection finish-output, force-output, clear-output                        [Function]

@code{finish-output}  @i{{&optional} output-stream} @result{}  @i{@b{nil}}

@code{force-output}  @i{{&optional} output-stream} @result{}  @i{@b{nil}}

@code{clear-output}  @i{{&optional} output-stream} @result{}  @i{@b{nil}}

@subsubheading  Arguments and Values::

@i{output-stream}---an @i{output} @i{stream designator}.
 The default is @i{standard output}.

@subsubheading  Description::

@b{finish-output}, @b{force-output}, and @b{clear-output}
exercise control over the internal handling of buffered stream output.  

@b{finish-output} attempts to ensure that any buffered output
sent to @i{output-stream} has reached its destination, and then returns.

@b{force-output} initiates the emptying of any
internal buffers but does not wait for completion
or acknowledgment to return.

@b{clear-output} attempts to abort any
outstanding output operation in progress in order
to allow as little output as possible
to continue to the destination.  

If any of these operations does not make sense for @i{output-stream}, 
then it does nothing. 
The precise actions of these @i{functions} are @i{implementation-dependent}.

@subsubheading  Examples::
@example
;; Implementation A
 (progn (princ "am i seen?") (clear-output))
@result{}  NIL

;; Implementation B
 (progn (princ "am i seen?") (clear-output))
@t{ |> } am i seen?
@result{}  NIL
@end example

@subsubheading  Affected By::

@b{*standard-output*}

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{output-stream} is not a @i{stream designator}.

@subsubheading  See Also::

@ref{clear-input}

@node y-or-n-p, make-synonym-stream, finish-output, Streams Dictionary
@subsection y-or-n-p, yes-or-no-p                                            [Function]

@code{y-or-n-p}  @i{{&optional} control {&rest} arguments} @result{}  @i{generalized-boolean}

@code{yes-or-no-p}  @i{{&optional} control {&rest} arguments} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{control}---a @i{format control}.

@i{arguments}---@i{format arguments} for @i{control}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

These functions ask a question and parse a response from the user.
They return @i{true} if the answer is affirmative,
or @i{false} if the answer is negative.

@b{y-or-n-p} is for asking the user a question whose answer is either
``yes'' or ``no.''  
It is intended that the reply require
the user to answer a yes-or-no question with a single
character.
@b{yes-or-no-p} is also for asking the user a question
whose answer is either ``Yes'' or ``No.''  
It is intended that the reply require
the user to take more action than just a single keystroke, such as typing
the full word @t{yes} or @t{no} followed by a newline.

@b{y-or-n-p} types out a message (if supplied), reads an answer
in some @i{implementation-dependent} manner (intended to be short and simple,
such as reading a single character such as @t{Y} or @t{N}).
@b{yes-or-no-p} types out a message (if supplied), 
attracts the user's attention (for example, by ringing
the terminal's bell),
and reads an answer
in some @i{implementation-dependent} manner (intended to be multiple characters,
such as @t{YES} or @t{NO}).

If @i{format-control} is supplied and not @b{nil},
then a @b{fresh-line} operation is performed; then
a message is printed as if @i{format-control} and @i{arguments}
were given to @b{format}.
In any case, @b{yes-or-no-p} and @b{y-or-n-p} will provide 
a prompt such as ``@t{(Y or N)}'' or ``@t{(Yes or No)}'' if appropriate.

All input and output are performed using @i{query I/O}.

@subsubheading  Examples::
@example
 (y-or-n-p "(t or nil) given by")
@t{ |> } (t or nil) given by (Y or N) @b{|>>}@t{Y}@b{<<|}
@result{}  @i{true}
 (yes-or-no-p "a ~S message" 'frightening) 
@t{ |> } a FRIGHTENING message (Yes or No) @b{|>>}@t{no}@b{<<|}
@result{}  @i{false}
 (y-or-n-p "Produce listing file?") 
@t{ |> } Produce listing file?
@t{ |> } Please respond with Y or N. @b{|>>}@t{n}@b{<<|}
@result{}  @i{false}
@end example

@subsubheading  Side Effects::

Output to and input from @i{query I/O} will occur.

@subsubheading  Affected By::

@b{*query-io*}.

@subsubheading  See Also::

@ref{format}

@subsubheading  Notes::

@b{yes-or-no-p} and @b{yes-or-no-p} do not add question marks
to the end of the prompt string, so any desired question mark or other
punctuation should be explicitly included in the text query.

@node make-synonym-stream, synonym-stream-symbol, y-or-n-p, Streams Dictionary
@subsection make-synonym-stream                                              [Function]

@code{make-synonym-stream}  @i{symbol} @result{}  @i{synonym-stream}

@subsubheading  Arguments and Values::

@i{symbol}---a @i{symbol} that names a @i{dynamic variable}.

@i{synonym-stream}---a @i{synonym stream}.

@subsubheading  Description::

Returns a @i{synonym stream} whose @i{synonym stream symbol} is @i{symbol}.

@subsubheading  Examples::

@example
 (setq a-stream (make-string-input-stream "a-stream")
        b-stream (make-string-input-stream "b-stream"))
@result{}  #<String Input Stream> 
 (setq s-stream (make-synonym-stream 'c-stream))
@result{}  #<SYNONYM-STREAM for C-STREAM> 
 (setq c-stream a-stream)
@result{}  #<String Input Stream> 
 (read s-stream) @result{}  A-STREAM
 (setq c-stream b-stream)
@result{}  #<String Input Stream> 
 (read s-stream) @result{}  B-STREAM
@end example

@subsubheading  Exceptional Situations::

Should signal @b{type-error} if its argument is not a @i{symbol}.

@subsubheading  See Also::

@ref{Stream Concepts}

@node synonym-stream-symbol, broadcast-stream-streams, make-synonym-stream, Streams Dictionary
@subsection synonym-stream-symbol                                            [Function]

@code{synonym-stream-symbol}  @i{synonym-stream} @result{}  @i{symbol}

@subsubheading  Arguments and Values::

@i{synonym-stream}---a @i{synonym stream}.

@i{symbol}---a @i{symbol}.

@subsubheading  Description::

Returns the @i{symbol} whose @b{symbol-value} the @i{synonym-stream} is using.  

@subsubheading  See Also::

@ref{make-synonym-stream}

@node broadcast-stream-streams, make-broadcast-stream, synonym-stream-symbol, Streams Dictionary
@subsection broadcast-stream-streams                                         [Function]

@code{broadcast-stream-streams}  @i{broadcast-stream} @result{}  @i{streams}

@subsubheading  Arguments and Values::

@i{broadcast-stream}---a @i{broadcast stream}.

@i{streams}---a @i{list} of @i{streams}.

@subsubheading  Description::

Returns a @i{list} of output @i{streams} that constitute
all the @i{streams} to which the @i{broadcast-stream} is broadcasting.  

@node make-broadcast-stream, make-two-way-stream, broadcast-stream-streams, Streams Dictionary
@subsection make-broadcast-stream                                            [Function]

@code{make-broadcast-stream}  @i{{&rest} streams} @result{}  @i{broadcast-stream}

@subsubheading  Arguments and Values::

@i{stream}---an @i{output} @i{stream}.

@i{broadcast-stream}---a @i{broadcast stream}.

@subsubheading  Description::

Returns a @i{broadcast stream}.

@subsubheading  Examples::

@example
 (setq a-stream (make-string-output-stream)
        b-stream (make-string-output-stream)) @result{}  #<String Output Stream>
 (format (make-broadcast-stream a-stream b-stream)
          "this will go to both streams") @result{}  NIL
 (get-output-stream-string a-stream) @result{}  "this will go to both streams"
 (get-output-stream-string b-stream) @result{}  "this will go to both streams"
@end example

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if any @i{stream} is not an @i{output} @i{stream}.

@subsubheading  See Also::

@ref{broadcast-stream-streams}

@node make-two-way-stream, two-way-stream-input-stream, make-broadcast-stream, Streams Dictionary
@subsection make-two-way-stream                                              [Function]

@code{make-two-way-stream}  @i{input-stream output-stream} @result{}  @i{two-way-stream}

@subsubheading  Arguments and Values::

@i{input-stream}---a @i{stream}.

@i{output-stream}---a @i{stream}.

@i{two-way-stream}---a @i{two-way stream}.

@subsubheading  Description::

Returns a @i{two-way stream} 
that gets  its input  from @i{input-stream}
and  sends its output to   @i{output-stream}.

@subsubheading  Examples::

@example
 (with-output-to-string (out)
    (with-input-from-string (in "input...")
      (let ((two (make-two-way-stream in out)))
        (format two "output...")
        (setq what-is-read (read two))))) @result{}  "output..."
 what-is-read @result{}  INPUT... 
@end example

@subsubheading  Exceptional Situations::

Should signal an error of @i{type} @b{type-error}
			      if @i{input-stream} is not an @i{input} @i{stream}.
Should signal an error of @i{type} @b{type-error}
			      if @i{output-stream} is not an @i{output} @i{stream}.

@node two-way-stream-input-stream, echo-stream-input-stream, make-two-way-stream, Streams Dictionary
@subsection two-way-stream-input-stream, two-way-stream-output-stream
@flushright
@i{[Function]}
@end flushright

@code{two-way-stream-input-stream}  @i{two-way-stream} @result{}  @i{input-stream}

@code{two-way-stream-output-stream}  @i{two-way-stream} @result{}  @i{output-stream}

@subsubheading  Arguments and Values::

@i{two-way-stream}---a @i{two-way stream}.

@i{input-stream}---an @i{input} @i{stream}.

@i{output-stream}---an @i{output} @i{stream}.

@subsubheading  Description::

@b{two-way-stream-input-stream} returns the @i{stream}
from which @i{two-way-stream} receives input.

@b{two-way-stream-output-stream} returns the @i{stream} 
to which @i{two-way-stream} sends output.

@node echo-stream-input-stream, make-echo-stream, two-way-stream-input-stream, Streams Dictionary
@subsection echo-stream-input-stream, echo-stream-output-stream              [Function]

@code{echo-stream-input-stream}  @i{echo-stream} @result{}  @i{input-stream}

@code{echo-stream-output-stream}  @i{echo-stream} @result{}  @i{output-stream}

@subsubheading  Arguments and Values::

@i{echo-stream}---an @i{echo stream}.

@i{input-stream}---an @i{input} @i{stream}.

@b{output-stream}---an @i{output} @i{stream}.

@subsubheading  Description::

@b{echo-stream-input-stream} returns the @i{input} @i{stream} 
from which @i{echo-stream} receives input.

@b{echo-stream-output-stream} returns the @i{output} @i{stream}
to which @i{echo-stream} sends output.

@node make-echo-stream, concatenated-stream-streams, echo-stream-input-stream, Streams Dictionary
@subsection make-echo-stream                                                 [Function]

@code{make-echo-stream}  @i{input-stream output-stream} @result{}  @i{echo-stream}

@subsubheading  Arguments and Values::

@i{input-stream}---an @i{input} @i{stream}.

@i{output-stream}---an @i{output} @i{stream}.

@i{echo-stream}---an @i{echo stream}.

@subsubheading  Description::

Creates and returns an @i{echo stream} 
that takes input  from @i{input-stream}
and  sends output to   @i{output-stream}.

@subsubheading  Examples::
@example
 (let ((out (make-string-output-stream)))
    (with-open-stream 
        (s (make-echo-stream
            (make-string-input-stream "this-is-read-and-echoed")
            out))
      (read s)
      (format s " * this-is-direct-output")
      (get-output-stream-string out)))
@result{}  "this-is-read-and-echoed * this-is-direct-output"
@end example

@subsubheading  See Also::

@ref{echo-stream-input-stream; echo-stream-output-stream}
,
@b{echo-stream-output-stream},
@ref{make-two-way-stream}

@node concatenated-stream-streams, make-concatenated-stream, make-echo-stream, Streams Dictionary
@subsection concatenated-stream-streams                                      [Function]

@code{concatenated-stream-streams}  @i{concatenated-stream} @result{}  @i{streams}

@subsubheading  Arguments and Values::

@i{concatenated-stream} -- a @i{concatenated stream}.

@i{streams}---a @i{list} of @i{input} @i{streams}.

@subsubheading  Description::

Returns a @i{list} of @i{input} @i{streams} that constitute the
ordered set of @i{streams} the @i{concatenated-stream} still 
has to read from, starting with the current one it is reading from.
The list may be @i{empty} if no more @i{streams} remain to be read.

The consequences are undefined if the @i{list structure} of the @i{streams}
is ever modified.

@node make-concatenated-stream, get-output-stream-string, concatenated-stream-streams, Streams Dictionary
@subsection make-concatenated-stream                                         [Function]

@code{make-concatenated-stream}  @i{{&rest} input-streams} @result{}  @i{concatenated-stream}

@subsubheading  Arguments and Values::

@i{input-stream}---an @i{input} @i{stream}.

@i{concatenated-stream}---a @i{concatenated stream}.

@subsubheading  Description::

Returns a @i{concatenated stream} that has the indicated @i{input-streams}
initially associated with it.

@subsubheading  Examples::
@example
 (read (make-concatenated-stream
         (make-string-input-stream "1")
         (make-string-input-stream "2"))) @result{}  12
@end example

@subsubheading  Exceptional Situations::

Should signal @b{type-error} if any argument is not an @i{input} @i{stream}.

@subsubheading  See Also::

@ref{concatenated-stream-streams}

@node get-output-stream-string, make-string-input-stream, make-concatenated-stream, Streams Dictionary
@subsection get-output-stream-string                                         [Function]

@code{get-output-stream-string}  @i{string-output-stream} @result{}  @i{string}

@subsubheading  Arguments and Values::

@i{string-output-stream}---a @i{stream}.

@i{string}---a @i{string}.

@subsubheading  Description::

Returns a @i{string} containing, in order, all the @i{characters} 
that have been output to @i{string-output-stream}.
This operation clears any @i{characters} on @i{string-output-stream},
so the @i{string} contains only those @i{characters} which have been output
   since the last call to @b{get-output-stream-string}
or since the creation of the @i{string-output-stream},
whichever occurred most recently.

@subsubheading  Examples::
@example
 (setq a-stream (make-string-output-stream)
        a-string "abcdefghijklm") @result{}  "abcdefghijklm"
 (write-string a-string a-stream) @result{}  "abcdefghijklm"
 (get-output-stream-string a-stream) @result{}  "abcdefghijklm"
 (get-output-stream-string a-stream) @result{}  ""
@end example

@subsubheading  Side Effects::

The @i{string-output-stream} is cleared.

@subsubheading  Exceptional Situations::

The consequences are undefined if @i{stream-output-string} is @i{closed}.

The consequences are undefined if @i{string-output-stream} is a @i{stream} that
was not produced by @b{make-string-output-stream}.

The consequences are undefined if @i{string-output-stream} was 
created implicitly by @b{with-output-to-string} or @b{format}.

@subsubheading  See Also::

@ref{make-string-output-stream}

@node make-string-input-stream, make-string-output-stream, get-output-stream-string, Streams Dictionary
@subsection make-string-input-stream                                         [Function]

@code{make-string-input-stream}  @i{string {&optional} start end} @result{}  @i{string-stream}

@subsubheading  Arguments and Values::

@i{string}---a @i{string}.

@i{start}, @i{end}---@i{bounding index designators} of @i{string}.
 The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.

@i{string-stream}---an @i{input} @i{string stream}.

@subsubheading  Description::

Returns an @i{input} @i{string stream}.  
This @i{stream} will supply, in order, the @i{characters} in the substring
of @i{string} @i{bounded} by @i{start} and @i{end}.
After the last @i{character} has been supplied, 
the @i{string stream} will then be at @i{end of file}.

@subsubheading  Examples::

@example
 (let ((string-stream (make-string-input-stream "1 one ")))
   (list (read string-stream nil nil)
         (read string-stream nil nil)
         (read string-stream nil nil)))
@result{}  (1 ONE NIL)

 (read (make-string-input-stream "prefixtargetsuffix" 6 12)) @result{}  TARGET
@end example

@subsubheading  See Also::

@ref{with-input-from-string}

@node make-string-output-stream, with-input-from-string, make-string-input-stream, Streams Dictionary
@subsection make-string-output-stream                                        [Function]

@code{make-string-output-stream}  @i{{&key} element-type} @result{}  @i{string-stream}

@subsubheading  Arguments and Values::

@i{element-type}---a @i{type specifier}.
  The default is @b{character}.

@i{string-stream}---an @i{output} @i{string stream}.

@subsubheading  Description::

Returns

an @i{output} @i{string stream} that accepts @i{characters}
and makes available (via @b{get-output-stream-string})
a @i{string} that contains the @i{characters} that were actually output.

The @i{element-type} names the @i{type} of the @i{elements}
of the @i{string}; a @i{string} is constructed of the most specialized
@i{type} that can accommodate @i{elements} of that @i{element-type}.

@subsubheading  Examples::

@example
 (let ((s (make-string-output-stream)))
   (write-string "testing... " s)
   (prin1 1234 s)
   (get-output-stream-string s))
@result{}  "testing... 1234"
@end example

None..

@subsubheading  See Also::

@ref{get-output-stream-string}
, 
@ref{with-output-to-string}

@node with-input-from-string, with-output-to-string, make-string-output-stream, Streams Dictionary
@subsection with-input-from-string                                              [Macro]

@code{with-input-from-string}  @i{@r{(}var string {&key} index start end@r{)}
			  @{@i{declaration}@}{*}
			  @{@i{form}@}{*}}@*
   @result{}  @i{@{@i{result}@}{*}}

@subsubheading  Arguments and Values::

@i{var}---a @i{variable} @i{name}.

@i{string}---a @i{form}; evaluated to produce a @i{string}.

@i{index}---a @i{place}.

@i{start}, @i{end}---@i{bounding index designators} of @i{string}.
 The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{forms}---an @i{implicit progn}.

@i{result}---the @i{values} returned by the @i{forms}.

@subsubheading  Description::

Creates an

@i{input} @i{string stream}, 

provides an opportunity to perform operations on the @i{stream} 
 (returning zero or more @i{values}),
and then closes the @i{string stream}.

@i{String} is evaluated first, and @i{var} is bound to
a character @i{input} @i{string stream} that supplies 
@i{characters} from the subsequence of the resulting @i{string} @i{bounded} by
@i{start} and @i{end}. 
The body is executed as an @i{implicit progn}.

The @i{input} @i{string stream} is automatically closed on exit from
@b{with-input-from-string}, no matter whether the exit is normal or abnormal.

The @i{input} @i{string stream} to which the @i{variable} @i{var}
is @i{bound} has @i{dynamic extent};
its @i{extent} ends when the @i{form} is exited.

The @i{index} is a pointer within the @i{string} to be advanced.
If @b{with-input-from-string}
is exited normally, then @i{index} will have
as its @i{value}
the index into the @i{string} indicating the first character not read
which is @t{(length @i{string})} if all characters were used.
The place specified by @i{index}
is not updated as reading progresses, but only at the
end of the operation. 

@i{start} and @i{index} may both specify the same variable, 
which is a pointer within the @i{string} to be advanced,
perhaps repeatedly by some containing loop.

The consequences are undefined if an attempt is made to @i{assign} 
the @i{variable} @i{var}.

@subsubheading  Examples::
@example
 (with-input-from-string (s "XXX1 2 3 4xxx"
                             :index ind
                             :start 3 :end 10)
    (+ (read s) (read s) (read s))) @result{}  6
 ind @result{}  9
 (with-input-from-string (s "Animal Crackers" :index j :start 6)
   (read s)) @result{}  CRACKERS
@end example

The variable @t{j} is set to @t{15}.

@subsubheading  Side Effects::

The @i{value} of the @i{place} named by @i{index}, if any, is modified.

@subsubheading  See Also::

@ref{make-string-input-stream}
,

@ref{Traversal Rules and Side Effects}

@node with-output-to-string, *debug-io*, with-input-from-string, Streams Dictionary
@subsection with-output-to-string                                               [Macro]

@code{with-output-to-string}  @i{@r{(}var {&optional} string-form {&key} element-type@r{)}
 			  @{@i{declaration}@}{*}
 			  @{@i{form}@}{*}}@*
   @result{}  @i{@{@i{result}@}{*}}

@subsubheading  Arguments and Values::

@i{var}---a @i{variable} @i{name}.

@i{string-form}---a @i{form} or @b{nil};
		      if @i{non-nil}, evaluated to produce @i{string}.

@i{string}---a @i{string} that has a @i{fill pointer}.

@i{element-type}---a @i{type specifier}; evaluated.

  The default is @b{character}.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{forms}---an @i{implicit progn}.

@i{results}---If a @i{string-form} is not supplied or @b{nil},
		  a @i{string}; otherwise, 
		  the @i{values} returned by the @i{forms}.

@subsubheading  Description::

@b{with-output-to-string} creates a

character @i{output} @i{stream}, performs a series of operations
that may send results to this @i{stream}, and then closes the @i{stream}. 

The @i{element-type} names the @i{type} of the elements
of the @i{stream}; a @i{stream} is constructed of the most specialized
@i{type} that can accommodate elements of the given @i{type}.  

The body is executed as an @i{implicit progn} with @i{var}
bound to an @i{output} @i{string stream}.  
All output to that @i{string stream} is saved in a @i{string}.  

If @i{string} is supplied, @i{element-type} is ignored, 
and the output is incrementally appended to @i{string} as
if by use of @b{vector-push-extend}.

The @i{output} @i{stream} 
is automatically closed on exit from @b{with-output-from-string},
no matter whether the exit is normal or abnormal.

The @i{output} @i{string stream} to which the @i{variable} @i{var}
is @i{bound} has @i{dynamic extent};
its @i{extent} ends when the @i{form} is exited.

If no @i{string} is provided, then @b{with-output-from-string} 

produces a @i{stream} that accepts characters and returns a @i{string} 
of the indicated @i{element-type}.

If @i{string} is provided,
@b{with-output-to-string} returns the results of evaluating the last @i{form}.

The consequences are undefined if an attempt is made to @i{assign}
the @i{variable} @i{var}.

@subsubheading  Examples::
@example
 (setq fstr (make-array '(0) :element-type 'base-char
                             :fill-pointer 0 :adjustable t)) @result{}  ""
 (with-output-to-string (s fstr)
    (format s "here's some output")
    (input-stream-p s)) @result{}  @i{false}
 fstr @result{}  "here's some output"
@end example

@subsubheading  Side Effects::

The @i{string} is modified.

@subsubheading  Exceptional Situations::

The consequences are undefined if destructive modifications are performed
directly on the @i{string} during the @i{dynamic extent} of the call.

@subsubheading  See Also::

@ref{make-string-output-stream}
,
@b{vector-push-extend},

@ref{Traversal Rules and Side Effects}

@node *debug-io*, *terminal-io*, with-output-to-string, Streams Dictionary
@subsection *debug-io*, *error-output*, *query-io*,
@subheading *standard-input*, *standard-output*,
@subheading *trace-output*
@flushright
@i{[Variable]}
@end flushright

@subsubheading  Value Type::

For @b{*standard-input*}:
 an @i{input} @i{stream}

For @b{*error-output*}, @b{*standard-output*}, and @b{*trace-output*}:
 an @i{output} @i{stream}.

For @b{*debug-io*}, @b{*query-io*}:
 a @i{bidirectional} @i{stream}.

@subsubheading  Initial Value::

@i{implementation-dependent}, but
it must be an @i{open} @i{stream} 
that is not a  @i{generalized synonym stream}
to an @i{I/O customization variables}
but that might be a @i{generalized synonym stream} to 
the value of some @i{I/O customization variable}.
The initial value might also be a @i{generalized synonym stream} 
to either the @i{symbol} @b{*terminal-io*} or to the @i{stream} 
that is its @i{value}.

@subsubheading  Description::

These @i{variables} are collectively called the
@i{standardized} @i{I/O customization variables}.
They can be @i{bound} or @i{assigned} in order to 
change the default destinations for input and/or output 
used by various @i{standardized} @i{operators} and facilities.

The @i{value} of @b{*debug-io*}, called @i{debug I/O},
is a @i{stream} to be used for interactive debugging purposes.  

The @i{value} of @b{*error-output*}, called @i{error output},
is a @i{stream} to which warnings and non-interactive error messages should be sent.

The @i{value} of @b{*query-io*}, called @i{query I/O},
is a @i{bidirectional} @i{stream} 
to be used when asking questions of the user.  The question should be output 
to this @i{stream}, and the answer read from it.

The @i{value} of @b{*standard-input*}, called @i{standard input},
is a @i{stream} that is used by many @i{operators} 
as a default source of input when no specific @i{input} @i{stream}
is explicitly supplied.

The @i{value} of @b{*standard-output*}, called @i{standard output},
is a @i{stream} that is used by many @i{operators} 
as a default destination for output when no specific @i{output} @i{stream}
is explicitly supplied.

The @i{value} of @b{*trace-output*}, called @i{trace output},
is the @i{stream} on which traced functions (see @b{trace})
and the @b{time} @i{macro} print their output.

@subsubheading  Examples::

@example
 (with-output-to-string (*error-output*)
   (warn "this string is sent to *error-output*"))
 @result{}  "Warning: this string is sent to *error-output*
" ;The exact format of this string is @i{implementation-dependent}.

 (with-input-from-string (*standard-input* "1001")
    (+ 990 (read))) @result{}  1991                       

 (progn (setq out (with-output-to-string (*standard-output*)
                     (print "print and format t send things to")
                     (format t "*standard-output* now going to a string")))
        :done)
@result{}  :DONE
 out
@result{}  "
\"print and format t send things to\" *standard-output* now going to a string"

 (defun fact (n) (if (< n 2) 1 (* n (fact (- n 1)))))
@result{}  FACT
 (trace fact)
@result{}  (FACT)
;; Of course, the format of traced output is implementation-dependent.
 (with-output-to-string (*trace-output*)
   (fact 3)) 
@result{}  "
1 Enter FACT 3
| 2 Enter FACT 2
|   3 Enter FACT 1
|   3 Exit FACT 1
| 2 Exit FACT 2
1 Exit FACT 6"
@end example

@subsubheading  See Also::

@b{*terminal-io*},
@b{synonym-stream},
@ref{Time}
,
@ref{trace; untrace}
,
{@ref{Conditions}},
{@ref{Reader}},
{@ref{Printer}}

@subsubheading  Notes::

The intent of the constraints on the initial @i{value} 
of the @i{I/O customization variables} is to ensure that it
is always safe to @i{bind} or @i{assign} such a @i{variable} to 
the @i{value} of another @i{I/O customization variable}, without
unduly restricting @i{implementation} flexibility.

It is common for an @i{implementation} to make
the initial @i{values} of @b{*debug-io*} and @b{*query-io*} 
be the @i{same} @i{stream}, 
and to make
the initial @i{values} of @b{*error-output*} and @b{*standard-output*} 
be the @i{same} @i{stream}.

The functions @b{y-or-n-p} and @b{yes-or-no-p} use @i{query I/O}
for their input and output.

In the normal @i{Lisp read-eval-print loop},
input is read from @i{standard input}.
Many input functions, including @b{read} and @b{read-char},
take a @i{stream} argument that defaults to @i{standard input}.

In the normal @i{Lisp read-eval-print loop}, output is sent to @i{standard output}.
Many output functions, including @b{print} and @b{write-char}, 
take a @i{stream} argument that defaults to @i{standard output}.

A program that wants, for example, to divert output to a file should do so by 
@i{binding} @b{*standard-output*}; that way error messages sent to
@b{*error-output*} can still get to the user by going through
@b{*terminal-io*} (if @b{*error-output*} is bound to @b{*terminal-io*}),
which is usually what is desired.

@node *terminal-io*, stream-error, *debug-io*, Streams Dictionary
@subsection *terminal-io*                                                    [Variable]

@subsubheading  Value Type::

a @i{bidirectional} @i{stream}.

@subsubheading  Initial Value::

@i{implementation-dependent}, but
it must be an @i{open} @i{stream} 
that is not a  @i{generalized synonym stream}
to an @i{I/O customization variables}
but that might be a @i{generalized synonym stream} to 
the @i{value} of some @i{I/O customization variable}.

@subsubheading  Description::

The @i{value} of @b{*terminal-io*}, called  @i{terminal I/O}, is ordinarily 
a @i{bidirectional} @i{stream} that connects to the user's console.
Typically, writing to this @i{stream} 
would cause the output to appear
on a display screen, for example, and reading from the @i{stream} would
accept input from a keyboard.  It is intended
that standard input functions such as @b{read} and @b{read-char},
when used with this @i{stream}, cause echoing of the input
into the output side of the @i{stream}. The means by which this is
accomplished are @i{implementation-dependent}.

The effect of changing the @i{value} of @b{*terminal-io*},
either by @i{binding} or @i{assignment},
is @i{implementation-defined}.

@subsubheading  Examples::

@example
 (progn (prin1 'foo) (prin1 'bar *terminal-io*))
@t{ |> } FOOBAR
@result{}  BAR
 (with-output-to-string (*standard-output*)
   (prin1 'foo) 
   (prin1 'bar *terminal-io*))
@t{ |> } BAR
@result{}  "FOO"
@end example

@subsubheading  See Also::

@b{*debug-io*},
@b{*error-output*},
@b{*query-io*},
@b{*standard-input*},
@b{*standard-output*},
@b{*trace-output*}

@node stream-error, stream-error-stream, *terminal-io*, Streams Dictionary
@subsection stream-error                                               [Condition Type]

@subsubheading  Class Precedence List::
@b{stream-error},
@b{error},
@b{serious-condition},
@b{condition},
@b{t}

@subsubheading  Description::

The @i{type} @b{stream-error}
consists of error conditions that are related to receiving input from
or sending output to a @i{stream}.  
The ``offending stream'' is initialized by
the @t{:stream} initialization argument to @b{make-condition}, 
and is @i{accessed} by the @i{function} @b{stream-error-stream}.

@subsubheading  See Also::

@ref{stream-error-stream}

@node stream-error-stream, end-of-file, stream-error, Streams Dictionary
@subsection stream-error-stream                                              [Function]

@code{stream-error-stream}  @i{condition} @result{}  @i{stream}

@subsubheading  Arguments and Values::

@i{condition}---a @i{condition} of @i{type} @b{stream-error}.

@i{stream}---a @i{stream}.

@subsubheading  Description::

Returns the offending @i{stream} of a @i{condition} of @i{type} @b{stream-error}.

@subsubheading  Examples::
@example
 (with-input-from-string (s "(FOO")
   (handler-case (read s)
     (end-of-file (c)
       (format nil "~&End of file on ~S." (stream-error-stream c)))))
"End of file on #<String Stream>."
@end example

@subsubheading  See Also::

@b{stream-error},
@ref{Conditions}

@node end-of-file,  , stream-error-stream, Streams Dictionary
@subsection end-of-file                                                [Condition Type]

@subsubheading  Class Precedence List::
@b{end-of-file},
@b{stream-error},
@b{error},
@b{serious-condition},
@b{condition},
@b{t}

@subsubheading  Description::

The @i{type} @b{end-of-file} consists of
error conditions related to read operations that are done on
@i{streams} that have no more data.

@subsubheading  See Also::

@ref{stream-error-stream}

@c end of including dict-streams

@c %**end of chapter

