a90e96fd0d463da8e0799ae45f2b8362e7b778be
[sbcl.git] / src / code / stream.lisp
1 ;;;; os-independent stream functions
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!IMPL")
13
14 ;;;; standard streams
15
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")
25
26 (defun ill-in (stream &rest ignore)
27   (declare (ignore ignore))
28   (error 'simple-type-error
29          :datum stream
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
36          :datum stream
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
43          :datum stream
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
50          :datum stream
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)))
59 \f
60 ;;; stream manipulation functions
61
62 (declaim (inline ansi-stream-input-stream-p))
63 (defun ansi-stream-input-stream-p (stream)
64   (declare (type ansi-stream stream))
65
66   #!+high-security
67   (when (synonym-stream-p stream)
68     (setf stream
69           (symbol-value (synonym-stream-symbol stream))))
70
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)))))
78
79 (defun input-stream-p (stream)
80   (declare (type stream stream))
81   (and (ansi-stream-p stream)
82        (ansi-stream-input-stream-p stream)))
83
84 (declaim (inline ansi-stream-output-stream-p))
85 (defun ansi-stream-output-stream-p (stream)
86   (declare (type ansi-stream stream))
87
88   #!+high-security
89   (when (synonym-stream-p stream)
90     (setf stream (symbol-value
91                   (synonym-stream-symbol stream))))
92
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)))))
96
97 (defun output-stream-p (stream)
98   (declare (type stream stream))
99
100   (and (ansi-stream-p stream)
101        (ansi-stream-output-stream-p stream)))
102
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)))
107
108 (defun open-stream-p (stream)
109   (ansi-stream-open-stream-p stream))
110
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))
115
116 (defun stream-element-type (stream)
117   (ansi-stream-element-type stream))
118
119 (defun interactive-stream-p (stream)
120   (declare (type stream stream))
121   (funcall (ansi-stream-misc stream) stream :interactive-p))
122
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))
128   t)
129
130 (defun close (stream &key abort)
131   (ansi-stream-close stream abort))
132
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))
142 \f
143 ;;;; file position and file length
144
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 (member nil :start :end)) position))
149   (cond
150    (position
151     (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
152     (funcall (ansi-stream-misc stream) stream :file-position position))
153    (t
154     (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
155       (when res
156         (- res
157            (- +ansi-stream-in-buffer-length+
158               (ansi-stream-in-index stream))))))))
159
160 ;;; This is a literal translation of the ANSI glossary entry "stream
161 ;;; associated with a file".
162 ;;;
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))))))
177
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)
188            :format-string
189            "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
190            :format-arguments (list stream))))
191
192 ;;; like FILE-POSITION, only using :FILE-LENGTH
193 (defun file-length (stream)
194   (declare (type (or file-stream synonym-stream) stream))
195   (stream-must-be-associated-with-file stream)
196   (funcall (ansi-stream-misc stream) stream :file-length))
197 \f
198 ;;;; input functions
199
200 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
201                             recursive-p)
202   (declare (ignore recursive-p))
203   (let ((stream (in-synonym-of stream)))
204     (if (ansi-stream-p stream)
205         (prepare-for-fast-read-char stream
206           (let ((res (make-string 80))
207                 (len 80)
208                 (index 0))
209             (loop
210              (let ((ch (fast-read-char nil nil)))
211                (cond (ch
212                       (when (char= ch #\newline)
213                         (done-with-fast-read-char)
214                         (return (values (shrink-vector res index) nil)))
215                       (when (= index len)
216                         (setq len (* len 2))
217                         (let ((new (make-string len)))
218                           (replace new res)
219                           (setq res new)))
220                       (setf (schar res index) ch)
221                       (incf index))
222                      ((zerop index)
223                       (done-with-fast-read-char)
224                       (return (values (eof-or-lose stream
225                                                    eof-error-p
226                                                    eof-value)
227                                       t)))
228                      ;; Since FAST-READ-CHAR already hit the eof char, we
229                      ;; shouldn't do another READ-CHAR.
230                      (t
231                       (done-with-fast-read-char)
232                       (return (values (shrink-vector res index) t))))))))
233         ;; must be Gray streams FUNDAMENTAL-STREAM
234         (multiple-value-bind (string eof) (stream-read-line stream)
235           (if (and eof (zerop (length string)))
236               (values (eof-or-lose stream eof-error-p eof-value) t)
237               (values string eof))))))
238
239 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
240 ;;; so, except in this file, they are not inline by default, but they can be.
241 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
242
243 (defun read-char (&optional (stream *standard-input*)
244                             (eof-error-p t)
245                             eof-value
246                             recursive-p)
247   (declare (ignore recursive-p))
248   (let ((stream (in-synonym-of stream)))
249     (if (ansi-stream-p stream)
250         (prepare-for-fast-read-char stream
251           (prog1
252               (fast-read-char eof-error-p eof-value)
253             (done-with-fast-read-char)))
254         ;; must be Gray streams FUNDAMENTAL-STREAM
255         (let ((char (stream-read-char stream)))
256           (if (eq char :eof)
257               (eof-or-lose stream eof-error-p eof-value)
258               char)))))
259
260 (defun unread-char (character &optional (stream *standard-input*))
261   (let ((stream (in-synonym-of stream)))
262     (if (ansi-stream-p stream)
263         (let ((index (1- (ansi-stream-in-index stream)))
264               (buffer (ansi-stream-in-buffer stream)))
265           (declare (fixnum index))
266           (when (minusp index) (error "nothing to unread"))
267           (cond (buffer
268                  (setf (aref buffer index) (char-code character))
269                  (setf (ansi-stream-in-index stream) index))
270                 (t
271                  (funcall (ansi-stream-misc stream) stream
272                           :unread character))))
273         ;; must be Gray streams FUNDAMENTAL-STREAM
274         (stream-unread-char stream character)))
275   nil)
276
277
278 ;;; In the interest of ``once and only once'' this macro contains the
279 ;;; framework necessary to implement a peek-char function, which has
280 ;;; two special-cases (one for gray streams and one for echo streams)
281 ;;; in addition to the normal case.
282 ;;;
283 ;;; All arguments are forms which will be used for a specific purpose
284 ;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
285 ;;; EOF-VALUE - the eof-value argument to peek-char
286 ;;; CHAR-VAR - the variable which will be used to store the current character
287 ;;; READ-FORM - the form which will be used to read a character
288 ;;; UNREAD-FORM - ditto for unread-char
289 ;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
290 ;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
291 ;;;                     (this will default to CHAR-VAR)
292 (defmacro generalized-peeking-mechanism (peek-type eof-value char-var read-form unread-form &optional (skipped-char-form nil) (eof-detected-form nil))
293   `(let ((,char-var ,read-form))
294     (cond ((eql ,char-var ,eof-value) 
295            ,(if eof-detected-form
296                 eof-detected-form
297                 char-var))
298           ((characterp ,peek-type)
299            (do ((,char-var ,char-var ,read-form))
300                ((or (eql ,char-var ,eof-value) 
301                     (char= ,char-var ,peek-type))
302                 (cond ((eql ,char-var ,eof-value)
303                        ,(if eof-detected-form
304                             eof-detected-form
305                             char-var))
306                       (t ,unread-form
307                          ,char-var)))
308              ,skipped-char-form))
309           ((eql ,peek-type t)
310            (do ((,char-var ,char-var ,read-form))
311                ((or (eql ,char-var ,eof-value)
312                     (not (whitespace-char-p ,char-var)))
313                 (cond ((eql ,char-var ,eof-value)
314                        ,(if eof-detected-form
315                             eof-detected-form
316                             char-var))
317                       (t ,unread-form
318                          ,char-var)))
319              ,skipped-char-form))
320           ((null ,peek-type)
321            ,unread-form
322            ,char-var)
323           (t
324            (bug "Impossible case reached in PEEK-CHAR")))))
325
326 (defun peek-char (&optional (peek-type nil)
327                             (stream *standard-input*)
328                             (eof-error-p t)
329                             eof-value
330                             recursive-p)
331   (declare (ignore recursive-p))
332   ;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
333   ;; the compiler doesn't seem to be smart enough to go from there to
334   ;; imposing a type check. Figure out why (because PEEK-TYPE is an
335   ;; &OPTIONAL argument?) and fix it, and then this explicit type
336   ;; check can go away.
337   (unless (typep peek-type '(or character boolean))
338     (error 'simple-type-error
339            :datum peek-type
340            :expected-type '(or character boolean)
341            :format-control "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>"
342            :format-arguments (list peek-type '(or character boolean))))
343   (let ((stream (in-synonym-of stream)))
344     (cond ((typep stream 'echo-stream)
345            (echo-misc stream 
346                       :peek-char
347                       peek-type
348                       (list eof-error-p eof-value)))
349           ((ansi-stream-p stream)
350            (generalized-peeking-mechanism
351             peek-type eof-value char
352             (read-char stream eof-error-p eof-value)
353             (unread-char char stream)))
354           (t
355            ;; by elimination, must be Gray streams FUNDAMENTAL-STREAM
356            (generalized-peeking-mechanism
357             peek-type :eof char
358             (if (null peek-type)
359                 (stream-peek-char stream)
360                 (stream-read-char stream))
361             (if (null peek-type)
362                 ()
363                 (stream-unread-char stream char))
364             ()
365             (eof-or-lose stream eof-error-p eof-value))))))
366
367 (defun listen (&optional (stream *standard-input*))
368   (let ((stream (in-synonym-of stream)))
369     (if (ansi-stream-p stream)
370         (or (/= (the fixnum (ansi-stream-in-index stream))
371                 +ansi-stream-in-buffer-length+)
372             ;; Test for T explicitly since misc methods return :EOF sometimes.
373             (eq (funcall (ansi-stream-misc stream) stream :listen) t))
374         ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
375         (stream-listen stream))))
376
377 (defun read-char-no-hang (&optional (stream *standard-input*)
378                                     (eof-error-p t)
379                                     eof-value
380                                     recursive-p)
381   (declare (ignore recursive-p))
382   (let ((stream (in-synonym-of stream)))
383     (if (ansi-stream-p stream)
384         (if (funcall (ansi-stream-misc stream) stream :listen)
385             ;; On T or :EOF get READ-CHAR to do the work.
386             (read-char stream eof-error-p eof-value)
387             nil)
388         ;; must be Gray streams FUNDAMENTAL-STREAM
389         (let ((char (stream-read-char-no-hang stream)))
390           (if (eq char :eof)
391               (eof-or-lose stream eof-error-p eof-value)
392               char)))))
393
394 (defun clear-input (&optional (stream *standard-input*))
395   (let ((stream (in-synonym-of stream)))
396     (cond ((ansi-stream-p stream)
397            (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
398            (funcall (ansi-stream-misc stream) stream :clear-input))
399           (t
400            (stream-clear-input stream))))
401   nil)
402 \f
403 (declaim (maybe-inline read-byte))
404 (defun read-byte (stream &optional (eof-error-p t) eof-value)
405   (let ((stream (in-synonym-of stream)))
406     (if (ansi-stream-p stream)
407         (prepare-for-fast-read-byte stream
408           (prog1
409               (fast-read-byte eof-error-p eof-value t)
410             (done-with-fast-read-byte)))
411         ;; must be Gray streams FUNDAMENTAL-STREAM
412         (let ((char (stream-read-byte stream)))
413           (if (eq char :eof)
414               (eof-or-lose stream eof-error-p eof-value)
415               char)))))
416
417 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
418 ;;; number of bytes read.
419 ;;;
420 ;;; Note: CMU CL's version of this had a special interpretation of
421 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
422 ;;; case, CMU CL's version would return as soon as any data became
423 ;;; available.) This could be useful behavior for things like pipes in
424 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
425 ;;; If we ever need it, it could be added later as a new variant N-BIN
426 ;;; method (perhaps N-BIN-ASAP?) or something.
427 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
428   (declare (type ansi-stream stream)
429            (type index numbytes start)
430            (type (or (simple-array * (*)) system-area-pointer) buffer))
431   (let* ((stream (in-synonym-of stream ansi-stream))
432          (in-buffer (ansi-stream-in-buffer stream))
433          (index (ansi-stream-in-index stream))
434          (num-buffered (- +ansi-stream-in-buffer-length+ index)))
435     (declare (fixnum index num-buffered))
436     (cond
437      ((not in-buffer)
438       (funcall (ansi-stream-n-bin stream)
439                stream
440                buffer
441                start
442                numbytes
443                eof-error-p))
444      ((<= numbytes num-buffered)
445       (%byte-blt in-buffer index
446                  buffer start (+ start numbytes))
447       (setf (ansi-stream-in-index stream) (+ index numbytes))
448       numbytes)
449      (t
450       (let ((end (+ start num-buffered)))
451         (%byte-blt in-buffer index buffer start end)
452         (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
453         (+ (funcall (ansi-stream-n-bin stream)
454                     stream
455                     buffer
456                     end
457                     (- numbytes num-buffered)
458                     eof-error-p)
459            num-buffered))))))
460
461 ;;; the amount of space we leave at the start of the in-buffer for
462 ;;; unreading
463 ;;;
464 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
465 (defconstant +ansi-stream-in-buffer-extra+
466   4) ; FIXME: should be symbolic constant
467
468 ;;; This function is called by the FAST-READ-CHAR expansion to refill
469 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
470 ;;; and hence must be an N-BIN method.
471 (defun fast-read-char-refill (stream eof-error-p eof-value)
472   (let* ((ibuf (ansi-stream-in-buffer stream))
473          (count (funcall (ansi-stream-n-bin stream)
474                          stream
475                          ibuf
476                          +ansi-stream-in-buffer-extra+
477                          (- +ansi-stream-in-buffer-length+
478                             +ansi-stream-in-buffer-extra+)
479                          nil))
480          (start (- +ansi-stream-in-buffer-length+ count)))
481     (declare (type index start count))
482     (cond ((zerop count)
483            (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
484            (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
485           (t
486            (when (/= start +ansi-stream-in-buffer-extra+)
487              (bit-bash-copy ibuf (+ (* +ansi-stream-in-buffer-extra+
488                                        sb!vm:n-byte-bits)
489                                     (* sb!vm:vector-data-offset
490                                        sb!vm:n-word-bits))
491                             ibuf (+ (the index (* start sb!vm:n-byte-bits))
492                                     (* sb!vm:vector-data-offset
493                                        sb!vm:n-word-bits))
494                             (* count sb!vm:n-byte-bits)))
495            (setf (ansi-stream-in-index stream) (1+ start))
496            (code-char (aref ibuf start))))))
497
498 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
499 ;;; leave room for unreading.
500 (defun fast-read-byte-refill (stream eof-error-p eof-value)
501   (let* ((ibuf (ansi-stream-in-buffer stream))
502          (count (funcall (ansi-stream-n-bin stream) stream
503                          ibuf 0 +ansi-stream-in-buffer-length+
504                          nil))
505          (start (- +ansi-stream-in-buffer-length+ count)))
506     (declare (type index start count))
507     (cond ((zerop count)
508            (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
509            (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
510           (t
511            (unless (zerop start)
512              (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:n-word-bits)
513                             ibuf (+ (the index (* start sb!vm:n-byte-bits))
514                                     (* sb!vm:vector-data-offset
515                                        sb!vm:n-word-bits))
516                             (* count sb!vm:n-byte-bits)))
517            (setf (ansi-stream-in-index stream) (1+ start))
518            (aref ibuf start)))))
519 \f
520 ;;; output functions
521
522 (defun write-char (character &optional (stream *standard-output*))
523   (with-out-stream stream (ansi-stream-out character)
524                    (stream-write-char character))
525   character)
526
527 (defun terpri (&optional (stream *standard-output*))
528   (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
529   nil)
530
531 (defun fresh-line (&optional (stream *standard-output*))
532   (let ((stream (out-synonym-of stream)))
533     (if (ansi-stream-p stream)
534         (when (/= (or (charpos stream) 1) 0)
535           (funcall (ansi-stream-out stream) stream #\newline)
536           t)
537         ;; must be Gray streams FUNDAMENTAL-STREAM
538         (stream-fresh-line stream))))
539
540 (defun write-string (string &optional (stream *standard-output*)
541                             &key (start 0) end)
542   (declare (type string string))
543   ;; Note that even though you might expect, based on the behavior of
544   ;; things like AREF, that the correct upper bound here is
545   ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
546   ;; "bounding index" and "length" indicate that in this case (i.e.
547   ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
548   ;; (LENGTH STRING) is the required upper bound. A foolish
549   ;; consistency is the hobgoblin of lesser languages..
550   (%write-string string stream start (%check-vector-sequence-bounds
551                                       string start end))
552   string)
553
554 (defun %write-string (string stream start end)
555   (declare (type string string))
556   (declare (type streamlike stream))
557   (declare (type index start end))
558   (let ((stream (out-synonym-of stream)))
559     (cond ((ansi-stream-p stream)
560            (if (array-header-p string)
561                (with-array-data ((data string) (offset-start start)
562                                  (offset-end end))
563                  (funcall (ansi-stream-sout stream)
564                           stream data offset-start offset-end))
565                (funcall (ansi-stream-sout stream) stream string start end))
566            string)
567           (t ; must be Gray streams FUNDAMENTAL-STREAM
568            (stream-write-string stream string start end)))))
569
570 (defun write-line (string &optional (stream *standard-output*)
571                           &key (start 0) end)
572   (declare (type string string))
573   ;; FIXME: Why is there this difference between the treatments of the
574   ;; STREAM argument in WRITE-STRING and WRITE-LINE?
575   (let ((defaulted-stream (out-synonym-of stream)))
576     (%write-string string defaulted-stream start (%check-vector-sequence-bounds
577                                                   string start end))
578     (write-char #\newline defaulted-stream))
579   string)
580
581 (defun charpos (&optional (stream *standard-output*))
582   (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
583
584 (defun line-length (&optional (stream *standard-output*))
585   (with-out-stream stream (ansi-stream-misc :line-length)
586                    (stream-line-length)))
587
588 (defun finish-output (&optional (stream *standard-output*))
589   (with-out-stream stream (ansi-stream-misc :finish-output)
590                    (stream-finish-output))
591   nil)
592
593 (defun force-output (&optional (stream *standard-output*))
594   (with-out-stream stream (ansi-stream-misc :force-output)
595                    (stream-force-output))
596   nil)
597
598 (defun clear-output (&optional (stream *standard-output*))
599   (with-out-stream stream (ansi-stream-misc :clear-output)
600                    (stream-force-output))
601   nil)
602
603 (defun write-byte (integer stream)
604   (with-out-stream stream (ansi-stream-bout integer)
605                    (stream-write-byte integer))
606   integer)
607 \f
608 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
609 ;;; streams to handle the misc routines and dispatch to the
610 ;;; appropriate Gray stream functions.
611 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
612   (declare (type fundamental-stream stream)
613            (ignore arg2))
614   (case operation
615     (:listen
616      ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
617      (let ((char (stream-read-char-no-hang stream)))
618        (when (characterp char)
619          (stream-unread-char stream char))
620        char))
621     (:unread
622      (stream-unread-char stream arg1))
623     (:close
624      (close stream))
625     (:clear-input
626      (stream-clear-input stream))
627     (:force-output
628      (stream-force-output stream))
629     (:finish-output
630      (stream-finish-output stream))
631     (:element-type
632      (stream-element-type stream))
633     (:interactive-p
634      (interactive-stream-p stream))
635     (:line-length
636      (stream-line-length stream))
637     (:charpos
638      (stream-line-column stream))
639     (:file-length
640      (file-length stream))
641     (:file-position
642      (file-position stream arg1))))
643 \f
644 ;;;; broadcast streams
645
646 (defstruct (broadcast-stream (:include ansi-stream
647                                        (out #'broadcast-out)
648                                        (bout #'broadcast-bout)
649                                        (sout #'broadcast-sout)
650                                        (misc #'broadcast-misc))
651                              (:constructor %make-broadcast-stream
652                                            (&rest streams))
653                              (:copier nil))
654   ;; a list of all the streams we broadcast to
655   (streams () :type list :read-only t))
656
657 (defun make-broadcast-stream (&rest streams)
658   (dolist (stream streams)
659     (unless (or (and (synonym-stream-p stream)
660                      (output-stream-p (symbol-value
661                                        (synonym-stream-symbol stream))))
662                 (output-stream-p stream))
663       (error 'type-error
664              :datum stream
665              :expected-type '(satisfies output-stream-p))))
666   (apply #'%make-broadcast-stream streams))
667
668 (macrolet ((out-fun (fun method stream-method &rest args)
669              `(defun ,fun (stream ,@args)
670                 (dolist (stream (broadcast-stream-streams stream))
671                   (if (ansi-stream-p stream)
672                       (funcall (,method stream) stream ,@args)
673                       (,stream-method stream ,@args))))))
674   (out-fun broadcast-out ansi-stream-out stream-write-char char)
675   (out-fun broadcast-bout ansi-stream-bout stream-write-byte byte)
676   (out-fun broadcast-sout ansi-stream-sout stream-write-string
677            string start end))
678
679 (defun broadcast-misc (stream operation &optional arg1 arg2)
680   (let ((streams (broadcast-stream-streams stream)))
681     (case operation
682       (:charpos
683        (dolist (stream streams)
684          (let ((charpos (charpos stream)))
685            (if charpos (return charpos)))))
686       (:line-length
687        (let ((min nil))
688          (dolist (stream streams min)
689            (let ((res (line-length stream)))
690              (when res (setq min (if min (min res min) res)))))))
691       (:element-type
692        (let (res)
693          (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
694            (pushnew (stream-element-type stream) res :test #'equal))))
695       (:close)
696       (t
697        (let ((res nil))
698          (dolist (stream streams res)
699            (setq res
700                  (if (ansi-stream-p stream)
701                      (funcall (ansi-stream-misc stream) stream operation
702                               arg1 arg2)
703                      (stream-misc-dispatch stream operation arg1 arg2)))))))))
704 \f
705 ;;;; synonym streams
706
707 (defstruct (synonym-stream (:include ansi-stream
708                                      (in #'synonym-in)
709                                      (bin #'synonym-bin)
710                                      (n-bin #'synonym-n-bin)
711                                      (out #'synonym-out)
712                                      (bout #'synonym-bout)
713                                      (sout #'synonym-sout)
714                                      (misc #'synonym-misc))
715                            (:constructor make-synonym-stream (symbol))
716                            (:copier nil))
717   ;; This is the symbol, the value of which is the stream we are synonym to.
718   (symbol nil :type symbol :read-only t))
719 (def!method print-object ((x synonym-stream) stream)
720   (print-unreadable-object (x stream :type t :identity t)
721     (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
722
723 ;;; The output simple output methods just call the corresponding method
724 ;;; in the synonymed stream.
725 (macrolet ((out-fun (name slot stream-method &rest args)
726              `(defun ,name (stream ,@args)
727                 (declare (optimize (safety 1)))
728                 (let ((syn (symbol-value (synonym-stream-symbol stream))))
729                   (if (ansi-stream-p syn)
730                       (funcall (,slot syn) syn ,@args)
731                       (,stream-method syn ,@args))))))
732   (out-fun synonym-out ansi-stream-out stream-write-char ch)
733   (out-fun synonym-bout ansi-stream-bout stream-write-byte n)
734   (out-fun synonym-sout ansi-stream-sout stream-write-string string start end))
735
736 ;;; For the input methods, we just call the corresponding function on the
737 ;;; synonymed stream. These functions deal with getting input out of
738 ;;; the In-Buffer if there is any.
739 (macrolet ((in-fun (name fun &rest args)
740              `(defun ,name (stream ,@args)
741                 (declare (optimize (safety 1)))
742                 (,fun (symbol-value (synonym-stream-symbol stream))
743                       ,@args))))
744   (in-fun synonym-in read-char eof-error-p eof-value)
745   (in-fun synonym-bin read-byte eof-error-p eof-value)
746   (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
747
748 (defun synonym-misc (stream operation &optional arg1 arg2)
749   (declare (optimize (safety 1)))
750   (let ((syn (symbol-value (synonym-stream-symbol stream))))
751     (if (ansi-stream-p syn)
752         ;; We have to special-case some operations which interact with
753         ;; the in-buffer of the wrapped stream, since just calling
754         ;; ANSI-STREAM-MISC on them
755         (case operation
756           (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
757                            +ansi-stream-in-buffer-length+)
758                        (funcall (ansi-stream-misc syn) syn :listen)))
759           (:clear-input (clear-input syn))
760           (:unread (unread-char arg1 syn))
761           (t
762            (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
763         (stream-misc-dispatch syn operation arg1 arg2))))
764 \f
765 ;;;; two-way streams
766
767 (defstruct (two-way-stream
768             (:include ansi-stream
769                       (in #'two-way-in)
770                       (bin #'two-way-bin)
771                       (n-bin #'two-way-n-bin)
772                       (out #'two-way-out)
773                       (bout #'two-way-bout)
774                       (sout #'two-way-sout)
775                       (misc #'two-way-misc))
776             (:constructor %make-two-way-stream (input-stream output-stream))
777             (:copier nil))
778   (input-stream (missing-arg) :type stream :read-only t)
779   (output-stream (missing-arg) :type stream :read-only t))
780 (defprinter (two-way-stream) input-stream output-stream)
781
782 (defun make-two-way-stream (input-stream output-stream)
783   #!+sb-doc
784   "Return a bidirectional stream which gets its input from INPUT-STREAM and
785    sends its output to OUTPUT-STREAM."
786   ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
787   ;; should be encapsulated in a function, and used here and most of
788   ;; the other places that SYNONYM-STREAM-P appears.
789   (unless (or (and (synonym-stream-p output-stream)
790                    (output-stream-p (symbol-value
791                                      (synonym-stream-symbol output-stream))))
792               (output-stream-p output-stream))
793     (error 'type-error
794            :datum output-stream
795            :expected-type '(satisfies output-stream-p)))
796   (unless (or (and (synonym-stream-p input-stream)
797                    (input-stream-p (symbol-value
798                                     (synonym-stream-symbol input-stream))))
799               (input-stream-p input-stream))
800     (error 'type-error
801            :datum input-stream
802            :expected-type '(satisfies input-stream-p)))
803   (funcall #'%make-two-way-stream input-stream output-stream))
804
805 (macrolet ((out-fun (name slot stream-method &rest args)
806              `(defun ,name (stream ,@args)
807                 (let ((syn (two-way-stream-output-stream stream)))
808                   (if (ansi-stream-p syn)
809                       (funcall (,slot syn) syn ,@args)
810                       (,stream-method syn ,@args))))))
811   (out-fun two-way-out ansi-stream-out stream-write-char ch)
812   (out-fun two-way-bout ansi-stream-bout stream-write-byte n)
813   (out-fun two-way-sout ansi-stream-sout stream-write-string string start end))
814
815 (macrolet ((in-fun (name fun &rest args)
816              `(defun ,name (stream ,@args)
817                 (force-output (two-way-stream-output-stream stream))
818                 (,fun (two-way-stream-input-stream stream) ,@args))))
819   (in-fun two-way-in read-char eof-error-p eof-value)
820   (in-fun two-way-bin read-byte eof-error-p eof-value)
821   (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
822
823 (defun two-way-misc (stream operation &optional arg1 arg2)
824   (let* ((in (two-way-stream-input-stream stream))
825          (out (two-way-stream-output-stream stream))
826          (in-ansi-stream-p (ansi-stream-p in))
827          (out-ansi-stream-p (ansi-stream-p out)))
828     (case operation
829       (:listen
830        (if in-ansi-stream-p
831            (or (/= (the fixnum (ansi-stream-in-index in))
832                    +ansi-stream-in-buffer-length+)
833                (funcall (ansi-stream-misc in) in :listen))
834            (stream-listen in)))
835       ((:finish-output :force-output :clear-output)
836        (if out-ansi-stream-p
837            (funcall (ansi-stream-misc out) out operation arg1 arg2)
838            (stream-misc-dispatch out operation arg1 arg2)))
839       (:clear-input (clear-input in))
840       (:unread (unread-char arg1 in))
841       (:element-type
842        (let ((in-type (stream-element-type in))
843              (out-type (stream-element-type out)))
844          (if (equal in-type out-type)
845              in-type `(and ,in-type ,out-type))))
846       (:close
847        (set-closed-flame stream))
848       (t
849        (or (if in-ansi-stream-p
850                (funcall (ansi-stream-misc in) in operation arg1 arg2)
851                (stream-misc-dispatch in operation arg1 arg2))
852            (if out-ansi-stream-p
853                (funcall (ansi-stream-misc out) out operation arg1 arg2)
854                (stream-misc-dispatch out operation arg1 arg2)))))))
855 \f
856 ;;;; concatenated streams
857
858 (defstruct (concatenated-stream
859             (:include ansi-stream
860                       (in #'concatenated-in)
861                       (bin #'concatenated-bin)
862                       (n-bin #'concatenated-n-bin)
863                       (misc #'concatenated-misc))
864             (:constructor %make-concatenated-stream
865                           (&rest streams &aux (current streams)))
866             (:copier nil))
867   ;; The car of this is the substream we are reading from now.
868   current
869   ;; This is a list of all the substreams there ever were. We need to
870   ;; remember them so that we can close them.
871   ;;
872   ;; FIXME: ANSI says this is supposed to be the list of streams that
873   ;; we still have to read from. So either this needs to become a
874   ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
875   ;; around it which discards closed files from the head of the list)
876   ;; or we need to update it as we run out of files.
877   (streams nil :type list :read-only t))
878 (def!method print-object ((x concatenated-stream) stream)
879   (print-unreadable-object (x stream :type t :identity t)
880     (format stream
881             ":STREAMS ~S"
882             (concatenated-stream-streams x))))
883
884 (defun make-concatenated-stream (&rest streams)
885   #!+sb-doc
886   "Return a stream which takes its input from each of the streams in turn,
887    going on to the next at EOF."
888   (dolist (stream streams)
889     (unless (or (and (synonym-stream-p stream)
890                      (input-stream-p (symbol-value
891                                       (synonym-stream-symbol stream))))
892                 (input-stream-p stream))
893       (error 'type-error
894              :datum stream
895              :expected-type '(satisfies input-stream-p))))
896   (apply #'%make-concatenated-stream streams))
897
898 (macrolet ((in-fun (name fun)
899              `(defun ,name (stream eof-error-p eof-value)
900                 (do ((current (concatenated-stream-current stream)
901                               (cdr current)))
902                     ((null current)
903                      (eof-or-lose stream eof-error-p eof-value))
904                   (let* ((stream (car current))
905                          (result (,fun stream nil nil)))
906                     (when result (return result)))
907                   (pop (concatenated-stream-current stream))))))
908   (in-fun concatenated-in read-char)
909   (in-fun concatenated-bin read-byte))
910
911 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
912   (do ((current (concatenated-stream-current stream) (cdr current))
913        (current-start start)
914        (remaining-bytes numbytes))
915       ((null current)
916        (if eof-errorp
917            (error 'end-of-file :stream stream)
918            (- numbytes remaining-bytes)))
919     (let* ((stream (car current))
920            (bytes-read (read-n-bytes stream buffer current-start
921                                      remaining-bytes nil)))
922       (incf current-start bytes-read)
923       (decf remaining-bytes bytes-read)
924       (when (zerop remaining-bytes) (return numbytes)))
925     (setf (concatenated-stream-current stream) (cdr current))))
926
927 (defun concatenated-misc (stream operation &optional arg1 arg2)
928   (let ((left (concatenated-stream-current stream)))
929     (when left
930       (let* ((current (car left)))
931         (case operation
932           (:listen
933            (loop
934              (let ((stuff (if (ansi-stream-p current)
935                               (funcall (ansi-stream-misc current) current
936                                        :listen)
937                               (stream-misc-dispatch current :listen))))
938                (cond ((eq stuff :eof)
939                       ;; Advance CURRENT, and try again.
940                       (pop (concatenated-stream-current stream))
941                       (setf current
942                             (car (concatenated-stream-current stream)))
943                       (unless current
944                         ;; No further streams. EOF.
945                         (return :eof)))
946                      (stuff
947                       ;; Stuff's available.
948                       (return t))
949                      (t
950                       ;; Nothing is available yet.
951                       (return nil))))))
952           (:clear-input (clear-input current))
953           (:unread (unread-char arg1 current))
954           (:close
955            (set-closed-flame stream))
956           (t
957            (if (ansi-stream-p current)
958                (funcall (ansi-stream-misc current) current operation arg1 arg2)
959                (stream-misc-dispatch current operation arg1 arg2))))))))
960 \f
961 ;;;; echo streams
962
963 (defstruct (echo-stream
964             (:include two-way-stream
965                       (in #'echo-in)
966                       (bin #'echo-bin)
967                       (misc #'echo-misc)
968                       (n-bin #'ill-bin))
969             (:constructor %make-echo-stream (input-stream output-stream))
970             (:copier nil))
971   unread-stuff)
972 (def!method print-object ((x echo-stream) stream)
973   (print-unreadable-object (x stream :type t :identity t)
974     (format stream
975             ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
976             (two-way-stream-input-stream x)
977             (two-way-stream-output-stream x))))
978
979 (defun make-echo-stream (input-stream output-stream)
980   #!+sb-doc
981   "Return a bidirectional stream which gets its input from INPUT-STREAM and
982    sends its output to OUTPUT-STREAM. In addition, all input is echoed to
983    the output stream."
984   (unless (or (and (synonym-stream-p output-stream)
985                    (output-stream-p (symbol-value
986                                      (synonym-stream-symbol output-stream))))
987               (output-stream-p output-stream))
988     (error 'type-error
989            :datum output-stream
990            :expected-type '(satisfies output-stream-p)))
991   (unless (or (and (synonym-stream-p input-stream)
992                    (input-stream-p (symbol-value
993                                     (synonym-stream-symbol input-stream))))
994               (input-stream-p input-stream))
995     (error 'type-error
996            :datum input-stream
997            :expected-type '(satisfies input-stream-p)))
998   (funcall #'%make-echo-stream input-stream output-stream))
999
1000 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
1001              `(defun ,name (stream ,@args)
1002                 (or (pop (echo-stream-unread-stuff stream))
1003                     (let* ((in (echo-stream-input-stream stream))
1004                            (out (echo-stream-output-stream stream))
1005                            (result (,fun in ,@args)))
1006                       (if (ansi-stream-p out)
1007                           (funcall (,out-slot out) out result)
1008                           (,stream-method out result))
1009                       result)))))
1010   (in-fun echo-in read-char ansi-stream-out stream-write-char
1011           eof-error-p eof-value)
1012   (in-fun echo-bin read-byte ansi-stream-bout stream-write-byte
1013           eof-error-p eof-value))
1014
1015 (defun echo-misc (stream operation &optional arg1 arg2)
1016   (let* ((in (two-way-stream-input-stream stream))
1017          (out (two-way-stream-output-stream stream)))
1018     (case operation
1019       (:listen
1020        (or (not (null (echo-stream-unread-stuff stream)))
1021            (if (ansi-stream-p in)
1022                (or (/= (the fixnum (ansi-stream-in-index in))
1023                        +ansi-stream-in-buffer-length+)
1024                    (funcall (ansi-stream-misc in) in :listen))
1025                (stream-misc-dispatch in :listen))))
1026       (:unread (push arg1 (echo-stream-unread-stuff stream)))
1027       (:element-type
1028        (let ((in-type (stream-element-type in))
1029              (out-type (stream-element-type out)))
1030          (if (equal in-type out-type)
1031              in-type `(and ,in-type ,out-type))))
1032       (:close
1033        (set-closed-flame stream))
1034       (:peek-char
1035        ;; For the special case of peeking into an echo-stream
1036        ;; arg1 is PEEK-TYPE, arg2 is (EOF-ERROR-P EOF-VALUE)
1037        ;; returns peeked-char, eof-value, or errors end-of-file
1038        ;;
1039        ;; Note: This code could be moved into PEEK-CHAR if desired.
1040        ;; I am unsure whether this belongs with echo-streams because it is
1041        ;; echo-stream specific, or PEEK-CHAR because it is peeking code.
1042        ;; -- mrd 2002-11-18
1043        ;;
1044        ;; UNREAD-CHAR-P indicates whether the current character was one
1045        ;; that was previously unread.  In that case, we need to ensure that
1046        ;; the semantics for UNREAD-CHAR are held; the character should
1047        ;; not be echoed again.
1048        (let ((unread-char-p nil))
1049          (flet ((outfn (c)
1050                   (unless unread-char-p
1051                     (if (ansi-stream-p out)
1052                         (funcall (ansi-stream-out out) out c)
1053                         ;; gray-stream
1054                         (stream-write-char out c))))
1055                 (infn ()
1056                   ;; Obtain input from unread buffer or input stream,
1057                   ;; and set the flag appropriately.
1058                   (cond ((not (null (echo-stream-unread-stuff stream)))
1059                          (setf unread-char-p t)
1060                          (pop (echo-stream-unread-stuff stream)))
1061                         (t
1062                          (setf unread-char-p nil)
1063                          (read-char in (first arg2) (second arg2))))))
1064            (generalized-peeking-mechanism
1065             arg1 (second arg2) char
1066             (infn)
1067             (unread-char char in)
1068             (outfn char)))))
1069       (t
1070        (or (if (ansi-stream-p in)
1071                (funcall (ansi-stream-misc in) in operation arg1 arg2)
1072                (stream-misc-dispatch in operation arg1 arg2))
1073            (if (ansi-stream-p out)
1074                (funcall (ansi-stream-misc out) out operation arg1 arg2)
1075                (stream-misc-dispatch out operation arg1 arg2)))))))
1076 \f
1077 ;;;; base STRING-STREAM stuff
1078
1079 (defstruct (string-stream
1080              (:include ansi-stream)
1081              (:constructor nil)
1082              (:copier nil))
1083   (string nil :type string))
1084 \f
1085 ;;;; STRING-INPUT-STREAM stuff
1086
1087 (defstruct (string-input-stream
1088              (:include string-stream
1089                        (in #'string-inch)
1090                        (bin #'string-binch)
1091                        (n-bin #'string-stream-read-n-bytes)
1092                        (misc #'string-in-misc)
1093                        (string nil :type simple-string))
1094              (:constructor internal-make-string-input-stream
1095                            (string current end))
1096              (:copier nil))
1097   (current nil :type index)
1098   (end nil :type index))
1099
1100 (defun string-inch (stream eof-error-p eof-value)
1101   (let ((string (string-input-stream-string stream))
1102         (index (string-input-stream-current stream)))
1103     (declare (simple-string string) (fixnum index))
1104     (cond ((= index (the index (string-input-stream-end stream)))
1105            (eof-or-lose stream eof-error-p eof-value))
1106           (t
1107            (setf (string-input-stream-current stream) (1+ index))
1108            (aref string index)))))
1109
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 (simple-string string)
1114              (type index index))
1115     (cond ((= index (the index (string-input-stream-end stream)))
1116            (eof-or-lose stream eof-error-p eof-value))
1117           (t
1118            (setf (string-input-stream-current stream) (1+ index))
1119            (char-code (aref string index))))))
1120
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 (simple-string string)
1129              (type index index available copy))
1130     (when (plusp 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)
1137                              buffer
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)
1143         copy)))
1144
1145 (defun string-in-misc (stream operation &optional arg1 arg2)
1146   (declare (ignore arg2))
1147   (case operation
1148     (:file-position
1149      (if arg1
1150          (setf (string-input-stream-current stream) arg1)
1151          (string-input-stream-current stream)))
1152     (:file-length (length (string-input-stream-string stream)))
1153     (:unread (decf (string-input-stream-current stream)))
1154     (:listen (or (/= (the fixnum (string-input-stream-current stream))
1155                      (the fixnum (string-input-stream-end stream)))
1156                  :eof))
1157     (:element-type 'base-char)))
1158
1159 (defun make-string-input-stream (string &optional
1160                                         (start 0) end)
1161   #!+sb-doc
1162   "Return an input stream which will supply the characters of STRING between
1163   START and END in order."
1164   (declare (type string string)
1165            (type index start)
1166            (type (or index null) end))
1167   
1168   (internal-make-string-input-stream
1169    (coerce string 'simple-string)
1170    start
1171    (%check-vector-sequence-bounds string start end)))
1172 \f
1173 ;;;; STRING-OUTPUT-STREAM stuff
1174
1175 (defstruct (string-output-stream
1176             (:include string-stream
1177                       (out #'string-ouch)
1178                       (sout #'string-sout)
1179                       (misc #'string-out-misc)
1180                       ;; The string we throw stuff in.
1181                       (string (make-string 40) :type simple-string))
1182             (:constructor make-string-output-stream ())
1183             (:copier nil))
1184   ;; Index of the next location to use.
1185   (index 0 :type fixnum))
1186
1187 #!+sb-doc
1188 (setf (fdocumentation 'make-string-output-stream 'function)
1189   "Return an output stream which will accumulate all output given it for
1190    the benefit of the function GET-OUTPUT-STREAM-STRING.")
1191
1192 (defun string-ouch (stream character)
1193   (let ((current (string-output-stream-index stream))
1194         (workspace (string-output-stream-string stream)))
1195     (declare (simple-string workspace) (fixnum current))
1196     (if (= current (the fixnum (length workspace)))
1197         (let ((new-workspace (make-string (* current 2))))
1198           (replace new-workspace workspace)
1199           (setf (aref new-workspace current) character)
1200           (setf (string-output-stream-string stream) new-workspace))
1201         (setf (aref workspace current) character))
1202     (setf (string-output-stream-index stream) (1+ current))))
1203
1204 (defun string-sout (stream string start end)
1205   (declare (simple-string string) (fixnum start end))
1206   (let* ((current (string-output-stream-index stream))
1207          (length (- end start))
1208          (dst-end (+ length current))
1209          (workspace (string-output-stream-string stream)))
1210     (declare (simple-string workspace)
1211              (fixnum current length dst-end))
1212     (if (> dst-end (the fixnum (length workspace)))
1213         (let ((new-workspace (make-string (+ (* current 2) length))))
1214           (replace new-workspace workspace :end2 current)
1215           (replace new-workspace string
1216                    :start1 current :end1 dst-end
1217                    :start2 start :end2 end)
1218           (setf (string-output-stream-string stream) new-workspace))
1219         (replace workspace string
1220                  :start1 current :end1 dst-end
1221                  :start2 start :end2 end))
1222     (setf (string-output-stream-index stream) dst-end)))
1223
1224 (defun string-out-misc (stream operation &optional arg1 arg2)
1225   (declare (ignore arg2))
1226   (case operation
1227     (:file-position
1228      (if (null arg1)
1229          (string-output-stream-index stream)))
1230     (:charpos
1231      (do ((index (1- (the fixnum (string-output-stream-index stream)))
1232                  (1- index))
1233           (count 0 (1+ count))
1234           (string (string-output-stream-string stream)))
1235          ((< index 0) count)
1236        (declare (simple-string string)
1237                 (fixnum index count))
1238        (if (char= (schar string index) #\newline)
1239            (return count))))
1240     (:element-type 'base-char)))
1241
1242 ;;; Return a string of all the characters sent to a stream made by
1243 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1244 (defun get-output-stream-string (stream)
1245   (declare (type string-output-stream stream))
1246   (let* ((length (string-output-stream-index stream))
1247          (result (make-string length)))
1248     (replace result (string-output-stream-string stream))
1249     (setf (string-output-stream-index stream) 0)
1250     result))
1251
1252 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1253 ;;; GET-OUTPUT-STREAM-STRING would return them.
1254 (defun dump-output-stream-string (in-stream out-stream)
1255   (%write-string (string-output-stream-string in-stream)
1256                  out-stream
1257                  0
1258                  (string-output-stream-index in-stream))
1259   (setf (string-output-stream-index in-stream) 0))
1260 \f
1261 ;;;; fill-pointer streams
1262
1263 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1264 ;;; the CLM, but they are required for the implementation of
1265 ;;; WITH-OUTPUT-TO-STRING.
1266
1267 (deftype string-with-fill-pointer ()
1268   '(and string
1269         (satisfies array-has-fill-pointer-p)))
1270
1271 (defstruct (fill-pointer-output-stream
1272             (:include string-stream
1273                       (out #'fill-pointer-ouch)
1274                       (sout #'fill-pointer-sout)
1275                       (misc #'fill-pointer-misc)
1276                       ;; a string with a fill pointer where we stuff
1277                       ;; the stuff we write
1278                       (string (error "missing argument")
1279                               :type string-with-fill-pointer
1280                               :read-only t))
1281             (:constructor make-fill-pointer-output-stream (string))
1282             (:copier nil)))
1283
1284 (defun fill-pointer-ouch (stream character)
1285   (let* ((buffer (fill-pointer-output-stream-string stream))
1286          (current (fill-pointer buffer))
1287          (current+1 (1+ current)))
1288     (declare (fixnum current))
1289     (with-array-data ((workspace buffer) (start) (end))
1290       (declare (simple-string workspace))
1291       (let ((offset-current (+ start current)))
1292         (declare (fixnum offset-current))
1293         (if (= offset-current end)
1294             (let* ((new-length (1+ (* current 2)))
1295                    (new-workspace (make-string new-length)))
1296               (declare (simple-string new-workspace))
1297               (%byte-blt workspace start
1298                          new-workspace 0 current)
1299               (setf workspace new-workspace)
1300               (setf offset-current current)
1301               (set-array-header buffer workspace new-length
1302                                 current+1 0 new-length nil))
1303             (setf (fill-pointer buffer) current+1))
1304         (setf (schar workspace offset-current) character)))
1305     current+1))
1306
1307 (defun fill-pointer-sout (stream string start end)
1308   (declare (simple-string string) (fixnum start end))
1309   (let* ((buffer (fill-pointer-output-stream-string stream))
1310          (current (fill-pointer buffer))
1311          (string-len (- end start))
1312          (dst-end (+ string-len current)))
1313     (declare (fixnum current dst-end string-len))
1314     (with-array-data ((workspace buffer) (dst-start) (dst-length))
1315       (declare (simple-string workspace))
1316       (let ((offset-dst-end (+ dst-start dst-end))
1317             (offset-current (+ dst-start current)))
1318         (declare (fixnum offset-dst-end offset-current))
1319         (if (> offset-dst-end dst-length)
1320             (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1321                    (new-workspace (make-string new-length)))
1322               (declare (simple-string new-workspace))
1323               (%byte-blt workspace dst-start
1324                          new-workspace 0 current)
1325               (setf workspace new-workspace)
1326               (setf offset-current current)
1327               (setf offset-dst-end dst-end)
1328               (set-array-header buffer
1329                                 workspace
1330                                 new-length
1331                                 dst-end
1332                                 0
1333                                 new-length
1334                                 nil))
1335             (setf (fill-pointer buffer) dst-end))
1336         (%byte-blt string start
1337                    workspace offset-current offset-dst-end)))
1338     dst-end))
1339
1340 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1341   (declare (ignore arg1 arg2))
1342   (case operation
1343     (:charpos
1344      (let* ((buffer (fill-pointer-output-stream-string stream))
1345             (current (fill-pointer buffer)))
1346        (with-array-data ((string buffer) (start) (end current))
1347          (declare (simple-string string) (ignore start))
1348          (let ((found (position #\newline string :test #'char=
1349                                 :end end :from-end t)))
1350            (if found
1351                (- end (the fixnum found))
1352                current)))))
1353      (:element-type 'base-char)))
1354 \f
1355 ;;;; indenting streams
1356
1357 (defstruct (indenting-stream (:include ansi-stream
1358                                        (out #'indenting-out)
1359                                        (sout #'indenting-sout)
1360                                        (misc #'indenting-misc))
1361                              (:constructor make-indenting-stream (stream))
1362                              (:copier nil))
1363   ;; the stream we're based on
1364   stream
1365   ;; how much we indent on each line
1366   (indentation 0))
1367
1368 #!+sb-doc
1369 (setf (fdocumentation 'make-indenting-stream 'function)
1370  "Return an output stream which indents its output by some amount.")
1371
1372 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1373 ;;; output on the given STREAM based on the specified SUB-STREAM.
1374 (defmacro indenting-indent (stream sub-stream)
1375   ;; KLUDGE: bare magic number 60
1376   `(do ((i 0 (+ i 60))
1377         (indentation (indenting-stream-indentation ,stream)))
1378        ((>= i indentation))
1379      (%write-string
1380       "                                                     "
1381       ,sub-stream
1382       0
1383       (min 60 (- indentation i)))))
1384
1385 ;;; INDENTING-OUT writes a character to an indenting stream.
1386 (defun indenting-out (stream char)
1387   (let ((sub-stream (indenting-stream-stream stream)))
1388     (write-char char sub-stream)
1389     (if (char= char #\newline)
1390         (indenting-indent stream sub-stream))))
1391
1392 ;;; INDENTING-SOUT writes a string to an indenting stream.
1393 (defun indenting-sout (stream string start end)
1394   (declare (simple-string string) (fixnum start end))
1395   (do ((i start)
1396        (sub-stream (indenting-stream-stream stream)))
1397       ((= i end))
1398     (let ((newline (position #\newline string :start i :end end)))
1399       (cond (newline
1400              (%write-string string sub-stream i (1+ newline))
1401              (indenting-indent stream sub-stream)
1402              (setq i (+ newline 1)))
1403             (t
1404              (%write-string string sub-stream i end)
1405              (setq i end))))))
1406
1407 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1408 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1409 ;;; the base stream minus the stream's indentation.
1410 (defun indenting-misc (stream operation &optional arg1 arg2)
1411   (let ((sub-stream (indenting-stream-stream stream)))
1412     (if (ansi-stream-p sub-stream)
1413         (let ((method (ansi-stream-misc sub-stream)))
1414           (case operation
1415             (:line-length
1416              (let ((line-length (funcall method sub-stream operation)))
1417                (if line-length
1418                    (- line-length (indenting-stream-indentation stream)))))
1419             (:charpos
1420              (let ((charpos (funcall method sub-stream operation)))
1421                (if charpos
1422                    (- charpos (indenting-stream-indentation stream)))))
1423             (t
1424              (funcall method sub-stream operation arg1 arg2))))
1425         ;; must be Gray streams FUNDAMENTAL-STREAM
1426         (case operation
1427           (:line-length
1428            (let ((line-length (stream-line-length sub-stream)))
1429              (if line-length
1430                  (- line-length (indenting-stream-indentation stream)))))
1431           (:charpos
1432            (let ((charpos (stream-line-column sub-stream)))
1433              (if charpos
1434                  (- charpos (indenting-stream-indentation stream)))))
1435           (t
1436            (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1437
1438 (declaim (maybe-inline read-char unread-char read-byte listen))
1439 \f
1440 ;;;; case frobbing streams, used by FORMAT ~(...~)
1441
1442 (defstruct (case-frob-stream
1443             (:include ansi-stream
1444                       (misc #'case-frob-misc))
1445             (:constructor %make-case-frob-stream (target out sout))
1446             (:copier nil))
1447   (target (missing-arg) :type stream))
1448
1449 (defun make-case-frob-stream (target kind)
1450   #!+sb-doc
1451   "Return a stream that sends all output to the stream TARGET, but modifies
1452    the case of letters, depending on KIND, which should be one of:
1453      :upcase - convert to upper case.
1454      :downcase - convert to lower case.
1455      :capitalize - convert the first letter of words to upper case and the
1456         rest of the word to lower case.
1457      :capitalize-first - convert the first letter of the first word to upper
1458         case and everything else to lower case."
1459   (declare (type stream target)
1460            (type (member :upcase :downcase :capitalize :capitalize-first)
1461                  kind)
1462            (values stream))
1463   (if (case-frob-stream-p target)
1464       ;; If we are going to be writing to a stream that already does
1465       ;; case frobbing, why bother frobbing the case just so it can
1466       ;; frob it again?
1467       target
1468       (multiple-value-bind (out sout)
1469           (ecase kind
1470             (:upcase
1471              (values #'case-frob-upcase-out
1472                      #'case-frob-upcase-sout))
1473             (:downcase
1474              (values #'case-frob-downcase-out
1475                      #'case-frob-downcase-sout))
1476             (:capitalize
1477              (values #'case-frob-capitalize-out
1478                      #'case-frob-capitalize-sout))
1479             (:capitalize-first
1480              (values #'case-frob-capitalize-first-out
1481                      #'case-frob-capitalize-first-sout)))
1482         (%make-case-frob-stream target out sout))))
1483
1484 (defun case-frob-misc (stream op &optional arg1 arg2)
1485   (declare (type case-frob-stream stream))
1486   (case op
1487     (:close)
1488     (t
1489      (let ((target (case-frob-stream-target stream)))
1490        (if (ansi-stream-p target)
1491            (funcall (ansi-stream-misc target) target op arg1 arg2)
1492            (stream-misc-dispatch target op arg1 arg2))))))
1493
1494 (defun case-frob-upcase-out (stream char)
1495   (declare (type case-frob-stream stream)
1496            (type base-char char))
1497   (let ((target (case-frob-stream-target stream))
1498         (char (char-upcase char)))
1499     (if (ansi-stream-p target)
1500         (funcall (ansi-stream-out target) target char)
1501         (stream-write-char target char))))
1502
1503 (defun case-frob-upcase-sout (stream str start end)
1504   (declare (type case-frob-stream stream)
1505            (type simple-base-string str)
1506            (type index start)
1507            (type (or index null) end))
1508   (let* ((target (case-frob-stream-target stream))
1509          (len (length str))
1510          (end (or end len))
1511          (string (if (and (zerop start) (= len end))
1512                      (string-upcase str)
1513                      (nstring-upcase (subseq str start end))))
1514          (string-len (- end start)))
1515     (if (ansi-stream-p target)
1516         (funcall (ansi-stream-sout target) target string 0 string-len)
1517         (stream-write-string target string 0 string-len))))
1518
1519 (defun case-frob-downcase-out (stream char)
1520   (declare (type case-frob-stream stream)
1521            (type base-char char))
1522   (let ((target (case-frob-stream-target stream))
1523         (char (char-downcase char)))
1524     (if (ansi-stream-p target)
1525         (funcall (ansi-stream-out target) target char)
1526         (stream-write-char target char))))
1527
1528 (defun case-frob-downcase-sout (stream str start end)
1529   (declare (type case-frob-stream stream)
1530            (type simple-base-string str)
1531            (type index start)
1532            (type (or index null) end))
1533   (let* ((target (case-frob-stream-target stream))
1534          (len (length str))
1535          (end (or end len))
1536          (string (if (and (zerop start) (= len end))
1537                      (string-downcase str)
1538                      (nstring-downcase (subseq str start end))))
1539          (string-len (- end start)))
1540     (if (ansi-stream-p target)
1541         (funcall (ansi-stream-sout target) target string 0 string-len)
1542         (stream-write-string target string 0 string-len))))
1543
1544 (defun case-frob-capitalize-out (stream char)
1545   (declare (type case-frob-stream stream)
1546            (type base-char char))
1547   (let ((target (case-frob-stream-target stream)))
1548     (cond ((alphanumericp char)
1549            (let ((char (char-upcase char)))
1550              (if (ansi-stream-p target)
1551                  (funcall (ansi-stream-out target) target char)
1552                  (stream-write-char target char)))
1553            (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1554            (setf (case-frob-stream-sout stream)
1555                  #'case-frob-capitalize-aux-sout))
1556           (t
1557            (if (ansi-stream-p target)
1558                (funcall (ansi-stream-out target) target char)
1559                (stream-write-char target char))))))
1560
1561 (defun case-frob-capitalize-sout (stream str start end)
1562   (declare (type case-frob-stream stream)
1563            (type simple-base-string str)
1564            (type index start)
1565            (type (or index null) end))
1566   (let* ((target (case-frob-stream-target stream))
1567          (str (subseq str start end))
1568          (len (length str))
1569          (inside-word nil))
1570     (dotimes (i len)
1571       (let ((char (schar str i)))
1572         (cond ((not (alphanumericp char))
1573                (setf inside-word nil))
1574               (inside-word
1575                (setf (schar str i) (char-downcase char)))
1576               (t
1577                (setf inside-word t)
1578                (setf (schar str i) (char-upcase char))))))
1579     (when inside-word
1580       (setf (case-frob-stream-out stream)
1581             #'case-frob-capitalize-aux-out)
1582       (setf (case-frob-stream-sout stream)
1583             #'case-frob-capitalize-aux-sout))
1584     (if (ansi-stream-p target)
1585         (funcall (ansi-stream-sout target) target str 0 len)
1586         (stream-write-string target str 0 len))))
1587
1588 (defun case-frob-capitalize-aux-out (stream char)
1589   (declare (type case-frob-stream stream)
1590            (type base-char char))
1591   (let ((target (case-frob-stream-target stream)))
1592     (cond ((alphanumericp char)
1593            (let ((char (char-downcase char)))
1594              (if (ansi-stream-p target)
1595                  (funcall (ansi-stream-out target) target char)
1596                  (stream-write-char target char))))
1597           (t
1598            (if (ansi-stream-p target)
1599                (funcall (ansi-stream-out target) target char)
1600                (stream-write-char target char))
1601            (setf (case-frob-stream-out stream)
1602                  #'case-frob-capitalize-out)
1603            (setf (case-frob-stream-sout stream)
1604                  #'case-frob-capitalize-sout)))))
1605
1606 (defun case-frob-capitalize-aux-sout (stream str start end)
1607   (declare (type case-frob-stream stream)
1608            (type simple-base-string str)
1609            (type index start)
1610            (type (or index null) end))
1611   (let* ((target (case-frob-stream-target stream))
1612          (str (subseq str start end))
1613          (len (length str))
1614          (inside-word t))
1615     (dotimes (i len)
1616       (let ((char (schar str i)))
1617         (cond ((not (alphanumericp char))
1618                (setf inside-word nil))
1619               (inside-word
1620                (setf (schar str i) (char-downcase char)))
1621               (t
1622                (setf inside-word t)
1623                (setf (schar str i) (char-upcase char))))))
1624     (unless inside-word
1625       (setf (case-frob-stream-out stream)
1626             #'case-frob-capitalize-out)
1627       (setf (case-frob-stream-sout stream)
1628             #'case-frob-capitalize-sout))
1629     (if (ansi-stream-p target)
1630         (funcall (ansi-stream-sout target) target str 0 len)
1631         (stream-write-string target str 0 len))))
1632
1633 (defun case-frob-capitalize-first-out (stream char)
1634   (declare (type case-frob-stream stream)
1635            (type base-char char))
1636   (let ((target (case-frob-stream-target stream)))
1637     (cond ((alphanumericp char)
1638            (let ((char (char-upcase char)))
1639              (if (ansi-stream-p target)
1640                  (funcall (ansi-stream-out target) target char)
1641                  (stream-write-char target char)))
1642            (setf (case-frob-stream-out stream)
1643                  #'case-frob-downcase-out)
1644            (setf (case-frob-stream-sout stream)
1645                  #'case-frob-downcase-sout))
1646           (t
1647            (if (ansi-stream-p target)
1648                (funcall (ansi-stream-out target) target char)
1649                (stream-write-char target char))))))
1650
1651 (defun case-frob-capitalize-first-sout (stream str start end)
1652   (declare (type case-frob-stream stream)
1653            (type simple-base-string str)
1654            (type index start)
1655            (type (or index null) end))
1656   (let* ((target (case-frob-stream-target stream))
1657          (str (subseq str start end))
1658          (len (length str)))
1659     (dotimes (i len)
1660       (let ((char (schar str i)))
1661         (when (alphanumericp char)
1662           (setf (schar str i) (char-upcase char))
1663           (do ((i (1+ i) (1+ i)))
1664               ((= i len))
1665             (setf (schar str i) (char-downcase (schar str i))))
1666           (setf (case-frob-stream-out stream)
1667                 #'case-frob-downcase-out)
1668           (setf (case-frob-stream-sout stream)
1669                 #'case-frob-downcase-sout)
1670           (return))))
1671     (if (ansi-stream-p target)
1672         (funcall (ansi-stream-sout target) target str 0 len)
1673         (stream-write-string target str 0 len))))
1674 \f
1675 ;;;; READ-SEQUENCE
1676
1677 (defun read-sequence (seq stream &key (start 0) end)
1678   #!+sb-doc
1679   "Destructively modify SEQ by reading elements from STREAM.
1680   That part of SEQ bounded by START and END is destructively modified by
1681   copying successive elements into it from STREAM. If the end of file
1682   for STREAM is reached before copying all elements of the subsequence,
1683   then the extra elements near the end of sequence are not updated, and
1684   the index of the next element is returned."
1685   (declare (type sequence seq)
1686            (type stream stream)
1687            (type index start)
1688            (type sequence-end end)
1689            (values index))
1690   (if (ansi-stream-p stream)
1691       (ansi-stream-read-sequence seq stream start end)
1692       ;; must be Gray streams FUNDAMENTAL-STREAM
1693       (stream-read-sequence stream seq start end)))
1694
1695 (defun ansi-stream-read-sequence (seq stream start %end)
1696   (declare (type sequence seq)
1697            (type ansi-stream stream)
1698            (type index start)
1699            (type sequence-end %end)
1700            (values index))
1701   (let ((end (or %end (length seq))))
1702     (declare (type index end))
1703     (etypecase seq
1704       (list
1705        (let ((read-function
1706               (if (subtypep (stream-element-type stream) 'character)
1707                   #'read-char
1708                   #'read-byte)))
1709          (do ((rem (nthcdr start seq) (rest rem))
1710               (i start (1+ i)))
1711              ((or (endp rem) (>= i end)) i)
1712            (declare (type list rem)
1713                     (type index i))
1714            (let ((el (funcall read-function stream nil :eof)))
1715              (when (eq el :eof)
1716                (return i))
1717              (setf (first rem) el)))))
1718       (vector
1719        (with-array-data ((data seq) (offset-start start) (offset-end end))
1720          (typecase data
1721            ((or (simple-array (unsigned-byte 8) (*))
1722                 (simple-array (signed-byte 8) (*))
1723                 simple-string)
1724             (let* ((numbytes (- end start))
1725                    (bytes-read (sb!sys:read-n-bytes stream
1726                                                     data
1727                                                     offset-start
1728                                                     numbytes
1729                                                     nil)))
1730               (if (< bytes-read numbytes)
1731                   (+ start bytes-read)
1732                   end)))
1733            (t
1734             (let ((read-function
1735                    (if (subtypep (stream-element-type stream) 'character)
1736                        #'read-char
1737                        #'read-byte)))
1738               (do ((i offset-start (1+ i)))
1739                   ((>= i offset-end) end)
1740                 (declare (type index i))
1741                 (let ((el (funcall read-function stream nil :eof)))
1742                   (when (eq el :eof)
1743                     (return (+ start (- i offset-start))))
1744                   (setf (aref data i) el)))))))))))
1745 \f
1746 ;;;; WRITE-SEQUENCE
1747
1748 (defun write-sequence (seq stream &key (start 0) (end nil))
1749   #!+sb-doc
1750   "Write the elements of SEQ bounded by START and END to STREAM."
1751   (declare (type sequence seq)
1752            (type stream stream)
1753            (type index start)
1754            (type sequence-end end)
1755            (values sequence))
1756   (if (ansi-stream-p stream)
1757       (ansi-stream-write-sequence seq stream start end)
1758       ;; must be Gray-streams FUNDAMENTAL-STREAM
1759       (stream-write-sequence stream seq start end)))
1760
1761 (defun ansi-stream-write-sequence (seq stream start %end)
1762   (declare (type sequence seq)
1763            (type ansi-stream stream)
1764            (type index start)
1765            (type sequence-end %end)
1766            (values sequence))
1767   (let ((end (or %end (length seq))))
1768     (declare (type index end))
1769     (etypecase seq
1770       (list
1771        (let ((write-function
1772               (if (subtypep (stream-element-type stream) 'character)
1773                   #'write-char
1774                   #'write-byte)))
1775          (do ((rem (nthcdr start seq) (rest rem))
1776               (i start (1+ i)))
1777              ((or (endp rem) (>= i end)) seq)
1778            (declare (type list rem)
1779                     (type index i))
1780            (funcall write-function (first rem) stream))))
1781       (string
1782        (%write-string seq stream start end))
1783       (vector
1784        (let ((write-function
1785               (if (subtypep (stream-element-type stream) 'character)
1786                   #'write-char
1787                   #'write-byte)))
1788          (do ((i start (1+ i)))
1789              ((>= i end) seq)
1790            (declare (type index i))
1791            (funcall write-function (aref seq i) stream)))))))
1792 \f
1793 ;;;; etc.
1794
1795 ;;; (These were inline throughout this file, but that's not appropriate
1796 ;;; globally.)
1797 (declaim (maybe-inline read-char unread-char read-byte listen))