1 ;;;; Gray streams implementation for SBCL, based on the Gray streams
2 ;;;; implementation for CMU CL, based on the stream-definition-by-user
3 ;;;; proposal by David N. Gray.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 (in-package "SB-GRAY")
14 ;;; BUG-OR-ERROR: because we have extensible streams, wherewith the
15 ;;; user is responsible for some of the protocol implementation, it's
16 ;;; not necessarily a bug in SBCL itself if we fall through to one of
17 ;;; these default methods.
19 ;;; FIXME: there's a lot of similarity in these Gray stream
20 ;;; implementation generic functions. All of them could (maybe
21 ;;; should?) have two default methods: one on STREAM calling
22 ;;; BUG-OR-ERROR, and one on T signalling a TYPE-ERROR.
23 (defmacro bug-or-error (stream fun)
25 "~@<The stream ~S has no suitable method for ~S, ~
26 and so has fallen through to this method. If you think that this is ~
27 a bug, please report it to the applicable authority (bugs in SBCL itself ~
28 should go to the mailing lists referenced from ~
29 <http://www.sbcl.org/>).~@:>"
32 (fmakunbound 'stream-element-type)
34 (defgeneric stream-element-type (stream)
37 "Return a type specifier for the kind of object returned by the
38 STREAM. The class FUNDAMENTAL-CHARACTER-STREAM provides a default method
39 which returns CHARACTER."))
41 (defmethod stream-element-type ((stream ansi-stream))
42 (ansi-stream-element-type stream))
44 (defmethod stream-element-type ((stream fundamental-character-stream))
47 (defmethod stream-element-type ((stream stream))
48 (bug-or-error stream 'stream-element-type))
50 (defmethod stream-element-type ((non-stream t))
51 (error 'type-error :datum non-stream :expected-type 'stream))
53 (defgeneric pcl-open-stream-p (stream)
56 "Return true if STREAM is not closed. A default method is provided
57 by class FUNDAMENTAL-STREAM which returns true if CLOSE has not been
58 called on the stream."))
60 (defmethod pcl-open-stream-p ((stream ansi-stream))
61 (ansi-stream-open-stream-p stream))
63 (defmethod pcl-open-stream-p ((stream fundamental-stream))
64 (stream-open-p stream))
66 (defmethod pcl-open-stream-p ((stream stream))
67 (bug-or-error stream 'open-stream-p))
69 (defmethod pcl-open-stream-p ((non-stream t))
70 (error 'type-error :datum non-stream :expected-type 'stream))
72 ;;; bootstrapping hack
73 (pcl-open-stream-p (make-string-output-stream))
74 (setf (fdefinition 'open-stream-p) #'pcl-open-stream-p)
76 (defgeneric pcl-close (stream &key abort)
79 "Close the given STREAM. No more I/O may be performed, but
80 inquiries may still be made. If :ABORT is true, an attempt is made
81 to clean up the side effects of having created the stream."))
83 (defmethod pcl-close ((stream ansi-stream) &key abort)
84 (ansi-stream-close stream abort))
86 (defmethod pcl-close ((stream fundamental-stream) &key abort)
87 (declare (ignore abort))
88 (setf (stream-open-p stream) nil)
91 (setf (fdefinition 'close) #'pcl-close)
94 (fmakunbound 'input-stream-p)
96 (defgeneric input-stream-p (stream)
98 (:documentation "Can STREAM perform input operations?"))
100 (defmethod input-stream-p ((stream ansi-stream))
101 (ansi-stream-input-stream-p stream))
103 (defmethod input-stream-p ((stream fundamental-stream))
106 (defmethod input-stream-p ((stream fundamental-input-stream))
109 (defmethod input-stream-p ((stream stream))
110 (bug-or-error stream 'input-stream-p))
112 (defmethod input-stream-p ((non-stream t))
113 (error 'type-error :datum non-stream :expected-type 'stream)))
116 (fmakunbound 'output-stream-p)
118 (defgeneric output-stream-p (stream)
120 (:documentation "Can STREAM perform output operations?"))
122 (defmethod output-stream-p ((stream ansi-stream))
123 (ansi-stream-output-stream-p stream))
125 (defmethod output-stream-p ((stream fundamental-stream))
128 (defmethod output-stream-p ((stream fundamental-output-stream))
131 (defmethod output-stream-p ((stream stream))
132 (bug-or-error stream 'output-stream-p))
134 (defmethod output-stream-p ((non-stream t))
135 (error 'type-error :datum non-stream :expected-type 'stream)))
137 ;;; character input streams
139 ;;; A character input stream can be created by defining a class that
140 ;;; includes FUNDAMENTAL-CHARACTER-INPUT-STREAM and defining methods
141 ;;; for the generic functions below.
143 (defgeneric stream-read-char (stream)
146 "Read one character from the stream. Return either a
147 character object, or the symbol :EOF if the stream is at end-of-file.
148 Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
149 method for this function."))
151 (defgeneric stream-unread-char (stream character)
154 "Un-do the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
155 Return NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
156 must define a method for this function."))
158 (defgeneric stream-read-char-no-hang (stream)
161 "This is used to implement READ-CHAR-NO-HANG. It returns either a
162 character, or NIL if no input is currently available, or :EOF if
163 end-of-file is reached. The default method provided by
164 FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; this
165 is sufficient for file streams, but interactive streams should define
168 (defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream))
169 (stream-read-char stream))
171 (defgeneric stream-peek-char (stream)
174 "This is used to implement PEEK-CHAR; this corresponds to PEEK-TYPE of NIL.
175 It returns either a character or :EOF. The default method calls
176 STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
178 (defmethod stream-peek-char ((stream fundamental-character-input-stream))
179 (let ((char (stream-read-char stream)))
180 (unless (eq char :eof)
181 (stream-unread-char stream char))
184 (defgeneric stream-listen (stream)
187 "This is used by LISTEN. It returns true or false. The default method uses
188 STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
189 define their own method since it will usually be trivial and will
190 always be more efficient than the default method."))
192 (defmethod stream-listen ((stream fundamental-character-input-stream))
193 (let ((char (stream-read-char-no-hang stream)))
194 (when (characterp char)
195 (stream-unread-char stream char)
198 (defgeneric stream-read-line (stream)
201 "This is used by READ-LINE. A string is returned as the first value. The
202 second value is true if the string was terminated by end-of-file
203 instead of the end of a line. The default method uses repeated
204 calls to STREAM-READ-CHAR."))
206 (defmethod stream-read-line ((stream fundamental-character-input-stream))
207 (let ((res (make-string 80))
211 (let ((ch (stream-read-char stream)))
213 (return (values (shrink-vector res index) t)))
215 (when (char= ch #\newline)
216 (return (values (shrink-vector res index) nil)))
219 (let ((new (make-string len)))
222 (setf (schar res index) ch)
225 (defgeneric stream-clear-input (stream)
228 "This is like CL:CLEAR-INPUT, but for Gray streams, returning NIL.
229 The default method does nothing."))
231 (defmethod stream-clear-input ((stream fundamental-character-input-stream))
233 (defmethod stream-clear-input ((stream stream))
234 (bug-or-error stream 'stream-clear-input))
235 (defmethod stream-clear-input ((non-stream t))
236 (error 'type-error :datum non-stream :expected-type 'stream))
238 (defgeneric stream-read-sequence (stream seq &optional start end)
240 "This is like CL:READ-SEQUENCE, but for Gray streams."))
242 ;;; Destructively modify SEQ by reading elements from STREAM. That
243 ;;; part of SEQ bounded by START and END is destructively modified by
244 ;;; copying successive elements into it from STREAM. If the end of
245 ;;; file for STREAM is reached before copying all elements of the
246 ;;; subsequence, then the extra elements near the end of sequence are
247 ;;; not updated, and the index of the next element is returned.
248 (defun basic-io-type-stream-read-sequence (stream seq start end read-fun)
249 (declare (type sequence seq)
252 (type sequence-end end)
253 (type function read-fun)
255 (let ((end (or end (length seq))))
256 (declare (type index end))
259 (do ((rem (nthcdr start seq) (rest rem))
261 ((or (endp rem) (>= i end)) i)
262 (declare (type list rem)
264 (let ((el (funcall read-fun stream)))
267 (setf (first rem) el))))
269 (with-array-data ((data seq) (offset-start start) (offset-end end))
270 (do ((i offset-start (1+ i)))
271 ((>= i offset-end) end)
272 (declare (type index i))
273 (let ((el (funcall read-fun stream)))
275 (return (+ start (- i offset-start))))
276 (setf (aref data i) el))))))))
278 (defmethod stream-read-sequence ((stream fundamental-character-input-stream)
280 &optional (start 0) (end nil))
281 (basic-io-type-stream-read-sequence stream seq start end
284 (defmethod stream-read-sequence ((stream fundamental-binary-input-stream)
286 &optional (start 0) (end nil))
287 (basic-io-type-stream-read-sequence stream seq start end
291 ;;; character output streams
293 ;;; A character output stream can be created by defining a class that
294 ;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods
295 ;;; for the generic functions below.
297 (defgeneric stream-write-char (stream character)
300 "Write CHARACTER to STREAM and return CHARACTER. Every
301 subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
302 defined for this function."))
304 (defgeneric stream-line-column (stream)
307 "Return the column number where the next character
308 will be written, or NIL if that is not meaningful for this stream.
309 The first column on a line is numbered 0. This function is used in
310 the implementation of PPRINT and the FORMAT ~T directive. For every
311 character output stream class that is defined, a method must be
312 defined for this function, although it is permissible for it to
313 always return NIL."))
315 (defmethod stream-line-column ((stream fundamental-character-output-stream))
318 ;;; STREAM-LINE-LENGTH is a CMU CL extension to Gray streams.
319 ;;; FIXME: Should we support it? Probably not..
320 (defgeneric stream-line-length (stream)
322 (:documentation "Return the stream line length or NIL."))
324 (defmethod stream-line-length ((stream fundamental-character-output-stream))
327 (defgeneric stream-start-line-p (stream)
330 "Is STREAM known to be positioned at the beginning of a line?
331 It is permissible for an implementation to always return
332 NIL. This is used in the implementation of FRESH-LINE. Note that
333 while a value of 0 from STREAM-LINE-COLUMN also indicates the
334 beginning of a line, there are cases where STREAM-START-LINE-P can be
335 meaningfully implemented although STREAM-LINE-COLUMN can't be. For
336 example, for a window using variable-width characters, the column
337 number isn't very meaningful, but the beginning of the line does have
338 a clear meaning. The default method for STREAM-START-LINE-P on class
339 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if
340 that is defined to return NIL, then a method should be provided for
341 either STREAM-START-LINE-P or STREAM-FRESH-LINE."))
343 (defmethod stream-start-line-p ((stream fundamental-character-output-stream))
344 (eql (stream-line-column stream) 0))
346 (defgeneric stream-write-string (stream string &optional start end)
349 "This is used by WRITE-STRING. It writes the string to the stream,
350 optionally delimited by start and end, which default to 0 and NIL.
351 The string argument is returned. The default method provided by
352 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to
353 STREAM-WRITE-CHAR."))
355 (defmethod stream-write-string ((stream fundamental-character-output-stream)
356 string &optional (start 0) end)
357 (declare (string string)
359 (let ((end (or end (length string))))
360 (declare (fixnum end))
361 (do ((pos start (1+ pos)))
363 (declare (type index pos))
364 (stream-write-char stream (aref string pos))))
367 (defgeneric stream-terpri (stream)
370 "Writes an end of line, as for TERPRI. Returns NIL. The default
371 method does (STREAM-WRITE-CHAR stream #\NEWLINE)."))
373 (defmethod stream-terpri ((stream fundamental-character-output-stream))
374 (stream-write-char stream #\Newline))
376 (defgeneric stream-fresh-line (stream)
379 "Outputs a new line to the Stream if it is not positioned at the
380 begining of a line. Returns T if it output a new line, nil
381 otherwise. Used by FRESH-LINE. The default method uses
382 STREAM-START-LINE-P and STREAM-TERPRI."))
384 (defmethod stream-fresh-line ((stream fundamental-character-output-stream))
385 (unless (stream-start-line-p stream)
386 (stream-terpri stream)
389 (defgeneric stream-finish-output (stream)
392 "Attempts to ensure that all output sent to the Stream has reached
393 its destination, and only then returns false. Implements
394 FINISH-OUTPUT. The default method does nothing."))
396 (defmethod stream-finish-output ((stream fundamental-output-stream))
398 (defmethod stream-finish-output ((stream stream))
399 (bug-or-error stream 'stream-finish-output))
400 (defmethod stream-finish-output ((non-stream t))
401 (error 'type-error :datum non-stream :expected-type 'stream))
403 (defgeneric stream-force-output (stream)
406 "Attempts to force any buffered output to be sent. Implements
407 FORCE-OUTPUT. The default method does nothing."))
409 (defmethod stream-force-output ((stream fundamental-output-stream))
411 (defmethod stream-force-output ((stream stream))
412 (bug-or-error stream 'stream-force-output))
413 (defmethod stream-force-output ((non-stream t))
414 (error 'type-error :datum non-stream :expected-type 'stream))
416 (defgeneric stream-clear-output (stream)
419 "This is like CL:CLEAR-OUTPUT, but for Gray streams: clear the given
420 output STREAM. The default method does nothing."))
422 (defmethod stream-clear-output ((stream fundamental-output-stream))
424 (defmethod stream-clear-output ((stream stream))
425 (bug-or-error stream 'stream-clear-output))
426 (defmethod stream-clear-output ((non-stream t))
427 (error 'type-error :datum non-stream :expected-type 'stream))
429 (defgeneric stream-advance-to-column (stream column)
432 "Write enough blank space so that the next character will be
433 written at the specified column. Returns true if the operation is
434 successful, or NIL if it is not supported for this stream. This is
435 intended for use by by PPRINT and FORMAT ~T. The default method uses
436 STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a
437 #\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns NIL."))
439 (defmethod stream-advance-to-column ((stream fundamental-character-output-stream)
441 (let ((current-column (stream-line-column stream)))
443 (let ((fill (- column current-column)))
445 (stream-write-char stream #\Space)))
448 (defgeneric stream-write-sequence (stream seq &optional start end)
450 "This is like CL:WRITE-SEQUENCE, but for Gray streams."))
452 ;;; Write the elements of SEQ bounded by START and END to STREAM.
453 (defun basic-io-type-stream-write-sequence (stream seq start end write-fun)
454 (declare (type sequence seq)
457 (type sequence-end end)
458 (type function write-fun)
460 (let ((end (or end (length seq))))
461 (declare (type index start end))
464 (do ((rem (nthcdr start seq) (rest rem))
466 ((or (endp rem) (>= i end)) seq)
467 (declare (type list rem)
469 (funcall write-fun stream (first rem))))
471 (do ((i start (1+ i)))
473 (declare (type index i))
474 (funcall write-fun stream (aref seq i)))))))
476 (defmethod stream-write-sequence ((stream fundamental-character-output-stream)
478 &optional (start 0) (end nil))
481 (stream-write-string stream seq start end))
483 (basic-io-type-stream-write-sequence stream seq start end
484 #'stream-write-char))))
489 ;;; Binary streams can be created by defining a class that includes
490 ;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or
491 ;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method
492 ;;; for STREAM-ELEMENT-TYPE and for one or both of the following
493 ;;; generic functions.
495 (defgeneric stream-read-byte (stream)
498 "Used by READ-BYTE; returns either an integer, or the symbol :EOF
499 if the stream is at end-of-file."))
501 (defmethod stream-read-byte ((stream stream))
502 (bug-or-error stream 'stream-read-byte))
503 (defmethod stream-read-byte ((non-stream t))
504 (error 'type-error :datum non-stream :expected-type 'stream))
506 (defgeneric stream-write-byte (stream integer)
509 "Implements WRITE-BYTE; writes the integer to the stream and
510 returns the integer as the result."))
512 (defmethod stream-write-byte ((stream stream) integer)
513 (bug-or-error stream 'stream-write-byte))
514 (defmethod stream-write-byte ((non-stream t) integer)
515 (error 'type-error :datum non-stream :expected-type 'stream))
517 ;; Provide a reasonable default for binary Gray streams. We might be
518 ;; able to do better by specializing on the sequence type, but at
519 ;; least the behaviour is reasonable. --tony 2003/05/08.
520 (defmethod stream-write-sequence ((stream fundamental-binary-output-stream)
522 &optional (start 0) (end nil))
523 (basic-io-type-stream-write-sequence stream seq start end
524 #'stream-write-byte))
527 ;;; This is not in the Gray stream proposal, so it is left here
530 ;;; example character output stream encapsulating a lisp-stream
531 (defun make-character-output-stream (lisp-stream)
532 (declare (type lisp-stream lisp-stream))
533 (make-instance 'character-output-stream :lisp-stream lisp-stream))
535 (defmethod open-stream-p ((stream character-output-stream))
536 (open-stream-p (character-output-stream-lisp-stream stream)))
538 (defmethod close ((stream character-output-stream) &key abort)
539 (close (character-output-stream-lisp-stream stream) :abort abort))
541 (defmethod input-stream-p ((stream character-output-stream))
542 (input-stream-p (character-output-stream-lisp-stream stream)))
544 (defmethod output-stream-p ((stream character-output-stream))
545 (output-stream-p (character-output-stream-lisp-stream stream)))
547 (defmethod stream-write-char ((stream character-output-stream) character)
548 (write-char character (character-output-stream-lisp-stream stream)))
550 (defmethod stream-line-column ((stream character-output-stream))
551 (charpos (character-output-stream-lisp-stream stream)))
553 (defmethod stream-line-length ((stream character-output-stream))
554 (line-length (character-output-stream-lisp-stream stream)))
556 (defmethod stream-finish-output ((stream character-output-stream))
557 (finish-output (character-output-stream-lisp-stream stream)))
559 (defmethod stream-force-output ((stream character-output-stream))
560 (force-output (character-output-stream-lisp-stream stream)))
562 (defmethod stream-clear-output ((stream character-output-stream))
563 (clear-output (character-output-stream-lisp-stream stream)))
565 ;;; example character input stream encapsulating a lisp-stream
567 (defun make-character-input-stream (lisp-stream)
568 (declare (type lisp-stream lisp-stream))
569 (make-instance 'character-input-stream :lisp-stream lisp-stream))
571 (defmethod open-stream-p ((stream character-input-stream))
572 (open-stream-p (character-input-stream-lisp-stream stream)))
574 (defmethod close ((stream character-input-stream) &key abort)
575 (close (character-input-stream-lisp-stream stream) :abort abort))
577 (defmethod input-stream-p ((stream character-input-stream))
578 (input-stream-p (character-input-stream-lisp-stream stream)))
580 (defmethod output-stream-p ((stream character-input-stream))
581 (output-stream-p (character-input-stream-lisp-stream stream)))
583 (defmethod stream-read-char ((stream character-input-stream))
584 (read-char (character-input-stream-lisp-stream stream) nil :eof))
586 (defmethod stream-unread-char ((stream character-input-stream) character)
587 (unread-char character (character-input-stream-lisp-stream stream)))
589 (defmethod stream-read-char-no-hang ((stream character-input-stream))
590 (read-char-no-hang (character-input-stream-lisp-stream stream) nil :eof))
593 (defmethod stream-peek-char ((stream character-input-stream))
594 (peek-char nil (character-input-stream-lisp-stream stream) nil :eof))
597 (defmethod stream-listen ((stream character-input-stream))
598 (listen (character-input-stream-lisp-stream stream)))
600 (defmethod stream-clear-input ((stream character-input-stream))
601 (clear-input (character-input-stream-lisp-stream stream)))