concept-streams.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. % -*- Mode: TeX -*-
  2. \beginsubSection{Introduction to Streams}
  3. %% 22.0.0 3
  4. %All input/output operations are performed on \term{streams} of various kinds.
  5. A \newterm{stream} is an \term{object} that can be used with an input or output
  6. function to identify an appropriate source or sink of \term{characters} or
  7. \term{bytes} for that operation.
  8. %% 21.0.0 3
  9. %% 21.0.0 4
  10. A \newterm{character} \newterm{stream} is a source or sink of \term{characters}.
  11. A \newterm{binary} \newterm{stream} is a source or sink of \term{bytes}.
  12. Some operations may be performed on any kind of \term{stream};
  13. \thenextfigure\ provides a list of \term{standardized} operations
  14. that are potentially useful with any kind of \term{stream}.
  15. \displaytwo{Some General-Purpose Stream Operations}{
  16. close&stream-element-type\cr
  17. input-stream-p&streamp\cr
  18. interactive-stream-p&with-open-stream\cr
  19. output-stream-p&\cr
  20. }
  21. Other operations are only meaningful on certain \term{stream} \term{types}.
  22. For example, \funref{read-char} is only defined for \term{character} \term{streams}
  23. and \funref{read-byte} is only defined for \term{binary} \term{streams}.
  24. \beginsubsubsection{Abstract Classifications of Streams}
  25. \beginsubsubsubsection{Input, Output, and Bidirectional Streams}
  26. %% 21.0.0 3
  27. A \term{stream}, whether a \term{character} \term{stream} or a \term{binary} \term{stream},
  28. can be an \newterm{input} \newterm{stream} (source of data),
  29. an \newterm{output} \newterm{stream} (sink for data),
  30. both,
  31. or (\eg when ``\f{:direction :probe}'' is given to \funref{open}) neither.
  32. \Thenextfigure\ shows \term{operators} relating to
  33. \term{input} \term{streams}.
  34. \DefineFigure{InputStreamOps}
  35. \displaythree{Operators relating to Input Streams.}{
  36. clear-input&read-byte&read-from-string\cr
  37. listen&read-char&read-line\cr
  38. peek-char&read-char-no-hang&read-preserving-whitespace\cr
  39. read&read-delimited-list&unread-char\cr
  40. }
  41. \Thenextfigure\ shows \term{operators} relating to
  42. \term{output} \term{streams}.
  43. \DefineFigure{OutputStreamOps}
  44. \displaythree{Operators relating to Output Streams.}{
  45. clear-output&prin1&write\cr
  46. finish-output&prin1-to-string&write-byte\cr
  47. force-output&princ&write-char\cr
  48. format&princ-to-string&write-line\cr
  49. fresh-line&print&write-string\cr
  50. pprint&terpri&write-to-string\cr
  51. }
  52. A \term{stream} that is both an \term{input} \term{stream} and an \term{output} \term{stream}
  53. is called a \newterm{bidirectional} \newterm{stream}.
  54. \Seefuns{input-stream-p} and \funref{output-stream-p}.
  55. Any of the \term{operators} listed in \figref\InputStreamOps\ or \figref\OutputStreamOps\
  56. can be used with \term{bidirectional} \term{streams}. In addition, \thenextfigure\
  57. shows a list of \term{operators} that relate specificaly to
  58. \term{bidirectional} \term{streams}.
  59. \displaythree{Operators relating to Bidirectional Streams.}{
  60. y-or-n-p&yes-or-no-p&\cr
  61. }
  62. \endsubsubsubsection%{Input, Output, and Bidirectional Streams}
  63. \beginsubsubsubsection{Open and Closed Streams}
  64. \DefineSection{OpenAndClosedStreams}
  65. \term{Streams} are either \newterm{open} or \newterm{closed}.
  66. Except as explicitly specified otherwise,
  67. operations that create and return \term{streams} return \term{open} \term{streams}.
  68. The action of \term{closing} a \term{stream} marks the end of its use as a source
  69. or sink of data, permitting the \term{implementation} to reclaim its internal data
  70. structures, and to free any external resources which might have been locked by the
  71. \term{stream} when it was opened.
  72. Except as explicitly specified otherwise,
  73. the consequences are undefined when a \term{closed} \term{stream}
  74. is used where a \term{stream} is called for.
  75. Coercion of \term{streams} to \term{pathnames}
  76. is permissible for \term{closed} \term{streams};
  77. in some situations, such as for a \term{truename} computation,
  78. the result might be different for an \term{open} \term{stream}
  79. and for that same \term{stream} once it has been \term{closed}.
  80. \endsubsubsubsection%{Open and Closed Streams}
  81. \beginsubsubsubsection{Interactive Streams}
  82. \DefineSection{InteractiveStreams}
  83. An \newterm{interactive stream} is one on which it makes sense to perform
  84. interactive querying.
  85. The precise meaning of an \term{interactive stream} is
  86. \term{implementation-defined}, and may depend on the underlying
  87. operating system. Some examples of the things that an
  88. \term{implementation} might choose to use as identifying characteristics
  89. of an \term{interactive stream} include:
  90. \beginlist
  91. \itemitem{\bull}
  92. The \term{stream} is connected to a person (or equivalent) in such a way
  93. that the program can prompt for information and expect to receive different
  94. input depending on the prompt.
  95. \itemitem{\bull}
  96. The program is expected to prompt for input and support ``normal input editing''.
  97. \itemitem{\bull}
  98. \funref{read-char} might wait for the user to type something before returning
  99. instead of immediately returning a character or end-of-file.
  100. \endlist
  101. The general intent of having some \term{streams} be classified as
  102. \term{interactive streams} is to allow them to be distinguished from
  103. streams containing batch (or background or command-file) input.
  104. Output to batch streams is typically discarded or saved for later viewing,
  105. so interactive queries to such streams might not have the expected effect.
  106. \term{Terminal I/O} might or might not be an \term{interactive stream}.
  107. \endsubsubsubsection%{Interactive Streams}
  108. \beginsubsubsection{Abstract Classifications of Streams}
  109. \beginsubsubsubsection{File Streams}
  110. %% 23.2.0 1
  111. Some \term{streams}, called \newtermidx{file streams}{file stream}, provide access to \term{files}.
  112. An \term{object} \ofclass{file-stream} is used to represent a \term{file stream}.
  113. The basic operation for opening a \term{file} is \funref{open},
  114. which typically returns a \term{file stream}
  115. (see its dictionary entry for details).
  116. The basic operation for closing a \term{stream} is \funref{close}.
  117. The macro \macref{with-open-file} is useful
  118. to express the common idiom of opening a \term{file}
  119. for the duration of a given body of \term{code},
  120. and assuring that the resulting \term{stream} is closed upon exit from that body.
  121. \endsubsubsubsection%{File Streams}
  122. \beginsubsubsection{Other Subclasses of Stream}
  123. \Theclass{stream} has a number of \term{subclasses} defined
  124. by this specification. \Thenextfigure\ shows some information
  125. about these subclasses.
  126. \tablefigtwo{Defined Names related to Specialized Streams}{Class}{Related Operators}{
  127. \typeref{broadcast-stream} & \funref{make-broadcast-stream} \cr
  128. & \funref{broadcast-stream-streams} \cr
  129. \typeref{concatenated-stream} & \funref{make-concatenated-stream} \cr
  130. & \funref{concatenated-stream-streams} \cr
  131. \typeref{echo-stream} & \funref{make-echo-stream} \cr
  132. & \funref{echo-stream-input-stream} \cr
  133. & \funref{echo-stream-output-stream} \cr
  134. \typeref{string-stream} & \funref{make-string-input-stream} \cr
  135. & \funref{with-input-from-string} \cr
  136. & \funref{make-string-output-stream} \cr
  137. & \funref{with-output-to-string} \cr
  138. & \funref{get-output-stream-string} \cr
  139. \typeref{synonym-stream} & \funref{make-synonym-stream} \cr
  140. & \funref{synonym-stream-symbol} \cr
  141. \typeref{two-way-stream} & \funref{make-two-way-stream} \cr
  142. & \funref{two-way-stream-input-stream} \cr
  143. & \funref{two-way-stream-output-stream} \cr
  144. }
  145. \endsubsubsection%{Other Subclasses of Stream}
  146. \endsubSection%{Introduction to Streams}
  147. \beginsubSection{Stream Variables}
  148. \term{Variables} whose \term{values} must be \term{streams} are sometimes called
  149. \newtermidx{stream variables}{stream variable}.
  150. Certain \term{stream variables} are defined by this specification
  151. to be the proper source of input or output in various \term{situations}
  152. where no specific \term{stream} has been specified instead.
  153. A complete list of such \term{standardized} \term{stream variables}
  154. appears in \thenextfigure.
  155. %Added by agreement of Barrett, Loosemore, and KMP. -kmp 14-Feb-92
  156. The consequences are undefined if at any time
  157. the \term{value} of any of these \term{variables} is not an \term{open} \term{stream}.
  158. \DefineFigure{StandardizedStreamVars}
  159. \tablefigtwo{Standardized Stream Variables}{Glossary Term}{Variable Name}{
  160. \term{debug I/O} & \varref{*debug-io*} \cr
  161. \term{error output} & \varref{*error-output*} \cr
  162. \term{query I/O} & \varref{*query-io*} \cr
  163. \term{standard input} & \varref{*standard-input*} \cr
  164. \term{standard output} & \varref{*standard-output*} \cr
  165. \term{terminal I/O} & \varref{*terminal-io*} \cr
  166. \term{trace output} & \varref{*trace-output*} \cr
  167. }
  168. Note that, by convention, \term{standardized} \term{stream variables} have names
  169. ending in ``\f{-input*}'' if they must be \term{input} \term{streams},
  170. ending in ``\f{-output*}'' if they must be \term{output} \term{streams},
  171. or ending in ``\f{-io*}'' if they must be \term{bidirectional} \term{streams}.
  172. User programs may \term{assign} or \term{bind} any \term{standardized} \term{stream variable}
  173. except \varref{*terminal-io*}.
  174. \endsubSection%{Stream Variables}
  175. \beginsubSection{Stream Arguments to Standardized Functions}
  176. \DefineSection{StreamArgsToStandardizedFns}
  177. %This list conjured by KMP, Barrett, and Loosemore. -kmp 14-Feb-92
  178. The \term{operators} in \thenextfigure\ accept \term{stream} \term{arguments} that
  179. might be either \term{open} or \term{closed} \term{streams}.
  180. \DefineFigure{OpenOrClosedStreamOps}
  181. \displaythree{Operators that accept either Open or Closed Streams}{
  182. broadcast-stream-streams&file-author&pathnamep\cr
  183. close&file-namestring&probe-file\cr
  184. compile-file&file-write-date&rename-file\cr
  185. compile-file-pathname&host-namestring&streamp\cr
  186. concatenated-stream-streams&load&synonym-stream-symbol\cr
  187. delete-file&logical-pathname&translate-logical-pathname\cr
  188. directory&merge-pathnames&translate-pathname\cr
  189. directory-namestring&namestring&truename\cr
  190. dribble&open&two-way-stream-input-stream\cr
  191. echo-stream-input-stream&open-stream-p&two-way-stream-output-stream\cr
  192. echo-stream-ouput-stream&parse-namestring&wild-pathname-p\cr
  193. ed&pathname&with-open-file\cr
  194. enough-namestring&pathname-match-p&\cr
  195. }
  196. %This list conjured by KMP, Barrett, and Loosemore. -kmp 14-Feb-92
  197. The \term{operators} in \thenextfigure\ accept \term{stream} \term{arguments} that
  198. must be \term{open} \term{streams}.
  199. \displaythree{Operators that accept Open Streams only}{
  200. clear-input&output-stream-p&read-char-no-hang\cr
  201. clear-output&peek-char&read-delimited-list\cr
  202. file-length&pprint&read-line\cr
  203. file-position&pprint-fill&read-preserving-whitespace\cr
  204. file-string-length&pprint-indent&stream-element-type\cr
  205. finish-output&pprint-linear&stream-external-format\cr
  206. force-output&pprint-logical-block&terpri\cr
  207. format&pprint-newline&unread-char\cr
  208. fresh-line&pprint-tab&with-open-stream\cr
  209. get-output-stream-string&pprint-tabular&write\cr
  210. input-stream-p&prin1&write-byte\cr
  211. interactive-stream-p&princ&write-char\cr
  212. listen&print&write-line\cr
  213. make-broadcast-stream&print-object&write-string\cr
  214. make-concatenated-stream&print-unreadable-object&y-or-n-p\cr
  215. make-echo-stream&read&yes-or-no-p\cr
  216. make-synonym-stream&read-byte&\cr
  217. make-two-way-stream&read-char&\cr
  218. }
  219. \endsubSection%{Stream Arguments to Standardized Functions}
  220. \beginsubSection{Restrictions on Composite Streams}
  221. %KMP, Loosemore, and Barrett thought this stuff should be made explicit. -kmp 14-Feb-92
  222. The consequences are undefined if any \term{component} of a \term{composite stream}
  223. is \term{closed} before the \term{composite stream} is \term{closed}.
  224. The consequences are undefined if the \term{synonym stream symbol} is not \term{bound}
  225. to an \term{open} \term{stream} from the time of the \term{synonym stream}'s creation
  226. until the time it is \term{closed}.
  227. \endsubSection%{Restrictions on Composite Streams}