tests: Avoid nonsensical classes and methods in deprecation.impure.lisp
[sbcl.git] / src / pcl / gray-streams.lisp
blobdd699913065762b68cc896846204a9db143fb7c6
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
6 ;;;; more information.
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.
18 ;;;
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 (declaim (ftype (function * nil) bug-or-error))
24 (defun bug-or-error (stream fun)
25 (declare (optimize allow-non-returning-tail-call))
26 (error
27 "~@<The stream ~S has no suitable method for ~S, ~
28 and so has fallen through to this method. If you think that this is ~
29 a bug, please report it to the applicable authority (bugs in SBCL itself ~
30 should go to the mailing lists referenced from ~
31 <http://www.sbcl.org/>).~@:>"
32 stream fun))
34 (fmakunbound 'stream-element-type)
36 (defgeneric stream-element-type (stream)
37 (:documentation
38 "Return a type specifier for the kind of object returned by the
39 STREAM. The class FUNDAMENTAL-CHARACTER-STREAM provides a default method
40 which returns CHARACTER."))
42 (defmethod stream-element-type ((stream ansi-stream))
43 (ansi-stream-element-type stream))
45 (defmethod stream-element-type ((stream fundamental-character-stream))
46 'character)
48 (defmethod stream-element-type ((stream stream))
49 (bug-or-error stream 'stream-element-type))
51 (defmethod stream-element-type ((non-stream t))
52 (error 'type-error :datum non-stream :expected-type 'stream))
54 (defgeneric pcl-open-stream-p (stream)
55 (:documentation
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)
77 (:documentation
78 "Close the given STREAM. No more I/O may be performed, but
79 inquiries may still be made. If :ABORT is true, an attempt is made
80 to clean up the side effects of having created the stream."))
82 (defmethod pcl-close ((stream ansi-stream) &key abort)
83 (ansi-stream-close stream abort))
85 (defmethod pcl-close ((stream fundamental-stream) &key abort)
86 (declare (ignore abort))
87 (setf (stream-open-p stream) nil)
90 (progn
91 ;; KLUDGE: Get in a call to PCL-CLOSE with a string-output-stream before
92 ;; setting it as CLOSE. Otherwise using NAMED-LAMBDAs as DFUNs causes a
93 ;; vicious metacircle from FORMAT NIL somewhere in the compiler. This is
94 ;; enough to get the dispatch settled down before we need it.
95 (pcl-close (make-string-output-stream))
96 (setf (fdefinition 'close) #'pcl-close))
98 (let ()
99 (fmakunbound 'input-stream-p)
101 (defgeneric input-stream-p (stream)
102 (:documentation "Can STREAM perform input operations?"))
104 (defmethod input-stream-p ((stream ansi-stream))
105 (ansi-stream-input-stream-p stream))
107 (defmethod input-stream-p ((stream fundamental-stream))
108 nil)
110 (defmethod input-stream-p ((stream fundamental-input-stream))
113 (defmethod input-stream-p ((stream stream))
114 (bug-or-error stream 'input-stream-p))
116 (defmethod input-stream-p ((non-stream t))
117 (error 'type-error :datum non-stream :expected-type 'stream)))
119 (let ()
120 (fmakunbound 'interactive-stream-p)
122 (defgeneric interactive-stream-p (stream)
123 (:documentation "Is STREAM an interactive stream?"))
125 (defmethod interactive-stream-p ((stream ansi-stream))
126 (funcall (ansi-stream-misc stream) stream :interactive-p))
128 (defmethod interactive-stream-p ((stream fundamental-stream))
129 nil)
131 (defmethod interactive-stream-p ((stream stream))
132 (bug-or-error stream 'interactive-stream-p))
134 (defmethod interactive-stream-p ((non-stream t))
135 (error 'type-error :datum non-stream :expected-type 'stream)))
137 (let ()
138 (fmakunbound 'output-stream-p)
140 (defgeneric output-stream-p (stream)
141 (:documentation "Can STREAM perform output operations?"))
143 (defmethod output-stream-p ((stream ansi-stream))
144 (ansi-stream-output-stream-p stream))
146 (defmethod output-stream-p ((stream fundamental-stream))
147 nil)
149 (defmethod output-stream-p ((stream fundamental-output-stream))
152 (defmethod output-stream-p ((stream stream))
153 (bug-or-error stream 'output-stream-p))
155 (defmethod output-stream-p ((non-stream t))
156 (error 'type-error :datum non-stream :expected-type 'stream)))
158 ;;; character input streams
160 ;;; A character input stream can be created by defining a class that
161 ;;; includes FUNDAMENTAL-CHARACTER-INPUT-STREAM and defining methods
162 ;;; for the generic functions below.
164 (defgeneric stream-read-char (stream)
165 (:documentation
166 "Read one character from the stream. Return either a
167 character object, or the symbol :EOF if the stream is at end-of-file.
168 Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
169 method for this function."))
171 (defgeneric stream-unread-char (stream character)
172 (:documentation
173 "Undo the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
174 Return NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
175 must define a method for this function."))
177 (defgeneric stream-read-char-no-hang (stream)
178 (:documentation
179 "This is used to implement READ-CHAR-NO-HANG. It returns either a
180 character, or NIL if no input is currently available, or :EOF if
181 end-of-file is reached. The default method provided by
182 FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; this
183 is sufficient for file streams, but interactive streams should define
184 their own method."))
186 (defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream))
187 (stream-read-char stream))
189 (defgeneric stream-peek-char (stream)
190 (:documentation
191 "This is used to implement PEEK-CHAR; this corresponds to PEEK-TYPE of NIL.
192 It returns either a character or :EOF. The default method calls
193 STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
195 (defmethod stream-peek-char ((stream fundamental-character-input-stream))
196 (let ((char (stream-read-char stream)))
197 (unless (eq char :eof)
198 (stream-unread-char stream char))
199 char))
201 (defgeneric stream-listen (stream)
202 (:documentation
203 "This is used by LISTEN. It returns true or false. The default method uses
204 STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
205 define their own method since it will usually be trivial and will
206 always be more efficient than the default method."))
208 (defmethod stream-listen ((stream fundamental-character-input-stream))
209 (let ((char (stream-read-char-no-hang stream)))
210 (when (characterp char)
211 (stream-unread-char stream char)
212 t)))
214 (defgeneric stream-read-line (stream)
215 (:documentation
216 "This is used by READ-LINE. A string is returned as the first value. The
217 second value is true if the string was terminated by end-of-file
218 instead of the end of a line. The default method uses repeated
219 calls to STREAM-READ-CHAR."))
221 (defmethod stream-read-line ((stream fundamental-character-input-stream))
222 (let ((res (make-string 80))
223 (len 80)
224 (index 0))
225 (loop
226 (let ((ch (stream-read-char stream)))
227 (cond ((eq ch :eof)
228 (return (values (%shrink-vector res index) t)))
230 (when (char= ch #\newline)
231 (return (values (%shrink-vector res index) nil)))
232 (when (= index len)
233 (setq len (* len 2))
234 (let ((new (make-string len)))
235 (replace new res)
236 (setq res new)))
237 (setf (schar res index) ch)
238 (incf index)))))))
240 (defgeneric stream-clear-input (stream)
241 (:documentation
242 "This is like CL:CLEAR-INPUT, but for Gray streams, returning NIL.
243 The default method does nothing."))
245 (defmethod stream-clear-input ((stream fundamental-character-input-stream))
246 nil)
247 (defmethod stream-clear-input ((stream stream))
248 (bug-or-error stream 'stream-clear-input))
249 (defmethod stream-clear-input ((non-stream t))
250 (error 'type-error :datum non-stream :expected-type 'stream))
252 (defgeneric stream-read-sequence (stream seq &optional start end)
253 (:documentation
254 "This is like CL:READ-SEQUENCE, but for Gray streams."))
256 (defmethod stream-read-sequence ((stream fundamental-character-input-stream)
257 (seq sequence)
258 &optional (start 0) (end nil))
259 (sb-impl::read-sequence/read-function
260 seq stream start end 'character
261 (lambda (stream eof-error-p eof-value recursive-p)
262 (aver (null eof-error-p))
263 (aver (eq :eof eof-value))
264 (aver (not recursive-p))
265 (stream-read-char stream))
266 #'ill-bin))
268 (defmethod stream-read-sequence ((stream fundamental-binary-input-stream)
269 (seq sequence)
270 &optional (start 0) (end nil))
271 (let ((stream-element-mode (sb-impl::stream-element-type-stream-element-mode
272 (stream-element-type stream))))
273 (sb-impl::read-sequence/read-function
274 seq stream start end stream-element-mode
275 #'ill-in
276 (lambda (stream eof-error-p eof-value recursive-p)
277 (aver (null eof-error-p))
278 (aver (eq :eof eof-value))
279 (aver (not recursive-p))
280 (stream-read-byte stream)))))
283 ;;; character output streams
285 ;;; A character output stream can be created by defining a class that
286 ;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods
287 ;;; for the generic functions below.
289 (defgeneric stream-write-char (stream character)
290 (:documentation
291 "Write CHARACTER to STREAM and return CHARACTER. Every
292 subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
293 defined for this function."))
295 (defgeneric stream-line-column (stream)
296 (:method ((stream sb-int:form-tracking-stream))
297 (cdr (sb-int:line/col-from-charpos stream)))
298 (:documentation
299 "Return the column number where the next character
300 will be written, or NIL if that is not meaningful for this stream.
301 The first column on a line is numbered 0. This function is used in
302 the implementation of PPRINT and the FORMAT ~T directive. For every
303 character output stream class that is defined, a method must be
304 defined for this function, although it is permissible for it to
305 always return NIL."))
307 (defmethod stream-line-column ((stream fundamental-character-output-stream))
308 nil)
310 ;;; STREAM-LINE-LENGTH is a CMU CL extension to Gray streams.
311 ;;; FIXME: Should we support it? Probably not..
312 (defgeneric stream-line-length (stream)
313 (:documentation "Return the stream line length or NIL."))
315 (defmethod stream-line-length ((stream fundamental-character-output-stream))
316 nil)
318 (defgeneric stream-start-line-p (stream)
319 (:documentation
320 "Is STREAM known to be positioned at the beginning of a line?
321 It is permissible for an implementation to always return
322 NIL. This is used in the implementation of FRESH-LINE. Note that
323 while a value of 0 from STREAM-LINE-COLUMN also indicates the
324 beginning of a line, there are cases where STREAM-START-LINE-P can be
325 meaningfully implemented although STREAM-LINE-COLUMN can't be. For
326 example, for a window using variable-width characters, the column
327 number isn't very meaningful, but the beginning of the line does have
328 a clear meaning. The default method for STREAM-START-LINE-P on class
329 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if
330 that is defined to return NIL, then a method should be provided for
331 either STREAM-START-LINE-P or STREAM-FRESH-LINE."))
333 (defmethod stream-start-line-p ((stream fundamental-character-output-stream))
334 (eql (stream-line-column stream) 0))
336 (defgeneric stream-write-string (stream string &optional start end)
337 (:documentation
338 "This is used by WRITE-STRING. It writes the string to the stream,
339 optionally delimited by start and end, which default to 0 and NIL.
340 The string argument is returned. The default method provided by
341 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to
342 STREAM-WRITE-CHAR."))
344 (defmethod stream-write-string ((stream fundamental-character-output-stream)
345 string &optional (start 0) end)
346 (with-array-data ((data string) (offset-start start) (offset-end end)
347 :check-fill-pointer t)
348 (sb-impl::write-sequence/vector
349 (data simple-string) stream offset-start offset-end #'stream-write-char))
350 string)
352 (defgeneric stream-terpri (stream)
353 (:documentation
354 "Writes an end of line, as for TERPRI. Returns NIL. The default
355 method does (STREAM-WRITE-CHAR stream #\NEWLINE)."))
357 (defmethod stream-terpri ((stream fundamental-character-output-stream))
358 (stream-write-char stream #\Newline))
360 (defgeneric stream-fresh-line (stream)
361 (:documentation
362 "Outputs a new line to the Stream if it is not positioned at the
363 beginning of a line. Returns T if it output a new line, nil
364 otherwise. Used by FRESH-LINE. The default method uses
365 STREAM-START-LINE-P and STREAM-TERPRI."))
367 (defmethod stream-fresh-line ((stream fundamental-character-output-stream))
368 (unless (stream-start-line-p stream)
369 (stream-terpri stream)
372 (defgeneric stream-finish-output (stream)
373 (:documentation
374 "Attempts to ensure that all output sent to the Stream has reached
375 its destination, and only then returns false. Implements
376 FINISH-OUTPUT. The default method does nothing."))
378 (defmethod stream-finish-output ((stream fundamental-output-stream))
379 nil)
380 (defmethod stream-finish-output ((stream stream))
381 (bug-or-error stream 'stream-finish-output))
382 (defmethod stream-finish-output ((non-stream t))
383 (error 'type-error :datum non-stream :expected-type 'stream))
385 (defgeneric stream-force-output (stream)
386 (:documentation
387 "Attempts to force any buffered output to be sent. Implements
388 FORCE-OUTPUT. The default method does nothing."))
390 (defmethod stream-force-output ((stream fundamental-output-stream))
391 nil)
392 (defmethod stream-force-output ((stream stream))
393 (bug-or-error stream 'stream-force-output))
394 (defmethod stream-force-output ((non-stream t))
395 (error 'type-error :datum non-stream :expected-type 'stream))
397 (defgeneric stream-clear-output (stream)
398 (:documentation
399 "This is like CL:CLEAR-OUTPUT, but for Gray streams: clear the given
400 output STREAM. The default method does nothing."))
402 (defmethod stream-clear-output ((stream fundamental-output-stream))
403 nil)
404 (defmethod stream-clear-output ((stream stream))
405 (bug-or-error stream 'stream-clear-output))
406 (defmethod stream-clear-output ((non-stream t))
407 (error 'type-error :datum non-stream :expected-type 'stream))
409 (defgeneric stream-advance-to-column (stream column)
410 (:documentation
411 "Write enough blank space so that the next character will be
412 written at the specified column. Returns true if the operation is
413 successful, or NIL if it is not supported for this stream. This is
414 intended for use by by PPRINT and FORMAT ~T. The default method uses
415 STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a
416 #\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns NIL."))
418 (defmethod stream-advance-to-column ((stream fundamental-character-output-stream)
419 column)
420 (let ((current-column (stream-line-column stream)))
421 (when current-column
422 (let ((fill (- column current-column)))
423 (dotimes (i fill)
424 (stream-write-char stream #\Space)))
425 T)))
427 (defgeneric stream-write-sequence (stream seq &optional start end)
428 (:documentation
429 "This is like CL:WRITE-SEQUENCE, but for Gray streams."))
431 (defmethod stream-write-sequence ((stream fundamental-character-output-stream)
432 (seq sequence)
433 &optional (start 0) (end nil))
434 (sb-impl::write-sequence/write-function
435 seq stream start end 'character #'stream-write-char #'ill-bout))
437 ;; Provide a reasonable default for binary Gray streams. We might be
438 ;; able to do better by specializing on the sequence type, but at
439 ;; least the behaviour is reasonable. --tony 2003/05/08.
440 (defmethod stream-write-sequence ((stream fundamental-binary-output-stream)
441 (seq sequence)
442 &optional (start 0) (end nil))
443 (let ((stream-element-mode (sb-impl::stream-element-type-stream-element-mode
444 (stream-element-type stream))))
445 (sb-impl::write-sequence/write-function
446 seq stream start end stream-element-mode
447 #'ill-out #'stream-write-byte)))
450 ;;; binary streams
452 ;;; Binary streams can be created by defining a class that includes
453 ;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or
454 ;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method
455 ;;; for STREAM-ELEMENT-TYPE and for one or both of the following
456 ;;; generic functions.
458 (defgeneric stream-read-byte (stream)
459 (:documentation
460 "Used by READ-BYTE; returns either an integer, or the symbol :EOF
461 if the stream is at end-of-file."))
463 (defmethod stream-read-byte ((stream stream))
464 (bug-or-error stream 'stream-read-byte))
465 (defmethod stream-read-byte ((non-stream t))
466 (error 'type-error :datum non-stream :expected-type 'stream))
468 (defgeneric stream-write-byte (stream integer)
469 (:documentation
470 "Implements WRITE-BYTE; writes the integer to the stream and
471 returns the integer as the result."))
473 (defmethod stream-write-byte ((stream stream) integer)
474 (bug-or-error stream 'stream-write-byte))
475 (defmethod stream-write-byte ((non-stream t) integer)
476 (error 'type-error :datum non-stream :expected-type 'stream))
478 (defgeneric stream-file-position (stream &optional position-spec)
479 (:documentation
480 "Used by FILE-POSITION. Returns or changes the current position within STREAM."))
482 (defmethod stream-file-position ((stream ansi-stream) &optional position-spec)
483 (ansi-stream-file-position stream position-spec))
485 (defmethod stream-file-position ((stream t) &optional position-spec)
486 (declare (ignore stream position-spec))
487 nil)
490 ;;; This is not in the Gray stream proposal, so it is left here
491 ;;; as example code.
493 ;;; example character output stream encapsulating a lisp-stream
494 (defun make-character-output-stream (lisp-stream)
495 (declare (type lisp-stream lisp-stream))
496 (make-instance 'character-output-stream :lisp-stream lisp-stream))
498 (defmethod open-stream-p ((stream character-output-stream))
499 (open-stream-p (character-output-stream-lisp-stream stream)))
501 (defmethod close ((stream character-output-stream) &key abort)
502 (close (character-output-stream-lisp-stream stream) :abort abort))
504 (defmethod input-stream-p ((stream character-output-stream))
505 (input-stream-p (character-output-stream-lisp-stream stream)))
507 (defmethod output-stream-p ((stream character-output-stream))
508 (output-stream-p (character-output-stream-lisp-stream stream)))
510 (defmethod stream-write-char ((stream character-output-stream) character)
511 (write-char character (character-output-stream-lisp-stream stream)))
513 (defmethod stream-line-column ((stream character-output-stream))
514 (charpos (character-output-stream-lisp-stream stream)))
516 (defmethod stream-line-length ((stream character-output-stream))
517 (line-length (character-output-stream-lisp-stream stream)))
519 (defmethod stream-finish-output ((stream character-output-stream))
520 (finish-output (character-output-stream-lisp-stream stream)))
522 (defmethod stream-force-output ((stream character-output-stream))
523 (force-output (character-output-stream-lisp-stream stream)))
525 (defmethod stream-clear-output ((stream character-output-stream))
526 (clear-output (character-output-stream-lisp-stream stream)))
528 ;;; example character input stream encapsulating a lisp-stream
530 (defun make-character-input-stream (lisp-stream)
531 (declare (type lisp-stream lisp-stream))
532 (make-instance 'character-input-stream :lisp-stream lisp-stream))
534 (defmethod open-stream-p ((stream character-input-stream))
535 (open-stream-p (character-input-stream-lisp-stream stream)))
537 (defmethod close ((stream character-input-stream) &key abort)
538 (close (character-input-stream-lisp-stream stream) :abort abort))
540 (defmethod input-stream-p ((stream character-input-stream))
541 (input-stream-p (character-input-stream-lisp-stream stream)))
543 (defmethod output-stream-p ((stream character-input-stream))
544 (output-stream-p (character-input-stream-lisp-stream stream)))
546 (defmethod stream-read-char ((stream character-input-stream))
547 (read-char (character-input-stream-lisp-stream stream) nil :eof))
549 (defmethod stream-unread-char ((stream character-input-stream) character)
550 (unread-char character (character-input-stream-lisp-stream stream)))
552 (defmethod stream-read-char-no-hang ((stream character-input-stream))
553 (read-char-no-hang (character-input-stream-lisp-stream stream) nil :eof))
555 #+nil
556 (defmethod stream-peek-char ((stream character-input-stream))
557 (peek-char nil (character-input-stream-lisp-stream stream) nil :eof))
559 #+nil
560 (defmethod stream-listen ((stream character-input-stream))
561 (listen (character-input-stream-lisp-stream stream)))
563 (defmethod stream-clear-input ((stream character-input-stream))
564 (clear-input (character-input-stream-lisp-stream stream)))