Newer
Older
;;; -*- Package: Lisp -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
(ext:file-comment
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/stream.lisp,v 1.48 2001/09/20 14:23:05 pw Exp $")
;;; **********************************************************************
;;;
;;; Stream functions for Spice Lisp.
;;; Written by Skef Wholey and Rob MacLachlan.
;;; Gray streams support by Douglas Crosher, 1998.
;;; This file contains the OS-independent stream functions.
(in-package "LISP")
(export '(broadcast-stream make-broadcast-stream broadcast-stream-streams
synonym-stream make-synonym-stream synonym-stream-symbol
concatenated-stream make-concatenated-stream
concatenated-stream-streams
two-way-stream make-two-way-stream two-way-stream-input-stream
two-way-stream-output-stream
echo-stream make-echo-stream echo-stream-input-stream
echo-stream-output-stream
make-string-input-stream make-string-output-stream
output-stream-p open-stream-p interactive-stream-p
open-stream-p close read-line read-char
unread-char peek-char listen read-char-no-hang clear-input read-byte
write-char write-string write-line terpri fresh-line
finish-output force-output clear-output write-byte
stream streamp string-stream
*standard-input* *standard-output*
*error-output* *query-io* *debug-io* *terminal-io* *trace-output*))
(in-package "EXT")
(export '(get-stream-command
stream-command stream-command-p stream-command-name
stream-command-args make-stream-command make-case-frob-stream))
(in-package "LISP")
(deftype string-stream ()
'(or string-input-stream string-output-stream
fill-pointer-output-stream))
;;;; Standard streams:
;;;
;;; The initialization of these streams is performed by Stream-Init,
;;; which lives in the file of machine-specific stream functions.
;;;
(defvar *terminal-io* () "Terminal I/O stream.")
(defvar *standard-input* () "Default input stream.")
(defvar *standard-output* () "Default output stream.")
(defvar *error-output* () "Error output stream.")
(defvar *query-io* () "Query I/O stream.")
(defvar *trace-output* () "Trace output stream.")
(defvar *debug-io* () "Interactive debugging stream.")
(defun ill-in (stream &rest ignore)
(declare (ignore ignore))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies input-stream-p)
:format-control "~S is not a character input stream."
:format-arguments (list stream)))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies output-stream-p)
:format-control "~S is not a character output stream."
:format-arguments (list stream)))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies input-stream-p)
:format-control "~S is not a binary input stream."
:format-arguments (list stream)))
(defun ill-bout (stream &rest ignore)
(declare (ignore ignore))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies output-stream-p)
:format-control "~S is not a binary output stream."
:format-arguments (list stream)))
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(defun closed-flame (stream &rest ignore)
(declare (ignore ignore))
(error "~S is closed." stream))
(defun do-nothing (&rest ignore)
(declare (ignore ignore)))
(defun %print-stream (structure stream d)
(declare (ignore d structure))
(write-string "#<Bare Stream>" stream))
;;; HOW THE STREAM STRUCTURE IS USED:
;;;
;;; Many of the slots of the stream structure contain functions
;;; which are called to perform some operation on the stream. Closed
;;; streams have #'Closed-Flame in all of their function slots. If
;;; one side of an I/O or echo stream is closed, the whole stream is
;;; considered closed. The functions in the operation slots take
;;; arguments as follows:
;;;
;;; In: Stream, Eof-Errorp, Eof-Value
;;; Bin: Stream, Eof-Errorp, Eof-Value
;;; N-Bin: Stream, Buffer, Start, Numbytes, Eof-Errorp
;;; Out: Stream, Character
;;; Bout: Stream, Integer
;;; Sout: Stream, String, Start, End
;;; Misc: Stream, Operation, &Optional Arg1, Arg2
;;;
;;; In order to save space, some of the less common stream operations
;;; are handled by just one function, the Misc method. This function
;;; is passed a keyword which indicates the operation to perform.
;;; The following keywords are used:
;;; :listen - Return the following values:
;;; t if any input waiting.
;;; :eof if at eof.
;;; nil if no input is available and not at eof.
;;; :unread - Unread the character Arg.
;;; :close - Do any stream specific stuff to close the stream.
;;; The methods are set to closed-flame by the close
;;; function, so that need not be done by this
;;; function.
;;; :clear-input - Clear any unread input
;;; :finish-output,
;;; :force-output - Cause output to happen
;;; :clear-output - Clear any undone output
;;; :element-type - Return the type of element the stream deals wit<h.
;;; :line-length - Return the length of a line of output.
;;; :charpos - Return current output position on the line.
;;; :file-length - Return the file length of a file stream.
;;; :file-position - Return or change the current position of a file stream.
;;; :file-name - Return the name of an associated file.
;;; :interactive-p - Is this an interactive device?
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;;
;;; In order to do almost anything useful, it is necessary to
;;; define a new type of structure that includes stream, so that the
;;; stream can have some state information.
;;;
;;; THE STREAM IN-BUFFER:
;;;
;;; The In-Buffer in the stream holds characters or bytes that
;;; are ready to be read by some input function. If there is any
;;; stuff in the In-Buffer, then the reading function can use it
;;; without calling any stream method. Any stream may put stuff in
;;; the In-Buffer, and may also assume that any input in the In-Buffer
;;; has been consumed before any in-method is called. If a text
;;; stream has in In-Buffer, then the first character should not be
;;; used to buffer normal input so that it is free for unreading into.
;;;
;;; The In-Buffer slot is a vector In-Buffer-Length long. The
;;; In-Index is the index in the In-Buffer of the first available
;;; object. The available objects are thus between In-Index and the
;;; length of the In-Buffer.
;;;
;;; When this buffer is only accessed by the normal stream
;;; functions, the number of function calls is halved, thus
;;; potentially doubling the speed of simple operations. If the
;;; Fast-Read-Char and Fast-Read-Byte macros are used, nearly all
;;; function call overhead is removed, vastly speeding up these
;;; important operations.
;;;
;;; If a stream does not have an In-Buffer, then the In-Buffer slot
;;; must be nil, and the In-Index must be In-Buffer-Length. These are
;;; the default values for the slots.
;;; Stream manipulation functions.
(defun input-stream-p (stream)
"Returns non-nil if the given Stream can perform input operations."
(and (lisp-stream-p stream)
(not (eq (lisp-stream-in stream) #'closed-flame))
(or (not (eq (lisp-stream-in stream) #'ill-in))
(not (eq (lisp-stream-bin stream) #'ill-bin)))))
(defun output-stream-p (stream)
"Returns non-nil if the given Stream can perform output operations."
(and (lisp-stream-p stream)
(not (eq (lisp-stream-in stream) #'closed-flame))
(or (not (eq (lisp-stream-out stream) #'ill-out))
(not (eq (lisp-stream-bout stream) #'ill-bout)))))
(defun open-stream-p (stream)
"Return true if Stream is not closed."
(declare (type stream stream))
Loading
Loading full blame...