1 ;;;; os-independent stream functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
16 ;;; The initialization of these streams is performed by
17 ;;; STREAM-COLD-INIT-OR-RESET.
18 (defvar *terminal-io* () #!+sb-doc "terminal I/O stream")
19 (defvar *standard-input* () #!+sb-doc "default input stream")
20 (defvar *standard-output* () #!+sb-doc "default output stream")
21 (defvar *error-output* () #!+sb-doc "error output stream")
22 (defvar *query-io* () #!+sb-doc "query I/O stream")
23 (defvar *trace-output* () #!+sb-doc "trace output stream")
24 (defvar *debug-io* () #!+sb-doc "interactive debugging stream")
26 (defun ill-in (stream &rest ignore)
27 (declare (ignore ignore))
28 (error 'simple-type-error
30 :expected-type '(satisfies input-stream-p)
31 :format-control "~S is not a character input stream."
32 :format-arguments (list stream)))
33 (defun ill-out (stream &rest ignore)
34 (declare (ignore ignore))
35 (error 'simple-type-error
37 :expected-type '(satisfies output-stream-p)
38 :format-control "~S is not a character output stream."
39 :format-arguments (list stream)))
40 (defun ill-bin (stream &rest ignore)
41 (declare (ignore ignore))
42 (error 'simple-type-error
44 :expected-type '(satisfies input-stream-p)
45 :format-control "~S is not a binary input stream."
46 :format-arguments (list stream)))
47 (defun ill-bout (stream &rest ignore)
48 (declare (ignore ignore))
49 (error 'simple-type-error
51 :expected-type '(satisfies output-stream-p)
52 :format-control "~S is not a binary output stream."
53 :format-arguments (list stream)))
54 (defun closed-flame (stream &rest ignore)
55 (declare (ignore ignore))
56 (error "~S is closed." stream))
57 (defun no-op-placeholder (&rest ignore)
58 (declare (ignore ignore)))
60 ;;; stream manipulation functions
62 (declaim (inline ansi-stream-input-stream-p))
63 (defun ansi-stream-input-stream-p (stream)
64 (declare (type ansi-stream stream))
66 (when (synonym-stream-p stream)
68 (symbol-value (synonym-stream-symbol stream))))
70 (and (not (eq (ansi-stream-in stream) #'closed-flame))
71 ;;; KLUDGE: It's probably not good to have EQ tests on function
72 ;;; values like this. What if someone's redefined the function?
73 ;;; Is there a better way? (Perhaps just VALID-FOR-INPUT and
74 ;;; VALID-FOR-OUTPUT flags? -- WHN 19990902
75 (or (not (eq (ansi-stream-in stream) #'ill-in))
76 (not (eq (ansi-stream-bin stream) #'ill-bin)))))
78 (defun input-stream-p (stream)
79 (declare (type stream stream))
80 (and (ansi-stream-p stream)
81 (ansi-stream-input-stream-p stream)))
83 (declaim (inline ansi-stream-output-stream-p))
84 (defun ansi-stream-output-stream-p (stream)
85 (declare (type ansi-stream stream))
87 (when (synonym-stream-p stream)
88 (setf stream (symbol-value
89 (synonym-stream-symbol stream))))
91 (and (not (eq (ansi-stream-in stream) #'closed-flame))
92 (or (not (eq (ansi-stream-out stream) #'ill-out))
93 (not (eq (ansi-stream-bout stream) #'ill-bout)))))
95 (defun output-stream-p (stream)
96 (declare (type stream stream))
98 (and (ansi-stream-p stream)
99 (ansi-stream-output-stream-p stream)))
101 (declaim (inline ansi-stream-open-stream-p))
102 (defun ansi-stream-open-stream-p (stream)
103 (declare (type ansi-stream stream))
104 ;; CLHS 22.1.4 lets us not worry about synonym streams here.
105 (not (eq (ansi-stream-in stream) #'closed-flame)))
107 (defun open-stream-p (stream)
108 (ansi-stream-open-stream-p stream))
110 (declaim (inline ansi-stream-element-type))
111 (defun ansi-stream-element-type (stream)
112 (declare (type ansi-stream stream))
113 (funcall (ansi-stream-misc stream) stream :element-type))
115 (defun stream-element-type (stream)
116 (ansi-stream-element-type stream))
118 (defun interactive-stream-p (stream)
119 (declare (type stream stream))
120 (funcall (ansi-stream-misc stream) stream :interactive-p))
122 (declaim (inline ansi-stream-close))
123 (defun ansi-stream-close (stream abort)
124 (declare (type ansi-stream stream))
125 (when (open-stream-p stream)
126 (funcall (ansi-stream-misc stream) stream :close abort))
129 (defun close (stream &key abort)
130 (ansi-stream-close stream abort))
132 (defun set-closed-flame (stream)
133 (setf (ansi-stream-in stream) #'closed-flame)
134 (setf (ansi-stream-bin stream) #'closed-flame)
135 (setf (ansi-stream-n-bin stream) #'closed-flame)
136 (setf (ansi-stream-in stream) #'closed-flame)
137 (setf (ansi-stream-out stream) #'closed-flame)
138 (setf (ansi-stream-bout stream) #'closed-flame)
139 (setf (ansi-stream-sout stream) #'closed-flame)
140 (setf (ansi-stream-misc stream) #'closed-flame))
142 ;;;; file position and file length
144 ;;; Call the MISC method with the :FILE-POSITION operation.
145 (defun file-position (stream &optional position)
146 (declare (type stream stream))
147 (declare (type (or index (alien sb!unix:off-t) (member nil :start :end)) position))
150 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
151 (funcall (ansi-stream-misc stream) stream :file-position position))
153 (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
156 (- +ansi-stream-in-buffer-length+
157 (ansi-stream-in-index stream))))))))
159 ;;; This is a literal translation of the ANSI glossary entry "stream
160 ;;; associated with a file".
162 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
163 ;;; file", and in particular stdin, stdout, and stderr are files, we
164 ;;; end up with this test being satisfied for weird things like
165 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
166 ;;; ANSI spec really had in mind, especially since this is used as a
167 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
168 ;;; probably thinking of something like what Unix calls block devices)
169 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
170 (defun stream-associated-with-file-p (x)
171 "Test for the ANSI concept \"stream associated with a file\"."
172 (or (typep x 'file-stream)
173 (and (synonym-stream-p x)
174 (stream-associated-with-file-p (symbol-value
175 (synonym-stream-symbol x))))))
177 (defun stream-must-be-associated-with-file (stream)
178 (declare (type stream stream))
179 (unless (stream-associated-with-file-p stream)
180 (error 'simple-type-error
181 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
182 ;; this should be TYPE-ERROR. But what then can we use for
183 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
184 ;; private predicate function..) is ugly and confusing, but
185 ;; I can't see any other way. -- WHN 2001-04-14
186 :expected-type '(satisfies stream-associated-with-file-p)
188 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
189 :format-arguments (list stream))))
191 ;;; like FILE-POSITION, only using :FILE-LENGTH
192 (defun file-length (stream)
193 ;; FIXME: The following declaration uses yet undefined types, which
194 ;; cause cross-compiler hangup.
196 ;; (declare (type (or file-stream synonym-stream) stream))
197 (stream-must-be-associated-with-file stream)
198 (funcall (ansi-stream-misc stream) stream :file-length))
202 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
204 (declare (ignore recursive-p))
205 (let ((stream (in-synonym-of stream)))
206 (if (ansi-stream-p stream)
207 (prepare-for-fast-read-char stream
208 (let ((res (make-string 80))
212 (let ((ch (fast-read-char nil nil)))
214 (when (char= ch #\newline)
215 (done-with-fast-read-char)
216 (return (values (shrink-vector res index) nil)))
219 (let ((new (make-string len)))
222 (setf (schar res index) ch)
225 (done-with-fast-read-char)
226 (return (values (eof-or-lose stream
230 ;; Since FAST-READ-CHAR already hit the eof char, we
231 ;; shouldn't do another READ-CHAR.
233 (done-with-fast-read-char)
234 (return (values (shrink-vector res index) t))))))))
235 ;; must be Gray streams FUNDAMENTAL-STREAM
236 (multiple-value-bind (string eof) (stream-read-line stream)
237 (if (and eof (zerop (length string)))
238 (values (eof-or-lose stream eof-error-p eof-value) t)
239 (values string eof))))))
241 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
242 ;;; so, except in this file, they are not inline by default, but they can be.
243 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
245 (defun read-char (&optional (stream *standard-input*)
249 (declare (ignore recursive-p))
250 (let ((stream (in-synonym-of stream)))
251 (if (ansi-stream-p stream)
252 (prepare-for-fast-read-char stream
254 (fast-read-char eof-error-p eof-value)
255 (done-with-fast-read-char)))
256 ;; must be Gray streams FUNDAMENTAL-STREAM
257 (let ((char (stream-read-char stream)))
259 (eof-or-lose stream eof-error-p eof-value)
262 (defun unread-char (character &optional (stream *standard-input*))
263 (let ((stream (in-synonym-of stream)))
264 (if (ansi-stream-p stream)
265 (let ((index (1- (ansi-stream-in-index stream)))
266 (buffer (ansi-stream-in-buffer stream)))
267 (declare (fixnum index))
268 (when (minusp index) (error "nothing to unread"))
270 (setf (aref buffer index) (char-code character))
271 (setf (ansi-stream-in-index stream) index))
273 (funcall (ansi-stream-misc stream) stream
274 :unread character))))
275 ;; must be Gray streams FUNDAMENTAL-STREAM
276 (stream-unread-char stream character)))
279 (defun listen (&optional (stream *standard-input*))
280 (let ((stream (in-synonym-of stream)))
281 (if (ansi-stream-p stream)
282 (or (/= (the fixnum (ansi-stream-in-index stream))
283 +ansi-stream-in-buffer-length+)
284 ;; Test for T explicitly since misc methods return :EOF sometimes.
285 (eq (funcall (ansi-stream-misc stream) stream :listen) t))
286 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
287 (stream-listen stream))))
289 (defun read-char-no-hang (&optional (stream *standard-input*)
293 (declare (ignore recursive-p))
294 (let ((stream (in-synonym-of stream)))
295 (if (ansi-stream-p stream)
296 (if (funcall (ansi-stream-misc stream) stream :listen)
297 ;; On T or :EOF get READ-CHAR to do the work.
298 (read-char stream eof-error-p eof-value)
300 ;; must be Gray streams FUNDAMENTAL-STREAM
301 (let ((char (stream-read-char-no-hang stream)))
303 (eof-or-lose stream eof-error-p eof-value)
306 (defun clear-input (&optional (stream *standard-input*))
307 (let ((stream (in-synonym-of stream)))
308 (cond ((ansi-stream-p stream)
309 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
310 (funcall (ansi-stream-misc stream) stream :clear-input))
312 (stream-clear-input stream))))
315 (declaim (maybe-inline read-byte))
316 (defun read-byte (stream &optional (eof-error-p t) eof-value)
317 (let ((stream (in-synonym-of stream)))
318 (if (ansi-stream-p stream)
319 (prepare-for-fast-read-byte stream
321 (fast-read-byte eof-error-p eof-value t)
322 (done-with-fast-read-byte)))
323 ;; must be Gray streams FUNDAMENTAL-STREAM
324 (let ((char (stream-read-byte stream)))
326 (eof-or-lose stream eof-error-p eof-value)
329 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
330 ;;; number of bytes read.
332 ;;; Note: CMU CL's version of this had a special interpretation of
333 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
334 ;;; case, CMU CL's version would return as soon as any data became
335 ;;; available.) This could be useful behavior for things like pipes in
336 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
337 ;;; If we ever need it, it could be added later as a new variant N-BIN
338 ;;; method (perhaps N-BIN-ASAP?) or something.
339 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
340 (declare (type ansi-stream stream)
341 (type index numbytes start)
342 (type (or (simple-array * (*)) system-area-pointer) buffer))
343 (let* ((stream (in-synonym-of stream ansi-stream))
344 (in-buffer (ansi-stream-in-buffer stream))
345 (index (ansi-stream-in-index stream))
346 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
347 (declare (fixnum index num-buffered))
350 (funcall (ansi-stream-n-bin stream)
356 ((<= numbytes num-buffered)
357 (%byte-blt in-buffer index
358 buffer start (+ start numbytes))
359 (setf (ansi-stream-in-index stream) (+ index numbytes))
362 (let ((end (+ start num-buffered)))
363 (%byte-blt in-buffer index buffer start end)
364 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
365 (+ (funcall (ansi-stream-n-bin stream)
369 (- numbytes num-buffered)
373 ;;; the amount of space we leave at the start of the in-buffer for
376 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
377 (defconstant +ansi-stream-in-buffer-extra+
378 4) ; FIXME: should be symbolic constant
380 ;;; This function is called by the FAST-READ-CHAR expansion to refill
381 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
382 ;;; and hence must be an N-BIN method.
383 (defun fast-read-char-refill (stream eof-error-p eof-value)
384 (let* ((ibuf (ansi-stream-in-buffer stream))
385 (count (funcall (ansi-stream-n-bin stream)
388 +ansi-stream-in-buffer-extra+
389 (- +ansi-stream-in-buffer-length+
390 +ansi-stream-in-buffer-extra+)
392 (start (- +ansi-stream-in-buffer-length+ count)))
393 (declare (type index start count))
395 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
396 (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
398 (when (/= start +ansi-stream-in-buffer-extra+)
399 (bit-bash-copy ibuf (+ (* +ansi-stream-in-buffer-extra+
401 (* sb!vm:vector-data-offset
403 ibuf (+ (the index (* start sb!vm:n-byte-bits))
404 (* sb!vm:vector-data-offset
406 (* count sb!vm:n-byte-bits)))
407 (setf (ansi-stream-in-index stream) (1+ start))
408 (code-char (aref ibuf start))))))
410 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
411 ;;; leave room for unreading.
412 (defun fast-read-byte-refill (stream eof-error-p eof-value)
413 (let* ((ibuf (ansi-stream-in-buffer stream))
414 (count (funcall (ansi-stream-n-bin stream) stream
415 ibuf 0 +ansi-stream-in-buffer-length+
417 (start (- +ansi-stream-in-buffer-length+ count)))
418 (declare (type index start count))
420 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
421 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
423 (unless (zerop start)
424 (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:n-word-bits)
425 ibuf (+ (the index (* start sb!vm:n-byte-bits))
426 (* sb!vm:vector-data-offset
428 (* count sb!vm:n-byte-bits)))
429 (setf (ansi-stream-in-index stream) (1+ start))
430 (aref ibuf start)))))
434 (defun write-char (character &optional (stream *standard-output*))
435 (with-out-stream stream (ansi-stream-out character)
436 (stream-write-char character))
439 (defun terpri (&optional (stream *standard-output*))
440 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
443 (defun fresh-line (&optional (stream *standard-output*))
444 (let ((stream (out-synonym-of stream)))
445 (if (ansi-stream-p stream)
446 (when (/= (or (charpos stream) 1) 0)
447 (funcall (ansi-stream-out stream) stream #\newline)
449 ;; must be Gray streams FUNDAMENTAL-STREAM
450 (stream-fresh-line stream))))
452 (defun write-string (string &optional (stream *standard-output*)
454 (declare (type string string))
455 ;; Note that even though you might expect, based on the behavior of
456 ;; things like AREF, that the correct upper bound here is
457 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
458 ;; "bounding index" and "length" indicate that in this case (i.e.
459 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
460 ;; (LENGTH STRING) is the required upper bound. A foolish
461 ;; consistency is the hobgoblin of lesser languages..
462 (%write-string string stream start (%check-vector-sequence-bounds
466 (defun %write-string (string stream start end)
467 (declare (type string string))
468 (declare (type streamlike stream))
469 (declare (type index start end))
470 (let ((stream (out-synonym-of stream)))
471 (cond ((ansi-stream-p stream)
472 (if (array-header-p string)
473 (with-array-data ((data string) (offset-start start)
475 (funcall (ansi-stream-sout stream)
476 stream data offset-start offset-end))
477 (funcall (ansi-stream-sout stream) stream string start end))
479 (t ; must be Gray streams FUNDAMENTAL-STREAM
480 (stream-write-string stream string start end)))))
482 ;;; A wrapper function for all those (MACROLET OUT-FUN) definitions,
483 ;;; which cannot deal with keyword arguments.
484 (declaim (inline write-string-no-key))
485 (defun write-string-no-key (string stream start end)
486 (write-string string stream :start start :end end))
488 (defun write-line (string &optional (stream *standard-output*)
490 (declare (type string string))
491 ;; FIXME: Why is there this difference between the treatments of the
492 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
493 (let ((defaulted-stream (out-synonym-of stream)))
494 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
496 (write-char #\newline defaulted-stream))
499 (defun charpos (&optional (stream *standard-output*))
500 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
502 (defun line-length (&optional (stream *standard-output*))
503 (with-out-stream stream (ansi-stream-misc :line-length)
504 (stream-line-length)))
506 (defun finish-output (&optional (stream *standard-output*))
507 (with-out-stream stream (ansi-stream-misc :finish-output)
508 (stream-finish-output))
511 (defun force-output (&optional (stream *standard-output*))
512 (with-out-stream stream (ansi-stream-misc :force-output)
513 (stream-force-output))
516 (defun clear-output (&optional (stream *standard-output*))
517 (with-out-stream stream (ansi-stream-misc :clear-output)
518 (stream-force-output))
521 (defun write-byte (integer stream)
522 (with-out-stream stream (ansi-stream-bout integer)
523 (stream-write-byte integer))
527 ;;; (These were inline throughout this file, but that's not appropriate
528 ;;; globally. And we must not inline them in the rest of this file if
529 ;;; dispatch to gray or simple streams is to work, since both redefine
530 ;;; these functions later.)
531 (declaim (maybe-inline read-char unread-char read-byte listen))
533 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
534 ;;; streams to handle the misc routines and dispatch to the
535 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
536 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
537 (declare (type stream stream) (ignore arg2))
540 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
541 (let ((char (read-char-no-hang stream nil :eof)))
542 (when (characterp char)
543 (unread-char char stream))
546 (unread-char arg1 stream))
550 (clear-input stream))
552 (force-output stream))
554 (finish-output stream))
556 (stream-element-type stream))
558 (interactive-stream-p stream))
560 (line-length stream))
564 (file-length stream))
566 (file-position stream arg1))))
568 ;;;; broadcast streams
570 (defstruct (broadcast-stream (:include ansi-stream
571 (out #'broadcast-out)
572 (bout #'broadcast-bout)
573 (sout #'broadcast-sout)
574 (misc #'broadcast-misc))
575 (:constructor %make-broadcast-stream
578 ;; a list of all the streams we broadcast to
579 (streams () :type list :read-only t))
581 (defun make-broadcast-stream (&rest streams)
582 (dolist (stream streams)
583 (unless (output-stream-p stream)
586 :expected-type '(satisfies output-stream-p))))
587 (apply #'%make-broadcast-stream streams))
589 (macrolet ((out-fun (name fun &rest args)
590 `(defun ,name (stream ,@args)
591 (dolist (stream (broadcast-stream-streams stream))
592 (,fun ,(car args) stream ,@(cdr args))))))
593 (out-fun broadcast-out write-char char)
594 (out-fun broadcast-bout write-byte byte)
595 (out-fun broadcast-sout write-string-no-key string start end))
597 (defun broadcast-misc (stream operation &optional arg1 arg2)
598 (let ((streams (broadcast-stream-streams stream)))
600 ;; FIXME: This may not be the best place to note this, but I
601 ;; think the :CHARPOS protocol needs revision. Firstly, I think
602 ;; this is the last place where a NULL return value was possible
603 ;; (before adjusting it to be 0), so a bunch of conditionals IF
604 ;; CHARPOS can be removed; secondly, it is my belief that
605 ;; FD-STREAMS, when running FILE-POSITION, do not update the
606 ;; CHARPOS, and consequently there will be much wrongness.
608 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
609 ;; is it testing the :charpos of an input stream?
611 ;; -- CSR, 2004-02-04
613 (dolist (stream streams 0)
614 (let ((charpos (charpos stream)))
615 (if charpos (return charpos)))))
618 (dolist (stream streams min)
619 (let ((res (line-length stream)))
620 (when res (setq min (if min (min res min) res)))))))
622 #+nil ; old, arguably more logical, version
624 (dolist (stream streams (if (> (length res) 1) `(and ,@res) t))
625 (pushnew (stream-element-type stream) res :test #'equal)))
626 ;; ANSI-specified version (under System Class BROADCAST-STREAM)
628 (do ((streams streams (cdr streams)))
630 (when (null (cdr streams))
631 (setq res (stream-element-type (car streams)))))))
633 (set-closed-flame stream))
636 (dolist (stream streams res)
638 (if (ansi-stream-p stream)
639 (funcall (ansi-stream-misc stream) stream operation
641 (stream-misc-dispatch stream operation arg1 arg2)))))))))
645 (defstruct (synonym-stream (:include ansi-stream
648 (n-bin #'synonym-n-bin)
650 (bout #'synonym-bout)
651 (sout #'synonym-sout)
652 (misc #'synonym-misc))
653 (:constructor make-synonym-stream (symbol))
655 ;; This is the symbol, the value of which is the stream we are synonym to.
656 (symbol nil :type symbol :read-only t))
657 (def!method print-object ((x synonym-stream) stream)
658 (print-unreadable-object (x stream :type t :identity t)
659 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
661 ;;; The output simple output methods just call the corresponding
662 ;;; function on the synonymed stream.
663 (macrolet ((out-fun (name fun &rest args)
664 `(defun ,name (stream ,@args)
665 (declare (optimize (safety 1)))
666 (let ((syn (symbol-value (synonym-stream-symbol stream))))
667 (,fun ,(car args) syn ,@(cdr args))))))
668 (out-fun synonym-out write-char ch)
669 (out-fun synonym-bout write-byte n)
670 (out-fun synonym-sout write-string-no-key string start end))
672 ;;; For the input methods, we just call the corresponding function on the
673 ;;; synonymed stream. These functions deal with getting input out of
674 ;;; the In-Buffer if there is any.
675 (macrolet ((in-fun (name fun &rest args)
676 `(defun ,name (stream ,@args)
677 (declare (optimize (safety 1)))
678 (,fun (symbol-value (synonym-stream-symbol stream))
680 (in-fun synonym-in read-char eof-error-p eof-value)
681 (in-fun synonym-bin read-byte eof-error-p eof-value)
682 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
684 (defun synonym-misc (stream operation &optional arg1 arg2)
685 (declare (optimize (safety 1)))
686 (let ((syn (symbol-value (synonym-stream-symbol stream))))
687 (if (ansi-stream-p syn)
688 ;; We have to special-case some operations which interact with
689 ;; the in-buffer of the wrapped stream, since just calling
690 ;; ANSI-STREAM-MISC on them
692 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
693 +ansi-stream-in-buffer-length+)
694 (funcall (ansi-stream-misc syn) syn :listen)))
695 (:clear-input (clear-input syn))
696 (:unread (unread-char arg1 syn))
698 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
699 (stream-misc-dispatch syn operation arg1 arg2))))
703 (defstruct (two-way-stream
704 (:include ansi-stream
707 (n-bin #'two-way-n-bin)
709 (bout #'two-way-bout)
710 (sout #'two-way-sout)
711 (misc #'two-way-misc))
712 (:constructor %make-two-way-stream (input-stream output-stream))
714 (input-stream (missing-arg) :type stream :read-only t)
715 (output-stream (missing-arg) :type stream :read-only t))
716 (defprinter (two-way-stream) input-stream output-stream)
718 (defun make-two-way-stream (input-stream output-stream)
720 "Return a bidirectional stream which gets its input from INPUT-STREAM and
721 sends its output to OUTPUT-STREAM."
722 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
723 ;; should be encapsulated in a function, and used here and most of
724 ;; the other places that SYNONYM-STREAM-P appears.
725 (unless (output-stream-p output-stream)
728 :expected-type '(satisfies output-stream-p)))
729 (unless (input-stream-p input-stream)
732 :expected-type '(satisfies input-stream-p)))
733 (funcall #'%make-two-way-stream input-stream output-stream))
735 (macrolet ((out-fun (name fun &rest args)
736 `(defun ,name (stream ,@args)
737 (let ((syn (two-way-stream-output-stream stream)))
738 (,fun ,(car args) syn ,@(cdr args))))))
739 (out-fun two-way-out write-char ch)
740 (out-fun two-way-bout write-byte n)
741 (out-fun two-way-sout write-string-no-key string start end))
743 (macrolet ((in-fun (name fun &rest args)
744 `(defun ,name (stream ,@args)
745 (force-output (two-way-stream-output-stream stream))
746 (,fun (two-way-stream-input-stream stream) ,@args))))
747 (in-fun two-way-in read-char eof-error-p eof-value)
748 (in-fun two-way-bin read-byte eof-error-p eof-value)
749 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
751 (defun two-way-misc (stream operation &optional arg1 arg2)
752 (let* ((in (two-way-stream-input-stream stream))
753 (out (two-way-stream-output-stream stream))
754 (in-ansi-stream-p (ansi-stream-p in))
755 (out-ansi-stream-p (ansi-stream-p out)))
759 (or (/= (the fixnum (ansi-stream-in-index in))
760 +ansi-stream-in-buffer-length+)
761 (funcall (ansi-stream-misc in) in :listen))
763 ((:finish-output :force-output :clear-output)
764 (if out-ansi-stream-p
765 (funcall (ansi-stream-misc out) out operation arg1 arg2)
766 (stream-misc-dispatch out operation arg1 arg2)))
767 (:clear-input (clear-input in))
768 (:unread (unread-char arg1 in))
770 (let ((in-type (stream-element-type in))
771 (out-type (stream-element-type out)))
772 (if (equal in-type out-type)
773 in-type `(and ,in-type ,out-type))))
775 (set-closed-flame stream))
777 (or (if in-ansi-stream-p
778 (funcall (ansi-stream-misc in) in operation arg1 arg2)
779 (stream-misc-dispatch in operation arg1 arg2))
780 (if out-ansi-stream-p
781 (funcall (ansi-stream-misc out) out operation arg1 arg2)
782 (stream-misc-dispatch out operation arg1 arg2)))))))
784 ;;;; concatenated streams
786 (defstruct (concatenated-stream
787 (:include ansi-stream
788 (in #'concatenated-in)
789 (bin #'concatenated-bin)
790 (n-bin #'concatenated-n-bin)
791 (misc #'concatenated-misc))
792 (:constructor %make-concatenated-stream
793 (&rest streams &aux (current streams)))
795 ;; The car of this is the substream we are reading from now.
797 ;; This is a list of all the substreams there ever were. We need to
798 ;; remember them so that we can close them.
800 ;; FIXME: ANSI says this is supposed to be the list of streams that
801 ;; we still have to read from. So either this needs to become a
802 ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
803 ;; around it which discards closed files from the head of the list)
804 ;; or we need to update it as we run out of files.
805 (streams nil :type list :read-only t))
806 (def!method print-object ((x concatenated-stream) stream)
807 (print-unreadable-object (x stream :type t :identity t)
810 (concatenated-stream-streams x))))
812 (defun make-concatenated-stream (&rest streams)
814 "Return a stream which takes its input from each of the streams in turn,
815 going on to the next at EOF."
816 (dolist (stream streams)
817 (unless (input-stream-p stream)
820 :expected-type '(satisfies input-stream-p))))
821 (apply #'%make-concatenated-stream streams))
823 (macrolet ((in-fun (name fun)
824 `(defun ,name (stream eof-error-p eof-value)
825 (do ((current (concatenated-stream-current stream)
828 (eof-or-lose stream eof-error-p eof-value))
829 (let* ((stream (car current))
830 (result (,fun stream nil nil)))
831 (when result (return result)))
832 (pop (concatenated-stream-current stream))))))
833 (in-fun concatenated-in read-char)
834 (in-fun concatenated-bin read-byte))
836 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
837 (do ((current (concatenated-stream-current stream) (cdr current))
838 (current-start start)
839 (remaining-bytes numbytes))
842 (error 'end-of-file :stream stream)
843 (- numbytes remaining-bytes)))
844 (let* ((stream (car current))
845 (bytes-read (read-n-bytes stream buffer current-start
846 remaining-bytes nil)))
847 (incf current-start bytes-read)
848 (decf remaining-bytes bytes-read)
849 (when (zerop remaining-bytes) (return numbytes)))
850 (setf (concatenated-stream-current stream) (cdr current))))
852 (defun concatenated-misc (stream operation &optional arg1 arg2)
853 (let ((left (concatenated-stream-current stream)))
855 (let* ((current (car left)))
859 (let ((stuff (if (ansi-stream-p current)
860 (funcall (ansi-stream-misc current) current
862 (stream-misc-dispatch current :listen))))
863 (cond ((eq stuff :eof)
864 ;; Advance CURRENT, and try again.
865 (pop (concatenated-stream-current stream))
867 (car (concatenated-stream-current stream)))
869 ;; No further streams. EOF.
872 ;; Stuff's available.
875 ;; Nothing is available yet.
877 (:clear-input (clear-input current))
878 (:unread (unread-char arg1 current))
880 (set-closed-flame stream))
882 (if (ansi-stream-p current)
883 (funcall (ansi-stream-misc current) current operation arg1 arg2)
884 (stream-misc-dispatch current operation arg1 arg2))))))))
888 (defstruct (echo-stream
889 (:include two-way-stream
894 (:constructor %make-echo-stream (input-stream output-stream))
897 (def!method print-object ((x echo-stream) stream)
898 (print-unreadable-object (x stream :type t :identity t)
900 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
901 (two-way-stream-input-stream x)
902 (two-way-stream-output-stream x))))
904 (defun make-echo-stream (input-stream output-stream)
906 "Return a bidirectional stream which gets its input from INPUT-STREAM and
907 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
909 (unless (output-stream-p output-stream)
912 :expected-type '(satisfies output-stream-p)))
913 (unless (input-stream-p input-stream)
916 :expected-type '(satisfies input-stream-p)))
917 (funcall #'%make-echo-stream input-stream output-stream))
919 (macrolet ((in-fun (name in-fun out-fun &rest args)
920 `(defun ,name (stream ,@args)
921 (or (pop (echo-stream-unread-stuff stream))
922 (let* ((in (echo-stream-input-stream stream))
923 (out (echo-stream-output-stream stream))
924 (result (,in-fun in ,@args)))
925 (,out-fun result out)
927 (in-fun echo-in read-char write-char eof-error-p eof-value)
928 (in-fun echo-bin read-byte write-byte eof-error-p eof-value))
930 ;;;; base STRING-STREAM stuff
932 (defstruct (string-stream
933 (:include ansi-stream)
936 ;; FIXME: This type declaration is true, and will probably continue
937 ;; to be true. However, note well the comments in DEFTRANSFORM
938 ;; REPLACE, implying that performance of REPLACE is somewhat
939 ;; critical to performance of string streams. If (VECTOR CHARACTER)
940 ;; ever becomes different from (VECTOR BASE-CHAR), the transform
941 ;; probably needs to be extended.
942 (string (missing-arg) :type (vector character)))
944 ;;;; STRING-INPUT-STREAM stuff
946 (defstruct (string-input-stream
947 (:include string-stream
950 (n-bin #'string-stream-read-n-bytes)
951 (misc #'string-in-misc)
952 (string (missing-arg) :type simple-string))
953 (:constructor internal-make-string-input-stream
954 (string current end))
956 (current (missing-arg) :type index)
957 (end (missing-arg) :type index))
959 (defun string-inch (stream eof-error-p eof-value)
960 (declare (type string-input-stream stream))
961 (let ((string (string-input-stream-string stream))
962 (index (string-input-stream-current stream)))
963 (cond ((>= index (the index (string-input-stream-end stream)))
964 (eof-or-lose stream eof-error-p eof-value))
966 (setf (string-input-stream-current stream) (1+ index))
967 (char string index)))))
969 (defun string-binch (stream eof-error-p eof-value)
970 (declare (type string-input-stream stream))
971 (let ((string (string-input-stream-string stream))
972 (index (string-input-stream-current stream)))
973 (cond ((>= index (the index (string-input-stream-end stream)))
974 (eof-or-lose stream eof-error-p eof-value))
976 (setf (string-input-stream-current stream) (1+ index))
977 (char-code (char string index))))))
979 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
980 (declare (type string-input-stream stream)
981 (type index start requested))
982 (let* ((string (string-input-stream-string stream))
983 (index (string-input-stream-current stream))
984 (available (- (string-input-stream-end stream) index))
985 (copy (min available requested)))
986 (declare (type simple-string string))
988 (setf (string-input-stream-current stream)
989 (truly-the index (+ index copy)))
990 (sb!sys:without-gcing
991 (system-area-copy (vector-sap string)
992 (* index sb!vm:n-byte-bits)
993 (if (typep buffer 'system-area-pointer)
996 (* start sb!vm:n-byte-bits)
997 (* copy sb!vm:n-byte-bits))))
998 (if (and (> requested copy) eof-error-p)
999 (error 'end-of-file :stream stream)
1002 (defun string-in-misc (stream operation &optional arg1 arg2)
1003 (declare (type string-input-stream stream)
1008 (setf (string-input-stream-current stream)
1011 (:end (string-input-stream-end stream))
1012 ;; We allow moving position beyond EOF. Errors happen
1013 ;; on read, not move -- or the user may extend the
1016 (string-input-stream-current stream)))
1017 ;; According to ANSI: "Should signal an error of type type-error
1018 ;; if stream is not a stream associated with a file."
1019 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1020 ;; (:file-length (length (string-input-stream-string stream)))
1021 (:unread (decf (string-input-stream-current stream)))
1022 (:listen (or (/= (the index (string-input-stream-current stream))
1023 (the index (string-input-stream-end stream)))
1025 (:element-type (array-element-type (string-input-stream-string stream)))))
1027 (defun make-string-input-stream (string &optional (start 0) end)
1029 "Return an input stream which will supply the characters of STRING between
1030 START and END in order."
1031 (declare (type string string)
1033 (type (or index null) end))
1034 (let ((end (%check-vector-sequence-bounds string start end)))
1035 (with-array-data ((string string) (start start) (end end))
1036 (internal-make-string-input-stream
1037 string ;; now simple
1041 ;;;; STRING-OUTPUT-STREAM stuff
1043 (defstruct (string-output-stream
1044 (:include string-stream
1046 (sout #'string-sout)
1047 (misc #'string-out-misc)
1048 ;; The string we throw stuff in.
1049 (string (missing-arg)
1050 :type (simple-array character (*))))
1051 (:constructor make-string-output-stream
1052 (&key (element-type 'character)
1053 &aux (string (make-string 40))))
1055 ;; Index of the next location to use.
1056 (index 0 :type fixnum)
1057 ;; Index cache for string-output-stream-last-index
1058 (index-cache 0 :type fixnum)
1059 ;; Requested element type
1060 (element-type 'character))
1063 (setf (fdocumentation 'make-string-output-stream 'function)
1064 "Return an output stream which will accumulate all output given it for
1065 the benefit of the function GET-OUTPUT-STREAM-STRING.")
1067 (defun string-output-stream-last-index (stream)
1068 (max (string-output-stream-index stream)
1069 (string-output-stream-index-cache stream)))
1071 (defun string-ouch (stream character)
1072 (let ((current (string-output-stream-index stream))
1073 (workspace (string-output-stream-string stream)))
1074 (declare (type (simple-array character (*)) workspace)
1075 (type fixnum current))
1076 (if (= current (the fixnum (length workspace)))
1077 (let ((new-workspace (make-string (* current 2))))
1078 (replace new-workspace workspace)
1079 (setf (aref new-workspace current) character
1080 (string-output-stream-string stream) new-workspace))
1081 (setf (aref workspace current) character))
1082 (setf (string-output-stream-index stream) (1+ current))))
1084 (defun string-sout (stream string start end)
1085 (declare (type simple-string string)
1086 (type fixnum start end))
1087 (let* ((string (if (typep string '(simple-array character (*)))
1089 (coerce string '(simple-array character (*)))))
1090 (current (string-output-stream-index stream))
1091 (length (- end start))
1092 (dst-end (+ length current))
1093 (workspace (string-output-stream-string stream)))
1094 (declare (type (simple-array character (*)) workspace string)
1095 (type fixnum current length dst-end))
1096 (if (> dst-end (the fixnum (length workspace)))
1097 (let ((new-workspace (make-string (+ (* current 2) length))))
1098 (replace new-workspace workspace :end2 current)
1099 (replace new-workspace string
1100 :start1 current :end1 dst-end
1101 :start2 start :end2 end)
1102 (setf (string-output-stream-string stream) new-workspace))
1103 (replace workspace string
1104 :start1 current :end1 dst-end
1105 :start2 start :end2 end))
1106 (setf (string-output-stream-index stream) dst-end)))
1108 (defun string-out-misc (stream operation &optional arg1 arg2)
1109 (declare (ignore arg2))
1113 (let ((end (string-output-stream-last-index stream)))
1114 (setf (string-output-stream-index-cache stream) end
1115 (string-output-stream-index stream)
1120 ;; We allow moving beyond the end of stream,
1121 ;; implicitly extending the output stream.
1122 (let ((buffer (string-output-stream-string stream)))
1123 (when (> arg1 (length buffer))
1124 (setf (string-output-stream-string stream)
1126 arg1 :element-type (array-element-type buffer))
1127 (subseq (string-output-stream-string stream)
1129 (subseq buffer 0 end))))
1131 (string-output-stream-index stream)))
1133 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1135 (count 0 (1+ count))
1136 (string (string-output-stream-string stream)))
1138 (declare (type (simple-array character (*)) string)
1139 (type fixnum index count))
1140 (if (char= (schar string index) #\newline)
1142 (:element-type (array-element-type (string-output-stream-string stream)))))
1144 ;;; Return a string of all the characters sent to a stream made by
1145 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1146 (defun get-output-stream-string (stream)
1147 (declare (type string-output-stream stream))
1148 (let* ((length (string-output-stream-last-index stream))
1149 (element-type (string-output-stream-element-type stream))
1152 ;; Overwhelmingly common case; can be inlined.
1153 ((character) (make-string length))
1154 (t (make-string length :element-type element-type)))))
1155 ;; For the benefit of the REPLACE transform, let's do this, so
1156 ;; that the common case isn't ludicrously expensive.
1158 ((simple-array character (*))
1159 (replace result (string-output-stream-string stream)))
1160 ((simple-array nil (*))
1161 (replace result (string-output-stream-string stream))))
1162 (setf (string-output-stream-index stream) 0
1163 (string-output-stream-index-cache stream) 0)
1166 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1167 ;;; GET-OUTPUT-STREAM-STRING would return them.
1168 (defun dump-output-stream-string (in-stream out-stream)
1169 (%write-string (string-output-stream-string in-stream)
1172 (string-output-stream-last-index in-stream))
1173 (setf (string-output-stream-index in-stream) 0
1174 (string-output-stream-index-cache in-stream) 0))
1176 ;;;; fill-pointer streams
1178 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1179 ;;; the CLM, but they are required for the implementation of
1180 ;;; WITH-OUTPUT-TO-STRING.
1182 (deftype string-with-fill-pointer ()
1183 '(and (vector character)
1184 (satisfies array-has-fill-pointer-p)))
1186 (defstruct (fill-pointer-output-stream
1187 (:include string-stream
1188 (out #'fill-pointer-ouch)
1189 (sout #'fill-pointer-sout)
1190 (misc #'fill-pointer-misc)
1191 ;; a string with a fill pointer where we stuff
1192 ;; the stuff we write
1193 (string (missing-arg)
1194 :type string-with-fill-pointer
1196 (:constructor make-fill-pointer-output-stream (string))
1199 (defun fill-pointer-ouch (stream character)
1200 (let* ((buffer (fill-pointer-output-stream-string stream))
1201 (current (fill-pointer buffer))
1202 (current+1 (1+ current)))
1203 (declare (fixnum current))
1204 (with-array-data ((workspace buffer) (start) (end))
1205 (declare (type (simple-array character (*)) workspace))
1206 (let ((offset-current (+ start current)))
1207 (declare (fixnum offset-current))
1208 (if (= offset-current end)
1209 (let* ((new-length (1+ (* current 2)))
1210 (new-workspace (make-string new-length)))
1211 (declare (simple-string new-workspace))
1212 (%byte-blt workspace start
1213 new-workspace 0 current)
1214 (setf workspace new-workspace
1215 offset-current current)
1216 (set-array-header buffer workspace new-length
1217 current+1 0 new-length nil))
1218 (setf (fill-pointer buffer) current+1))
1219 (setf (schar workspace offset-current) character)))
1222 (defun fill-pointer-sout (stream string start end)
1223 (declare (simple-string string) (fixnum start end))
1224 (let* ((string (if (typep string '(simple-array character (*)))
1226 (coerce string '(simple-array character (*)))))
1227 (buffer (fill-pointer-output-stream-string stream))
1228 (current (fill-pointer buffer))
1229 (string-len (- end start))
1230 (dst-end (+ string-len current)))
1231 (declare (fixnum current dst-end string-len))
1232 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1233 (declare (type (simple-array character (*)) workspace))
1234 (let ((offset-dst-end (+ dst-start dst-end))
1235 (offset-current (+ dst-start current)))
1236 (declare (fixnum offset-dst-end offset-current))
1237 (if (> offset-dst-end dst-length)
1238 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1239 (new-workspace (make-string new-length)))
1240 (declare (type (simple-array character (*)) new-workspace))
1241 (%byte-blt workspace dst-start
1242 new-workspace 0 current)
1243 (setf workspace new-workspace)
1244 (setf offset-current current)
1245 (setf offset-dst-end dst-end)
1246 (set-array-header buffer
1253 (setf (fill-pointer buffer) dst-end))
1254 (%byte-blt string start
1255 workspace offset-current offset-dst-end)))
1258 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1259 (declare (ignore arg1 arg2))
1262 (let ((buffer (fill-pointer-output-stream-string stream)))
1264 (setf (fill-pointer buffer)
1267 ;; Fill-pointer is always at fill-pointer we will
1268 ;; make :END move to the end of the actual string.
1269 (:end (array-total-size buffer))
1270 ;; We allow moving beyond the end of string if the
1271 ;; string is adjustable.
1272 (t (when (>= arg1 (array-total-size buffer))
1273 (if (adjustable-array-p buffer)
1274 (adjust-array buffer arg1)
1275 (error "Cannot move FILE-POSITION beyond the end ~
1276 of WITH-OUTPUT-TO-STRING stream ~
1277 constructed with non-adjustable string.")))
1279 (fill-pointer buffer))))
1281 (let* ((buffer (fill-pointer-output-stream-string stream))
1282 (current (fill-pointer buffer)))
1283 (with-array-data ((string buffer) (start) (end current))
1284 (declare (simple-string string) (ignore start))
1285 (let ((found (position #\newline string :test #'char=
1286 :end end :from-end t)))
1288 (- end (the fixnum found))
1290 (:element-type (array-element-type
1291 (fill-pointer-output-stream-string stream)))))
1293 ;;;; indenting streams
1295 (defstruct (indenting-stream (:include ansi-stream
1296 (out #'indenting-out)
1297 (sout #'indenting-sout)
1298 (misc #'indenting-misc))
1299 (:constructor make-indenting-stream (stream))
1301 ;; the stream we're based on
1303 ;; how much we indent on each line
1307 (setf (fdocumentation 'make-indenting-stream 'function)
1308 "Return an output stream which indents its output by some amount.")
1310 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1311 ;;; output on the given STREAM based on the specified SUB-STREAM.
1312 (defmacro indenting-indent (stream sub-stream)
1313 ;; KLUDGE: bare magic number 60
1314 `(do ((i 0 (+ i 60))
1315 (indentation (indenting-stream-indentation ,stream)))
1316 ((>= i indentation))
1321 (min 60 (- indentation i)))))
1323 ;;; INDENTING-OUT writes a character to an indenting stream.
1324 (defun indenting-out (stream char)
1325 (let ((sub-stream (indenting-stream-stream stream)))
1326 (write-char char sub-stream)
1327 (if (char= char #\newline)
1328 (indenting-indent stream sub-stream))))
1330 ;;; INDENTING-SOUT writes a string to an indenting stream.
1331 (defun indenting-sout (stream string start end)
1332 (declare (simple-string string) (fixnum start end))
1334 (sub-stream (indenting-stream-stream stream)))
1336 (let ((newline (position #\newline string :start i :end end)))
1338 (%write-string string sub-stream i (1+ newline))
1339 (indenting-indent stream sub-stream)
1340 (setq i (+ newline 1)))
1342 (%write-string string sub-stream i end)
1345 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1346 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1347 ;;; the base stream minus the stream's indentation.
1348 (defun indenting-misc (stream operation &optional arg1 arg2)
1349 (let ((sub-stream (indenting-stream-stream stream)))
1350 (if (ansi-stream-p sub-stream)
1351 (let ((method (ansi-stream-misc sub-stream)))
1354 (let ((line-length (funcall method sub-stream operation)))
1356 (- line-length (indenting-stream-indentation stream)))))
1358 (let ((charpos (funcall method sub-stream operation)))
1360 (- charpos (indenting-stream-indentation stream)))))
1362 (funcall method sub-stream operation arg1 arg2))))
1363 ;; must be Gray streams FUNDAMENTAL-STREAM
1366 (let ((line-length (stream-line-length sub-stream)))
1368 (- line-length (indenting-stream-indentation stream)))))
1370 (let ((charpos (stream-line-column sub-stream)))
1372 (- charpos (indenting-stream-indentation stream)))))
1374 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1376 (declaim (maybe-inline read-char unread-char read-byte listen))
1378 ;;;; case frobbing streams, used by FORMAT ~(...~)
1380 (defstruct (case-frob-stream
1381 (:include ansi-stream
1382 (misc #'case-frob-misc))
1383 (:constructor %make-case-frob-stream (target out sout))
1385 (target (missing-arg) :type stream))
1387 (defun make-case-frob-stream (target kind)
1389 "Return a stream that sends all output to the stream TARGET, but modifies
1390 the case of letters, depending on KIND, which should be one of:
1391 :upcase - convert to upper case.
1392 :downcase - convert to lower case.
1393 :capitalize - convert the first letter of words to upper case and the
1394 rest of the word to lower case.
1395 :capitalize-first - convert the first letter of the first word to upper
1396 case and everything else to lower case."
1397 (declare (type stream target)
1398 (type (member :upcase :downcase :capitalize :capitalize-first)
1401 (if (case-frob-stream-p target)
1402 ;; If we are going to be writing to a stream that already does
1403 ;; case frobbing, why bother frobbing the case just so it can
1406 (multiple-value-bind (out sout)
1409 (values #'case-frob-upcase-out
1410 #'case-frob-upcase-sout))
1412 (values #'case-frob-downcase-out
1413 #'case-frob-downcase-sout))
1415 (values #'case-frob-capitalize-out
1416 #'case-frob-capitalize-sout))
1418 (values #'case-frob-capitalize-first-out
1419 #'case-frob-capitalize-first-sout)))
1420 (%make-case-frob-stream target out sout))))
1422 (defun case-frob-misc (stream op &optional arg1 arg2)
1423 (declare (type case-frob-stream stream))
1426 (set-closed-flame stream))
1428 (let ((target (case-frob-stream-target stream)))
1429 (if (ansi-stream-p target)
1430 (funcall (ansi-stream-misc target) target op arg1 arg2)
1431 (stream-misc-dispatch target op arg1 arg2))))))
1433 (defun case-frob-upcase-out (stream char)
1434 (declare (type case-frob-stream stream)
1435 (type base-char char))
1436 (let ((target (case-frob-stream-target stream))
1437 (char (char-upcase char)))
1438 (if (ansi-stream-p target)
1439 (funcall (ansi-stream-out target) target char)
1440 (stream-write-char target char))))
1442 (defun case-frob-upcase-sout (stream str start end)
1443 (declare (type case-frob-stream stream)
1444 (type simple-base-string str)
1446 (type (or index null) end))
1447 (let* ((target (case-frob-stream-target stream))
1450 (string (if (and (zerop start) (= len end))
1452 (nstring-upcase (subseq str start end))))
1453 (string-len (- end start)))
1454 (if (ansi-stream-p target)
1455 (funcall (ansi-stream-sout target) target string 0 string-len)
1456 (stream-write-string target string 0 string-len))))
1458 (defun case-frob-downcase-out (stream char)
1459 (declare (type case-frob-stream stream)
1460 (type base-char char))
1461 (let ((target (case-frob-stream-target stream))
1462 (char (char-downcase char)))
1463 (if (ansi-stream-p target)
1464 (funcall (ansi-stream-out target) target char)
1465 (stream-write-char target char))))
1467 (defun case-frob-downcase-sout (stream str start end)
1468 (declare (type case-frob-stream stream)
1469 (type simple-base-string str)
1471 (type (or index null) end))
1472 (let* ((target (case-frob-stream-target stream))
1475 (string (if (and (zerop start) (= len end))
1476 (string-downcase str)
1477 (nstring-downcase (subseq str start end))))
1478 (string-len (- end start)))
1479 (if (ansi-stream-p target)
1480 (funcall (ansi-stream-sout target) target string 0 string-len)
1481 (stream-write-string target string 0 string-len))))
1483 (defun case-frob-capitalize-out (stream char)
1484 (declare (type case-frob-stream stream)
1485 (type base-char char))
1486 (let ((target (case-frob-stream-target stream)))
1487 (cond ((alphanumericp char)
1488 (let ((char (char-upcase char)))
1489 (if (ansi-stream-p target)
1490 (funcall (ansi-stream-out target) target char)
1491 (stream-write-char target char)))
1492 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1493 (setf (case-frob-stream-sout stream)
1494 #'case-frob-capitalize-aux-sout))
1496 (if (ansi-stream-p target)
1497 (funcall (ansi-stream-out target) target char)
1498 (stream-write-char target char))))))
1500 (defun case-frob-capitalize-sout (stream str start end)
1501 (declare (type case-frob-stream stream)
1502 (type simple-base-string str)
1504 (type (or index null) end))
1505 (let* ((target (case-frob-stream-target stream))
1506 (str (subseq str start end))
1510 (let ((char (schar str i)))
1511 (cond ((not (alphanumericp char))
1512 (setf inside-word nil))
1514 (setf (schar str i) (char-downcase char)))
1516 (setf inside-word t)
1517 (setf (schar str i) (char-upcase char))))))
1519 (setf (case-frob-stream-out stream)
1520 #'case-frob-capitalize-aux-out)
1521 (setf (case-frob-stream-sout stream)
1522 #'case-frob-capitalize-aux-sout))
1523 (if (ansi-stream-p target)
1524 (funcall (ansi-stream-sout target) target str 0 len)
1525 (stream-write-string target str 0 len))))
1527 (defun case-frob-capitalize-aux-out (stream char)
1528 (declare (type case-frob-stream stream)
1529 (type base-char char))
1530 (let ((target (case-frob-stream-target stream)))
1531 (cond ((alphanumericp char)
1532 (let ((char (char-downcase char)))
1533 (if (ansi-stream-p target)
1534 (funcall (ansi-stream-out target) target char)
1535 (stream-write-char target char))))
1537 (if (ansi-stream-p target)
1538 (funcall (ansi-stream-out target) target char)
1539 (stream-write-char target char))
1540 (setf (case-frob-stream-out stream)
1541 #'case-frob-capitalize-out)
1542 (setf (case-frob-stream-sout stream)
1543 #'case-frob-capitalize-sout)))))
1545 (defun case-frob-capitalize-aux-sout (stream str start end)
1546 (declare (type case-frob-stream stream)
1547 (type simple-base-string str)
1549 (type (or index null) end))
1550 (let* ((target (case-frob-stream-target stream))
1551 (str (subseq str start end))
1555 (let ((char (schar str i)))
1556 (cond ((not (alphanumericp char))
1557 (setf inside-word nil))
1559 (setf (schar str i) (char-downcase char)))
1561 (setf inside-word t)
1562 (setf (schar str i) (char-upcase char))))))
1564 (setf (case-frob-stream-out stream)
1565 #'case-frob-capitalize-out)
1566 (setf (case-frob-stream-sout stream)
1567 #'case-frob-capitalize-sout))
1568 (if (ansi-stream-p target)
1569 (funcall (ansi-stream-sout target) target str 0 len)
1570 (stream-write-string target str 0 len))))
1572 (defun case-frob-capitalize-first-out (stream char)
1573 (declare (type case-frob-stream stream)
1574 (type base-char char))
1575 (let ((target (case-frob-stream-target stream)))
1576 (cond ((alphanumericp char)
1577 (let ((char (char-upcase char)))
1578 (if (ansi-stream-p target)
1579 (funcall (ansi-stream-out target) target char)
1580 (stream-write-char target char)))
1581 (setf (case-frob-stream-out stream)
1582 #'case-frob-downcase-out)
1583 (setf (case-frob-stream-sout stream)
1584 #'case-frob-downcase-sout))
1586 (if (ansi-stream-p target)
1587 (funcall (ansi-stream-out target) target char)
1588 (stream-write-char target char))))))
1590 (defun case-frob-capitalize-first-sout (stream str start end)
1591 (declare (type case-frob-stream stream)
1592 (type simple-base-string str)
1594 (type (or index null) end))
1595 (let* ((target (case-frob-stream-target stream))
1596 (str (subseq str start end))
1599 (let ((char (schar str i)))
1600 (when (alphanumericp char)
1601 (setf (schar str i) (char-upcase char))
1602 (do ((i (1+ i) (1+ i)))
1604 (setf (schar str i) (char-downcase (schar str i))))
1605 (setf (case-frob-stream-out stream)
1606 #'case-frob-downcase-out)
1607 (setf (case-frob-stream-sout stream)
1608 #'case-frob-downcase-sout)
1610 (if (ansi-stream-p target)
1611 (funcall (ansi-stream-sout target) target str 0 len)
1612 (stream-write-string target str 0 len))))
1616 (defun read-sequence (seq stream &key (start 0) end)
1618 "Destructively modify SEQ by reading elements from STREAM.
1619 That part of SEQ bounded by START and END is destructively modified by
1620 copying successive elements into it from STREAM. If the end of file
1621 for STREAM is reached before copying all elements of the subsequence,
1622 then the extra elements near the end of sequence are not updated, and
1623 the index of the next element is returned."
1624 (declare (type sequence seq)
1625 (type stream stream)
1627 (type sequence-end end)
1629 (if (ansi-stream-p stream)
1630 (ansi-stream-read-sequence seq stream start end)
1631 ;; must be Gray streams FUNDAMENTAL-STREAM
1632 (stream-read-sequence stream seq start end)))
1634 (defun ansi-stream-read-sequence (seq stream start %end)
1635 (declare (type sequence seq)
1636 (type ansi-stream stream)
1638 (type sequence-end %end)
1640 (let ((end (or %end (length seq))))
1641 (declare (type index end))
1644 (let ((read-function
1645 (if (subtypep (stream-element-type stream) 'character)
1648 (do ((rem (nthcdr start seq) (rest rem))
1650 ((or (endp rem) (>= i end)) i)
1651 (declare (type list rem)
1653 (let ((el (funcall read-function stream nil :eof)))
1656 (setf (first rem) el)))))
1658 (with-array-data ((data seq) (offset-start start) (offset-end end))
1660 ((or (simple-array (unsigned-byte 8) (*))
1661 (simple-array (signed-byte 8) (*))
1663 (let* ((numbytes (- end start))
1664 (bytes-read (sb!sys:read-n-bytes stream
1669 (if (< bytes-read numbytes)
1670 (+ start bytes-read)
1673 (let ((read-function
1674 (if (subtypep (stream-element-type stream) 'character)
1677 (do ((i offset-start (1+ i)))
1678 ((>= i offset-end) end)
1679 (declare (type index i))
1680 (let ((el (funcall read-function stream nil :eof)))
1682 (return (+ start (- i offset-start))))
1683 (setf (aref data i) el)))))))))))
1687 (defun write-sequence (seq stream &key (start 0) (end nil))
1689 "Write the elements of SEQ bounded by START and END to STREAM."
1690 (declare (type sequence seq)
1691 (type stream stream)
1693 (type sequence-end end)
1695 (if (ansi-stream-p stream)
1696 (ansi-stream-write-sequence seq stream start end)
1697 ;; must be Gray-streams FUNDAMENTAL-STREAM
1698 (stream-write-sequence stream seq start end)))
1700 (defun ansi-stream-write-sequence (seq stream start %end)
1701 (declare (type sequence seq)
1702 (type ansi-stream stream)
1704 (type sequence-end %end)
1706 (let ((end (or %end (length seq))))
1707 (declare (type index end))
1710 (let ((write-function
1711 (if (subtypep (stream-element-type stream) 'character)
1714 (do ((rem (nthcdr start seq) (rest rem))
1716 ((or (endp rem) (>= i end)) seq)
1717 (declare (type list rem)
1719 (funcall write-function (first rem) stream))))
1721 (%write-string seq stream start end))
1723 (let ((write-function
1724 (if (subtypep (stream-element-type stream) 'character)
1727 (do ((i start (1+ i)))
1729 (declare (type index i))
1730 (funcall write-function (aref seq i) stream)))))))