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))
67 (when (synonym-stream-p stream)
69 (symbol-value (synonym-stream-symbol stream))))
71 (and (not (eq (ansi-stream-in stream) #'closed-flame))
72 ;;; KLUDGE: It's probably not good to have EQ tests on function
73 ;;; values like this. What if someone's redefined the function?
74 ;;; Is there a better way? (Perhaps just VALID-FOR-INPUT and
75 ;;; VALID-FOR-OUTPUT flags? -- WHN 19990902
76 (or (not (eq (ansi-stream-in stream) #'ill-in))
77 (not (eq (ansi-stream-bin stream) #'ill-bin)))))
79 (defun input-stream-p (stream)
80 (declare (type stream stream))
81 (and (ansi-stream-p stream)
82 (ansi-stream-input-stream-p stream)))
84 (declaim (inline ansi-stream-output-stream-p))
85 (defun ansi-stream-output-stream-p (stream)
86 (declare (type ansi-stream stream))
89 (when (synonym-stream-p stream)
90 (setf stream (symbol-value
91 (synonym-stream-symbol stream))))
93 (and (not (eq (ansi-stream-in stream) #'closed-flame))
94 (or (not (eq (ansi-stream-out stream) #'ill-out))
95 (not (eq (ansi-stream-bout stream) #'ill-bout)))))
97 (defun output-stream-p (stream)
98 (declare (type stream stream))
100 (and (ansi-stream-p stream)
101 (ansi-stream-output-stream-p stream)))
103 (declaim (inline ansi-stream-open-stream-p))
104 (defun ansi-stream-open-stream-p (stream)
105 (declare (type ansi-stream stream))
106 (not (eq (ansi-stream-in stream) #'closed-flame)))
108 (defun open-stream-p (stream)
109 (ansi-stream-open-stream-p stream))
111 (declaim (inline ansi-stream-element-type))
112 (defun ansi-stream-element-type (stream)
113 (declare (type ansi-stream stream))
114 (funcall (ansi-stream-misc stream) stream :element-type))
116 (defun stream-element-type (stream)
117 (ansi-stream-element-type stream))
119 (defun interactive-stream-p (stream)
120 (declare (type stream stream))
121 (funcall (ansi-stream-misc stream) stream :interactive-p))
123 (declaim (inline ansi-stream-close))
124 (defun ansi-stream-close (stream abort)
125 (declare (type ansi-stream stream))
126 (when (open-stream-p stream)
127 (funcall (ansi-stream-misc stream) stream :close abort))
130 (defun close (stream &key abort)
131 (ansi-stream-close stream abort))
133 (defun set-closed-flame (stream)
134 (setf (ansi-stream-in stream) #'closed-flame)
135 (setf (ansi-stream-bin stream) #'closed-flame)
136 (setf (ansi-stream-n-bin stream) #'closed-flame)
137 (setf (ansi-stream-in stream) #'closed-flame)
138 (setf (ansi-stream-out stream) #'closed-flame)
139 (setf (ansi-stream-bout stream) #'closed-flame)
140 (setf (ansi-stream-sout stream) #'closed-flame)
141 (setf (ansi-stream-misc stream) #'closed-flame))
143 ;;;; file position and file length
145 ;;; Call the MISC method with the :FILE-POSITION operation.
146 (defun file-position (stream &optional position)
147 (declare (type stream stream))
148 (declare (type (or index (alien sb!unix:off-t) (member nil :start :end)) position))
151 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
152 (funcall (ansi-stream-misc stream) stream :file-position position))
154 (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
157 (- +ansi-stream-in-buffer-length+
158 (ansi-stream-in-index stream))))))))
160 ;;; This is a literal translation of the ANSI glossary entry "stream
161 ;;; associated with a file".
163 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
164 ;;; file", and in particular stdin, stdout, and stderr are files, we
165 ;;; end up with this test being satisfied for weird things like
166 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
167 ;;; ANSI spec really had in mind, especially since this is used as a
168 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
169 ;;; probably thinking of something like what Unix calls block devices)
170 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
171 (defun stream-associated-with-file-p (x)
172 "Test for the ANSI concept \"stream associated with a file\"."
173 (or (typep x 'file-stream)
174 (and (synonym-stream-p x)
175 (stream-associated-with-file-p (symbol-value
176 (synonym-stream-symbol x))))))
178 (defun stream-must-be-associated-with-file (stream)
179 (declare (type stream stream))
180 (unless (stream-associated-with-file-p stream)
181 (error 'simple-type-error
182 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
183 ;; this should be TYPE-ERROR. But what then can we use for
184 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
185 ;; private predicate function..) is ugly and confusing, but
186 ;; I can't see any other way. -- WHN 2001-04-14
187 :expected-type '(satisfies stream-associated-with-file-p)
189 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
190 :format-arguments (list stream))))
192 ;;; like FILE-POSITION, only using :FILE-LENGTH
193 (defun file-length (stream)
194 ;; FIXME: The following declaration uses yet undefined types, which
195 ;; cause cross-compiler hangup.
197 ;; (declare (type (or file-stream synonym-stream) stream))
198 (stream-must-be-associated-with-file stream)
199 (funcall (ansi-stream-misc stream) stream :file-length))
203 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
205 (declare (ignore recursive-p))
206 (let ((stream (in-synonym-of stream)))
207 (if (ansi-stream-p stream)
208 (prepare-for-fast-read-char stream
209 (let ((res (make-string 80))
213 (let ((ch (fast-read-char nil nil)))
215 (when (char= ch #\newline)
216 (done-with-fast-read-char)
217 (return (values (shrink-vector res index) nil)))
220 (let ((new (make-string len)))
223 (setf (schar res index) ch)
226 (done-with-fast-read-char)
227 (return (values (eof-or-lose stream
231 ;; Since FAST-READ-CHAR already hit the eof char, we
232 ;; shouldn't do another READ-CHAR.
234 (done-with-fast-read-char)
235 (return (values (shrink-vector res index) t))))))))
236 ;; must be Gray streams FUNDAMENTAL-STREAM
237 (multiple-value-bind (string eof) (stream-read-line stream)
238 (if (and eof (zerop (length string)))
239 (values (eof-or-lose stream eof-error-p eof-value) t)
240 (values string eof))))))
242 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
243 ;;; so, except in this file, they are not inline by default, but they can be.
244 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
246 (defun read-char (&optional (stream *standard-input*)
250 (declare (ignore recursive-p))
251 (let ((stream (in-synonym-of stream)))
252 (if (ansi-stream-p stream)
253 (prepare-for-fast-read-char stream
255 (fast-read-char eof-error-p eof-value)
256 (done-with-fast-read-char)))
257 ;; must be Gray streams FUNDAMENTAL-STREAM
258 (let ((char (stream-read-char stream)))
260 (eof-or-lose stream eof-error-p eof-value)
263 (defun unread-char (character &optional (stream *standard-input*))
264 (let ((stream (in-synonym-of stream)))
265 (if (ansi-stream-p stream)
266 (let ((index (1- (ansi-stream-in-index stream)))
267 (buffer (ansi-stream-in-buffer stream)))
268 (declare (fixnum index))
269 (when (minusp index) (error "nothing to unread"))
271 (setf (aref buffer index) (char-code character))
272 (setf (ansi-stream-in-index stream) index))
274 (funcall (ansi-stream-misc stream) stream
275 :unread character))))
276 ;; must be Gray streams FUNDAMENTAL-STREAM
277 (stream-unread-char stream character)))
281 ;;; In the interest of ``once and only once'' this macro contains the
282 ;;; framework necessary to implement a peek-char function, which has
283 ;;; two special-cases (one for gray streams and one for echo streams)
284 ;;; in addition to the normal case.
286 ;;; All arguments are forms which will be used for a specific purpose
287 ;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
288 ;;; EOF-VALUE - the eof-value argument to peek-char
289 ;;; CHAR-VAR - the variable which will be used to store the current character
290 ;;; READ-FORM - the form which will be used to read a character
291 ;;; UNREAD-FORM - ditto for unread-char
292 ;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
293 ;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
294 ;;; (this will default to CHAR-VAR)
295 (defmacro generalized-peeking-mechanism (peek-type eof-value char-var read-form unread-form &optional (skipped-char-form nil) (eof-detected-form nil))
296 `(let ((,char-var ,read-form))
297 (cond ((eql ,char-var ,eof-value)
298 ,(if eof-detected-form
301 ((characterp ,peek-type)
302 (do ((,char-var ,char-var ,read-form))
303 ((or (eql ,char-var ,eof-value)
304 (char= ,char-var ,peek-type))
305 (cond ((eql ,char-var ,eof-value)
306 ,(if eof-detected-form
313 (do ((,char-var ,char-var ,read-form))
314 ((or (eql ,char-var ,eof-value)
315 (not (whitespace-char-p ,char-var)))
316 (cond ((eql ,char-var ,eof-value)
317 ,(if eof-detected-form
327 (bug "Impossible case reached in PEEK-CHAR")))))
329 (defun peek-char (&optional (peek-type nil)
330 (stream *standard-input*)
334 (declare (ignore recursive-p))
335 (let ((stream (in-synonym-of stream)))
336 (cond ((typep stream 'echo-stream)
340 (list eof-error-p eof-value)))
341 ((ansi-stream-p stream)
342 (generalized-peeking-mechanism
343 peek-type eof-value char
344 (read-char stream eof-error-p eof-value)
345 (unread-char char stream)))
347 ;; by elimination, must be Gray streams FUNDAMENTAL-STREAM
348 (generalized-peeking-mechanism
351 (stream-peek-char stream)
352 (stream-read-char stream))
355 (stream-unread-char stream char))
357 (eof-or-lose stream eof-error-p eof-value))))))
359 (defun listen (&optional (stream *standard-input*))
360 (let ((stream (in-synonym-of stream)))
361 (if (ansi-stream-p stream)
362 (or (/= (the fixnum (ansi-stream-in-index stream))
363 +ansi-stream-in-buffer-length+)
364 ;; Test for T explicitly since misc methods return :EOF sometimes.
365 (eq (funcall (ansi-stream-misc stream) stream :listen) t))
366 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
367 (stream-listen stream))))
369 (defun read-char-no-hang (&optional (stream *standard-input*)
373 (declare (ignore recursive-p))
374 (let ((stream (in-synonym-of stream)))
375 (if (ansi-stream-p stream)
376 (if (funcall (ansi-stream-misc stream) stream :listen)
377 ;; On T or :EOF get READ-CHAR to do the work.
378 (read-char stream eof-error-p eof-value)
380 ;; must be Gray streams FUNDAMENTAL-STREAM
381 (let ((char (stream-read-char-no-hang stream)))
383 (eof-or-lose stream eof-error-p eof-value)
386 (defun clear-input (&optional (stream *standard-input*))
387 (let ((stream (in-synonym-of stream)))
388 (cond ((ansi-stream-p stream)
389 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
390 (funcall (ansi-stream-misc stream) stream :clear-input))
392 (stream-clear-input stream))))
395 (declaim (maybe-inline read-byte))
396 (defun read-byte (stream &optional (eof-error-p t) eof-value)
397 (let ((stream (in-synonym-of stream)))
398 (if (ansi-stream-p stream)
399 (prepare-for-fast-read-byte stream
401 (fast-read-byte eof-error-p eof-value t)
402 (done-with-fast-read-byte)))
403 ;; must be Gray streams FUNDAMENTAL-STREAM
404 (let ((char (stream-read-byte stream)))
406 (eof-or-lose stream eof-error-p eof-value)
409 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
410 ;;; number of bytes read.
412 ;;; Note: CMU CL's version of this had a special interpretation of
413 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
414 ;;; case, CMU CL's version would return as soon as any data became
415 ;;; available.) This could be useful behavior for things like pipes in
416 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
417 ;;; If we ever need it, it could be added later as a new variant N-BIN
418 ;;; method (perhaps N-BIN-ASAP?) or something.
419 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
420 (declare (type ansi-stream stream)
421 (type index numbytes start)
422 (type (or (simple-array * (*)) system-area-pointer) buffer))
423 (let* ((stream (in-synonym-of stream ansi-stream))
424 (in-buffer (ansi-stream-in-buffer stream))
425 (index (ansi-stream-in-index stream))
426 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
427 (declare (fixnum index num-buffered))
430 (funcall (ansi-stream-n-bin stream)
436 ((<= numbytes num-buffered)
437 (%byte-blt in-buffer index
438 buffer start (+ start numbytes))
439 (setf (ansi-stream-in-index stream) (+ index numbytes))
442 (let ((end (+ start num-buffered)))
443 (%byte-blt in-buffer index buffer start end)
444 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
445 (+ (funcall (ansi-stream-n-bin stream)
449 (- numbytes num-buffered)
453 ;;; the amount of space we leave at the start of the in-buffer for
456 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
457 (defconstant +ansi-stream-in-buffer-extra+
458 4) ; FIXME: should be symbolic constant
460 ;;; This function is called by the FAST-READ-CHAR expansion to refill
461 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
462 ;;; and hence must be an N-BIN method.
463 (defun fast-read-char-refill (stream eof-error-p eof-value)
464 (let* ((ibuf (ansi-stream-in-buffer stream))
465 (count (funcall (ansi-stream-n-bin stream)
468 +ansi-stream-in-buffer-extra+
469 (- +ansi-stream-in-buffer-length+
470 +ansi-stream-in-buffer-extra+)
472 (start (- +ansi-stream-in-buffer-length+ count)))
473 (declare (type index start count))
475 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
476 (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
478 (when (/= start +ansi-stream-in-buffer-extra+)
479 (bit-bash-copy ibuf (+ (* +ansi-stream-in-buffer-extra+
481 (* sb!vm:vector-data-offset
483 ibuf (+ (the index (* start sb!vm:n-byte-bits))
484 (* sb!vm:vector-data-offset
486 (* count sb!vm:n-byte-bits)))
487 (setf (ansi-stream-in-index stream) (1+ start))
488 (code-char (aref ibuf start))))))
490 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
491 ;;; leave room for unreading.
492 (defun fast-read-byte-refill (stream eof-error-p eof-value)
493 (let* ((ibuf (ansi-stream-in-buffer stream))
494 (count (funcall (ansi-stream-n-bin stream) stream
495 ibuf 0 +ansi-stream-in-buffer-length+
497 (start (- +ansi-stream-in-buffer-length+ count)))
498 (declare (type index start count))
500 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
501 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
503 (unless (zerop start)
504 (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:n-word-bits)
505 ibuf (+ (the index (* start sb!vm:n-byte-bits))
506 (* sb!vm:vector-data-offset
508 (* count sb!vm:n-byte-bits)))
509 (setf (ansi-stream-in-index stream) (1+ start))
510 (aref ibuf start)))))
514 (defun write-char (character &optional (stream *standard-output*))
515 (with-out-stream stream (ansi-stream-out character)
516 (stream-write-char character))
519 (defun terpri (&optional (stream *standard-output*))
520 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
523 (defun fresh-line (&optional (stream *standard-output*))
524 (let ((stream (out-synonym-of stream)))
525 (if (ansi-stream-p stream)
526 (when (/= (or (charpos stream) 1) 0)
527 (funcall (ansi-stream-out stream) stream #\newline)
529 ;; must be Gray streams FUNDAMENTAL-STREAM
530 (stream-fresh-line stream))))
532 (defun write-string (string &optional (stream *standard-output*)
534 (declare (type string string))
535 ;; Note that even though you might expect, based on the behavior of
536 ;; things like AREF, that the correct upper bound here is
537 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
538 ;; "bounding index" and "length" indicate that in this case (i.e.
539 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
540 ;; (LENGTH STRING) is the required upper bound. A foolish
541 ;; consistency is the hobgoblin of lesser languages..
542 (%write-string string stream start (%check-vector-sequence-bounds
546 (defun %write-string (string stream start end)
547 (declare (type string string))
548 (declare (type streamlike stream))
549 (declare (type index start end))
550 (let ((stream (out-synonym-of stream)))
551 (cond ((ansi-stream-p stream)
552 (if (array-header-p string)
553 (with-array-data ((data string) (offset-start start)
555 (funcall (ansi-stream-sout stream)
556 stream data offset-start offset-end))
557 (funcall (ansi-stream-sout stream) stream string start end))
559 (t ; must be Gray streams FUNDAMENTAL-STREAM
560 (stream-write-string stream string start end)))))
562 (defun write-line (string &optional (stream *standard-output*)
564 (declare (type string string))
565 ;; FIXME: Why is there this difference between the treatments of the
566 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
567 (let ((defaulted-stream (out-synonym-of stream)))
568 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
570 (write-char #\newline defaulted-stream))
573 (defun charpos (&optional (stream *standard-output*))
574 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
576 (defun line-length (&optional (stream *standard-output*))
577 (with-out-stream stream (ansi-stream-misc :line-length)
578 (stream-line-length)))
580 (defun finish-output (&optional (stream *standard-output*))
581 (with-out-stream stream (ansi-stream-misc :finish-output)
582 (stream-finish-output))
585 (defun force-output (&optional (stream *standard-output*))
586 (with-out-stream stream (ansi-stream-misc :force-output)
587 (stream-force-output))
590 (defun clear-output (&optional (stream *standard-output*))
591 (with-out-stream stream (ansi-stream-misc :clear-output)
592 (stream-force-output))
595 (defun write-byte (integer stream)
596 (with-out-stream stream (ansi-stream-bout integer)
597 (stream-write-byte integer))
600 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
601 ;;; streams to handle the misc routines and dispatch to the
602 ;;; appropriate Gray stream functions.
603 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
604 (declare (type fundamental-stream stream)
608 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
609 (let ((char (stream-read-char-no-hang stream)))
610 (when (characterp char)
611 (stream-unread-char stream char))
614 (stream-unread-char stream arg1))
618 (stream-clear-input stream))
620 (stream-force-output stream))
622 (stream-finish-output stream))
624 (stream-element-type stream))
626 (interactive-stream-p stream))
628 (stream-line-length stream))
630 (stream-line-column stream))
632 (file-length stream))
634 (file-position stream arg1))))
636 ;;;; broadcast streams
638 (defstruct (broadcast-stream (:include ansi-stream
639 (out #'broadcast-out)
640 (bout #'broadcast-bout)
641 (sout #'broadcast-sout)
642 (misc #'broadcast-misc))
643 (:constructor %make-broadcast-stream
646 ;; a list of all the streams we broadcast to
647 (streams () :type list :read-only t))
649 (defun make-broadcast-stream (&rest streams)
650 (dolist (stream streams)
651 (unless (or (and (synonym-stream-p stream)
652 (output-stream-p (symbol-value
653 (synonym-stream-symbol stream))))
654 (output-stream-p stream))
657 :expected-type '(satisfies output-stream-p))))
658 (apply #'%make-broadcast-stream streams))
660 (macrolet ((out-fun (fun method stream-method &rest args)
661 `(defun ,fun (stream ,@args)
662 (dolist (stream (broadcast-stream-streams stream))
663 (if (ansi-stream-p stream)
664 (funcall (,method stream) stream ,@args)
665 (,stream-method stream ,@args))))))
666 (out-fun broadcast-out ansi-stream-out stream-write-char char)
667 (out-fun broadcast-bout ansi-stream-bout stream-write-byte byte)
668 (out-fun broadcast-sout ansi-stream-sout stream-write-string
671 (defun broadcast-misc (stream operation &optional arg1 arg2)
672 (let ((streams (broadcast-stream-streams stream)))
675 (dolist (stream streams)
676 (let ((charpos (charpos stream)))
677 (if charpos (return charpos)))))
680 (dolist (stream streams min)
681 (let ((res (line-length stream)))
682 (when res (setq min (if min (min res min) res)))))))
685 (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
686 (pushnew (stream-element-type stream) res :test #'equal))))
690 (dolist (stream streams res)
692 (if (ansi-stream-p stream)
693 (funcall (ansi-stream-misc stream) stream operation
695 (stream-misc-dispatch stream operation arg1 arg2)))))))))
699 (defstruct (synonym-stream (:include ansi-stream
702 (n-bin #'synonym-n-bin)
704 (bout #'synonym-bout)
705 (sout #'synonym-sout)
706 (misc #'synonym-misc))
707 (:constructor make-synonym-stream (symbol))
709 ;; This is the symbol, the value of which is the stream we are synonym to.
710 (symbol nil :type symbol :read-only t))
711 (def!method print-object ((x synonym-stream) stream)
712 (print-unreadable-object (x stream :type t :identity t)
713 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
715 ;;; The output simple output methods just call the corresponding method
716 ;;; in the synonymed stream.
717 (macrolet ((out-fun (name slot stream-method &rest args)
718 `(defun ,name (stream ,@args)
719 (declare (optimize (safety 1)))
720 (let ((syn (symbol-value (synonym-stream-symbol stream))))
721 (if (ansi-stream-p syn)
722 (funcall (,slot syn) syn ,@args)
723 (,stream-method syn ,@args))))))
724 (out-fun synonym-out ansi-stream-out stream-write-char ch)
725 (out-fun synonym-bout ansi-stream-bout stream-write-byte n)
726 (out-fun synonym-sout ansi-stream-sout stream-write-string string start end))
728 ;;; For the input methods, we just call the corresponding function on the
729 ;;; synonymed stream. These functions deal with getting input out of
730 ;;; the In-Buffer if there is any.
731 (macrolet ((in-fun (name fun &rest args)
732 `(defun ,name (stream ,@args)
733 (declare (optimize (safety 1)))
734 (,fun (symbol-value (synonym-stream-symbol stream))
736 (in-fun synonym-in read-char eof-error-p eof-value)
737 (in-fun synonym-bin read-byte eof-error-p eof-value)
738 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
740 (defun synonym-misc (stream operation &optional arg1 arg2)
741 (declare (optimize (safety 1)))
742 (let ((syn (symbol-value (synonym-stream-symbol stream))))
743 (if (ansi-stream-p syn)
744 ;; We have to special-case some operations which interact with
745 ;; the in-buffer of the wrapped stream, since just calling
746 ;; ANSI-STREAM-MISC on them
748 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
749 +ansi-stream-in-buffer-length+)
750 (funcall (ansi-stream-misc syn) syn :listen)))
751 (:clear-input (clear-input syn))
752 (:unread (unread-char arg1 syn))
754 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
755 (stream-misc-dispatch syn operation arg1 arg2))))
759 (defstruct (two-way-stream
760 (:include ansi-stream
763 (n-bin #'two-way-n-bin)
765 (bout #'two-way-bout)
766 (sout #'two-way-sout)
767 (misc #'two-way-misc))
768 (:constructor %make-two-way-stream (input-stream output-stream))
770 (input-stream (missing-arg) :type stream :read-only t)
771 (output-stream (missing-arg) :type stream :read-only t))
772 (defprinter (two-way-stream) input-stream output-stream)
774 (defun make-two-way-stream (input-stream output-stream)
776 "Return a bidirectional stream which gets its input from INPUT-STREAM and
777 sends its output to OUTPUT-STREAM."
778 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
779 ;; should be encapsulated in a function, and used here and most of
780 ;; the other places that SYNONYM-STREAM-P appears.
781 (unless (or (and (synonym-stream-p output-stream)
782 (output-stream-p (symbol-value
783 (synonym-stream-symbol output-stream))))
784 (output-stream-p output-stream))
787 :expected-type '(satisfies output-stream-p)))
788 (unless (or (and (synonym-stream-p input-stream)
789 (input-stream-p (symbol-value
790 (synonym-stream-symbol input-stream))))
791 (input-stream-p input-stream))
794 :expected-type '(satisfies input-stream-p)))
795 (funcall #'%make-two-way-stream input-stream output-stream))
797 (macrolet ((out-fun (name slot stream-method &rest args)
798 `(defun ,name (stream ,@args)
799 (let ((syn (two-way-stream-output-stream stream)))
800 (if (ansi-stream-p syn)
801 (funcall (,slot syn) syn ,@args)
802 (,stream-method syn ,@args))))))
803 (out-fun two-way-out ansi-stream-out stream-write-char ch)
804 (out-fun two-way-bout ansi-stream-bout stream-write-byte n)
805 (out-fun two-way-sout ansi-stream-sout stream-write-string string start end))
807 (macrolet ((in-fun (name fun &rest args)
808 `(defun ,name (stream ,@args)
809 (force-output (two-way-stream-output-stream stream))
810 (,fun (two-way-stream-input-stream stream) ,@args))))
811 (in-fun two-way-in read-char eof-error-p eof-value)
812 (in-fun two-way-bin read-byte eof-error-p eof-value)
813 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
815 (defun two-way-misc (stream operation &optional arg1 arg2)
816 (let* ((in (two-way-stream-input-stream stream))
817 (out (two-way-stream-output-stream stream))
818 (in-ansi-stream-p (ansi-stream-p in))
819 (out-ansi-stream-p (ansi-stream-p out)))
823 (or (/= (the fixnum (ansi-stream-in-index in))
824 +ansi-stream-in-buffer-length+)
825 (funcall (ansi-stream-misc in) in :listen))
827 ((:finish-output :force-output :clear-output)
828 (if out-ansi-stream-p
829 (funcall (ansi-stream-misc out) out operation arg1 arg2)
830 (stream-misc-dispatch out operation arg1 arg2)))
831 (:clear-input (clear-input in))
832 (:unread (unread-char arg1 in))
834 (let ((in-type (stream-element-type in))
835 (out-type (stream-element-type out)))
836 (if (equal in-type out-type)
837 in-type `(and ,in-type ,out-type))))
839 (set-closed-flame stream))
841 (or (if in-ansi-stream-p
842 (funcall (ansi-stream-misc in) in operation arg1 arg2)
843 (stream-misc-dispatch in operation arg1 arg2))
844 (if out-ansi-stream-p
845 (funcall (ansi-stream-misc out) out operation arg1 arg2)
846 (stream-misc-dispatch out operation arg1 arg2)))))))
848 ;;;; concatenated streams
850 (defstruct (concatenated-stream
851 (:include ansi-stream
852 (in #'concatenated-in)
853 (bin #'concatenated-bin)
854 (n-bin #'concatenated-n-bin)
855 (misc #'concatenated-misc))
856 (:constructor %make-concatenated-stream
857 (&rest streams &aux (current streams)))
859 ;; The car of this is the substream we are reading from now.
861 ;; This is a list of all the substreams there ever were. We need to
862 ;; remember them so that we can close them.
864 ;; FIXME: ANSI says this is supposed to be the list of streams that
865 ;; we still have to read from. So either this needs to become a
866 ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
867 ;; around it which discards closed files from the head of the list)
868 ;; or we need to update it as we run out of files.
869 (streams nil :type list :read-only t))
870 (def!method print-object ((x concatenated-stream) stream)
871 (print-unreadable-object (x stream :type t :identity t)
874 (concatenated-stream-streams x))))
876 (defun make-concatenated-stream (&rest streams)
878 "Return a stream which takes its input from each of the streams in turn,
879 going on to the next at EOF."
880 (dolist (stream streams)
881 (unless (or (and (synonym-stream-p stream)
882 (input-stream-p (symbol-value
883 (synonym-stream-symbol stream))))
884 (input-stream-p stream))
887 :expected-type '(satisfies input-stream-p))))
888 (apply #'%make-concatenated-stream streams))
890 (macrolet ((in-fun (name fun)
891 `(defun ,name (stream eof-error-p eof-value)
892 (do ((current (concatenated-stream-current stream)
895 (eof-or-lose stream eof-error-p eof-value))
896 (let* ((stream (car current))
897 (result (,fun stream nil nil)))
898 (when result (return result)))
899 (pop (concatenated-stream-current stream))))))
900 (in-fun concatenated-in read-char)
901 (in-fun concatenated-bin read-byte))
903 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
904 (do ((current (concatenated-stream-current stream) (cdr current))
905 (current-start start)
906 (remaining-bytes numbytes))
909 (error 'end-of-file :stream stream)
910 (- numbytes remaining-bytes)))
911 (let* ((stream (car current))
912 (bytes-read (read-n-bytes stream buffer current-start
913 remaining-bytes nil)))
914 (incf current-start bytes-read)
915 (decf remaining-bytes bytes-read)
916 (when (zerop remaining-bytes) (return numbytes)))
917 (setf (concatenated-stream-current stream) (cdr current))))
919 (defun concatenated-misc (stream operation &optional arg1 arg2)
920 (let ((left (concatenated-stream-current stream)))
922 (let* ((current (car left)))
926 (let ((stuff (if (ansi-stream-p current)
927 (funcall (ansi-stream-misc current) current
929 (stream-misc-dispatch current :listen))))
930 (cond ((eq stuff :eof)
931 ;; Advance CURRENT, and try again.
932 (pop (concatenated-stream-current stream))
934 (car (concatenated-stream-current stream)))
936 ;; No further streams. EOF.
939 ;; Stuff's available.
942 ;; Nothing is available yet.
944 (:clear-input (clear-input current))
945 (:unread (unread-char arg1 current))
947 (set-closed-flame stream))
949 (if (ansi-stream-p current)
950 (funcall (ansi-stream-misc current) current operation arg1 arg2)
951 (stream-misc-dispatch current operation arg1 arg2))))))))
955 (defstruct (echo-stream
956 (:include two-way-stream
961 (:constructor %make-echo-stream (input-stream output-stream))
964 (def!method print-object ((x echo-stream) stream)
965 (print-unreadable-object (x stream :type t :identity t)
967 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
968 (two-way-stream-input-stream x)
969 (two-way-stream-output-stream x))))
971 (defun make-echo-stream (input-stream output-stream)
973 "Return a bidirectional stream which gets its input from INPUT-STREAM and
974 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
976 (unless (or (and (synonym-stream-p output-stream)
977 (output-stream-p (symbol-value
978 (synonym-stream-symbol output-stream))))
979 (output-stream-p output-stream))
982 :expected-type '(satisfies output-stream-p)))
983 (unless (or (and (synonym-stream-p input-stream)
984 (input-stream-p (symbol-value
985 (synonym-stream-symbol input-stream))))
986 (input-stream-p input-stream))
989 :expected-type '(satisfies input-stream-p)))
990 (funcall #'%make-echo-stream input-stream output-stream))
992 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
993 `(defun ,name (stream ,@args)
994 (or (pop (echo-stream-unread-stuff stream))
995 (let* ((in (echo-stream-input-stream stream))
996 (out (echo-stream-output-stream stream))
997 (result (,fun in ,@args)))
998 (if (ansi-stream-p out)
999 (funcall (,out-slot out) out result)
1000 (,stream-method out result))
1002 (in-fun echo-in read-char ansi-stream-out stream-write-char
1003 eof-error-p eof-value)
1004 (in-fun echo-bin read-byte ansi-stream-bout stream-write-byte
1005 eof-error-p eof-value))
1007 (defun echo-misc (stream operation &optional arg1 arg2)
1008 (let* ((in (two-way-stream-input-stream stream))
1009 (out (two-way-stream-output-stream stream)))
1012 (or (not (null (echo-stream-unread-stuff stream)))
1013 (if (ansi-stream-p in)
1014 (or (/= (the fixnum (ansi-stream-in-index in))
1015 +ansi-stream-in-buffer-length+)
1016 (funcall (ansi-stream-misc in) in :listen))
1017 (stream-misc-dispatch in :listen))))
1018 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1020 (let ((in-type (stream-element-type in))
1021 (out-type (stream-element-type out)))
1022 (if (equal in-type out-type)
1023 in-type `(and ,in-type ,out-type))))
1025 (set-closed-flame stream))
1027 ;; For the special case of peeking into an echo-stream
1028 ;; arg1 is PEEK-TYPE, arg2 is (EOF-ERROR-P EOF-VALUE)
1029 ;; returns peeked-char, eof-value, or errors end-of-file
1031 ;; Note: This code could be moved into PEEK-CHAR if desired.
1032 ;; I am unsure whether this belongs with echo-streams because it is
1033 ;; echo-stream specific, or PEEK-CHAR because it is peeking code.
1034 ;; -- mrd 2002-11-18
1036 ;; UNREAD-CHAR-P indicates whether the current character was one
1037 ;; that was previously unread. In that case, we need to ensure that
1038 ;; the semantics for UNREAD-CHAR are held; the character should
1039 ;; not be echoed again.
1040 (let ((unread-char-p nil))
1042 (unless unread-char-p
1043 (if (ansi-stream-p out)
1044 (funcall (ansi-stream-out out) out c)
1046 (stream-write-char out c))))
1048 ;; Obtain input from unread buffer or input stream,
1049 ;; and set the flag appropriately.
1050 (cond ((not (null (echo-stream-unread-stuff stream)))
1051 (setf unread-char-p t)
1052 (pop (echo-stream-unread-stuff stream)))
1054 (setf unread-char-p nil)
1055 (read-char in (first arg2) (second arg2))))))
1056 (generalized-peeking-mechanism
1057 arg1 (second arg2) char
1059 (unread-char char in)
1062 (or (if (ansi-stream-p in)
1063 (funcall (ansi-stream-misc in) in operation arg1 arg2)
1064 (stream-misc-dispatch in operation arg1 arg2))
1065 (if (ansi-stream-p out)
1066 (funcall (ansi-stream-misc out) out operation arg1 arg2)
1067 (stream-misc-dispatch out operation arg1 arg2)))))))
1069 ;;;; base STRING-STREAM stuff
1071 (defstruct (string-stream
1072 (:include ansi-stream)
1075 ;; FIXME: This type declaration is true, and will probably continue
1076 ;; to be true. However, note well the comments in DEFTRANSFORM
1077 ;; REPLACE, implying that performance of REPLACE is somewhat
1078 ;; critical to performance of string streams. If (VECTOR CHARACTER)
1079 ;; ever becomes different from (VECTOR BASE-CHAR), the transform
1080 ;; probably needs to be extended.
1081 (string (missing-arg) :type (vector character)))
1083 ;;;; STRING-INPUT-STREAM stuff
1085 (defstruct (string-input-stream
1086 (:include string-stream
1088 (bin #'string-binch)
1089 (n-bin #'string-stream-read-n-bytes)
1090 (misc #'string-in-misc)
1091 (string (missing-arg)
1092 :type (simple-array character (*))))
1093 (:constructor internal-make-string-input-stream
1094 (string current end))
1096 (current nil :type index)
1097 (end nil :type index))
1099 (defun string-inch (stream eof-error-p eof-value)
1100 (let ((string (string-input-stream-string stream))
1101 (index (string-input-stream-current stream)))
1102 (declare (type (simple-array character (*)) string)
1103 (type fixnum index))
1104 (cond ((= index (the index (string-input-stream-end stream)))
1105 (eof-or-lose stream eof-error-p eof-value))
1107 (setf (string-input-stream-current stream) (1+ index))
1108 (aref string index)))))
1110 (defun string-binch (stream eof-error-p eof-value)
1111 (let ((string (string-input-stream-string stream))
1112 (index (string-input-stream-current stream)))
1113 (declare (type (simple-array character (*)) string)
1115 (cond ((= index (the index (string-input-stream-end stream)))
1116 (eof-or-lose stream eof-error-p eof-value))
1118 (setf (string-input-stream-current stream) (1+ index))
1119 (char-code (aref string index))))))
1121 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1122 (declare (type string-input-stream stream)
1123 (type index start requested))
1124 (let* ((string (string-input-stream-string stream))
1125 (index (string-input-stream-current stream))
1126 (available (- (string-input-stream-end stream) index))
1127 (copy (min available requested)))
1128 (declare (type (simple-array character (*)) string)
1129 (type index index available copy))
1131 (setf (string-input-stream-current stream)
1132 (truly-the index (+ index copy)))
1133 (sb!sys:without-gcing
1134 (system-area-copy (vector-sap string)
1135 (* index sb!vm:n-byte-bits)
1136 (if (typep buffer 'system-area-pointer)
1138 (vector-sap buffer))
1139 (* start sb!vm:n-byte-bits)
1140 (* copy sb!vm:n-byte-bits))))
1141 (if (and (> requested copy) eof-error-p)
1142 (error 'end-of-file :stream stream)
1145 (defun string-in-misc (stream operation &optional arg1 arg2)
1146 (declare (ignore arg2))
1150 (setf (string-input-stream-current stream)
1153 (:end (string-input-stream-end stream))
1155 (string-input-stream-current stream)))
1156 (:file-length (length (string-input-stream-string stream)))
1157 (:unread (decf (string-input-stream-current stream)))
1158 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1159 (the fixnum (string-input-stream-end stream)))
1161 (:element-type 'base-char)))
1163 (defun make-string-input-stream (string &optional
1166 "Return an input stream which will supply the characters of STRING between
1167 START and END in order."
1168 (declare (type string string)
1170 (type (or index null) end))
1172 (internal-make-string-input-stream
1173 (coerce string 'simple-string)
1175 (%check-vector-sequence-bounds string start end)))
1177 ;;;; STRING-OUTPUT-STREAM stuff
1179 (defstruct (string-output-stream
1180 (:include string-stream
1182 (sout #'string-sout)
1183 (misc #'string-out-misc)
1184 ;; The string we throw stuff in.
1185 (string (missing-arg)
1186 :type (simple-array character (*))))
1187 (:constructor make-string-output-stream
1188 (&key (element-type 'character)
1189 &aux (string (make-string 40))))
1191 ;; Index of the next location to use.
1192 (index 0 :type fixnum)
1193 ;; Requested element type
1194 (element-type 'character))
1197 (setf (fdocumentation 'make-string-output-stream 'function)
1198 "Return an output stream which will accumulate all output given it for
1199 the benefit of the function GET-OUTPUT-STREAM-STRING.")
1201 (defun string-ouch (stream character)
1202 (let ((current (string-output-stream-index stream))
1203 (workspace (string-output-stream-string stream)))
1204 (declare (type (simple-array character (*)) workspace)
1205 (type fixnum current))
1206 (if (= current (the fixnum (length workspace)))
1207 (let ((new-workspace (make-string (* current 2))))
1208 (replace new-workspace workspace)
1209 (setf (aref new-workspace current) character)
1210 (setf (string-output-stream-string stream) new-workspace))
1211 (setf (aref workspace current) character))
1212 (setf (string-output-stream-index stream) (1+ current))))
1214 (defun string-sout (stream string start end)
1215 (declare (type simple-string string)
1216 (type fixnum start end))
1217 (let* ((string (if (typep string '(simple-array character (*)))
1219 (coerce string '(simple-array character (*)))))
1220 (current (string-output-stream-index stream))
1221 (length (- end start))
1222 (dst-end (+ length current))
1223 (workspace (string-output-stream-string stream)))
1224 (declare (type (simple-array character (*)) workspace string)
1225 (type fixnum current length dst-end))
1226 (if (> dst-end (the fixnum (length workspace)))
1227 (let ((new-workspace (make-string (+ (* current 2) length))))
1228 (replace new-workspace workspace :end2 current)
1229 (replace new-workspace string
1230 :start1 current :end1 dst-end
1231 :start2 start :end2 end)
1232 (setf (string-output-stream-string stream) new-workspace))
1233 (replace workspace string
1234 :start1 current :end1 dst-end
1235 :start2 start :end2 end))
1236 (setf (string-output-stream-index stream) dst-end)))
1238 (defun string-out-misc (stream operation &optional arg1 arg2)
1239 (declare (ignore arg2))
1243 (string-output-stream-index stream)))
1245 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1247 (count 0 (1+ count))
1248 (string (string-output-stream-string stream)))
1250 (declare (type (simple-array character (*)) string)
1251 (type fixnum index count))
1252 (if (char= (schar string index) #\newline)
1254 (:element-type 'base-char)))
1256 ;;; Return a string of all the characters sent to a stream made by
1257 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1258 (defun get-output-stream-string (stream)
1259 (declare (type string-output-stream stream))
1260 (let* ((length (string-output-stream-index stream))
1261 (element-type (string-output-stream-element-type stream))
1264 ;; Overwhelmingly common case; can be inlined.
1265 ((character) (make-string length))
1266 (t (make-string length :element-type element-type)))))
1267 ;; For the benefit of the REPLACE transform, let's do this, so
1268 ;; that the common case isn't ludicrously expensive.
1270 ((simple-array character (*))
1271 (replace result (string-output-stream-string stream)))
1272 ((simple-array nil (*))
1273 (replace result (string-output-stream-string stream))))
1274 (setf (string-output-stream-index stream) 0)
1277 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1278 ;;; GET-OUTPUT-STREAM-STRING would return them.
1279 (defun dump-output-stream-string (in-stream out-stream)
1280 (%write-string (string-output-stream-string in-stream)
1283 (string-output-stream-index in-stream))
1284 (setf (string-output-stream-index in-stream) 0))
1286 ;;;; fill-pointer streams
1288 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1289 ;;; the CLM, but they are required for the implementation of
1290 ;;; WITH-OUTPUT-TO-STRING.
1292 (deftype string-with-fill-pointer ()
1293 '(and (vector character)
1294 (satisfies array-has-fill-pointer-p)))
1296 (defstruct (fill-pointer-output-stream
1297 (:include string-stream
1298 (out #'fill-pointer-ouch)
1299 (sout #'fill-pointer-sout)
1300 (misc #'fill-pointer-misc)
1301 ;; a string with a fill pointer where we stuff
1302 ;; the stuff we write
1303 (string (error "missing argument")
1304 :type string-with-fill-pointer
1306 (:constructor make-fill-pointer-output-stream (string))
1309 (defun fill-pointer-ouch (stream character)
1310 (let* ((buffer (fill-pointer-output-stream-string stream))
1311 (current (fill-pointer buffer))
1312 (current+1 (1+ current)))
1313 (declare (fixnum current))
1314 (with-array-data ((workspace buffer) (start) (end))
1315 (declare (type (simple-array character (*)) workspace))
1316 (let ((offset-current (+ start current)))
1317 (declare (fixnum offset-current))
1318 (if (= offset-current end)
1319 (let* ((new-length (1+ (* current 2)))
1320 (new-workspace (make-string new-length)))
1321 (declare (simple-string new-workspace))
1322 (%byte-blt workspace start
1323 new-workspace 0 current)
1324 (setf workspace new-workspace)
1325 (setf offset-current current)
1326 (set-array-header buffer workspace new-length
1327 current+1 0 new-length nil))
1328 (setf (fill-pointer buffer) current+1))
1329 (setf (schar workspace offset-current) character)))
1332 (defun fill-pointer-sout (stream string start end)
1333 (declare (simple-string string) (fixnum start end))
1334 (let* ((string (if (typep string '(simple-array character (*)))
1336 (coerce string '(simple-array character (*)))))
1337 (buffer (fill-pointer-output-stream-string stream))
1338 (current (fill-pointer buffer))
1339 (string-len (- end start))
1340 (dst-end (+ string-len current)))
1341 (declare (fixnum current dst-end string-len))
1342 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1343 (declare (type (simple-array character (*)) workspace))
1344 (let ((offset-dst-end (+ dst-start dst-end))
1345 (offset-current (+ dst-start current)))
1346 (declare (fixnum offset-dst-end offset-current))
1347 (if (> offset-dst-end dst-length)
1348 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1349 (new-workspace (make-string new-length)))
1350 (declare (type (simple-array character (*)) new-workspace))
1351 (%byte-blt workspace dst-start
1352 new-workspace 0 current)
1353 (setf workspace new-workspace)
1354 (setf offset-current current)
1355 (setf offset-dst-end dst-end)
1356 (set-array-header buffer
1363 (setf (fill-pointer buffer) dst-end))
1364 (%byte-blt string start
1365 workspace offset-current offset-dst-end)))
1368 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1369 (declare (ignore arg1 arg2))
1372 (let* ((buffer (fill-pointer-output-stream-string stream))
1373 (current (fill-pointer buffer)))
1374 (with-array-data ((string buffer) (start) (end current))
1375 (declare (simple-string string) (ignore start))
1376 (let ((found (position #\newline string :test #'char=
1377 :end end :from-end t)))
1379 (- end (the fixnum found))
1381 (:element-type 'base-char)))
1383 ;;;; indenting streams
1385 (defstruct (indenting-stream (:include ansi-stream
1386 (out #'indenting-out)
1387 (sout #'indenting-sout)
1388 (misc #'indenting-misc))
1389 (:constructor make-indenting-stream (stream))
1391 ;; the stream we're based on
1393 ;; how much we indent on each line
1397 (setf (fdocumentation 'make-indenting-stream 'function)
1398 "Return an output stream which indents its output by some amount.")
1400 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1401 ;;; output on the given STREAM based on the specified SUB-STREAM.
1402 (defmacro indenting-indent (stream sub-stream)
1403 ;; KLUDGE: bare magic number 60
1404 `(do ((i 0 (+ i 60))
1405 (indentation (indenting-stream-indentation ,stream)))
1406 ((>= i indentation))
1411 (min 60 (- indentation i)))))
1413 ;;; INDENTING-OUT writes a character to an indenting stream.
1414 (defun indenting-out (stream char)
1415 (let ((sub-stream (indenting-stream-stream stream)))
1416 (write-char char sub-stream)
1417 (if (char= char #\newline)
1418 (indenting-indent stream sub-stream))))
1420 ;;; INDENTING-SOUT writes a string to an indenting stream.
1421 (defun indenting-sout (stream string start end)
1422 (declare (simple-string string) (fixnum start end))
1424 (sub-stream (indenting-stream-stream stream)))
1426 (let ((newline (position #\newline string :start i :end end)))
1428 (%write-string string sub-stream i (1+ newline))
1429 (indenting-indent stream sub-stream)
1430 (setq i (+ newline 1)))
1432 (%write-string string sub-stream i end)
1435 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1436 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1437 ;;; the base stream minus the stream's indentation.
1438 (defun indenting-misc (stream operation &optional arg1 arg2)
1439 (let ((sub-stream (indenting-stream-stream stream)))
1440 (if (ansi-stream-p sub-stream)
1441 (let ((method (ansi-stream-misc sub-stream)))
1444 (let ((line-length (funcall method sub-stream operation)))
1446 (- line-length (indenting-stream-indentation stream)))))
1448 (let ((charpos (funcall method sub-stream operation)))
1450 (- charpos (indenting-stream-indentation stream)))))
1452 (funcall method sub-stream operation arg1 arg2))))
1453 ;; must be Gray streams FUNDAMENTAL-STREAM
1456 (let ((line-length (stream-line-length sub-stream)))
1458 (- line-length (indenting-stream-indentation stream)))))
1460 (let ((charpos (stream-line-column sub-stream)))
1462 (- charpos (indenting-stream-indentation stream)))))
1464 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1466 (declaim (maybe-inline read-char unread-char read-byte listen))
1468 ;;;; case frobbing streams, used by FORMAT ~(...~)
1470 (defstruct (case-frob-stream
1471 (:include ansi-stream
1472 (misc #'case-frob-misc))
1473 (:constructor %make-case-frob-stream (target out sout))
1475 (target (missing-arg) :type stream))
1477 (defun make-case-frob-stream (target kind)
1479 "Return a stream that sends all output to the stream TARGET, but modifies
1480 the case of letters, depending on KIND, which should be one of:
1481 :upcase - convert to upper case.
1482 :downcase - convert to lower case.
1483 :capitalize - convert the first letter of words to upper case and the
1484 rest of the word to lower case.
1485 :capitalize-first - convert the first letter of the first word to upper
1486 case and everything else to lower case."
1487 (declare (type stream target)
1488 (type (member :upcase :downcase :capitalize :capitalize-first)
1491 (if (case-frob-stream-p target)
1492 ;; If we are going to be writing to a stream that already does
1493 ;; case frobbing, why bother frobbing the case just so it can
1496 (multiple-value-bind (out sout)
1499 (values #'case-frob-upcase-out
1500 #'case-frob-upcase-sout))
1502 (values #'case-frob-downcase-out
1503 #'case-frob-downcase-sout))
1505 (values #'case-frob-capitalize-out
1506 #'case-frob-capitalize-sout))
1508 (values #'case-frob-capitalize-first-out
1509 #'case-frob-capitalize-first-sout)))
1510 (%make-case-frob-stream target out sout))))
1512 (defun case-frob-misc (stream op &optional arg1 arg2)
1513 (declare (type case-frob-stream stream))
1517 (let ((target (case-frob-stream-target stream)))
1518 (if (ansi-stream-p target)
1519 (funcall (ansi-stream-misc target) target op arg1 arg2)
1520 (stream-misc-dispatch target op arg1 arg2))))))
1522 (defun case-frob-upcase-out (stream char)
1523 (declare (type case-frob-stream stream)
1524 (type base-char char))
1525 (let ((target (case-frob-stream-target stream))
1526 (char (char-upcase char)))
1527 (if (ansi-stream-p target)
1528 (funcall (ansi-stream-out target) target char)
1529 (stream-write-char target char))))
1531 (defun case-frob-upcase-sout (stream str start end)
1532 (declare (type case-frob-stream stream)
1533 (type simple-base-string str)
1535 (type (or index null) end))
1536 (let* ((target (case-frob-stream-target stream))
1539 (string (if (and (zerop start) (= len end))
1541 (nstring-upcase (subseq str start end))))
1542 (string-len (- end start)))
1543 (if (ansi-stream-p target)
1544 (funcall (ansi-stream-sout target) target string 0 string-len)
1545 (stream-write-string target string 0 string-len))))
1547 (defun case-frob-downcase-out (stream char)
1548 (declare (type case-frob-stream stream)
1549 (type base-char char))
1550 (let ((target (case-frob-stream-target stream))
1551 (char (char-downcase char)))
1552 (if (ansi-stream-p target)
1553 (funcall (ansi-stream-out target) target char)
1554 (stream-write-char target char))))
1556 (defun case-frob-downcase-sout (stream str start end)
1557 (declare (type case-frob-stream stream)
1558 (type simple-base-string str)
1560 (type (or index null) end))
1561 (let* ((target (case-frob-stream-target stream))
1564 (string (if (and (zerop start) (= len end))
1565 (string-downcase str)
1566 (nstring-downcase (subseq str start end))))
1567 (string-len (- end start)))
1568 (if (ansi-stream-p target)
1569 (funcall (ansi-stream-sout target) target string 0 string-len)
1570 (stream-write-string target string 0 string-len))))
1572 (defun case-frob-capitalize-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) #'case-frob-capitalize-aux-out)
1582 (setf (case-frob-stream-sout stream)
1583 #'case-frob-capitalize-aux-sout))
1585 (if (ansi-stream-p target)
1586 (funcall (ansi-stream-out target) target char)
1587 (stream-write-char target char))))))
1589 (defun case-frob-capitalize-sout (stream str start end)
1590 (declare (type case-frob-stream stream)
1591 (type simple-base-string str)
1593 (type (or index null) end))
1594 (let* ((target (case-frob-stream-target stream))
1595 (str (subseq str start end))
1599 (let ((char (schar str i)))
1600 (cond ((not (alphanumericp char))
1601 (setf inside-word nil))
1603 (setf (schar str i) (char-downcase char)))
1605 (setf inside-word t)
1606 (setf (schar str i) (char-upcase char))))))
1608 (setf (case-frob-stream-out stream)
1609 #'case-frob-capitalize-aux-out)
1610 (setf (case-frob-stream-sout stream)
1611 #'case-frob-capitalize-aux-sout))
1612 (if (ansi-stream-p target)
1613 (funcall (ansi-stream-sout target) target str 0 len)
1614 (stream-write-string target str 0 len))))
1616 (defun case-frob-capitalize-aux-out (stream char)
1617 (declare (type case-frob-stream stream)
1618 (type base-char char))
1619 (let ((target (case-frob-stream-target stream)))
1620 (cond ((alphanumericp char)
1621 (let ((char (char-downcase char)))
1622 (if (ansi-stream-p target)
1623 (funcall (ansi-stream-out target) target char)
1624 (stream-write-char target char))))
1626 (if (ansi-stream-p target)
1627 (funcall (ansi-stream-out target) target char)
1628 (stream-write-char target char))
1629 (setf (case-frob-stream-out stream)
1630 #'case-frob-capitalize-out)
1631 (setf (case-frob-stream-sout stream)
1632 #'case-frob-capitalize-sout)))))
1634 (defun case-frob-capitalize-aux-sout (stream str start end)
1635 (declare (type case-frob-stream stream)
1636 (type simple-base-string str)
1638 (type (or index null) end))
1639 (let* ((target (case-frob-stream-target stream))
1640 (str (subseq str start end))
1644 (let ((char (schar str i)))
1645 (cond ((not (alphanumericp char))
1646 (setf inside-word nil))
1648 (setf (schar str i) (char-downcase char)))
1650 (setf inside-word t)
1651 (setf (schar str i) (char-upcase char))))))
1653 (setf (case-frob-stream-out stream)
1654 #'case-frob-capitalize-out)
1655 (setf (case-frob-stream-sout stream)
1656 #'case-frob-capitalize-sout))
1657 (if (ansi-stream-p target)
1658 (funcall (ansi-stream-sout target) target str 0 len)
1659 (stream-write-string target str 0 len))))
1661 (defun case-frob-capitalize-first-out (stream char)
1662 (declare (type case-frob-stream stream)
1663 (type base-char char))
1664 (let ((target (case-frob-stream-target stream)))
1665 (cond ((alphanumericp char)
1666 (let ((char (char-upcase char)))
1667 (if (ansi-stream-p target)
1668 (funcall (ansi-stream-out target) target char)
1669 (stream-write-char target char)))
1670 (setf (case-frob-stream-out stream)
1671 #'case-frob-downcase-out)
1672 (setf (case-frob-stream-sout stream)
1673 #'case-frob-downcase-sout))
1675 (if (ansi-stream-p target)
1676 (funcall (ansi-stream-out target) target char)
1677 (stream-write-char target char))))))
1679 (defun case-frob-capitalize-first-sout (stream str start end)
1680 (declare (type case-frob-stream stream)
1681 (type simple-base-string str)
1683 (type (or index null) end))
1684 (let* ((target (case-frob-stream-target stream))
1685 (str (subseq str start end))
1688 (let ((char (schar str i)))
1689 (when (alphanumericp char)
1690 (setf (schar str i) (char-upcase char))
1691 (do ((i (1+ i) (1+ i)))
1693 (setf (schar str i) (char-downcase (schar str i))))
1694 (setf (case-frob-stream-out stream)
1695 #'case-frob-downcase-out)
1696 (setf (case-frob-stream-sout stream)
1697 #'case-frob-downcase-sout)
1699 (if (ansi-stream-p target)
1700 (funcall (ansi-stream-sout target) target str 0 len)
1701 (stream-write-string target str 0 len))))
1705 (defun read-sequence (seq stream &key (start 0) end)
1707 "Destructively modify SEQ by reading elements from STREAM.
1708 That part of SEQ bounded by START and END is destructively modified by
1709 copying successive elements into it from STREAM. If the end of file
1710 for STREAM is reached before copying all elements of the subsequence,
1711 then the extra elements near the end of sequence are not updated, and
1712 the index of the next element is returned."
1713 (declare (type sequence seq)
1714 (type stream stream)
1716 (type sequence-end end)
1718 (if (ansi-stream-p stream)
1719 (ansi-stream-read-sequence seq stream start end)
1720 ;; must be Gray streams FUNDAMENTAL-STREAM
1721 (stream-read-sequence stream seq start end)))
1723 (defun ansi-stream-read-sequence (seq stream start %end)
1724 (declare (type sequence seq)
1725 (type ansi-stream stream)
1727 (type sequence-end %end)
1729 (let ((end (or %end (length seq))))
1730 (declare (type index end))
1733 (let ((read-function
1734 (if (subtypep (stream-element-type stream) 'character)
1737 (do ((rem (nthcdr start seq) (rest rem))
1739 ((or (endp rem) (>= i end)) i)
1740 (declare (type list rem)
1742 (let ((el (funcall read-function stream nil :eof)))
1745 (setf (first rem) el)))))
1747 (with-array-data ((data seq) (offset-start start) (offset-end end))
1749 ((or (simple-array (unsigned-byte 8) (*))
1750 (simple-array (signed-byte 8) (*))
1752 (let* ((numbytes (- end start))
1753 (bytes-read (sb!sys:read-n-bytes stream
1758 (if (< bytes-read numbytes)
1759 (+ start bytes-read)
1762 (let ((read-function
1763 (if (subtypep (stream-element-type stream) 'character)
1766 (do ((i offset-start (1+ i)))
1767 ((>= i offset-end) end)
1768 (declare (type index i))
1769 (let ((el (funcall read-function stream nil :eof)))
1771 (return (+ start (- i offset-start))))
1772 (setf (aref data i) el)))))))))))
1776 (defun write-sequence (seq stream &key (start 0) (end nil))
1778 "Write the elements of SEQ bounded by START and END to STREAM."
1779 (declare (type sequence seq)
1780 (type stream stream)
1782 (type sequence-end end)
1784 (if (ansi-stream-p stream)
1785 (ansi-stream-write-sequence seq stream start end)
1786 ;; must be Gray-streams FUNDAMENTAL-STREAM
1787 (stream-write-sequence stream seq start end)))
1789 (defun ansi-stream-write-sequence (seq stream start %end)
1790 (declare (type sequence seq)
1791 (type ansi-stream stream)
1793 (type sequence-end %end)
1795 (let ((end (or %end (length seq))))
1796 (declare (type index end))
1799 (let ((write-function
1800 (if (subtypep (stream-element-type stream) 'character)
1803 (do ((rem (nthcdr start seq) (rest rem))
1805 ((or (endp rem) (>= i end)) seq)
1806 (declare (type list rem)
1808 (funcall write-function (first rem) stream))))
1810 (%write-string seq stream start end))
1812 (let ((write-function
1813 (if (subtypep (stream-element-type stream) 'character)
1816 (do ((i start (1+ i)))
1818 (declare (type index i))
1819 (funcall write-function (aref seq i) stream)))))))
1823 ;;; (These were inline throughout this file, but that's not appropriate
1825 (declaim (maybe-inline read-char unread-char read-byte listen))