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