"benchmarks" did not exist on "edac2d808e4d48a1a71747334d78e6fbb38d94bc"
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.81 2005/07/01 14:54:41 rtoy Exp $")
;;; **********************************************************************
;;;
;;; Stream functions for Spice Lisp.
;;; Written by Skef Wholey and Rob MacLachlan.
;;; Gray streams support by Douglas Crosher, 1998.
;;; Simple-streams support by Paul Foley, 2003.
;;; 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")
;;;; 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-any (stream &rest ignore)
(declare (ignore ignore))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies input-stream-p)
:format-control "~S is not an input stream."
:format-arguments (list stream)))
(defun ill-out-any (stream &rest ignore)
(declare (ignore ignore))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies output-stream-p)
:format-control "~S is not an output stream."
:format-arguments (list stream)))
(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-n-bin (stream &rest ignore)
(declare (ignore ignore))
(error 'simple-type-error
:datum stream
:expected-type '(satisfies input-stream-p)
:format-control "~S is not a binary input stream ~
or does not support multi-byte read operations."
: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)))
(defun closed-flame (stream &rest ignore)
(declare (ignore ignore))
(error "~S is closed." stream))
(defun do-nothing (&rest ignore)
(declare (ignore ignore)))
(defun no-gray-streams (stream)
(error 'simple-type-error
:datum stream
:expected-type 'stream
:format-control "~S is an unsupported Gray stream."
:format-arguments (list stream)))
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(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 with.
;;; :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?
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;;
;;; 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."
(declare (type stream stream))
;; Note: Gray streams redefines this function; any changes made here need
;; to be duplicated in .../pcl/gray-streams.lisp
(stream-dispatch stream
;; simple-stream
(stream::%input-stream-p stream)
;; lisp-stream
(let ((stream (if (synonym-stream-p stream)
(symbol-value (synonym-stream-symbol stream))
stream)))
(and (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))
(not (eq (lisp-stream-n-bin stream) #'ill-n-bin)))))))
(defun output-stream-p (stream)
"Returns non-nil if the given Stream can perform output operations."
(declare (type stream stream))
;; Note: Gray streams redefines this function; any changes made here need
;; to be duplicated in .../pcl/gray-streams.lisp
(stream-dispatch stream
;; simple-stream
(stream::%output-stream-p stream)
;; lisp-stream
(let ((stream (if (synonym-stream-p stream)
(symbol-value (synonym-stream-symbol stream))
stream)))
(and (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))
;; Note: Gray streams redefines this function; any changes made here need
;; to be duplicated in .../pcl/gray-streams.lisp
(stream-dispatch stream
;; simple-stream
(stream::%open-stream-p stream)
;; lisp-stream
(not (eq (lisp-stream-in stream) #'closed-flame))))
(defun stream-element-type (stream)
"Returns a type specifier for the kind of object returned by the Stream."
(declare (type stream stream))
;; Note: Gray streams redefines this function; any changes made here need
;; to be duplicated in .../pcl/gray-streams.lisp
(stream-dispatch stream
;; simple-stream
'(unsigned-byte 8)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :element-type)))
(defun interactive-stream-p (stream)
"Return true if Stream does I/O on a terminal or other interactive device."
(declare (type stream stream))
(stream-dispatch stream
;; simple-stream
(stream::%interactive-stream-p stream)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :interactive-p)))
(defun (setf interactive-stream-p) (flag stream)
(declare (type stream stream))
(stream-dispatch stream
;; simple-stream
(if flag
(stream::%interactive-stream-y stream)
(stream::%interactive-stream-n stream))
;; lisp-stream
(error 'simple-type-error
:datum stream
:expected-type 'stream:simple-stream
:format-control "Can't set interactive flag on ~S."
:format-arguments (list stream))))
(defun stream-external-format (stream)
"Returns the external format used by the given Stream."
(declare (type stream stream))
(stream-dispatch stream
;; simple-stream
(stream::%stream-external-format stream)
;; lisp-stream
:default
;; fundamental-stream
:default))
(defun close (stream &key abort)
"Closes the given Stream. No more I/O may be performed, but inquiries
may still be made. If :Abort is non-nil, an attempt is made to clean
up the side effects of having created the stream."
(declare (type stream stream))
;; Note: Gray streams redefines this function; any changes made here need
;; to be duplicated in .../pcl/gray-streams.lisp
(stream-dispatch stream
;; simple-stream
(stream:device-close stream abort)
;; lisp-stream
(when (open-stream-p stream)
(funcall (lisp-stream-misc stream) stream :close abort)))
(declare (type lisp-stream stream))
(setf (lisp-stream-in stream) #'closed-flame)
(setf (lisp-stream-bin stream) #'closed-flame)
(setf (lisp-stream-n-bin stream) #'closed-flame)
(setf (lisp-stream-in stream) #'closed-flame)
(setf (lisp-stream-out stream) #'closed-flame)
(setf (lisp-stream-bout stream) #'closed-flame)
(setf (lisp-stream-sout stream) #'closed-flame)
(setf (lisp-stream-misc stream) #'closed-flame))
;;;; File position and file length.
;;; File-Position -- Public
;;;
;;; Call the misc method with the :file-position operation.
;;;
(defun file-position (stream &optional position)
"With one argument returns the current position within the file
File-Stream is open to. If the second argument is supplied, then
this becomes the new file position. The second argument may also
be :start or :end for the start and end of the file, respectively."
(declare (type stream stream)
(type (or (integer 0 *) (member nil :start :end)) position))
(stream-dispatch stream
;; simple-stream
(stream::%file-position stream position)
;; lisp-stream
(cond
(position
(setf (lisp-stream-in-index stream) in-buffer-length)
(funcall (lisp-stream-misc stream) stream :file-position position))
(t
(let ((res (funcall (lisp-stream-misc stream) stream
:file-position nil)))
(when res
(- res (- in-buffer-length (lisp-stream-in-index stream)))))))))
;;; File-Length -- Public
;;;
;;; Like File-Position, only use :file-length.
;;;
(defun file-length (stream)
"This function returns the length of the file that File-Stream is open to."
(stream-dispatch stream
;; simple-stream
(stream::%file-length stream)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :file-length)))
;;; Input functions:
(defun read-line (&optional (stream *standard-input*) (eof-errorp t) eof-value
recursive-p)
"Returns a line of text read from the Stream as a string, discarding the
newline character."
(let ((stream (in-synonym-of stream)))
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
(stream-dispatch stream
;; simple-stream
(stream::%read-line stream eof-errorp eof-value recursive-p)
;; lisp-stream
(prepare-for-fast-read-char stream
(let ((res (make-string 80))
(len 80)
(index 0))
(loop
(let ((ch (fast-read-char nil nil)))
(cond (ch
(when (char= ch #\newline)
(done-with-fast-read-char)
(return (values (shrink-vector res index) nil)))
(when (= index len)
(setq len (* len 2))
(let ((new (make-string len)))
(replace new res)
(setq res new)))
(setf (schar res index) ch)
(incf index))
((zerop index)
(done-with-fast-read-char)
(return (values (eof-or-lose stream eof-errorp eof-value)
t)))
;; since fast-read-char hit already the eof char, we
;; shouldn't do another read-char
(t
(done-with-fast-read-char)
(return (values (shrink-vector res index) t))))))))
;; fundamental-stream
(multiple-value-bind (string eof)
(stream-read-line stream)
(if (and eof (zerop (length string)))
(values (eof-or-lose stream eof-errorp eof-value) t)
(values string eof))))))
;;; We proclaim them inline here, then proclaim them notinline at EOF,
;;; so, except in this file, they are not inline by default, but they can be.
;;;
(declaim (inline read-char unread-char read-byte listen))
(defun read-char (&optional (stream *standard-input*) (eof-errorp t) eof-value
recursive-p)
"Inputs a character from Stream and returns it."
(let ((stream (in-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%read-char stream eof-errorp eof-value recursive-p t)
;; lisp-stream
(prepare-for-fast-read-char stream
(prog1
(fast-read-char eof-errorp eof-value)
(done-with-fast-read-char)))
;; fundamental-stream
(let ((char (stream-read-char stream)))
(if (eq char :eof)
(eof-or-lose stream eof-errorp eof-value)
char)))))
(defun unread-char (character &optional (stream *standard-input*))
"Puts the Character back on the front of the input Stream."
(let ((stream (in-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%unread-char stream character)
;; lisp-stream
(let ((index (1- (lisp-stream-in-index stream)))
(buffer (lisp-stream-in-buffer stream)))
(declare (fixnum index))
(when (minusp index) (error "Nothing to unread."))
(cond (buffer
(setf (aref buffer index) (char-code character))
(setf (lisp-stream-in-index stream) index))
(t
(funcall (lisp-stream-misc stream) stream
:unread character))))
;; fundamental-stream
(stream-unread-char stream character)))
;;; In the interest of ``once and only once'' this macro contains the
;;; framework necessary to implement a peek-char function, which has
;;; two special-cases (one for gray streams and one for echo streams)
;;; in addition to the normal case.
;;;
;;; All arguments are forms which will be used for a specific purpose
;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
;;; EOF-VALUE - the eof-value argument to peek-char
;;; CHAR-VAR - the variable which will be used to store the current character
;;; READ-FORM - the form which will be used to read a character
;;; READ-EOF - the result returned from READ-FORM when eof is reached.
;;; UNREAD-FORM - ditto for unread-char
;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
;;; (this will default to CHAR-VAR)
(defmacro generalized-peeking-mechanism (peek-type eof-value char-var
read-form read-eof unread-form
&optional (skipped-char-form nil)
(eof-detected-form nil))
`(let ((,char-var ,read-form))
(cond ((eql ,char-var ,read-eof)
,(if eof-detected-form
eof-detected-form
((characterp ,peek-type)
(do ((,char-var ,char-var ,read-form))
((or (eql ,char-var ,read-eof)
(cond ((eql ,char-var ,read-eof)
,(if eof-detected-form
eof-detected-form
(t ,unread-form
,char-var)))
,skipped-char-form))
((eql ,peek-type t)
(do ((,char-var ,char-var ,read-form))
((or (eql ,char-var ,read-eof)
(not (eql (get-cat-entry ,char-var *readtable*) whitespace)))
(cond ((eql ,char-var ,read-eof)
,(if eof-detected-form
eof-detected-form
(t ,unread-form
,char-var)))
,skipped-char-form))
((null ,peek-type)
,unread-form
(when (eql char-var read-eof)
,char-var)
(t
(error "Impossible case reached in PEEK-CHAR")))))
(defun peek-char (&optional (peek-type nil) (stream *standard-input*)
(eof-errorp t) eof-value recursive-p)
"Peeks at the next character in the input Stream. See manual for details."
;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
;; the compiler doesn't seem to be smart enough to go from there to
;; imposing a type check. Figure out why (because PEEK-TYPE is an
;; &OPTIONAL argument?) and fix it, and then this explicit type
;; check can go away.
(unless (typep peek-type '(or character boolean))
(error 'simple-type-error
:datum peek-type
:expected-type '(or character boolean)
:format-control "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>"
:format-arguments (list peek-type '(or character boolean))))
(let ((stream (in-synonym-of stream)))
(if (typep stream 'echo-stream)
(echo-misc stream :peek-char peek-type (list eof-errorp eof-value))
(stream-dispatch stream
;; simple-stream
(stream::%peek-char stream peek-type eof-errorp eof-value
recursive-p)
;; lisp-stream
(generalized-peeking-mechanism
peek-type eof-value char
(read-char stream eof-errorp :eof)
:eof
(unread-char char stream)
nil
(eof-or-lose stream (or eof-errorp recursive-p) eof-value))
;; fundamental-stream
(generalized-peeking-mechanism
peek-type :eof char
(if (null peek-type)
(stream-peek-char stream)
(stream-read-char stream))
(if (null peek-type)
()
(stream-unread-char stream char))
()
(eof-or-lose stream eof-errorp eof-value))))))
(defun listen (&optional (stream *standard-input*) (width 1))
"Returns T if a character is available on the given Stream."
(declare (type streamlike stream))
(let ((stream (in-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%listen stream width)
;; lisp-stream
(or (/= (the fixnum (lisp-stream-in-index stream)) in-buffer-length)
;; Test for t explicitly since misc methods return :eof sometimes.
(eq (funcall (lisp-stream-misc stream) stream :listen) t))
;; fundamental-stream
(stream-listen stream))))
(defun read-char-no-hang (&optional (stream *standard-input*)
(eof-errorp t) eof-value recursive-p)
"Returns the next character from the Stream if one is available, or nil."
(let ((stream (in-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%read-char stream eof-errorp eof-value recursive-p nil)
;; lisp-stream
(if (funcall (lisp-stream-misc stream) stream :listen)
;; On t or :eof get READ-CHAR to do the work.
(read-char stream eof-errorp eof-value)
nil)
;; fundamental-stream
(let ((char (stream-read-char-no-hang stream)))
(if (eq char :eof)
(eof-or-lose stream eof-errorp eof-value)
char)))))
(defun clear-input (&optional (stream *standard-input*) buffer-only)
(declare (type streamlike stream))
(let ((stream (in-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%clear-input stream buffer-only)
;; lisp-stream
(progn
(setf (lisp-stream-in-index stream) in-buffer-length)
(funcall (lisp-stream-misc stream) stream :clear-input))
;; fundamental-stream
(stream-clear-input stream)))
(defun read-byte (stream &optional (eof-errorp t) eof-value)
"Returns the next byte of the Stream."
(declare (type stream stream))
(let ((stream (in-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%read-byte stream eof-errorp eof-value)
;; lisp-stream
(prepare-for-fast-read-byte stream
(prog1
(fast-read-byte eof-errorp eof-value t)
(done-with-fast-read-byte)))
;; fundamental-stream
(let ((char (stream-read-byte stream)))
(if (eq char :eof)
(eof-or-lose stream eof-errorp eof-value)
char)))))
(defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
"Reads Numbytes bytes into the Buffer starting at Start, returning the number
of bytes read.
-- If EOF-ERROR-P is true, an END-OF-FILE condition is signalled if
end-of-file is encountered before Count bytes have been read.
-- If EOF-ERROR-P is false, READ-N-BYTES reads as much data as is currently
available (up to count bytes). On pipes or similar devices, this
function returns as soon as any data is available, even if the amount
read is less than Count and eof has not been hit."
(declare (type lisp-stream stream)
(type index numbytes start)
(type (or (simple-array * (*)) system-area-pointer) buffer))
(let* ((stream (in-synonym-of stream lisp-stream))
(in-buffer (lisp-stream-in-buffer stream))
(index (lisp-stream-in-index stream))
(num-buffered (- in-buffer-length index)))
(declare (fixnum index num-buffered))
(cond
((not in-buffer)
(funcall (lisp-stream-n-bin stream) stream
buffer start numbytes eof-errorp))
((<= numbytes num-buffered)
(%primitive byte-blt in-buffer index buffer start (+ start numbytes))
numbytes)
(t
(let ((end (+ start num-buffered)))
(%primitive byte-blt in-buffer index buffer start end)
(setf (lisp-stream-in-index stream) in-buffer-length)
(+ (funcall (lisp-stream-n-bin stream) stream buffer end
(- numbytes num-buffered)
eof-errorp)
;;; Amount of space we leave at the start of the in-buffer for unreading. 4
;;; instead of 1 to allow word-aligned copies.
;;;
(defconstant in-buffer-extra 4)
;;; FAST-READ-CHAR-REFILL -- Interface
;;;
;;; This function is called by the fast-read-char expansion to refill the
;;; in-buffer for text streams. There is definitely an in-buffer, and hence
;;; myst be an n-bin method.
;;;
(defun fast-read-char-refill (stream eof-errorp eof-value)
(let* ((ibuf (lisp-stream-in-buffer stream))
(count (funcall (lisp-stream-n-bin stream) stream
ibuf in-buffer-extra
(- in-buffer-length in-buffer-extra)
nil))
(start (- in-buffer-length count)))
(declare (type index start count))
(cond ((zerop count)
(setf (lisp-stream-in-index stream) in-buffer-length)
(funcall (lisp-stream-in stream) stream eof-errorp eof-value))
(t
(when (/= start in-buffer-extra)
(bit-bash-copy ibuf (+ (* in-buffer-extra vm:byte-bits)
(* vm:vector-data-offset vm:word-bits))
ibuf (+ (the index (* start vm:byte-bits))
(* vm:vector-data-offset vm:word-bits))
(* count vm:byte-bits)))
(code-char (aref ibuf start))))))
;;; FAST-READ-BYTE-REFILL -- Interface
;;;
;;; Similar to FAST-READ-CHAR-REFILL, but we don't have to leave room for
;;; unreading.
;;;
(defun fast-read-byte-refill (stream eof-errorp eof-value)
(let* ((ibuf (lisp-stream-in-buffer stream))
(count (funcall (lisp-stream-n-bin stream) stream
ibuf 0 in-buffer-length
nil))
(start (- in-buffer-length count)))
(declare (type index start count))
(cond ((zerop count)
(setf (lisp-stream-in-index stream) in-buffer-length)
(funcall (lisp-stream-bin stream) stream eof-errorp eof-value))
(t
(unless (zerop start)
(bit-bash-copy ibuf (* vm:vector-data-offset vm:word-bits)
ibuf (+ (the index (* start vm:byte-bits))
(* vm:vector-data-offset vm:word-bits))
(* count vm:byte-bits)))
;;; Output functions:
(defun write-char (character &optional (stream *standard-output*))
"Outputs the Character to the Stream."
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%write-char stream character)
;; lisp-stream
(funcall (lisp-stream-out stream) stream character)
;; fundamental-stream
(stream-write-char stream character)))
character)
(defun terpri (&optional (stream *standard-output*))
"Outputs a new line to the Stream."
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%write-char stream #\Newline)
;; lisp-stream
(funcall (lisp-stream-out stream) stream #\Newline)
;; fundamental-stream
(stream-terpri stream)))
nil)
(defun fresh-line (&optional (stream *standard-output*))
"Outputs a new line to the Stream if it is not positioned at the beginning of
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%fresh-line stream)
;; lisp-stream
(when (/= (or (charpos stream) 1) 0)
(funcall (lisp-stream-out stream) stream #\newline)
t)
;; fundamental-stream
(stream-fresh-line stream))))
(defun write-string (string &optional (stream *standard-output*)
(write-string* string stream start (or end (length (the vector string)))))
(defun write-string* (string &optional (stream *standard-output*)
(start 0) (end (length (the vector string))))
(declare (type streamlike stream) (fixnum start end))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(if (array-header-p string)
(with-array-data ((data string) (offset-start start)
(offset-end end))
(stream::%write-string stream data offset-start offset-end))
(stream::%write-string stream string start end))
(if (array-header-p string)
(with-array-data ((data string) (offset-start start)
(offset-end end))
(funcall (lisp-stream-sout stream)
stream data offset-start offset-end))
(funcall (lisp-stream-sout stream) stream string start end))
;; fundamental-stream
(stream-write-string stream string start end)))
string)
(defun write-line (string &optional (stream *standard-output*)
&key (start 0) (end (length string)))
"Outputs the String to the given Stream, followed by a newline character."
(write-line* string stream start (or end (length string))))
(defun write-line* (string &optional (stream *standard-output*)
(start 0) (end (length string)))
(declare (type streamlike stream) (fixnum start end))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(progn
(if (array-header-p string)
(with-array-data ((data string) (offset-start start)
(offset-end end))
(stream::%write-string stream data offset-start offset-end))
(stream::%write-string stream string start end))
(stream::%write-char stream #\Newline))
;; lisp-stream
(progn
(if (array-header-p string)
(with-array-data ((data string) (offset-start start)
(offset-end end))
(funcall (lisp-stream-sout stream) stream data offset-start
offset-end))
(funcall (lisp-stream-sout stream) stream string start end))
(funcall (lisp-stream-out stream) stream #\newline))
;; fundamental-stream
(progn
(stream-write-string stream string start end)
(stream-write-char stream #\Newline)))
(defun charpos (&optional (stream *standard-output*))
"Returns the number of characters on the current line of output of the given
Stream, or Nil if that information is not availible."
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%charpos stream)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :charpos)
;; fundamental-stream
(stream-line-column stream))))
(defun line-length (&optional (stream *standard-output*))
"Returns the number of characters that will fit on a line of output on the
given Stream, or Nil if that information is not available."
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%line-length stream)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :line-length)
;; fundamental-stream
(stream-line-length stream))))
"Attempts to ensure that all output sent to the Stream has reached its
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%finish-output stream)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :finish-output)
;; fundamental-stream
(stream-finish-output stream)))
nil)
(defun force-output (&optional (stream *standard-output*))
"Attempts to force any buffered output to be sent."
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%force-output stream)
;; lisp-stream-misc
(funcall (lisp-stream-misc stream) stream :force-output)
;; fundamental-stream
(stream-force-output stream)))
nil)
(defun clear-output (&optional (stream *standard-output*))
"Clears the given output Stream."
(declare (type streamlike stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%clear-output stream)
;; lisp-stream
(funcall (lisp-stream-misc stream) stream :clear-output)
;; fundamental-stream
(stream-force-output stream)))
nil)
(defun write-byte (integer stream)
"Outputs the Integer to the binary Stream."
(declare (type stream stream))
(let ((stream (out-synonym-of stream)))
(stream-dispatch stream
;; simple-stream
(stream::%write-byte stream integer)
;; lisp-stream
(funcall (lisp-stream-bout stream) stream integer)
;; fundamental-stream
(stream-write-byte stream integer)))
;;; Stream-misc-dispatch
;;;
;;; Called from lisp-stream routines that encapsulate CLOS streams to
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
;;; handle the misc routines and dispatch to the appropriate Gray
;;; stream functions.
;;;
(defun stream-misc-dispatch (stream operation &optional arg1 arg2)
(declare (type fundamental-stream stream)
(ignore arg2))
(case operation
(:listen
;; Return true is input available, :eof for eof-of-file, otherwise Nil.
(let ((char (stream-read-char-no-hang stream)))
(when (characterp char)
(stream-unread-char stream char))
char))
(:unread
(stream-unread-char stream arg1))
(:close
(close stream))
(:clear-input
(stream-clear-input stream))
(:force-output
(stream-force-output stream))
(:finish-output
(stream-finish-output stream))
(:element-type
(stream-element-type stream))
(:interactive-p
(interactive-stream-p stream))
(:line-length
(stream-line-length stream))
(:charpos
(stream-line-column stream))
(:file-length
(file-length stream))
(:file-position
(file-position stream arg1))))
(out #'broadcast-out)
(bout #'broadcast-bout)
(sout #'broadcast-sout)
(misc #'broadcast-misc))
(:print-function %print-broadcast-stream)
(:constructor %make-broadcast-stream (&rest streams)))
(streams () :type list :read-only t))
(defun make-broadcast-stream (&rest streams)
"Returns an output stream which sends its output to all of the given
streams."
(dolist (s streams)
(unless (output-stream-p s)
(ill-out-any s)))
(apply #'%make-broadcast-stream streams))
(defun %print-broadcast-stream (s stream d)
(declare (ignore s d))
(write-string "#<Broadcast Stream>" stream))
(macrolet ((out-fun (fun method stream-method &rest args)
`(defun ,fun (stream ,@args)
(dolist (stream (broadcast-stream-streams stream))
(stream-dispatch stream
;; simple-stream
(,stream-method stream ,@args) ; use gray-compat for now
;; lisp-stream
(funcall (,method stream) stream ,@args)
;; fundamental-stream
(,stream-method stream ,@args))))))
(out-fun broadcast-out lisp-stream-out stream-write-char char)
(out-fun broadcast-bout lisp-stream-bout stream-write-byte byte)
(out-fun broadcast-sout lisp-stream-sout stream-write-string
string start end))
(defun broadcast-misc (stream operation &optional arg1 arg2)
(let ((streams (broadcast-stream-streams stream)))
(case operation
(:charpos
(let ((charpos (charpos stream)))
(if charpos (return charpos)))))
(:line-length
(let ((min nil))
(dolist (stream streams min)
(let ((res (line-length stream)))
;; CLHS: The functions file-length, file-position, file-string-length, and
;; stream-external-format return the value from the last component
;; stream; if there are no component streams, file-length and
;; file-position return 0, file-string-length returns 1, and
;; stream-external-format returns :default.
(:file-length
(if (null streams) 0