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