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
)
92 ;; KLUDGE: Get in a call to PCL-CLOSE with a string-output-stream before
93 ;; setting it as CLOSE. Otherwise using NAMED-LAMBDAs as DFUNs causes a
94 ;; vicious metacircle from FORMAT NIL somewhere in the compiler. This is
95 ;; enough to get the dispatch settled down before we need it.
96 (pcl-close (make-string-output-stream))
97 (setf (fdefinition 'close
) #'pcl-close
))
100 (fmakunbound 'input-stream-p
)
102 (defgeneric input-stream-p
(stream)
104 (:documentation
"Can STREAM perform input operations?"))
106 (defmethod input-stream-p ((stream ansi-stream
))
107 (ansi-stream-input-stream-p stream
))
109 (defmethod input-stream-p ((stream fundamental-stream
))
112 (defmethod input-stream-p ((stream fundamental-input-stream
))
115 (defmethod input-stream-p ((stream stream
))
116 (bug-or-error stream
'input-stream-p
))
118 (defmethod input-stream-p ((non-stream t
))
119 (error 'type-error
:datum non-stream
:expected-type
'stream
)))
122 (fmakunbound 'interactive-stream-p
)
124 (defgeneric interactive-stream-p
(stream)
126 (:documentation
"Is STREAM an interactive stream?"))
128 (defmethod interactive-stream-p ((stream ansi-stream
))
129 (funcall (ansi-stream-misc stream
) stream
:interactive-p
))
131 (defmethod interactive-stream-p ((stream fundamental-stream
))
134 (defmethod interactive-stream-p ((stream stream
))
135 (bug-or-error stream
'interactive-stream-p
))
137 (defmethod interactive-stream-p ((non-stream t
))
138 (error 'type-error
:datum non-stream
:expected-type
'stream
)))
141 (fmakunbound 'output-stream-p
)
143 (defgeneric output-stream-p
(stream)
145 (:documentation
"Can STREAM perform output operations?"))
147 (defmethod output-stream-p ((stream ansi-stream
))
148 (ansi-stream-output-stream-p stream
))
150 (defmethod output-stream-p ((stream fundamental-stream
))
153 (defmethod output-stream-p ((stream fundamental-output-stream
))
156 (defmethod output-stream-p ((stream stream
))
157 (bug-or-error stream
'output-stream-p
))
159 (defmethod output-stream-p ((non-stream t
))
160 (error 'type-error
:datum non-stream
:expected-type
'stream
)))
162 ;;; character input streams
164 ;;; A character input stream can be created by defining a class that
165 ;;; includes FUNDAMENTAL-CHARACTER-INPUT-STREAM and defining methods
166 ;;; for the generic functions below.
168 (defgeneric stream-read-char
(stream)
171 "Read one character from the stream. Return either a
172 character object, or the symbol :EOF if the stream is at end-of-file.
173 Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
174 method for this function."))
176 (defgeneric stream-unread-char
(stream character
)
179 "Undo the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
180 Return NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
181 must define a method for this function."))
183 (defgeneric stream-read-char-no-hang
(stream)
186 "This is used to implement READ-CHAR-NO-HANG. It returns either a
187 character, or NIL if no input is currently available, or :EOF if
188 end-of-file is reached. The default method provided by
189 FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; this
190 is sufficient for file streams, but interactive streams should define
193 (defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream
))
194 (stream-read-char stream
))
196 (defgeneric stream-peek-char
(stream)
199 "This is used to implement PEEK-CHAR; this corresponds to PEEK-TYPE of NIL.
200 It returns either a character or :EOF. The default method calls
201 STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
203 (defmethod stream-peek-char ((stream fundamental-character-input-stream
))
204 (let ((char (stream-read-char stream
)))
205 (unless (eq char
:eof
)
206 (stream-unread-char stream char
))
209 (defgeneric stream-listen
(stream)
212 "This is used by LISTEN. It returns true or false. The default method uses
213 STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
214 define their own method since it will usually be trivial and will
215 always be more efficient than the default method."))
217 (defmethod stream-listen ((stream fundamental-character-input-stream
))
218 (let ((char (stream-read-char-no-hang stream
)))
219 (when (characterp char
)
220 (stream-unread-char stream char
)
223 (defgeneric stream-read-line
(stream)
226 "This is used by READ-LINE. A string is returned as the first value. The
227 second value is true if the string was terminated by end-of-file
228 instead of the end of a line. The default method uses repeated
229 calls to STREAM-READ-CHAR."))
231 (defmethod stream-read-line ((stream fundamental-character-input-stream
))
232 (let ((res (make-string 80))
236 (let ((ch (stream-read-char stream
)))
238 (return (values (%shrink-vector res index
) t
)))
240 (when (char= ch
#\newline
)
241 (return (values (%shrink-vector res index
) nil
)))
244 (let ((new (make-string len
)))
247 (setf (schar res index
) ch
)
250 (defgeneric stream-clear-input
(stream)
253 "This is like CL:CLEAR-INPUT, but for Gray streams, returning NIL.
254 The default method does nothing."))
256 (defmethod stream-clear-input ((stream fundamental-character-input-stream
))
258 (defmethod stream-clear-input ((stream stream
))
259 (bug-or-error stream
'stream-clear-input
))
260 (defmethod stream-clear-input ((non-stream t
))
261 (error 'type-error
:datum non-stream
:expected-type
'stream
))
263 (defgeneric stream-read-sequence
(stream seq
&optional start end
)
266 "This is like CL:READ-SEQUENCE, but for Gray streams."))
268 (defmethod stream-read-sequence ((stream fundamental-character-input-stream
)
270 &optional
(start 0) (end nil
))
271 (sb-impl::read-sequence
/read-function
272 seq stream start end
'character
273 (lambda (stream eof-error-p eof-value recursive-p
)
274 (aver (null eof-error-p
))
275 (aver (eq :eof eof-value
))
276 (aver (not recursive-p
))
277 (stream-read-char stream
))
280 (defmethod stream-read-sequence ((stream fundamental-binary-input-stream
)
282 &optional
(start 0) (end nil
))
283 (let ((stream-element-mode (sb-impl::stream-element-type-stream-element-mode
284 (stream-element-type stream
))))
285 (sb-impl::read-sequence
/read-function
286 seq stream start end stream-element-mode
288 (lambda (stream eof-error-p eof-value recursive-p
)
289 (aver (null eof-error-p
))
290 (aver (eq :eof eof-value
))
291 (aver (not recursive-p
))
292 (stream-read-byte stream
)))))
295 ;;; character output streams
297 ;;; A character output stream can be created by defining a class that
298 ;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods
299 ;;; for the generic functions below.
301 (defgeneric stream-write-char
(stream character
)
304 "Write CHARACTER to STREAM and return CHARACTER. Every
305 subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
306 defined for this function."))
308 (defgeneric stream-line-column
(stream)
309 (:method
((stream sb-int
:form-tracking-stream
))
310 (cdr (sb-int:line
/col-from-charpos stream
)))
313 "Return the column number where the next character
314 will be written, or NIL if that is not meaningful for this stream.
315 The first column on a line is numbered 0. This function is used in
316 the implementation of PPRINT and the FORMAT ~T directive. For every
317 character output stream class that is defined, a method must be
318 defined for this function, although it is permissible for it to
319 always return NIL."))
321 (defmethod stream-line-column ((stream fundamental-character-output-stream
))
324 ;;; STREAM-LINE-LENGTH is a CMU CL extension to Gray streams.
325 ;;; FIXME: Should we support it? Probably not..
326 (defgeneric stream-line-length
(stream)
328 (:documentation
"Return the stream line length or NIL."))
330 (defmethod stream-line-length ((stream fundamental-character-output-stream
))
333 (defgeneric stream-start-line-p
(stream)
336 "Is STREAM known to be positioned at the beginning of a line?
337 It is permissible for an implementation to always return
338 NIL. This is used in the implementation of FRESH-LINE. Note that
339 while a value of 0 from STREAM-LINE-COLUMN also indicates the
340 beginning of a line, there are cases where STREAM-START-LINE-P can be
341 meaningfully implemented although STREAM-LINE-COLUMN can't be. For
342 example, for a window using variable-width characters, the column
343 number isn't very meaningful, but the beginning of the line does have
344 a clear meaning. The default method for STREAM-START-LINE-P on class
345 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if
346 that is defined to return NIL, then a method should be provided for
347 either STREAM-START-LINE-P or STREAM-FRESH-LINE."))
349 (defmethod stream-start-line-p ((stream fundamental-character-output-stream
))
350 (eql (stream-line-column stream
) 0))
352 (defgeneric stream-write-string
(stream string
&optional start end
)
355 "This is used by WRITE-STRING. It writes the string to the stream,
356 optionally delimited by start and end, which default to 0 and NIL.
357 The string argument is returned. The default method provided by
358 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to
359 STREAM-WRITE-CHAR."))
361 (defmethod stream-write-string ((stream fundamental-character-output-stream
)
362 string
&optional
(start 0) end
)
363 (with-array-data ((data string
) (offset-start start
) (offset-end end
)
364 :check-fill-pointer t
)
365 (sb-impl::write-sequence
/vector
366 (data simple-string
) stream offset-start offset-end
#'stream-write-char
))
369 (defgeneric stream-terpri
(stream)
372 "Writes an end of line, as for TERPRI. Returns NIL. The default
373 method does (STREAM-WRITE-CHAR stream #\NEWLINE)."))
375 (defmethod stream-terpri ((stream fundamental-character-output-stream
))
376 (stream-write-char stream
#\Newline
))
378 (defgeneric stream-fresh-line
(stream)
381 "Outputs a new line to the Stream if it is not positioned at the
382 beginning of a line. Returns T if it output a new line, nil
383 otherwise. Used by FRESH-LINE. The default method uses
384 STREAM-START-LINE-P and STREAM-TERPRI."))
386 (defmethod stream-fresh-line ((stream fundamental-character-output-stream
))
387 (unless (stream-start-line-p stream
)
388 (stream-terpri stream
)
391 (defgeneric stream-finish-output
(stream)
394 "Attempts to ensure that all output sent to the Stream has reached
395 its destination, and only then returns false. Implements
396 FINISH-OUTPUT. The default method does nothing."))
398 (defmethod stream-finish-output ((stream fundamental-output-stream
))
400 (defmethod stream-finish-output ((stream stream
))
401 (bug-or-error stream
'stream-finish-output
))
402 (defmethod stream-finish-output ((non-stream t
))
403 (error 'type-error
:datum non-stream
:expected-type
'stream
))
405 (defgeneric stream-force-output
(stream)
408 "Attempts to force any buffered output to be sent. Implements
409 FORCE-OUTPUT. The default method does nothing."))
411 (defmethod stream-force-output ((stream fundamental-output-stream
))
413 (defmethod stream-force-output ((stream stream
))
414 (bug-or-error stream
'stream-force-output
))
415 (defmethod stream-force-output ((non-stream t
))
416 (error 'type-error
:datum non-stream
:expected-type
'stream
))
418 (defgeneric stream-clear-output
(stream)
421 "This is like CL:CLEAR-OUTPUT, but for Gray streams: clear the given
422 output STREAM. The default method does nothing."))
424 (defmethod stream-clear-output ((stream fundamental-output-stream
))
426 (defmethod stream-clear-output ((stream stream
))
427 (bug-or-error stream
'stream-clear-output
))
428 (defmethod stream-clear-output ((non-stream t
))
429 (error 'type-error
:datum non-stream
:expected-type
'stream
))
431 (defgeneric stream-advance-to-column
(stream column
)
434 "Write enough blank space so that the next character will be
435 written at the specified column. Returns true if the operation is
436 successful, or NIL if it is not supported for this stream. This is
437 intended for use by by PPRINT and FORMAT ~T. The default method uses
438 STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a
439 #\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns NIL."))
441 (defmethod stream-advance-to-column ((stream fundamental-character-output-stream
)
443 (let ((current-column (stream-line-column stream
)))
445 (let ((fill (- column current-column
)))
447 (stream-write-char stream
#\Space
)))
450 (defgeneric stream-write-sequence
(stream seq
&optional start end
)
453 "This is like CL:WRITE-SEQUENCE, but for Gray streams."))
455 (defmethod stream-write-sequence ((stream fundamental-character-output-stream
)
457 &optional
(start 0) (end nil
))
458 (sb-impl::write-sequence
/write-function
459 seq stream start end
'character
#'stream-write-char
#'ill-bout
))
461 ;; Provide a reasonable default for binary Gray streams. We might be
462 ;; able to do better by specializing on the sequence type, but at
463 ;; least the behaviour is reasonable. --tony 2003/05/08.
464 (defmethod stream-write-sequence ((stream fundamental-binary-output-stream
)
466 &optional
(start 0) (end nil
))
467 (let ((stream-element-mode (sb-impl::stream-element-type-stream-element-mode
468 (stream-element-type stream
))))
469 (sb-impl::write-sequence
/write-function
470 seq stream start end stream-element-mode
471 #'ill-out
#'stream-write-byte
)))
476 ;;; Binary streams can be created by defining a class that includes
477 ;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or
478 ;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method
479 ;;; for STREAM-ELEMENT-TYPE and for one or both of the following
480 ;;; generic functions.
482 (defgeneric stream-read-byte
(stream)
485 "Used by READ-BYTE; returns either an integer, or the symbol :EOF
486 if the stream is at end-of-file."))
488 (defmethod stream-read-byte ((stream stream
))
489 (bug-or-error stream
'stream-read-byte
))
490 (defmethod stream-read-byte ((non-stream t
))
491 (error 'type-error
:datum non-stream
:expected-type
'stream
))
493 (defgeneric stream-write-byte
(stream integer
)
496 "Implements WRITE-BYTE; writes the integer to the stream and
497 returns the integer as the result."))
499 (defmethod stream-write-byte ((stream stream
) integer
)
500 (bug-or-error stream
'stream-write-byte
))
501 (defmethod stream-write-byte ((non-stream t
) integer
)
502 (error 'type-error
:datum non-stream
:expected-type
'stream
))
504 (defgeneric stream-file-position
(stream &optional position-spec
)
507 "Used by FILE-POSITION. Returns or changes the current position within STREAM."))
509 (defmethod stream-file-position ((stream ansi-stream
) &optional position-spec
)
510 (ansi-stream-file-position stream position-spec
))
512 (defmethod stream-file-position ((stream t
) &optional position-spec
)
513 (declare (ignore stream position-spec
))
517 ;;; This is not in the Gray stream proposal, so it is left here
520 ;;; example character output stream encapsulating a lisp-stream
521 (defun make-character-output-stream (lisp-stream)
522 (declare (type lisp-stream lisp-stream
))
523 (make-instance 'character-output-stream
:lisp-stream lisp-stream
))
525 (defmethod open-stream-p ((stream character-output-stream
))
526 (open-stream-p (character-output-stream-lisp-stream stream
)))
528 (defmethod close ((stream character-output-stream
) &key abort
)
529 (close (character-output-stream-lisp-stream stream
) :abort abort
))
531 (defmethod input-stream-p ((stream character-output-stream
))
532 (input-stream-p (character-output-stream-lisp-stream stream
)))
534 (defmethod output-stream-p ((stream character-output-stream
))
535 (output-stream-p (character-output-stream-lisp-stream stream
)))
537 (defmethod stream-write-char ((stream character-output-stream
) character
)
538 (write-char character
(character-output-stream-lisp-stream stream
)))
540 (defmethod stream-line-column ((stream character-output-stream
))
541 (charpos (character-output-stream-lisp-stream stream
)))
543 (defmethod stream-line-length ((stream character-output-stream
))
544 (line-length (character-output-stream-lisp-stream stream
)))
546 (defmethod stream-finish-output ((stream character-output-stream
))
547 (finish-output (character-output-stream-lisp-stream stream
)))
549 (defmethod stream-force-output ((stream character-output-stream
))
550 (force-output (character-output-stream-lisp-stream stream
)))
552 (defmethod stream-clear-output ((stream character-output-stream
))
553 (clear-output (character-output-stream-lisp-stream stream
)))
555 ;;; example character input stream encapsulating a lisp-stream
557 (defun make-character-input-stream (lisp-stream)
558 (declare (type lisp-stream lisp-stream
))
559 (make-instance 'character-input-stream
:lisp-stream lisp-stream
))
561 (defmethod open-stream-p ((stream character-input-stream
))
562 (open-stream-p (character-input-stream-lisp-stream stream
)))
564 (defmethod close ((stream character-input-stream
) &key abort
)
565 (close (character-input-stream-lisp-stream stream
) :abort abort
))
567 (defmethod input-stream-p ((stream character-input-stream
))
568 (input-stream-p (character-input-stream-lisp-stream stream
)))
570 (defmethod output-stream-p ((stream character-input-stream
))
571 (output-stream-p (character-input-stream-lisp-stream stream
)))
573 (defmethod stream-read-char ((stream character-input-stream
))
574 (read-char (character-input-stream-lisp-stream stream
) nil
:eof
))
576 (defmethod stream-unread-char ((stream character-input-stream
) character
)
577 (unread-char character
(character-input-stream-lisp-stream stream
)))
579 (defmethod stream-read-char-no-hang ((stream character-input-stream
))
580 (read-char-no-hang (character-input-stream-lisp-stream stream
) nil
:eof
))
583 (defmethod stream-peek-char ((stream character-input-stream
))
584 (peek-char nil
(character-input-stream-lisp-stream stream
) nil
:eof
))
587 (defmethod stream-listen ((stream character-input-stream
))
588 (listen (character-input-stream-lisp-stream stream
)))
590 (defmethod stream-clear-input ((stream character-input-stream
))
591 (clear-input (character-input-stream-lisp-stream stream
)))