| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2004, Juan Jose Garcia-Ripoll |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
(in-package "GRAY") |
| 10 |
|
| 11 |
(import 'ext:ansi-stream) |
| 12 |
|
| 13 |
;;; |
| 14 |
;;; This is the generic function interface for CLOS streams. |
| 15 |
;;; |
| 16 |
;;; The following is a port of SBCL's implementation of Gray |
| 17 |
;;; Streams. Minor caveats with respect to the proposal are that we |
| 18 |
;;; rather keep CLOSE, STREAM-ELEMENT-TYPE, INPUT-STREAM-P, |
| 19 |
;;; OUTPUT-STREAM-P and OPEN-STREAM-P these as normal functions that |
| 20 |
;;; call the user extensible EXT:STREAM-{CLOSE, ELT-TYPE, INPUT-P, |
| 21 |
;;; OUTPUT-P, OPEN-P}. |
| 22 |
;;; |
| 23 |
|
| 24 |
(defgeneric stream-advance-to-column (stream column) |
| 25 |
(:documentation |
| 26 |
"Write enough blank space so that the next character will be |
| 27 |
written at the specified column. Returns true if the operation is |
| 28 |
successful, or NIL if it is not supported for this stream. This is |
| 29 |
intended for use by by PPRINT and FORMAT ~T. The default method uses |
| 30 |
STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a |
| 31 |
#\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns |
| 32 |
NIL.")) |
| 33 |
|
| 34 |
(defgeneric stream-clear-input (stream) |
| 35 |
(:documentation |
| 36 |
"This is like CL:CLEAR-INPUT, but for Gray streams, returning NIL. |
| 37 |
The default method does nothing.")) |
| 38 |
|
| 39 |
(defgeneric stream-clear-output (stream) |
| 40 |
(:documentation |
| 41 |
"This is like CL:CLEAR-OUTPUT, but for Gray streams: clear the |
| 42 |
given output STREAM. The default method does nothing.")) |
| 43 |
|
| 44 |
(defgeneric close (stream &key abort) |
| 45 |
(:documentation |
| 46 |
"Close the given STREAM. No more I/O may be performed, but |
| 47 |
inquiries may still be made. If :ABORT is true, an attempt is made |
| 48 |
to clean up the side effects of having created the stream.")) |
| 49 |
|
| 50 |
(defgeneric stream-element-type (stream) |
| 51 |
(:documentation |
| 52 |
"Return a type specifier for the kind of object returned by the |
| 53 |
STREAM. The class FUNDAMENTAL-CHARACTER-STREAM provides a default |
| 54 |
method which returns CHARACTER.")) |
| 55 |
|
| 56 |
(defgeneric stream-finish-output (stream) |
| 57 |
(:documentation |
| 58 |
"Attempts to ensure that all output sent to the Stream has reached |
| 59 |
its destination, and only then returns false. Implements |
| 60 |
FINISH-OUTPUT. The default method does nothing.")) |
| 61 |
|
| 62 |
(defgeneric stream-force-output (stream) |
| 63 |
(:documentation |
| 64 |
"Attempts to force any buffered output to be sent. Implements |
| 65 |
FORCE-OUTPUT. The default method does nothing.")) |
| 66 |
|
| 67 |
(defgeneric stream-fresh-line (stream) |
| 68 |
(:documentation |
| 69 |
"Outputs a new line to the Stream if it is not positioned at the |
| 70 |
beginning of a line. Returns T if it output a new line, nil |
| 71 |
otherwise. Used by FRESH-LINE. The default method uses |
| 72 |
STREAM-START-LINE-P and STREAM-TERPRI.")) |
| 73 |
|
| 74 |
(defgeneric input-stream-p (stream) |
| 75 |
(:documentation "Can STREAM perform input operations?")) |
| 76 |
|
| 77 |
(defgeneric stream-p (stream) |
| 78 |
(:documentation "Is this object a STREAM?")) |
| 79 |
|
| 80 |
(defgeneric stream-interactive-p (stream) |
| 81 |
(:documentation "Is stream interactive (For instance, a tty)?")) |
| 82 |
|
| 83 |
(defgeneric stream-line-column (stream) |
| 84 |
(:documentation |
| 85 |
"Return the column number where the next character will be written, |
| 86 |
or NIL if that is not meaningful for this stream. The first column |
| 87 |
on a line is numbered 0. This function is used in the implementation |
| 88 |
of PPRINT and the FORMAT ~T directive. For every character output |
| 89 |
stream class that is defined, a method must be defined for this |
| 90 |
function, although it is permissible for it to always return NIL.")) |
| 91 |
|
| 92 |
;; Extension from CLASP, CMUCL, SBCL, Mezzano and SICL |
| 93 |
|
| 94 |
(defgeneric stream-line-length (stream) |
| 95 |
(:documentation "Return the stream line length or NIL.")) |
| 96 |
|
| 97 |
(defgeneric stream-listen (stream) |
| 98 |
(:documentation |
| 99 |
"This is used by LISTEN. It returns true or false. The default method uses |
| 100 |
STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should |
| 101 |
define their own method since it will usually be trivial and will |
| 102 |
always be more efficient than the default method.")) |
| 103 |
|
| 104 |
(defgeneric open-stream-p (stream) |
| 105 |
(:documentation |
| 106 |
"Return true if STREAM is not closed. A default method is provided |
| 107 |
by class FUNDAMENTAL-STREAM which returns true if CLOSE has not been |
| 108 |
called on the stream.")) |
| 109 |
|
| 110 |
(defgeneric output-stream-p (stream) |
| 111 |
(:documentation "Can STREAM perform output operations?")) |
| 112 |
|
| 113 |
;;; Extension |
| 114 |
(defgeneric stream-peek-byte (stream) |
| 115 |
(:documentation |
| 116 |
"This is used to implement EXT:PEEK-BYTE. |
| 117 |
It returns either a byte or :EOF. The default method calls STREAM-READ-BYTE |
| 118 |
and STREAM-UNREAD-BYTE.")) |
| 119 |
|
| 120 |
(defgeneric stream-peek-char (stream) |
| 121 |
(:documentation |
| 122 |
"This is used to implement PEEK-CHAR; this corresponds to PEEK-TYPE of NIL. |
| 123 |
It returns either a character or :EOF. The default method calls |
| 124 |
STREAM-READ-CHAR and STREAM-UNREAD-CHAR.")) |
| 125 |
|
| 126 |
(defgeneric stream-read-byte (stream) |
| 127 |
(:documentation |
| 128 |
"Used by READ-BYTE; returns either an integer, or the symbol :EOF |
| 129 |
if the stream is at end-of-file.")) |
| 130 |
|
| 131 |
(defgeneric stream-read-char (stream) |
| 132 |
(:documentation |
| 133 |
"Read one character from the stream. Return either a character |
| 134 |
object, or the symbol :EOF if the stream is at end-of-file. Every |
| 135 |
subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a method |
| 136 |
for this function.")) |
| 137 |
|
| 138 |
(defgeneric stream-read-char-no-hang (stream) |
| 139 |
(:documentation |
| 140 |
"This is used to implement READ-CHAR-NO-HANG. It returns either a |
| 141 |
character, or NIL if no input is currently available, or :EOF if |
| 142 |
end-of-file is reached. The default method provided by |
| 143 |
FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; |
| 144 |
this is sufficient for file streams, but interactive streams should |
| 145 |
define their own method.")) |
| 146 |
|
| 147 |
(defgeneric stream-read-line (stream) |
| 148 |
(:documentation |
| 149 |
"This is used by READ-LINE. A string is returned as the first |
| 150 |
value. The second value is true if the string was terminated by |
| 151 |
end-of-file instead of the end of a line. The default method uses |
| 152 |
repeated calls to STREAM-READ-CHAR.")) |
| 153 |
|
| 154 |
(defgeneric stream-read-sequence (stream sequence &optional start end) |
| 155 |
(:documentation |
| 156 |
"This is like CL:READ-SEQUENCE, but for Gray streams.")) |
| 157 |
|
| 158 |
(defgeneric stream-start-line-p (stream) |
| 159 |
(:documentation |
| 160 |
"Is STREAM known to be positioned at the beginning of a line? |
| 161 |
It is permissible for an implementation to always return |
| 162 |
NIL. This is used in the implementation of FRESH-LINE. Note that |
| 163 |
while a value of 0 from STREAM-LINE-COLUMN also indicates the |
| 164 |
beginning of a line, there are cases where STREAM-START-LINE-P can be |
| 165 |
meaningfully implemented although STREAM-LINE-COLUMN can't be. For |
| 166 |
example, for a window using variable-width characters, the column |
| 167 |
number isn't very meaningful, but the beginning of the line does have |
| 168 |
a clear meaning. The default method for STREAM-START-LINE-P on class |
| 169 |
FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if |
| 170 |
that is defined to return NIL, then a method should be provided for |
| 171 |
either STREAM-START-LINE-P or STREAM-FRESH-LINE.")) |
| 172 |
|
| 173 |
(defgeneric stream-terpri (stream) |
| 174 |
(:documentation |
| 175 |
"Writes an end of line, as for TERPRI. Returns NIL. The default |
| 176 |
method does (STREAM-WRITE-CHAR stream #\NEWLINE).")) |
| 177 |
|
| 178 |
;;; Extension |
| 179 |
(defgeneric stream-unread-byte (stream byte) |
| 180 |
(:documentation |
| 181 |
"Un-do the last call to STREAM-READ-BYTE, as in EXT:UNREAD-BYTE. |
| 182 |
Return NIL. Every subclass of FUNDAMENTAL-BINARY-INPUT-STREAM |
| 183 |
must define a method for this function.")) |
| 184 |
|
| 185 |
(defgeneric stream-unread-char (stream character) |
| 186 |
(:documentation |
| 187 |
"Un-do the last call to STREAM-READ-CHAR, as in UNREAD-CHAR. |
| 188 |
Return NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM |
| 189 |
must define a method for this function.")) |
| 190 |
|
| 191 |
(defgeneric stream-write-byte (stream integer) |
| 192 |
(:documentation |
| 193 |
"Implements WRITE-BYTE; writes the integer to the stream and |
| 194 |
returns the integer as the result.")) |
| 195 |
|
| 196 |
(defgeneric stream-write-char (stream character) |
| 197 |
(:documentation |
| 198 |
"Write CHARACTER to STREAM and return CHARACTER. Every |
| 199 |
subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method |
| 200 |
defined for this function.")) |
| 201 |
|
| 202 |
(defgeneric stream-write-string (stream string &optional start end) |
| 203 |
(:documentation |
| 204 |
"This is used by WRITE-STRING. It writes the string to the stream, |
| 205 |
optionally delimited by start and end, which default to 0 and NIL. |
| 206 |
The string argument is returned. The default method provided by |
| 207 |
FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to |
| 208 |
STREAM-WRITE-CHAR.")) |
| 209 |
|
| 210 |
(defgeneric stream-write-sequence (stream sequence &optional start end) |
| 211 |
(:documentation |
| 212 |
"This is like CL:WRITE-SEQUENCE, but for Gray streams.")) |
| 213 |
|
| 214 |
(defgeneric stream-file-position (stream &optional position) |
| 215 |
(:documentation |
| 216 |
"This is like CL:FILE-POSITION, but for Gray streams.")) |
| 217 |
|
| 218 |
(defgeneric stream-file-length (stream) |
| 219 |
(:documentation |
| 220 |
"This is like CL:FILE-LENGTH, but for Gray streams.")) |
| 221 |
|
| 222 |
(defgeneric stream-file-string-length (stream string) |
| 223 |
(:documentation |
| 224 |
"This is like CL:FILE-STRING-LENGTH, but for Gray streams.")) |
| 225 |
|
| 226 |
(defgeneric stream-file-descriptor (stream &optional direction) |
| 227 |
(:documentation |
| 228 |
"Return the file-descriptor underlaying STREAM, or NIL if not |
| 229 |
available. DIRECTION must be either :INPUT, or :OUTPUT and is |
| 230 |
supposed to discriminate in case STREAM is a bidirectional |
| 231 |
stream. DIRECTION is supposed to default to :INPUT. |
| 232 |
|
| 233 |
An error is signaled if DIRECTION is :INPUT (:OUTPUT), and STREAM |
| 234 |
is not an input (output) stream. A system-provided :BEFORE method |
| 235 |
handles this case; user methods do not need to take care of it. |
| 236 |
|
| 237 |
In case STREAM-FILE-DESCRIPTOR is not implemented for STREAM, an |
| 238 |
error is signaled. That is, users must add methods to explicitly |
| 239 |
decline by returning NIL.")) |
| 240 |
|
| 241 |
(defgeneric pathname (pathspec) |
| 242 |
(:documentation |
| 243 |
"Returns the pathname denoted by pathspec.")) |
| 244 |
|
| 245 |
(defgeneric truename (pathspec) |
| 246 |
(:documentation |
| 247 |
"truename tries to find the file indicated by filespec and returns its truename.")) |
| 248 |
|
| 249 |
|
| 250 |
;;; |
| 251 |
;;; Our class hierarchy looks like the one from Gray streams |
| 252 |
;;; |
| 253 |
;;; character output streams |
| 254 |
;;; |
| 255 |
;;; A character output stream can be created by defining a class that |
| 256 |
;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods |
| 257 |
;;; for the generic functions below. |
| 258 |
;;; |
| 259 |
;;; binary streams |
| 260 |
;;; |
| 261 |
;;; Binary streams can be created by defining a class that includes |
| 262 |
;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or |
| 263 |
;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method |
| 264 |
;;; for STREAM-ELEMENT-TYPE and for one or both of the following |
| 265 |
;;; generic functions. |
| 266 |
;;; |
| 267 |
|
| 268 |
(let ((clos::*clos-booted* 'clos:map-dependents)) |
| 269 |
(defclass fundamental-stream (standard-object stream) |
| 270 |
((open-p :initform t :accessor open-stream-p)) |
| 271 |
(:documentation "the base class for all CLOS streams"))) |
| 272 |
|
| 273 |
(defclass fundamental-input-stream (fundamental-stream) nil) |
| 274 |
|
| 275 |
(defclass fundamental-output-stream (fundamental-stream) nil) |
| 276 |
|
| 277 |
(defclass fundamental-character-stream (fundamental-stream) nil) |
| 278 |
|
| 279 |
(defclass fundamental-binary-stream (fundamental-stream) nil) |
| 280 |
|
| 281 |
(defclass fundamental-character-input-stream |
| 282 |
(fundamental-input-stream fundamental-character-stream) nil) |
| 283 |
|
| 284 |
(defclass fundamental-character-output-stream |
| 285 |
(fundamental-output-stream fundamental-character-stream) nil) |
| 286 |
|
| 287 |
(defclass fundamental-binary-input-stream |
| 288 |
(fundamental-input-stream fundamental-binary-stream) nil) |
| 289 |
|
| 290 |
(defclass fundamental-binary-output-stream |
| 291 |
(fundamental-output-stream fundamental-binary-stream) nil) |
| 292 |
|
| 293 |
|
| 294 |
;;; |
| 295 |
;;; The following methods constitute default implementations. |
| 296 |
;;; |
| 297 |
|
| 298 |
(defun bug-or-error (stream fun) |
| 299 |
(declare (si::c-local)) |
| 300 |
(if (typep stream 'stream) |
| 301 |
(error "The stream ~S has no suitable method for ~S." stream fun) |
| 302 |
(error 'type-error :datum stream :expected-type 'stream))) |
| 303 |
|
| 304 |
;; STREAM-ADVANCE-TO-COLUMN |
| 305 |
|
| 306 |
(defmethod stream-advance-to-column ((stream fundamental-character-output-stream) |
| 307 |
column) |
| 308 |
(let ((current-column (stream-line-column stream))) |
| 309 |
(when current-column |
| 310 |
(let ((fill (floor (- column current-column)))) |
| 311 |
(dotimes (i fill) |
| 312 |
(stream-write-char stream #\Space))) |
| 313 |
T))) |
| 314 |
|
| 315 |
|
| 316 |
;; CLEAR-INPUT |
| 317 |
|
| 318 |
(defmethod stream-clear-input ((stream fundamental-character-input-stream)) |
| 319 |
(declare (ignore stream)) |
| 320 |
nil) |
| 321 |
|
| 322 |
(defmethod stream-clear-input ((stream ansi-stream)) |
| 323 |
(cl:clear-input stream)) |
| 324 |
|
| 325 |
(defmethod stream-clear-input ((stream t)) |
| 326 |
(bug-or-error stream 'stream-clear-input)) |
| 327 |
|
| 328 |
|
| 329 |
;; CLEAR-OUTPUT |
| 330 |
|
| 331 |
(defmethod stream-clear-output ((stream fundamental-output-stream)) |
| 332 |
(declare (ignore stream)) |
| 333 |
nil) |
| 334 |
|
| 335 |
(defmethod stream-clear-output ((stream ansi-stream)) |
| 336 |
(cl:clear-output stream)) |
| 337 |
|
| 338 |
(defmethod stream-clear-output ((stream t)) |
| 339 |
(bug-or-error stream 'stream-clear-output)) |
| 340 |
|
| 341 |
|
| 342 |
;; CLOSE |
| 343 |
|
| 344 |
(defmethod close ((stream fundamental-stream) &key abort) |
| 345 |
(declare (ignore abort)) |
| 346 |
(setf (open-stream-p stream) nil) |
| 347 |
t) |
| 348 |
|
| 349 |
(defmethod close ((stream ansi-stream) &key abort) |
| 350 |
(cl:close stream :abort abort)) |
| 351 |
|
| 352 |
(defmethod close ((stream stream) &key abort) |
| 353 |
(declare (ignore abort)) |
| 354 |
(bug-or-error stream 'close)) |
| 355 |
|
| 356 |
|
| 357 |
;; STREAM-ELEMENT-TYPE |
| 358 |
|
| 359 |
(defmethod stream-element-type ((stream fundamental-character-stream)) |
| 360 |
(declare (ignore stream)) |
| 361 |
'character) |
| 362 |
|
| 363 |
(defmethod stream-element-type ((stream ansi-stream)) |
| 364 |
(cl:stream-element-type stream)) |
| 365 |
|
| 366 |
(defmethod stream-element-type ((stream t)) |
| 367 |
(bug-or-error stream 'stream-element-type)) |
| 368 |
|
| 369 |
;; FINISH-OUTPUT |
| 370 |
|
| 371 |
(defmethod stream-finish-output ((stream fundamental-output-stream)) |
| 372 |
(declare (ignore stream)) |
| 373 |
nil) |
| 374 |
|
| 375 |
(defmethod stream-finish-output ((stream ansi-stream)) |
| 376 |
(cl:finish-output stream)) |
| 377 |
|
| 378 |
(defmethod stream-finish-output ((stream t)) |
| 379 |
(bug-or-error stream 'stream-finish-output)) |
| 380 |
|
| 381 |
|
| 382 |
;; FORCE-OUTPUT |
| 383 |
|
| 384 |
(defmethod stream-force-output ((stream fundamental-output-stream)) |
| 385 |
(declare (ignore stream)) |
| 386 |
nil) |
| 387 |
|
| 388 |
(defmethod stream-force-output ((stream ansi-stream)) |
| 389 |
(cl:force-output stream)) |
| 390 |
|
| 391 |
(defmethod stream-force-output ((stream t)) |
| 392 |
(bug-or-error stream 'stream-force-output)) |
| 393 |
|
| 394 |
|
| 395 |
;; FRESH-LINE |
| 396 |
|
| 397 |
(defmethod stream-fresh-line ((stream fundamental-character-output-stream)) |
| 398 |
(unless (stream-start-line-p stream) |
| 399 |
(stream-terpri stream) |
| 400 |
t)) |
| 401 |
|
| 402 |
(defmethod stream-fresh-line ((stream ansi-stream)) |
| 403 |
(cl:fresh-line stream)) |
| 404 |
|
| 405 |
|
| 406 |
;; INPUT-STREAM-P |
| 407 |
|
| 408 |
(defmethod input-stream-p ((stream fundamental-stream)) |
| 409 |
(declare (ignore stream)) |
| 410 |
nil) |
| 411 |
|
| 412 |
(defmethod input-stream-p ((stream fundamental-input-stream)) |
| 413 |
(declare (ignore stream)) |
| 414 |
t) |
| 415 |
|
| 416 |
(defmethod input-stream-p ((stream ansi-stream)) |
| 417 |
(cl:input-stream-p stream)) |
| 418 |
|
| 419 |
(defmethod input-stream-p ((stream t)) |
| 420 |
(bug-or-error stream 'input-stream-p)) |
| 421 |
|
| 422 |
|
| 423 |
;; INTERACTIVE-STREAM-P |
| 424 |
|
| 425 |
(defmethod stream-interactive-p ((stream fundamental-stream)) |
| 426 |
(declare (ignore stream)) |
| 427 |
nil) |
| 428 |
|
| 429 |
(defmethod stream-interactive-p ((stream ansi-stream)) |
| 430 |
(cl:interactive-stream-p stream)) |
| 431 |
|
| 432 |
(defmethod stream-interactive-p ((stream t)) |
| 433 |
(bug-or-error stream 'stream-interactive-p)) |
| 434 |
|
| 435 |
|
| 436 |
;; LINE-COLUMN |
| 437 |
|
| 438 |
(defmethod stream-line-column ((stream fundamental-character-output-stream)) |
| 439 |
(declare (ignore stream)) |
| 440 |
nil) |
| 441 |
|
| 442 |
;; LINE-LENGTH |
| 443 |
|
| 444 |
(defmethod stream-line-length ((stream fundamental-character-output-stream)) |
| 445 |
nil) |
| 446 |
|
| 447 |
(defmethod stream-line-length ((stream ansi-stream)) |
| 448 |
nil) |
| 449 |
|
| 450 |
(defmethod stream-line-length ((stream t)) |
| 451 |
(bug-or-error stream 'stream-line-length)) |
| 452 |
|
| 453 |
;; LISTEN |
| 454 |
|
| 455 |
(defmethod stream-listen ((stream fundamental-character-input-stream)) |
| 456 |
(let ((char (stream-read-char-no-hang stream))) |
| 457 |
(when (characterp char) |
| 458 |
(stream-unread-char stream char) |
| 459 |
t))) |
| 460 |
|
| 461 |
(defmethod stream-listen ((stream ansi-stream)) |
| 462 |
(cl:listen stream)) |
| 463 |
|
| 464 |
(defmethod stream-listen ((stream t)) |
| 465 |
(bug-or-error stream 'stream-listen)) |
| 466 |
|
| 467 |
|
| 468 |
;; OPEN-STREAM-P |
| 469 |
|
| 470 |
(defmethod open-stream-p ((stream ansi-stream)) |
| 471 |
(cl:open-stream-p stream)) |
| 472 |
|
| 473 |
(defmethod open-stream-p ((stream t)) |
| 474 |
(bug-or-error stream 'open-stream-p)) |
| 475 |
|
| 476 |
|
| 477 |
;; OUTPUT-STREAM-P |
| 478 |
|
| 479 |
(defmethod output-stream-p ((stream fundamental-stream)) |
| 480 |
(declare (ignore stream)) |
| 481 |
nil) |
| 482 |
|
| 483 |
(defmethod output-stream-p ((stream fundamental-output-stream)) |
| 484 |
(declare (ignore stream)) |
| 485 |
t) |
| 486 |
|
| 487 |
(defmethod output-stream-p ((stream ansi-stream)) |
| 488 |
(cl:output-stream-p stream)) |
| 489 |
|
| 490 |
(defmethod output-stream-p ((stream t)) |
| 491 |
(bug-or-error stream 'output-stream-p)) |
| 492 |
|
| 493 |
;; PEEK-BYTE |
| 494 |
|
| 495 |
(defmethod stream-peek-byte ((stream fundamental-binary-input-stream)) |
| 496 |
(let ((byte (stream-read-byte stream))) |
| 497 |
(unless (eq byte :eof) |
| 498 |
(stream-unread-byte stream byte)) |
| 499 |
byte)) |
| 500 |
|
| 501 |
(defmethod stream-peek-byte ((stream ansi-stream)) |
| 502 |
(ext:peek-byte stream :eof)) |
| 503 |
|
| 504 |
(defmethod stream-peek-byte ((stream t)) |
| 505 |
(bug-or-error stream 'stream-peek-byte)) |
| 506 |
|
| 507 |
;; PEEK-CHAR |
| 508 |
|
| 509 |
(defmethod stream-peek-char ((stream fundamental-character-input-stream)) |
| 510 |
(let ((char (stream-read-char stream))) |
| 511 |
(unless (eq char :eof) |
| 512 |
(stream-unread-char stream char)) |
| 513 |
char)) |
| 514 |
|
| 515 |
(defmethod stream-peek-char ((stream ansi-stream)) |
| 516 |
(cl:peek-char stream)) |
| 517 |
|
| 518 |
(defmethod stream-peek-char ((stream t)) |
| 519 |
(bug-or-error stream 'stream-peek-char)) |
| 520 |
|
| 521 |
|
| 522 |
;; READ-BYTE |
| 523 |
|
| 524 |
(defmethod stream-read-byte ((stream ansi-stream)) |
| 525 |
(cl:read-byte stream)) |
| 526 |
|
| 527 |
(defmethod stream-read-byte ((stream t)) |
| 528 |
(bug-or-error stream 'stream-read-byte)) |
| 529 |
|
| 530 |
|
| 531 |
;; READ-CHAR |
| 532 |
|
| 533 |
(defmethod stream-read-char ((stream ansi-stream)) |
| 534 |
(cl:read-char stream)) |
| 535 |
|
| 536 |
(defmethod stream-read-char ((stream t)) |
| 537 |
(bug-or-error stream 'stream-read-char)) |
| 538 |
|
| 539 |
;; UNREAD-BYTE |
| 540 |
|
| 541 |
(defmethod stream-unread-byte ((stream ansi-stream) byte) |
| 542 |
(ext:unread-byte stream byte)) |
| 543 |
|
| 544 |
(defmethod stream-unread-byte ((stream t) byte) |
| 545 |
(declare (ignore byte)) |
| 546 |
(bug-or-error stream 'stream-unread-byte)) |
| 547 |
|
| 548 |
;; UNREAD-CHAR |
| 549 |
|
| 550 |
(defmethod stream-unread-char ((stream ansi-stream) character) |
| 551 |
(cl:unread-char character stream)) |
| 552 |
|
| 553 |
(defmethod stream-unread-char ((stream t) character) |
| 554 |
(declare (ignore character)) |
| 555 |
(bug-or-error stream 'stream-unread-char)) |
| 556 |
|
| 557 |
|
| 558 |
;; READ-CHAR-NO-HANG |
| 559 |
|
| 560 |
(defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream)) |
| 561 |
(stream-read-char stream)) |
| 562 |
|
| 563 |
(defmethod stream-read-char-no-hang ((stream ansi-stream)) |
| 564 |
(cl:read-char-no-hang stream)) |
| 565 |
|
| 566 |
(defmethod stream-read-char-no-hang ((stream t)) |
| 567 |
(bug-or-error stream 'stream-read-char-no-hang)) |
| 568 |
|
| 569 |
|
| 570 |
;; READ-LINE |
| 571 |
|
| 572 |
(defmethod stream-read-line ((stream fundamental-character-input-stream)) |
| 573 |
(let ((res (make-string 80)) |
| 574 |
(len 80) |
| 575 |
(index 0)) |
| 576 |
(loop |
| 577 |
(let ((ch (stream-read-char stream))) |
| 578 |
(cond ((eq ch :eof) |
| 579 |
(return (values (si::shrink-vector res index) |
| 580 |
t))) |
| 581 |
(t |
| 582 |
(when (char= ch #\newline) |
| 583 |
(return (values (si::shrink-vector res index) nil))) |
| 584 |
(when (= index len) |
| 585 |
(setq len (* len 2)) |
| 586 |
(let ((new (make-string len))) |
| 587 |
(replace new res) |
| 588 |
(setq res new))) |
| 589 |
(setf (schar res index) ch) |
| 590 |
(incf index))))))) |
| 591 |
|
| 592 |
(defmethod stream-read-line ((stream ansi-stream)) |
| 593 |
(cl:read-line stream)) |
| 594 |
|
| 595 |
(defmethod stream-read-line ((stream t)) |
| 596 |
(bug-or-error stream 'stream-read-line)) |
| 597 |
|
| 598 |
|
| 599 |
;; READ-SEQUENCE |
| 600 |
|
| 601 |
;; Simple default stream-write-sequence method for CLOS streams. Note |
| 602 |
;; that we cannot use si:do-read-sequence for this purpose, since it |
| 603 |
;; will call stream-read-sequence again. |
| 604 |
(defun clos-default-read-sequence (stream sequence start end) |
| 605 |
(declare (type t stream) ; check for c::stream-designator ignored |
| 606 |
(type sequence sequence) |
| 607 |
(fixnum start) |
| 608 |
(ext:check-arguments-type)) |
| 609 |
(let ((end (or end (length sequence))) |
| 610 |
(elttype (stream-element-type stream))) |
| 611 |
(declare (fixnum end)) |
| 612 |
(if (or (eql elttype 'character) (eql elttype 'base-char)) |
| 613 |
(loop for pos from start below end |
| 614 |
do (let ((c (stream-read-char stream))) |
| 615 |
(if (eql c :eof) |
| 616 |
(return pos) |
| 617 |
(setf (elt sequence pos) c))) |
| 618 |
finally (return pos)) |
| 619 |
(loop for pos from start below end |
| 620 |
do (let ((b (stream-read-byte stream))) |
| 621 |
(if (eql b :eof) |
| 622 |
(return pos) |
| 623 |
(setf (elt sequence pos) b))) |
| 624 |
finally (return pos))))) |
| 625 |
|
| 626 |
(defmethod stream-read-sequence ((stream fundamental-character-input-stream) |
| 627 |
sequence &optional (start 0) (end nil)) |
| 628 |
(clos-default-read-sequence stream sequence start end)) |
| 629 |
|
| 630 |
(defmethod stream-read-sequence ((stream fundamental-binary-input-stream) |
| 631 |
sequence &optional (start 0) (end nil)) |
| 632 |
(clos-default-read-sequence stream sequence start end)) |
| 633 |
|
| 634 |
(defmethod stream-read-sequence ((stream ansi-stream) sequence |
| 635 |
&optional (start 0) (end nil)) |
| 636 |
(si:do-read-sequence sequence stream start end)) |
| 637 |
|
| 638 |
(defmethod stream-read-sequence ((stream t) sequence &optional start end) |
| 639 |
(declare (ignore sequence start end)) |
| 640 |
(bug-or-error stream 'stream-read-sequence)) |
| 641 |
|
| 642 |
|
| 643 |
;; START-LINE-P |
| 644 |
|
| 645 |
(defmethod stream-start-line-p ((stream fundamental-character-output-stream)) |
| 646 |
(eql (stream-line-column stream) 0)) |
| 647 |
|
| 648 |
;; FILE-POSITION |
| 649 |
|
| 650 |
(defmethod stream-file-position ((stream ansi-stream) &optional position) |
| 651 |
(file-position stream position)) |
| 652 |
|
| 653 |
(defmethod stream-file-position ((stream t) &optional position) |
| 654 |
(declare (ignore stream position)) |
| 655 |
nil) |
| 656 |
|
| 657 |
;; FILE-LENGTH |
| 658 |
|
| 659 |
(defmethod stream-file-length ((stream ansi-stream)) |
| 660 |
(file-length stream)) |
| 661 |
|
| 662 |
(defmethod stream-file-length ((stream t)) |
| 663 |
(error 'type-error :datum stream :expected-type 'file-stream)) |
| 664 |
|
| 665 |
;; FILE-STRING-LENGTH |
| 666 |
|
| 667 |
(defmethod stream-file-string-length ((stream ansi-stream) string) |
| 668 |
(file-string-length stream string)) |
| 669 |
|
| 670 |
(defmethod stream-file-string-length ((stream fundamental-character-output-stream) string) |
| 671 |
nil) |
| 672 |
|
| 673 |
;; STREAM-P |
| 674 |
|
| 675 |
(defmethod streamp ((stream stream)) |
| 676 |
(declare (ignore stream)) |
| 677 |
t) |
| 678 |
|
| 679 |
(defmethod streamp ((stream t)) |
| 680 |
(declare (ignore stream)) |
| 681 |
nil) |
| 682 |
|
| 683 |
|
| 684 |
;; WRITE-BYTE |
| 685 |
|
| 686 |
(defmethod stream-write-byte ((stream ansi-stream) integer) |
| 687 |
(cl:write-byte integer stream)) |
| 688 |
|
| 689 |
(defmethod stream-write-byte ((stream t) integer) |
| 690 |
(declare (ignore integer)) |
| 691 |
(bug-or-error stream 'stream-write-byte)) |
| 692 |
|
| 693 |
|
| 694 |
;; WRITE-CHAR |
| 695 |
|
| 696 |
(defmethod stream-write-char ((stream ansi-stream) character) |
| 697 |
(cl:write-char character stream)) |
| 698 |
|
| 699 |
(defmethod stream-write-char ((stream t) character) |
| 700 |
(declare (ignore character)) |
| 701 |
(bug-or-error stream 'stream-write-char)) |
| 702 |
|
| 703 |
|
| 704 |
;; WRITE-SEQUENCE |
| 705 |
|
| 706 |
;; Simple default stream-write-sequence method for CLOS streams. Note |
| 707 |
;; that we cannot use si:do-write-sequence for this purpose, since it |
| 708 |
;; will call stream-write-sequence again. |
| 709 |
(defun clos-default-write-sequence (stream sequence start end) |
| 710 |
(declare (type t stream) ; check for c::stream-designator ignored |
| 711 |
(type sequence sequence) |
| 712 |
(fixnum start) |
| 713 |
(ext:check-arguments-type)) |
| 714 |
(let ((end (or end (length sequence))) |
| 715 |
(elttype (stream-element-type stream))) |
| 716 |
(declare (fixnum end)) |
| 717 |
(if (or (eql elttype 'character) (eql elttype 'base-char)) |
| 718 |
(loop for pos from start below end |
| 719 |
do (stream-write-char stream (elt sequence pos))) |
| 720 |
(loop for pos from start below end |
| 721 |
do (stream-write-byte stream (elt sequence pos))))) |
| 722 |
sequence) |
| 723 |
|
| 724 |
(defmethod stream-write-sequence ((stream fundamental-character-output-stream) sequence |
| 725 |
&optional (start 0) end) |
| 726 |
(if (stringp sequence) |
| 727 |
(stream-write-string stream sequence start end) |
| 728 |
(clos-default-write-sequence stream sequence start end))) |
| 729 |
|
| 730 |
(defmethod stream-write-sequence ((stream fundamental-binary-output-stream) sequence |
| 731 |
&optional (start 0) end) |
| 732 |
(clos-default-write-sequence stream sequence start end)) |
| 733 |
|
| 734 |
(defmethod stream-write-sequence ((stream ansi-stream) sequence |
| 735 |
&optional (start 0) end) |
| 736 |
(si::do-write-sequence sequence stream start end)) |
| 737 |
|
| 738 |
(defmethod stream-write-sequence ((stream t) sequence |
| 739 |
&optional start end) |
| 740 |
(declare (ignore sequence start end)) |
| 741 |
(bug-or-error stream 'stream-write-sequence)) |
| 742 |
|
| 743 |
|
| 744 |
;; WRITE-STRING |
| 745 |
|
| 746 |
(defmethod stream-write-string ((stream fundamental-character-output-stream) |
| 747 |
string &optional (start 0) end) |
| 748 |
(declare (type t stream) ; check for c::stream-designator ignored |
| 749 |
(string string) |
| 750 |
(fixnum start) |
| 751 |
(ext:check-arguments-type)) |
| 752 |
(let ((end (or end (length string)))) |
| 753 |
(declare (fixnum end)) |
| 754 |
(do ((pos start (1+ pos))) |
| 755 |
((>= pos end)) |
| 756 |
(declare (type si::index pos)) |
| 757 |
(stream-write-char stream (aref string pos)))) |
| 758 |
string) |
| 759 |
|
| 760 |
(defmethod stream-write-string ((stream ansi-stream) string &optional (start 0) end) |
| 761 |
(cl:write-string string stream :start start :end end)) |
| 762 |
|
| 763 |
(defmethod stream-write-string ((stream t) string &optional start end) |
| 764 |
(declare (ignore string start end)) |
| 765 |
(bug-or-error stream 'stream-write-string)) |
| 766 |
|
| 767 |
|
| 768 |
;; TERPRI |
| 769 |
|
| 770 |
(defmethod stream-terpri ((stream fundamental-character-output-stream)) |
| 771 |
(stream-write-char stream #\Newline) |
| 772 |
nil) |
| 773 |
|
| 774 |
(defmethod stream-terpri ((stream ansi-stream)) |
| 775 |
(cl:terpri stream)) |
| 776 |
|
| 777 |
(defmethod stream-terpri ((stream t)) |
| 778 |
(bug-or-error stream 'stream-terpri)) |
| 779 |
|
| 780 |
|
| 781 |
;;; FILE-DESCRIPTOR |
| 782 |
|
| 783 |
(defmethod stream-file-descriptor :before (stream |
| 784 |
&optional (direction :input)) |
| 785 |
(multiple-value-bind (predicate kind) |
| 786 |
(case direction |
| 787 |
(:input (values 'input-stream-p "input")) |
| 788 |
(:output (values 'output-stream-p "output")) |
| 789 |
(t |
| 790 |
(error 'simple-type-error |
| 791 |
:format-control "Not a valid direction, ~S; must be one of ~ |
| 792 |
:INPUT or :OUTPUT." |
| 793 |
:format-arguments (list direction) |
| 794 |
:datum direction |
| 795 |
:expected-type '(member :input :output)))) |
| 796 |
(unless (funcall predicate stream) |
| 797 |
(error 'simple-type-error |
| 798 |
:format-control "Not an ~A stream, ~S, although ~S ~ |
| 799 |
was provided as DIRECTION." |
| 800 |
:format-arguments (list kind stream direction) |
| 801 |
:datum stream |
| 802 |
:expected-type `(satisfies ,predicate))))) |
| 803 |
|
| 804 |
(defmethod stream-file-descriptor (stream &optional direction) |
| 805 |
(declare (ignore direction)) |
| 806 |
(bug-or-error stream 'stream-file-descriptor)) |
| 807 |
|
| 808 |
(defmethod stream-file-descriptor ((stream two-way-stream) |
| 809 |
&optional (direction :input)) |
| 810 |
(stream-file-descriptor |
| 811 |
(case direction |
| 812 |
(:input (two-way-stream-input-stream stream)) |
| 813 |
(:output (two-way-stream-output-stream stream))) |
| 814 |
direction)) |
| 815 |
|
| 816 |
(defmethod stream-file-descriptor ((stream file-stream) |
| 817 |
&optional (direction :input)) |
| 818 |
(declare (ignore direction)) |
| 819 |
(si:file-stream-fd stream)) |
| 820 |
|
| 821 |
|
| 822 |
;;; PATHNAME |
| 823 |
|
| 824 |
(defmethod pathname ((pathspec string)) |
| 825 |
(cl:pathname pathspec)) |
| 826 |
|
| 827 |
(defmethod pathname ((pathspec cl:pathname)) |
| 828 |
pathspec) |
| 829 |
|
| 830 |
(defmethod pathname ((pathspec ansi-stream)) |
| 831 |
(cl:pathname pathspec)) |
| 832 |
|
| 833 |
(defmethod pathname (pathspec) |
| 834 |
(error 'type-error :datum pathspec |
| 835 |
:expected-type '(or string cl:pathname file-stream))) |
| 836 |
|
| 837 |
|
| 838 |
;;; TRUENAME |
| 839 |
|
| 840 |
(defmethod truename ((filespec string)) |
| 841 |
(cl:truename filespec)) |
| 842 |
|
| 843 |
(defmethod truename ((filespec cl:pathname)) |
| 844 |
(cl:truename filespec)) |
| 845 |
|
| 846 |
(defmethod truename ((filespec ansi-stream)) |
| 847 |
(cl:truename filespec)) |
| 848 |
|
| 849 |
(defmethod truename (filespec) |
| 850 |
(error 'type-error :datum filespec |
| 851 |
:expected-type '(or string cl:pathname file-stream))) |
| 852 |
|
| 853 |
|
| 854 |
;;; Setup |
| 855 |
|
| 856 |
(eval-when (:compile-toplevel :execute) |
| 857 |
(defconstant +conflicting-symbols+ |
| 858 |
'(cl:close cl:stream-element-type cl:input-stream-p |
| 859 |
cl:open-stream-p cl:output-stream-p cl:streamp |
| 860 |
cl:pathname cl:truename))) |
| 861 |
|
| 862 |
(let ((p (find-package "GRAY"))) |
| 863 |
(export '(nil) p) |
| 864 |
(do-external-symbols (s (find-package "COMMON-LISP")) |
| 865 |
(unless (member s '#.+conflicting-symbols+) |
| 866 |
(export s p)))) |
| 867 |
|
| 868 |
;;; Redefining the IO functions |
| 869 |
;; |
| 870 |
;; I guess that because of efficiency reasons most of the IO functions |
| 871 |
;; in CL are normal functions (ie. not generic functions); but that |
| 872 |
;; doesn't work with packages like FLEXI-STREAMS that want to define |
| 873 |
;; new stream types that work with the same symbols from CL. |
| 874 |
;; |
| 875 |
;; TRIVIAL-GRAY-STREAMS tries to unify that mess across different |
| 876 |
;; implementations, by importing most of (for ECL) GRAY into |
| 877 |
;; IMPL-SPECIFIC-GRAY, importing from I-S-G into T-G-S, and |
| 878 |
;; overloading/extending there where necessary. |
| 879 |
;; |
| 880 |
;; |
| 881 |
;; REDEFINE-CL-FUNCTIONS should now make the functions that are bound |
| 882 |
;; to CL symbols generic functions. |
| 883 |
;; |
| 884 |
;; |
| 885 |
;; So... |
| 886 |
;; |
| 887 |
;; CL has a function |
| 888 |
;; GRAY has a function |
| 889 |
;; |
| 890 |
;; TRIVIAL-GRAY-STREAMS imports from GRAY |
| 891 |
;; |
| 892 |
;; But calling eg. CL:FILE-POSITION should make use of all the methods |
| 893 |
;; defined on T-G-S:STREAMS-FILE-POSITION ... |
| 894 |
;; |
| 895 |
|
| 896 |
(defun %redefine-cl-functions (cl-symbol gray-symbol gray-package) |
| 897 |
(unless (typep (fdefinition cl-symbol) 'generic-function) |
| 898 |
(let ((gf (fdefinition gray-symbol))) |
| 899 |
;; Given a symbol in CL, and one in GRAY, we want to keep the CL |
| 900 |
;; symbol (in case there are references to it stored somewhere), |
| 901 |
;; but it shall get the generic-function ... |
| 902 |
(setf (fdefinition cl-symbol) gf) |
| 903 |
;; and become EQ to the GRAY symbol. |
| 904 |
;; |
| 905 |
;; But: unintern/import removes the package from the symbol used |
| 906 |
;; as name by the GF, making it equivalent to a GENSYM - and |
| 907 |
;; then no new methods can be registered for it ... |
| 908 |
;; |
| 909 |
;; For same symbol-names, we can unintern/import/export; |
| 910 |
;; for different symbol-names, we can only copy the fdefinition. |
| 911 |
(when (string= (symbol-name cl-symbol) |
| 912 |
(symbol-name gray-symbol)) |
| 913 |
(unintern gray-symbol gray-package) |
| 914 |
(import cl-symbol gray-package) |
| 915 |
(export cl-symbol gray-package)) |
| 916 |
;; so now make the GF accessible again |
| 917 |
(setf (slot-value gf 'clos::name) |
| 918 |
cl-symbol)))) |
| 919 |
|
| 920 |
(defun redefine-cl-functions () |
| 921 |
"Some functions in CL package are expected to be generic. We make |
| 922 |
them so." |
| 923 |
(let ((x (si::package-lock "COMMON-LISP" nil)) |
| 924 |
(gray-package (find-package "GRAY"))) |
| 925 |
(loop for cl-symbol in '#.+conflicting-symbols+ |
| 926 |
for gray-symbol = (find-symbol (symbol-name cl-symbol) |
| 927 |
gray-package) |
| 928 |
do (%redefine-cl-functions cl-symbol |
| 929 |
gray-symbol |
| 930 |
gray-package)) |
| 931 |
;; things that are called differently |
| 932 |
(%redefine-cl-functions 'cl:file-position |
| 933 |
'gray:stream-file-position |
| 934 |
gray-package) |
| 935 |
(%redefine-cl-functions 'cl:file-length |
| 936 |
'gray:stream-file-length |
| 937 |
gray-package) |
| 938 |
(si::package-lock "COMMON-LISP" x) |
| 939 |
(provide '#:gray-streams) |
| 940 |
nil)) |
| 941 |
|
| 942 |
(pushnew :gray-streams-module *features*) |
| 943 |
|
| 944 |
(pushnew #'(lambda (module) |
| 945 |
(when (string-equal module '#:gray-streams) |
| 946 |
(redefine-cl-functions) |
| 947 |
t)) |
| 948 |
sys:*module-provider-functions*) |
| 949 |
|
| 950 |
(setf clos::*clos-booted* t) |