1 ;;;; os-independent stream functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
16 ;;; The initialization of these streams is performed by
17 ;;; STREAM-COLD-INIT-OR-RESET.
18 (defvar *terminal-io
* () "terminal I/O stream")
19 (defvar *standard-input
* () "default input stream")
20 (defvar *standard-output
* () "default output stream")
21 (defvar *error-output
* () "error output stream")
22 (defvar *query-io
* () "query I/O stream")
23 (defvar *trace-output
* () "trace output stream")
24 (defvar *debug-io
* () "interactive debugging stream")
26 (defun stream-element-type-stream-element-mode (element-type)
27 (cond ((or (not element-type
)
29 (eq element-type
:default
)) :bivalent
)
30 ((or (eq element-type
'character
)
31 (eq element-type
'base-char
))
33 ((memq element-type
'(signed-byte unsigned-byte
))
35 ((and (proper-list-of-length-p element-type
2)
36 (memq (car element-type
)
37 '(signed-byte unsigned-byte
)))
41 (type-or-nil-if-unknown element-type t
))))
43 ((eq element-type
*empty-type
*)
45 ((csubtypep element-type
(specifier-type 'character
))
47 ((csubtypep element-type
(specifier-type 'unsigned-byte
))
49 ((csubtypep element-type
(specifier-type 'signed-byte
))
54 (defun ill-in (stream &rest ignore
)
55 (declare (ignore ignore
))
56 (error 'simple-type-error
58 :expected-type
'(satisfies input-stream-p
)
59 :format-control
"~S is not a character input stream."
60 :format-arguments
(list stream
)))
61 (defun ill-out (stream &rest ignore
)
62 (declare (ignore ignore
))
63 (error 'simple-type-error
65 :expected-type
'(satisfies output-stream-p
)
66 :format-control
"~S is not a character output stream."
67 :format-arguments
(list stream
)))
68 (defun ill-bin (stream &rest ignore
)
69 (declare (ignore ignore
))
70 (error 'simple-type-error
72 :expected-type
'(satisfies input-stream-p
)
73 :format-control
"~S is not a binary input stream."
74 :format-arguments
(list stream
)))
75 (defun ill-bout (stream &rest ignore
)
76 (declare (ignore ignore
))
77 (error 'simple-type-error
79 :expected-type
'(satisfies output-stream-p
)
80 :format-control
"~S is not a binary output stream."
81 :format-arguments
(list stream
)))
82 (defun closed-flame (stream &rest ignore
)
83 (declare (ignore ignore
))
84 (error 'closed-stream-error
:stream stream
))
85 (defun no-op-placeholder (&rest ignore
)
86 (declare (ignore ignore
)))
88 ;;; stream manipulation functions
90 ;;; SYNONYM-STREAM type is needed by ANSI-STREAM-{INPUT,OUTPUT}-STREAM-P
91 (defstruct (synonym-stream (:include ansi-stream
94 (n-bin #'synonym-n-bin
)
98 (misc #'synonym-misc
))
99 (:constructor make-synonym-stream
(symbol))
101 ;; This is the symbol, the value of which is the stream we are synonym to.
102 (symbol nil
:type symbol
:read-only t
))
103 (declaim (freeze-type synonym-stream
))
105 (defun ansi-stream-input-stream-p (stream)
106 (declare (type ansi-stream stream
))
107 (if (synonym-stream-p stream
)
108 (input-stream-p (symbol-value (synonym-stream-symbol stream
)))
109 (and (not (eq (ansi-stream-in stream
) #'closed-flame
))
110 ;;; KLUDGE: It's probably not good to have EQ tests on function
111 ;;; values like this. What if someone's redefined the function?
112 ;;; Is there a better way? (Perhaps just VALID-FOR-INPUT and
113 ;;; VALID-FOR-OUTPUT flags? -- WHN 19990902
114 (or (not (eq (ansi-stream-in stream
) #'ill-in
))
115 (not (eq (ansi-stream-bin stream
) #'ill-bin
))))))
117 ;;; Temporary definition that gets overwritten by pcl/gray-streams
118 (defun input-stream-p (stream)
119 (declare (type stream stream
))
120 (and (ansi-stream-p stream
)
121 (ansi-stream-input-stream-p stream
)))
123 (defun ansi-stream-output-stream-p (stream)
124 (declare (type ansi-stream stream
))
125 (if (synonym-stream-p stream
)
126 (output-stream-p (symbol-value (synonym-stream-symbol stream
)))
127 (and (not (eq (ansi-stream-in stream
) #'closed-flame
))
128 (or (not (eq (ansi-stream-out stream
) #'ill-out
))
129 (not (eq (ansi-stream-bout stream
) #'ill-bout
))))))
131 ;;; Temporary definition that gets overwritten by pcl/gray-streams
132 (defun output-stream-p (stream)
133 (declare (type stream stream
))
134 (and (ansi-stream-p stream
)
135 (ansi-stream-output-stream-p stream
)))
137 (declaim (inline ansi-stream-open-stream-p
))
138 (defun ansi-stream-open-stream-p (stream)
139 (declare (type ansi-stream stream
))
140 ;; CLHS 22.1.4 lets us not worry about synonym streams here.
141 (not (eq (ansi-stream-in stream
) #'closed-flame
)))
143 (defun open-stream-p (stream)
144 (ansi-stream-open-stream-p stream
))
146 (declaim (inline ansi-stream-element-type
))
147 (defun ansi-stream-element-type (stream)
148 (declare (type ansi-stream stream
))
149 (funcall (ansi-stream-misc stream
) stream
:element-type
))
151 (defun stream-element-type (stream)
152 (ansi-stream-element-type stream
))
154 (defun stream-external-format (stream)
155 (funcall (ansi-stream-misc stream
) stream
:external-format
))
157 (defun interactive-stream-p (stream)
158 (declare (type stream stream
))
159 (funcall (ansi-stream-misc stream
) stream
:interactive-p
))
161 (declaim (inline ansi-stream-close
))
162 (defun ansi-stream-close (stream abort
)
163 (declare (type ansi-stream stream
))
164 (when (open-stream-p stream
)
165 (funcall (ansi-stream-misc stream
) stream
:close abort
))
168 (defun close (stream &key abort
)
169 (ansi-stream-close stream abort
))
171 (defun set-closed-flame (stream)
172 (setf (ansi-stream-in stream
) #'closed-flame
)
173 (setf (ansi-stream-bin stream
) #'closed-flame
)
174 (setf (ansi-stream-n-bin stream
) #'closed-flame
)
175 (setf (ansi-stream-out stream
) #'closed-flame
)
176 (setf (ansi-stream-bout stream
) #'closed-flame
)
177 (setf (ansi-stream-sout stream
) #'closed-flame
)
178 (setf (ansi-stream-misc stream
) #'closed-flame
))
180 ;;;; for file position and file length
181 (defun external-format-char-size (external-format)
182 (ef-char-size (get-external-format external-format
)))
184 ;;; Call the MISC method with the :FILE-POSITION operation.
185 #!-sb-fluid
(declaim (inline ansi-stream-file-position
))
186 (defun ansi-stream-file-position (stream position
)
187 (declare (type stream stream
))
188 (declare (type (or index
(alien sb
!unix
:unix-offset
) (member nil
:start
:end
))
190 ;; FIXME: It would be good to comment on the stuff that is done here...
191 ;; FIXME: This doesn't look interrupt safe.
194 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
195 (funcall (ansi-stream-misc stream
) stream
:file-position position
))
197 (let ((res (funcall (ansi-stream-misc stream
) stream
:file-position nil
)))
201 (- +ansi-stream-in-buffer-length
+
202 (ansi-stream-in-index stream
)))
204 (let ((char-size (if (fd-stream-p stream
)
205 (fd-stream-char-size stream
)
206 (external-format-char-size (stream-external-format stream
)))))
210 (loop with buffer
= (ansi-stream-cin-buffer stream
)
211 with start
= (ansi-stream-in-index stream
)
212 for i from start below
+ansi-stream-in-buffer-length
+
213 sum
(funcall char-size
(aref buffer i
))))
216 (- +ansi-stream-in-buffer-length
+
217 (ansi-stream-in-index stream
))))))))))))
219 (defun file-position (stream &optional position
)
220 (if (ansi-stream-p stream
)
221 (ansi-stream-file-position stream position
)
222 (stream-file-position stream position
)))
224 ;;; This is a literal translation of the ANSI glossary entry "stream
225 ;;; associated with a file".
227 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
228 ;;; file", and in particular stdin, stdout, and stderr are files, we
229 ;;; end up with this test being satisfied for weird things like
230 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
231 ;;; ANSI spec really had in mind, especially since this is used as a
232 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
233 ;;; probably thinking of something like what Unix calls block devices)
234 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
235 (defun stream-associated-with-file-p (x)
236 "Test for the ANSI concept \"stream associated with a file\"."
237 (or (typep x
'file-stream
)
238 (and (synonym-stream-p x
)
239 (stream-associated-with-file-p (symbol-value
240 (synonym-stream-symbol x
))))))
242 (defun stream-must-be-associated-with-file (stream)
243 (declare (type stream stream
))
244 (unless (stream-associated-with-file-p stream
)
245 (error 'simple-type-error
246 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
247 ;; this should be TYPE-ERROR. But what then can we use for
248 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
249 ;; private predicate function..) is ugly and confusing, but
250 ;; I can't see any other way. -- WHN 2001-04-14
252 :expected-type
'(satisfies stream-associated-with-file-p
)
254 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
255 :format-arguments
(list stream
))))
257 (defun file-string-length (stream object
)
258 (funcall (ansi-stream-misc stream
) stream
:file-string-length object
))
262 (defun ansi-stream-read-line-from-frc-buffer (stream eof-error-p eof-value
)
263 (prepare-for-fast-read-char stream
264 (declare (ignore %frc-method%
))
265 (declare (type ansi-stream-cin-buffer %frc-buffer%
))
266 (let ((chunks-total-length 0)
268 (declare (type index chunks-total-length
)
270 (labels ((refill-buffer ()
271 (prog1 (fast-read-char-refill stream nil
)
272 (setf %frc-index%
(ansi-stream-in-index %frc-stream%
))))
273 (build-result (pos n-more-chars
)
274 (let ((res (make-string (+ chunks-total-length n-more-chars
)))
275 (start1 chunks-total-length
))
276 (declare (type index start1
))
278 (replace res %frc-buffer%
279 :start1 start1
:start2 %frc-index%
:end2 pos
)
280 (setf %frc-index%
(1+ pos
)))
281 (done-with-fast-read-char)
282 (dolist (chunk chunks res
)
283 (declare (type (simple-array character
(*)) chunk
))
284 (decf start1
(length chunk
))
285 (replace res chunk
:start1 start1
)))))
286 (declare (inline refill-buffer
))
287 (if (or (< %frc-index%
+ansi-stream-in-buffer-length
+) (refill-buffer))
289 (let ((pos (position #\Newline %frc-buffer%
290 :test
#'char
= :start %frc-index%
)))
292 (return (values (build-result pos
(- pos %frc-index%
)) nil
)))
293 (let ((chunk (subseq %frc-buffer% %frc-index%
)))
294 (incf chunks-total-length
(length chunk
))
296 (unless (refill-buffer)
297 (return (values (build-result -
1 0) t
)))))
298 ;; EOF had been reached before we read anything
299 ;; at all. Return the EOF value or signal the error.
300 (progn (done-with-fast-read-char)
301 (eof-or-lose stream eof-error-p
(values eof-value t
))))))))
303 #!-sb-fluid
(declaim (inline ansi-stream-read-line
))
304 (defun ansi-stream-read-line (stream eof-error-p eof-value recursive-p
)
305 (declare (ignore recursive-p
))
306 (if (ansi-stream-cin-buffer stream
)
307 ;; Stream has a fast-read-char buffer. Copy large chunks directly
308 ;; out of the buffer.
309 (ansi-stream-read-line-from-frc-buffer stream eof-error-p eof-value
)
310 ;; Slow path, character by character.
311 (prepare-for-fast-read-char stream
312 (let ((res (make-string 80))
316 (let ((ch (fast-read-char nil nil
)))
318 (when (char= ch
#\newline
)
319 (done-with-fast-read-char)
320 (return (values (%shrink-vector res index
) nil
)))
323 (let ((new (make-string len
)))
326 (setf (schar res index
) ch
)
329 (done-with-fast-read-char)
330 (return (values (eof-or-lose stream
334 ;; Since FAST-READ-CHAR already hit the eof char, we
335 ;; shouldn't do another READ-CHAR.
337 (done-with-fast-read-char)
338 (return (values (%shrink-vector res index
) t
))))))))))
340 (defun read-line (&optional
(stream *standard-input
*) (eof-error-p t
) eof-value
342 (declare (explicit-check))
343 (let ((stream (in-stream-from-designator stream
)))
344 (if (ansi-stream-p stream
)
345 (ansi-stream-read-line stream eof-error-p eof-value recursive-p
)
346 ;; must be Gray streams FUNDAMENTAL-STREAM
347 (multiple-value-bind (string eof
) (stream-read-line stream
)
348 (if (and eof
(zerop (length string
)))
349 (values (eof-or-lose stream eof-error-p eof-value
) t
)
350 (values string eof
))))))
352 ;;; We proclaim them INLINE here, then proclaim them NOTINLINE later on,
353 ;;; so, except in this file, they are not inline by default, but they can be.
354 #!-sb-fluid
(declaim (inline read-char unread-char read-byte listen
))
356 #!-sb-fluid
(declaim (inline ansi-stream-read-char
))
357 (defun ansi-stream-read-char (stream eof-error-p eof-value recursive-p
)
358 (declare (ignore recursive-p
))
359 (prepare-for-fast-read-char stream
361 (fast-read-char eof-error-p eof-value
)
362 (done-with-fast-read-char))))
364 (defun read-char (&optional
(stream *standard-input
*)
368 (declare (explicit-check))
369 (let ((stream (in-stream-from-designator stream
)))
370 (if (ansi-stream-p stream
)
371 (ansi-stream-read-char stream eof-error-p eof-value recursive-p
)
372 ;; must be Gray streams FUNDAMENTAL-STREAM
373 (let ((char (stream-read-char stream
)))
375 (eof-or-lose stream eof-error-p eof-value
)
376 (the character char
))))))
378 #!-sb-fluid
(declaim (inline ansi-stream-unread-char
))
379 (defun ansi-stream-unread-char (character stream
)
380 (let ((index (1- (ansi-stream-in-index stream
)))
381 (buffer (ansi-stream-cin-buffer stream
)))
382 (declare (fixnum index
))
383 (when (minusp index
) (error "nothing to unread"))
385 (setf (aref buffer index
) character
)
386 (setf (ansi-stream-in-index stream
) index
)
387 ;; Ugh. an ANSI-STREAM with a char buffer never gives a chance to
388 ;; the stream's misc routine to handle the UNREAD operation.
389 (when (ansi-stream-input-char-pos stream
)
390 (decf (ansi-stream-input-char-pos stream
))))
392 (funcall (ansi-stream-misc stream
) stream
393 :unread character
)))))
395 (defun unread-char (character &optional
(stream *standard-input
*))
396 (declare (explicit-check))
397 (let ((stream (in-stream-from-designator stream
)))
398 (if (ansi-stream-p stream
)
399 (ansi-stream-unread-char character stream
)
400 ;; must be Gray streams FUNDAMENTAL-STREAM
401 (stream-unread-char stream character
)))
404 #!-sb-fluid
(declaim (inline ansi-stream-listen
))
405 (defun ansi-stream-listen (stream)
406 (or (/= (the fixnum
(ansi-stream-in-index stream
))
407 +ansi-stream-in-buffer-length
+)
408 ;; Handle :EOF return from misc methods specially
409 (let ((result (funcall (ansi-stream-misc stream
) stream
:listen
)))
414 (defun listen (&optional
(stream *standard-input
*))
415 (declare (explicit-check))
416 (let ((stream (in-stream-from-designator stream
)))
417 (if (ansi-stream-p stream
)
418 (ansi-stream-listen stream
)
419 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
420 (stream-listen stream
))))
422 #!-sb-fluid
(declaim (inline ansi-stream-read-char-no-hang
))
423 (defun ansi-stream-read-char-no-hang (stream eof-error-p eof-value recursive-p
)
424 (if (funcall (ansi-stream-misc stream
) stream
:listen
)
425 ;; On T or :EOF get READ-CHAR to do the work.
426 (ansi-stream-read-char stream eof-error-p eof-value recursive-p
)
429 (defun read-char-no-hang (&optional
(stream *standard-input
*)
433 (declare (explicit-check))
434 (let ((stream (in-stream-from-designator stream
)))
435 (if (ansi-stream-p stream
)
436 (ansi-stream-read-char-no-hang stream eof-error-p eof-value
438 ;; must be Gray streams FUNDAMENTAL-STREAM
439 (let ((char (stream-read-char-no-hang stream
)))
441 (eof-or-lose stream eof-error-p eof-value
)
442 (the (or character null
) char
))))))
444 #!-sb-fluid
(declaim (inline ansi-stream-clear-input
))
445 (defun ansi-stream-clear-input (stream)
446 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
447 (funcall (ansi-stream-misc stream
) stream
:clear-input
))
449 (defun clear-input (&optional
(stream *standard-input
*))
450 (declare (explicit-check))
451 (let ((stream (in-stream-from-designator stream
)))
452 (if (ansi-stream-p stream
)
453 (ansi-stream-clear-input stream
)
454 ;; must be Gray streams FUNDAMENTAL-STREAM
455 (stream-clear-input stream
)))
458 #!-sb-fluid
(declaim (inline ansi-stream-read-byte
))
459 (defun ansi-stream-read-byte (stream eof-error-p eof-value recursive-p
)
460 ;; Why the "recursive-p" parameter? a-s-r-b is funcall'ed from
461 ;; a-s-read-sequence and needs a lambda list that's congruent with
462 ;; that of a-s-read-char
463 (declare (ignore recursive-p
))
464 (with-fast-read-byte (t stream eof-error-p eof-value
)
465 ;; FIXME: the overhead of the UNWIND-PROTECT inserted by
466 ;; WITH-FAST-READ-BYTE significantly impacts the time taken
467 ;; by single byte reads. For this use-case we could
468 ;; probably just change it to a PROG1.
471 (defun read-byte (stream &optional
(eof-error-p t
) eof-value
)
472 (declare (explicit-check))
473 (if (ansi-stream-p stream
)
474 (ansi-stream-read-byte stream eof-error-p eof-value nil
)
475 ;; must be Gray streams FUNDAMENTAL-STREAM
476 (let ((byte (stream-read-byte stream
)))
478 (eof-or-lose stream eof-error-p eof-value
)
479 (the integer byte
)))))
481 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
482 ;;; number of bytes read.
484 ;;; Note: CMU CL's version of this had a special interpretation of
485 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
486 ;;; case, CMU CL's version would return as soon as any data became
487 ;;; available.) This could be useful behavior for things like pipes in
488 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
489 ;;; If we ever need it, it could be added later as a new variant N-BIN
490 ;;; method (perhaps N-BIN-ASAP?) or something.
491 #!-sb-fluid
(declaim (inline read-n-bytes
))
492 (defun read-n-bytes (stream buffer start numbytes
&optional
(eof-error-p t
))
493 (if (ansi-stream-p stream
)
494 (ansi-stream-read-n-bytes stream buffer start numbytes eof-error-p
)
495 ;; We don't need to worry about element-type size here is that
496 ;; callers are supposed to have checked everything is kosher.
497 (let* ((end (+ start numbytes
))
498 (read-end (stream-read-sequence stream buffer start end
)))
499 (eof-or-lose stream
(and eof-error-p
(< read-end end
)) (- read-end start
)))))
501 (defun ansi-stream-read-n-bytes (stream buffer start numbytes eof-error-p
)
502 (declare (type ansi-stream stream
)
503 (type index numbytes start
)
504 (type (or (simple-array * (*)) system-area-pointer
) buffer
))
505 (let* ((in-buffer (ansi-stream-in-buffer stream
))
506 (index (ansi-stream-in-index stream
))
507 (num-buffered (- +ansi-stream-in-buffer-length
+ index
)))
508 (declare (fixnum index num-buffered
))
511 (funcall (ansi-stream-n-bin stream
)
517 ((<= numbytes num-buffered
)
519 (let ((copy-function (typecase buffer
520 ((simple-array * (*)) #'ub8-bash-copy
)
521 (system-area-pointer #'copy-ub8-to-system-area
))))
522 (funcall copy-function in-buffer index buffer start numbytes
))
523 (%byte-blt in-buffer index
524 buffer start
(+ start numbytes
))
525 (setf (ansi-stream-in-index stream
) (+ index numbytes
))
528 (let ((end (+ start num-buffered
)))
530 (let ((copy-function (typecase buffer
531 ((simple-array * (*)) #'ub8-bash-copy
)
532 (system-area-pointer #'copy-ub8-to-system-area
))))
533 (funcall copy-function in-buffer index buffer start num-buffered
))
534 (%byte-blt in-buffer index buffer start end
)
535 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
536 (+ (funcall (ansi-stream-n-bin stream
)
540 (- numbytes num-buffered
)
544 ;;; the amount of space we leave at the start of the in-buffer for
547 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
548 (defconstant +ansi-stream-in-buffer-extra
+
549 4) ; FIXME: should be symbolic constant
551 ;;; This function is called by the FAST-READ-CHAR expansion to refill
552 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
553 ;;; and hence must be an N-BIN method. It's also called by other stream
554 ;;; functions which directly peek into the frc buffer.
555 ;;; If EOF is hit and EOF-ERROR-P is false, then return NIL,
556 ;;; otherwise return the new index into CIN-BUFFER.
557 (defun fast-read-char-refill (stream eof-error-p
)
558 (when (ansi-stream-input-char-pos stream
)
559 ;; Characters between (ANSI-STREAM-IN-INDEX %FRC-STREAM%)
560 ;; and +ANSI-STREAM-IN-BUFFER-LENGTH+ have to be re-scanned.
561 (update-input-char-pos stream
))
562 (let* ((ibuf (ansi-stream-cin-buffer stream
))
563 (count (funcall (ansi-stream-n-bin stream
)
566 +ansi-stream-in-buffer-extra
+
567 (- +ansi-stream-in-buffer-length
+
568 +ansi-stream-in-buffer-extra
+)
570 (start (- +ansi-stream-in-buffer-length
+ count
)))
571 (declare (type index start count
))
573 ;; An empty count does not necessarily mean that we reached
574 ;; the EOF, it's also possible that it's e.g. due to a
575 ;; invalid octet sequence in a multibyte stream. To handle
576 ;; the resyncing case correctly we need to call the reading
577 ;; function and check whether an EOF was really reached. If
578 ;; not, we can just fill the buffer by one character, and
579 ;; hope that the next refill will not need to resync.
581 ;; KLUDGE: we can't use FD-STREAM functions (which are the
582 ;; only ones which will give us decoding errors) here,
583 ;; because this code is generic. We can't call the N-BIN
584 ;; function, because near the end of a real file that can
585 ;; legitimately bounce us to the IN function. So we have
586 ;; to call ANSI-STREAM-IN.
587 (let* ((index (1- +ansi-stream-in-buffer-length
+))
588 (value (funcall (ansi-stream-in stream
) stream nil
:eof
)))
590 ;; When not signaling an error, it is important that IN-INDEX
591 ;; be set to +ANSI-STREAM-IN-BUFFER-LENGTH+ here, even though
592 ;; DONE-WITH-FAST-READ-CHAR will do the same, thereby writing
593 ;; the caller's %FRC-INDEX% (= +ANSI-STREAM-IN-BUFFER-LENGTH+)
594 ;; into the slot. But because we've already bumped INPUT-CHAR-POS
595 ;; and scanned characters between the original %FRC-INDEX%
596 ;; and the buffer end (above), we must *not* do that again.
598 ;; definitely EOF now
599 (setf (ansi-stream-in-index stream
)
600 +ansi-stream-in-buffer-length
+)
601 (eof-or-lose stream eof-error-p nil
))
602 ;; we resynced or were given something instead
604 (setf (aref ibuf index
) value
)
605 (setf (ansi-stream-in-index stream
) index
)))))
607 (when (/= start
+ansi-stream-in-buffer-extra
+)
608 (#.
(let* ((n-character-array-bits
611 sb
!vm
:*specialized-array-element-type-properties
*
612 :key
#'sb
!vm
:saetp-specifier
)))
613 (bash-function (intern (format nil
"UB~D-BASH-COPY" n-character-array-bits
)
614 (find-package "SB!KERNEL"))))
616 ibuf
+ansi-stream-in-buffer-extra
+
619 (setf (ansi-stream-in-index stream
) start
)))))
621 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
622 ;;; leave room for unreading.
623 (defun fast-read-byte-refill (stream eof-error-p eof-value
)
624 (let* ((ibuf (ansi-stream-in-buffer stream
))
625 (count (funcall (ansi-stream-n-bin stream
) stream
626 ibuf
0 +ansi-stream-in-buffer-length
+
628 (start (- +ansi-stream-in-buffer-length
+ count
)))
629 (declare (type index start count
))
631 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
632 (funcall (ansi-stream-bin stream
) stream eof-error-p eof-value
))
634 (unless (zerop start
)
635 (ub8-bash-copy ibuf
0
638 (setf (ansi-stream-in-index stream
) (1+ start
))
639 (aref ibuf start
)))))
643 (defun write-char (character &optional
(stream *standard-output
*))
644 (declare (explicit-check))
645 (with-out-stream stream
(ansi-stream-out character
)
646 (stream-write-char character
))
649 (defun terpri (&optional
(stream *standard-output
*))
650 (declare (explicit-check))
651 (with-out-stream stream
(ansi-stream-out #\newline
) (stream-terpri))
654 #!-sb-fluid
(declaim (inline ansi-stream-fresh-line
))
655 (defun ansi-stream-fresh-line (stream)
656 (when (/= (or (charpos stream
) 1) 0)
657 (funcall (ansi-stream-out stream
) stream
#\newline
)
660 (defun fresh-line (&optional
(stream *standard-output
*))
661 (declare (explicit-check))
662 (let ((stream (out-stream-from-designator stream
)))
663 (if (ansi-stream-p stream
)
664 (ansi-stream-fresh-line stream
)
665 ;; must be Gray streams FUNDAMENTAL-STREAM
666 (stream-fresh-line stream
))))
668 #!-sb-fluid
(declaim (inline ansi-stream-write-string
))
669 (defun ansi-stream-write-string (string stream start end
)
670 (with-array-data ((data string
) (offset-start start
)
672 :check-fill-pointer t
)
673 (funcall (ansi-stream-sout stream
)
674 stream data offset-start offset-end
)))
676 (defun %write-string
(string stream start end
)
677 (let ((stream (out-stream-from-designator stream
)))
678 (if (ansi-stream-p stream
)
679 (ansi-stream-write-string string stream start end
)
680 ;; must be Gray streams FUNDAMENTAL-STREAM
681 (stream-write-string stream string start end
)))
684 (defun write-string (string &optional
(stream *standard-output
*)
686 (declare (type string string
))
687 (declare (type stream-designator stream
))
688 (declare (explicit-check))
689 (%write-string string stream start end
))
691 (defun write-line (string &optional
(stream *standard-output
*)
693 (declare (type string string
))
694 (declare (type stream-designator stream
))
695 (declare (explicit-check))
696 (let ((stream (out-stream-from-designator stream
)))
697 (cond ((ansi-stream-p stream
)
698 (ansi-stream-write-string string stream start end
)
699 (funcall (ansi-stream-out stream
) stream
#\newline
))
701 (stream-write-string stream string start end
)
702 (stream-write-char stream
#\newline
))))
705 (defun charpos (&optional
(stream *standard-output
*))
706 (with-out-stream stream
(ansi-stream-misc :charpos
) (stream-line-column)))
708 (defun line-length (&optional
(stream *standard-output
*))
709 (with-out-stream stream
(ansi-stream-misc :line-length
)
710 (stream-line-length)))
712 (defun finish-output (&optional
(stream *standard-output
*))
713 (declare (explicit-check))
714 (with-out-stream stream
(ansi-stream-misc :finish-output
)
715 (stream-finish-output))
718 (defun force-output (&optional
(stream *standard-output
*))
719 (declare (explicit-check))
720 (with-out-stream stream
(ansi-stream-misc :force-output
)
721 (stream-force-output))
724 (defun clear-output (&optional
(stream *standard-output
*))
725 (declare (explicit-check))
726 (with-out-stream stream
(ansi-stream-misc :clear-output
)
727 (stream-clear-output))
730 (defun write-byte (integer stream
)
731 (declare (explicit-check))
732 ;; The STREAM argument is not allowed to be a designator.
733 (%with-out-stream stream
(ansi-stream-bout integer
) (stream-write-byte integer
))
737 ;;; Meta: the following comment is mostly true, but gray stream support
738 ;;; is already incorporated into the definitions within this file.
739 ;;; But these need to redefinable, otherwise the relative order of
740 ;;; loading sb-simple-streams and any user-defined code which executes
741 ;;; (F #'read-char ...) is sensitive to the order in which those
742 ;;; are loaded, though insensitive at compile-time.
743 ;;; (These were inline throughout this file, but that's not appropriate
744 ;;; globally. And we must not inline them in the rest of this file if
745 ;;; dispatch to gray or simple streams is to work, since both redefine
746 ;;; these functions later.)
747 (declaim (notinline read-char unread-char read-byte listen
))
749 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
750 ;;; streams to handle the misc routines and dispatch to the
751 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
752 (defun stream-misc-dispatch (stream operation
&optional arg1 arg2
)
753 (declare (type stream stream
) (ignore arg2
))
756 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
757 (let ((char (read-char-no-hang stream nil
:eof
)))
758 (when (characterp char
)
759 (unread-char char stream
))
762 (unread-char arg1 stream
))
766 (clear-input stream
))
768 (force-output stream
))
770 (finish-output stream
))
772 (stream-element-type stream
))
774 (stream-element-type-stream-element-mode
775 (stream-element-type stream
)))
776 (:stream-external-format
777 (stream-external-format stream
))
779 (interactive-stream-p stream
))
781 (line-length stream
))
785 (file-length stream
))
787 (file-string-length stream arg1
))
789 (file-position stream arg1
))))
791 (declaim (inline stream-element-mode
))
792 (defun stream-element-mode (stream)
793 (declare (type stream stream
))
795 ((fd-stream-p stream
)
796 (fd-stream-element-mode stream
))
797 ((and (ansi-stream-p stream
)
798 (funcall (ansi-stream-misc stream
) stream
:element-mode
)))
800 (stream-element-type-stream-element-mode
801 (stream-element-type stream
)))))
803 ;;;; broadcast streams
805 (defstruct (broadcast-stream (:include ansi-stream
806 (out #'broadcast-out
)
807 (bout #'broadcast-bout
)
808 (sout #'broadcast-sout
)
809 (misc #'broadcast-misc
))
810 (:constructor %make-broadcast-stream
814 ;; a list of all the streams we broadcast to
815 (streams () :type list
:read-only t
))
817 (declaim (freeze-type broadcast-stream
))
819 (defun make-broadcast-stream (&rest streams
)
821 (return-from make-broadcast-stream
822 (load-time-value (let ((out (lambda (stream arg
)
823 (declare (ignore stream arg
)
824 (optimize speed
(safety 0)))))
825 (sout (lambda (stream string start end
)
826 (declare (ignore stream string start end
)
827 (optimize speed
(safety 0)))))
828 (stream (%make-broadcast-stream nil
)))
829 (setf (broadcast-stream-out stream
) out
830 (broadcast-stream-bout stream
) out
831 (broadcast-stream-sout stream
) sout
)
834 (dolist (stream streams
)
835 (unless (output-stream-p stream
)
838 :expected-type
'(satisfies output-stream-p
))))
839 (%make-broadcast-stream streams
))
841 (macrolet ((out-fun (name fun
&rest args
)
842 `(defun ,name
(stream ,@args
)
843 (dolist (stream (broadcast-stream-streams stream
))
844 (,fun
,(car args
) stream
,@(cdr args
))))))
845 (out-fun broadcast-out write-char char
)
846 (out-fun broadcast-bout write-byte byte
)
847 (out-fun broadcast-sout %write-string string start end
))
849 (defun broadcast-misc (stream operation
&optional arg1 arg2
)
850 (let ((streams (broadcast-stream-streams stream
)))
852 ;; FIXME: This may not be the best place to note this, but I
853 ;; think the :CHARPOS protocol needs revision. Firstly, I think
854 ;; this is the last place where a NULL return value was possible
855 ;; (before adjusting it to be 0), so a bunch of conditionals IF
856 ;; CHARPOS can be removed; secondly, it is my belief that
857 ;; FD-STREAMS, when running FILE-POSITION, do not update the
858 ;; CHARPOS, and consequently there will be much wrongness.
860 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
861 ;; is it testing the :charpos of an input stream?
863 ;; -- CSR, 2004-02-04
865 (dolist (stream streams
0)
866 (let ((charpos (charpos stream
)))
871 (dolist (stream streams min
)
872 (let ((res (line-length stream
)))
873 (when res
(setq min
(if min
(min res min
) res
)))))))
875 (let ((last (last streams
)))
877 (stream-element-type (car last
))
880 (awhen (last streams
)
881 (stream-element-mode (car it
))))
883 (let ((last (last streams
)))
885 (stream-external-format (car last
))
888 (let ((last (last streams
)))
890 (file-length (car last
))
894 (let ((res (or (eql arg1
:start
) (eql arg1
0))))
895 (dolist (stream streams res
)
896 (setq res
(file-position stream arg1
))))
897 (let ((last (last streams
)))
899 (file-position (car last
))
902 (let ((last (last streams
)))
904 (file-string-length (car last
) arg1
)
907 (when (broadcast-stream-streams stream
)
908 (set-closed-flame stream
)))
911 (dolist (stream streams res
)
913 (if (ansi-stream-p stream
)
914 (funcall (ansi-stream-misc stream
) stream operation
916 (stream-misc-dispatch stream operation arg1 arg2
)))))))))
920 (defmethod print-object ((x synonym-stream
) stream
)
921 (print-unreadable-object (x stream
:type t
:identity t
)
922 (format stream
":SYMBOL ~S" (synonym-stream-symbol x
))))
924 ;;; The output simple output methods just call the corresponding
925 ;;; function on the synonymed stream.
926 (macrolet ((out-fun (name fun
&rest args
)
927 `(defun ,name
(stream ,@args
)
928 (declare (optimize (safety 1)))
929 (let ((syn (symbol-value (synonym-stream-symbol stream
))))
930 (,fun
,(car args
) syn
,@(cdr args
))))))
931 (out-fun synonym-out write-char ch
)
932 (out-fun synonym-bout write-byte n
)
933 (out-fun synonym-sout %write-string string start end
))
935 ;;; For the input methods, we just call the corresponding function on the
936 ;;; synonymed stream. These functions deal with getting input out of
937 ;;; the In-Buffer if there is any.
938 (macrolet ((in-fun (name fun
&rest args
)
939 `(defun ,name
(stream ,@args
)
940 (declare (optimize (safety 1)))
941 (,fun
(symbol-value (synonym-stream-symbol stream
))
943 (in-fun synonym-in read-char eof-error-p eof-value
)
944 (in-fun synonym-bin read-byte eof-error-p eof-value
)
945 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p
))
947 (defun synonym-misc (stream operation
&optional arg1 arg2
)
948 (declare (optimize (safety 1)))
949 (let ((syn (symbol-value (synonym-stream-symbol stream
))))
950 (if (ansi-stream-p syn
)
951 ;; We have to special-case some operations which interact with
952 ;; the in-buffer of the wrapped stream, since just calling
953 ;; ANSI-STREAM-MISC on them
955 (:listen
(or (/= (the fixnum
(ansi-stream-in-index syn
))
956 +ansi-stream-in-buffer-length
+)
957 (funcall (ansi-stream-misc syn
) syn
:listen
)))
958 (:clear-input
(clear-input syn
))
959 (:unread
(unread-char arg1 syn
))
961 (funcall (ansi-stream-misc syn
) syn operation arg1 arg2
)))
962 (stream-misc-dispatch syn operation arg1 arg2
))))
966 (defstruct (two-way-stream
967 (:include ansi-stream
970 (n-bin #'two-way-n-bin
)
972 (bout #'two-way-bout
)
973 (sout #'two-way-sout
)
974 (misc #'two-way-misc
))
975 (:constructor %make-two-way-stream
(input-stream output-stream
))
978 (input-stream (missing-arg) :type stream
:read-only t
)
979 (output-stream (missing-arg) :type stream
:read-only t
))
981 (defprinter (two-way-stream) input-stream output-stream
)
983 (defun make-two-way-stream (input-stream output-stream
)
984 "Return a bidirectional stream which gets its input from INPUT-STREAM and
985 sends its output to OUTPUT-STREAM."
986 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
987 ;; should be encapsulated in a function, and used here and most of
988 ;; the other places that SYNONYM-STREAM-P appears.
989 (unless (output-stream-p output-stream
)
992 :expected-type
'(satisfies output-stream-p
)))
993 (unless (input-stream-p input-stream
)
996 :expected-type
'(satisfies input-stream-p
)))
997 (%make-two-way-stream input-stream output-stream
))
999 (macrolet ((out-fun (name fun
&rest args
)
1000 `(defun ,name
(stream ,@args
)
1001 (let ((syn (two-way-stream-output-stream stream
)))
1002 (,fun
,(car args
) syn
,@(cdr args
))))))
1003 (out-fun two-way-out write-char ch
)
1004 (out-fun two-way-bout write-byte n
)
1005 (out-fun two-way-sout %write-string string start end
))
1007 (macrolet ((in-fun (name fun
&rest args
)
1008 `(defun ,name
(stream ,@args
)
1009 (,fun
(two-way-stream-input-stream stream
) ,@args
))))
1010 (in-fun two-way-in read-char eof-error-p eof-value
)
1011 (in-fun two-way-bin read-byte eof-error-p eof-value
)
1012 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p
))
1014 (defun two-way-misc (stream operation
&optional arg1 arg2
)
1015 (let* ((in (two-way-stream-input-stream stream
))
1016 (out (two-way-stream-output-stream stream
))
1017 (in-ansi-stream-p (ansi-stream-p in
))
1018 (out-ansi-stream-p (ansi-stream-p out
)))
1021 (if in-ansi-stream-p
1022 (or (/= (the fixnum
(ansi-stream-in-index in
))
1023 +ansi-stream-in-buffer-length
+)
1024 (funcall (ansi-stream-misc in
) in
:listen
))
1026 ((:finish-output
:force-output
:clear-output
)
1027 (if out-ansi-stream-p
1028 (funcall (ansi-stream-misc out
) out operation arg1 arg2
)
1029 (stream-misc-dispatch out operation arg1 arg2
)))
1030 (:clear-input
(clear-input in
))
1031 (:unread
(unread-char arg1 in
))
1033 (let ((in-type (stream-element-type in
))
1034 (out-type (stream-element-type out
)))
1035 (if (equal in-type out-type
)
1037 `(and ,in-type
,out-type
))))
1039 (let ((in-mode (stream-element-mode in
))
1040 (out-mode (stream-element-mode out
)))
1041 (when (equal in-mode out-mode
)
1044 (set-closed-flame stream
))
1046 (or (if in-ansi-stream-p
1047 (funcall (ansi-stream-misc in
) in operation arg1 arg2
)
1048 (stream-misc-dispatch in operation arg1 arg2
))
1049 (if out-ansi-stream-p
1050 (funcall (ansi-stream-misc out
) out operation arg1 arg2
)
1051 (stream-misc-dispatch out operation arg1 arg2
)))))))
1053 ;;;; concatenated streams
1055 (defstruct (concatenated-stream
1056 (:include ansi-stream
1057 (in #'concatenated-in
)
1058 (bin #'concatenated-bin
)
1059 (n-bin #'concatenated-n-bin
)
1060 (misc #'concatenated-misc
))
1061 (:constructor %make-concatenated-stream
(streams))
1064 ;; The car of this is the substream we are reading from now.
1065 (streams nil
:type list
))
1067 (declaim (freeze-type concatenated-stream
))
1069 (defmethod print-object ((x concatenated-stream
) stream
)
1070 (print-unreadable-object (x stream
:type t
:identity t
)
1073 (concatenated-stream-streams x
))))
1075 (defun make-concatenated-stream (&rest streams
)
1076 "Return a stream which takes its input from each of the streams in turn,
1077 going on to the next at EOF."
1078 (dolist (stream streams
)
1079 (unless (input-stream-p stream
)
1082 :expected-type
'(satisfies input-stream-p
))))
1083 (%make-concatenated-stream streams
))
1085 (macrolet ((in-fun (name fun
)
1086 `(defun ,name
(stream eof-error-p eof-value
)
1087 (do ((streams (concatenated-stream-streams stream
)
1090 (eof-or-lose stream eof-error-p eof-value
))
1091 (let* ((stream (car streams
))
1092 (result (,fun stream nil nil
)))
1093 (when result
(return result
)))
1094 (pop (concatenated-stream-streams stream
))))))
1095 (in-fun concatenated-in read-char
)
1096 (in-fun concatenated-bin read-byte
))
1098 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp
)
1099 (do ((streams (concatenated-stream-streams stream
) (cdr streams
))
1100 (current-start start
)
1101 (remaining-bytes numbytes
))
1104 (error 'end-of-file
:stream stream
)
1105 (- numbytes remaining-bytes
)))
1106 (let* ((stream (car streams
))
1107 (bytes-read (read-n-bytes stream buffer current-start
1108 remaining-bytes nil
)))
1109 (incf current-start bytes-read
)
1110 (decf remaining-bytes bytes-read
)
1111 (when (zerop remaining-bytes
) (return numbytes
)))
1112 (setf (concatenated-stream-streams stream
) (cdr streams
))))
1114 (defun concatenated-misc (stream operation
&optional arg1 arg2
)
1115 (let* ((left (concatenated-stream-streams stream
))
1116 (current (car left
)))
1120 (return-from concatenated-misc
:eof
))
1122 (let ((stuff (if (ansi-stream-p current
)
1123 (funcall (ansi-stream-misc current
) current
1125 (stream-misc-dispatch current
:listen
))))
1126 (cond ((eq stuff
:eof
)
1127 ;; Advance STREAMS, and try again.
1128 (pop (concatenated-stream-streams stream
))
1130 (car (concatenated-stream-streams stream
)))
1132 ;; No further streams. EOF.
1135 ;; Stuff's available.
1138 ;; Nothing is available yet.
1140 (:clear-input
(when left
(clear-input current
)))
1141 (:unread
(when left
(unread-char arg1 current
)))
1143 (set-closed-flame stream
))
1146 (if (ansi-stream-p current
)
1147 (funcall (ansi-stream-misc current
) current operation arg1 arg2
)
1148 (stream-misc-dispatch current operation arg1 arg2
)))))))
1152 (defstruct (echo-stream
1153 (:include two-way-stream
1157 (n-bin #'echo-n-bin
))
1158 (:constructor %make-echo-stream
(input-stream output-stream
))
1161 (unread-stuff nil
:type boolean
))
1163 (declaim (freeze-type echo-stream
))
1165 (defmethod print-object ((x echo-stream
) stream
)
1166 (print-unreadable-object (x stream
:type t
:identity t
)
1168 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1169 (two-way-stream-input-stream x
)
1170 (two-way-stream-output-stream x
))))
1172 (defun make-echo-stream (input-stream output-stream
)
1173 "Return a bidirectional stream which gets its input from INPUT-STREAM and
1174 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
1176 (unless (output-stream-p output-stream
)
1178 :datum output-stream
1179 :expected-type
'(satisfies output-stream-p
)))
1180 (unless (input-stream-p input-stream
)
1183 :expected-type
'(satisfies input-stream-p
)))
1184 (%make-echo-stream input-stream output-stream
))
1186 (macrolet ((in-fun (name in-fun out-fun
&rest args
)
1187 `(defun ,name
(stream ,@args
)
1188 (let* ((unread-stuff-p (echo-stream-unread-stuff stream
))
1189 (in (echo-stream-input-stream stream
))
1190 (out (echo-stream-output-stream stream
))
1191 (result (if eof-error-p
1193 (,in-fun in nil in
))))
1194 (setf (echo-stream-unread-stuff stream
) nil
)
1196 ((eql result in
) eof-value
)
1197 ;; If unread-stuff was true, the character read
1198 ;; from the input stream was previously echoed.
1199 (t (unless unread-stuff-p
(,out-fun result out
)) result
))))))
1200 (in-fun echo-in read-char write-char eof-error-p eof-value
)
1201 (in-fun echo-bin read-byte write-byte eof-error-p eof-value
))
1203 (defun echo-n-bin (stream buffer start numbytes eof-error-p
)
1204 (let ((bytes-read 0))
1205 ;; Note: before ca 1.0.27.18, the logic for handling unread
1206 ;; characters never could have worked, so probably nobody has ever
1207 ;; tried doing bivalent block I/O through an echo stream; this may
1209 (when (echo-stream-unread-stuff stream
)
1210 (let* ((char (read-char stream
))
1211 (octets (string-to-octets
1214 (stream-external-format
1215 (echo-stream-input-stream stream
))))
1216 (octet-count (length octets
))
1217 (blt-count (min octet-count numbytes
)))
1218 (replace buffer octets
:start1 start
:end1
(+ start blt-count
))
1219 (incf start blt-count
)
1220 (decf numbytes blt-count
)))
1221 (incf bytes-read
(read-n-bytes (echo-stream-input-stream stream
) buffer
1222 start numbytes nil
))
1225 (write-sequence buffer
(echo-stream-output-stream stream
)
1226 :start start
:end
(+ start bytes-read
))
1228 ((> numbytes bytes-read
)
1229 (write-sequence buffer
(echo-stream-output-stream stream
)
1230 :start start
:end
(+ start bytes-read
))
1231 (error 'end-of-file
:stream stream
))
1233 (write-sequence buffer
(echo-stream-output-stream stream
)
1234 :start start
:end
(+ start bytes-read
))
1235 (aver (= numbytes
(+ start bytes-read
)))
1238 ;;;; STRING-INPUT-STREAM stuff
1240 (defstruct (string-input-stream
1241 (:include ansi-stream
1243 (misc #'string-in-misc
))
1244 (:constructor %make-string-input-stream
1245 (string current end
))
1248 (string (missing-arg) :type simple-string
:read-only t
)
1249 (current (missing-arg) :type index
)
1250 (end (missing-arg) :type index
))
1252 (declaim (freeze-type string-input-stream
))
1254 (defun string-inch (stream eof-error-p eof-value
)
1255 (declare (type string-input-stream stream
))
1256 (let ((string (string-input-stream-string stream
))
1257 (index (string-input-stream-current stream
)))
1258 (cond ((>= index
(the index
(string-input-stream-end stream
)))
1259 (eof-or-lose stream eof-error-p eof-value
))
1261 (setf (string-input-stream-current stream
) (1+ index
))
1262 (char string index
)))))
1264 (defun string-binch (stream eof-error-p eof-value
)
1265 (declare (type string-input-stream stream
))
1266 (let ((string (string-input-stream-string stream
))
1267 (index (string-input-stream-current stream
)))
1268 (cond ((>= index
(the index
(string-input-stream-end stream
)))
1269 (eof-or-lose stream eof-error-p eof-value
))
1271 (setf (string-input-stream-current stream
) (1+ index
))
1272 (char-code (char string index
))))))
1274 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p
)
1275 (declare (type string-input-stream stream
)
1276 (type index start requested
))
1277 (let* ((string (string-input-stream-string stream
))
1278 (index (string-input-stream-current stream
))
1279 (available (- (string-input-stream-end stream
) index
))
1280 (copy (min available requested
)))
1281 (declare (type simple-string string
))
1283 (setf (string-input-stream-current stream
)
1284 (truly-the index
(+ index copy
)))
1285 ;; FIXME: why are we VECTOR-SAP'ing things here? what's the point?
1286 ;; and are there SB-UNICODE issues here as well? --njf, 2005-03-24
1287 (with-pinned-objects (string buffer
)
1288 (system-area-ub8-copy (vector-sap string
)
1290 (if (typep buffer
'system-area-pointer
)
1292 (vector-sap buffer
))
1295 (if (and (> requested copy
) eof-error-p
)
1296 (error 'end-of-file
:stream stream
)
1299 (defun string-in-misc (stream operation
&optional arg1 arg2
)
1300 (declare (type string-input-stream stream
)
1305 (setf (string-input-stream-current stream
)
1308 (:end
(string-input-stream-end stream
))
1309 ;; We allow moving position beyond EOF. Errors happen
1310 ;; on read, not move.
1312 (string-input-stream-current stream
)))
1313 ;; According to ANSI: "Should signal an error of type type-error
1314 ;; if stream is not a stream associated with a file."
1315 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1316 ;; (:file-length (length (string-input-stream-string stream)))
1317 (:unread
(decf (string-input-stream-current stream
)))
1318 (:close
(set-closed-flame stream
))
1319 (:listen
(or (/= (the index
(string-input-stream-current stream
))
1320 (the index
(string-input-stream-end stream
)))
1322 (:element-type
(array-element-type (string-input-stream-string stream
)))
1323 (:element-mode
'character
)))
1325 (defun make-string-input-stream (string &optional
(start 0) end
)
1326 "Return an input stream which will supply the characters of STRING between
1327 START and END in order."
1328 (declare (type string string
)
1330 (type (or index null
) end
))
1331 ;; FIXME: very inefficient if the input string is, say a 100000-character
1332 ;; adjustable string but (- END START) is 100 characters. We should use
1333 ;; SUBSEQ instead of coercing the whole string. And if STRING is non-simple
1334 ;; but has element type CHARACTER, wouldn't it work to just use the
1335 ;; underlying simple-string since %MAKE-STRING-INPUT-STREAM accepts bounding
1336 ;; indices that can be fudged to deal with any offset?
1337 ;; And (for unicode builds) if the input is BASE-STRING, we should use
1338 ;; MAKE-ARRAY and REPLACE to coerce just the specified piece.
1339 (let* ((string (coerce string
'(simple-array character
(*)))))
1340 ;; Why WITH-ARRAY-DATA, since the array is already simple?
1341 ;; because it's a nice abstract way to check the START and END.
1342 (with-array-data ((string string
) (start start
) (end end
))
1343 (%make-string-input-stream
1344 string
;; now simple
1347 ;;;; STRING-OUTPUT-STREAM stuff
1349 ;;;; FIXME: This, like almost none of the stream code is particularly
1350 ;;;; interrupt or thread-safe. While it should not be possible to
1351 ;;;; corrupt the heap here, it certainly is possible to end up with
1352 ;;;; a string-output-stream whose internal state is messed up.
1354 ;;;; FIXME: It would be nice to support space-efficient
1355 ;;;; string-output-streams with element-type base-char. This would
1356 ;;;; mean either a separate subclass, or typecases in functions.
1357 ;;;; (Partially done, but only for bounded amount of output)
1359 (defconstant +string-output-stream-buffer-initial-size
+ 64)
1361 (defstruct (string-output-stream
1362 (:include ansi-stream
1363 (sout #'string-sout
)
1364 (misc #'string-out-misc
))
1365 (:constructor %make-string-output-stream
(element-type out
))
1368 ;; The string we throw stuff in.
1369 (buffer (make-string
1370 +string-output-stream-buffer-initial-size
+)
1371 :type
(simple-array character
(*)))
1372 ;; Chains of buffers to use
1373 (prev nil
:type list
)
1374 (next nil
:type list
)
1375 ;; Index of the next location to use in the current string.
1376 (pointer 0 :type index
)
1377 ;; Global location in the stream
1378 (index 0 :type index
)
1379 ;; Index cache: when we move backwards we save the greater of this
1380 ;; and index here, so the greater of index and this is always the
1381 ;; end of the stream.
1382 (index-cache 0 :type index
)
1383 ;; Requested element type
1385 ;; It doesn't help anything to declare this slot's type.
1386 ;; Readers don't really benefit, and the public constructor
1387 ;; checks for validity.
1388 #|
:type
(or #!+sb-unicode
(eql :default
) type-specifier
)|
#
1391 (declaim (freeze-type string-output-stream
))
1392 (defun make-string-output-stream (&key
(element-type 'character
))
1393 "Return an output stream which will accumulate all output given it for the
1394 benefit of the function GET-OUTPUT-STREAM-STRING."
1395 (declare (explicit-check))
1396 (if (csubtypep (specifier-type element-type
) (specifier-type 'character
))
1397 (%make-string-output-stream
1398 element-type
(case element-type
1399 (base-char #'string-ouch
/base-char
)
1401 (error "~S is not a subtype of CHARACTER" element-type
)))
1403 (defstruct (finite-base-string-output-stream
1404 (:include ansi-stream
1405 (out #'finite-base-string-ouch
)
1406 (misc #'finite-base-string-out-misc
))
1407 (:constructor %make-finite-base-string-output-stream
(buffer))
1410 (buffer nil
:type simple-base-string
:read-only t
)
1411 (pointer 0 :type index
))
1412 (declaim (freeze-type finite-base-string-output-stream
))
1414 ;;; Pushes the current segment onto the prev-list, and either pops
1415 ;;; or allocates a new one.
1416 (defun string-output-stream-new-buffer (stream size
)
1417 (declare (index size
))
1418 (/noshow0
"/string-output-stream-new-buffer")
1419 (push (string-output-stream-buffer stream
)
1420 (string-output-stream-prev stream
))
1421 (setf (string-output-stream-buffer stream
)
1422 (or (pop (string-output-stream-next stream
))
1423 ;; FIXME: This would be the correct place to detect that
1424 ;; more than FIXNUM characters are being written to the
1425 ;; stream, and do something about it.
1426 (make-string size
))))
1428 ;;; Moves to the end of the next segment or the current one if there are
1429 ;;; no more segments. Returns true as long as there are next segments.
1430 (defun string-output-stream-next-buffer (stream)
1431 (/noshow0
"/string-output-stream-next-buffer")
1432 (let* ((old (string-output-stream-buffer stream
))
1433 (new (pop (string-output-stream-next stream
)))
1434 (old-size (length old
))
1435 (skipped (- old-size
(string-output-stream-pointer stream
))))
1437 (let ((new-size (length new
)))
1438 (push old
(string-output-stream-prev stream
))
1439 (setf (string-output-stream-buffer stream
) new
1440 (string-output-stream-pointer stream
) new-size
)
1441 (incf (string-output-stream-index stream
) (+ skipped new-size
)))
1444 (setf (string-output-stream-pointer stream
) old-size
)
1445 (incf (string-output-stream-index stream
) skipped
)
1448 ;;; Moves to the start of the previous segment or the current one if there
1449 ;;; are no more segments. Returns true as long as there are prev segments.
1450 (defun string-output-stream-prev-buffer (stream)
1451 (/noshow0
"/string-output-stream-prev-buffer")
1452 (let ((old (string-output-stream-buffer stream
))
1453 (new (pop (string-output-stream-prev stream
)))
1454 (skipped (string-output-stream-pointer stream
)))
1456 (push old
(string-output-stream-next stream
))
1457 (setf (string-output-stream-buffer stream
) new
1458 (string-output-stream-pointer stream
) 0)
1459 (decf (string-output-stream-index stream
) (+ skipped
(length new
)))
1462 (setf (string-output-stream-pointer stream
) 0)
1463 (decf (string-output-stream-index stream
) skipped
)
1466 (macrolet ((def (name char-type
)
1467 `(defun ,name
(stream character
)
1468 (let ((pointer (string-output-stream-pointer stream
))
1469 (buffer (string-output-stream-buffer stream
))
1470 (index (string-output-stream-index stream
)))
1471 (when (= pointer
(length buffer
))
1472 (setf buffer
(string-output-stream-new-buffer stream index
)
1474 (setf (aref buffer pointer
) (the ,char-type character
)
1475 (string-output-stream-pointer stream
) (1+ pointer
))
1476 (setf (string-output-stream-index stream
) (1+ index
))))))
1477 (def string-ouch character
)
1478 (def string-ouch
/base-char base-char
))
1480 (defun string-sout (stream string start end
)
1481 (declare (type simple-string string
)
1482 (type index start end
))
1484 (when (and (typep string
'sb
!kernel
:simple-character-string
)
1485 (eq (string-output-stream-element-type stream
) 'base-char
))
1486 (do ((i (1- end
) (1- i
))) ((< i start
))
1487 (declare (optimize (sb!c
::insert-array-bounds-checks
0)))
1488 (the base-char
(char string i
))))
1489 (let* ((full-length (- end start
))
1490 (length full-length
)
1491 (buffer (string-output-stream-buffer stream
))
1492 (pointer (string-output-stream-pointer stream
))
1493 (space (- (length buffer
) pointer
))
1494 (here (min space length
))
1495 (stop (+ start here
))
1496 (overflow (- length space
)))
1497 (declare (index length space here stop full-length
)
1499 (type (simple-array character
(*)) buffer
))
1504 (simple-character-string simple-base-string sb
!kernel
::simple-array-nil
)
1506 (replace buffer string
:start1 pointer
:start2 start
:end2 stop
))
1507 (setf (string-output-stream-pointer stream
) (+ here pointer
)))
1508 (when (plusp overflow
)
1510 length
(- end start
)
1511 buffer
(string-output-stream-new-buffer
1512 stream
(max overflow
(string-output-stream-index stream
)))
1514 space
(length buffer
)
1515 here
(min space length
)
1517 ;; there may be more overflow if we used a buffer
1518 ;; already allocated to the stream
1519 overflow
(- length space
))
1521 (incf (string-output-stream-index stream
) full-length
)))
1523 ;;; Factored out of the -misc method due to size.
1524 (defun set-string-output-stream-file-position (stream pos
)
1525 (let* ((index (string-output-stream-index stream
))
1526 (end (max index
(string-output-stream-index-cache stream
))))
1527 (declare (index index end
))
1528 (setf (string-output-stream-index-cache stream
) end
)
1529 (cond ((eq :start pos
)
1530 (loop while
(string-output-stream-prev-buffer stream
)))
1532 (loop while
(string-output-stream-next-buffer stream
))
1533 (let ((over (- (string-output-stream-index stream
) end
)))
1534 (decf (string-output-stream-pointer stream
) over
))
1535 (setf (string-output-stream-index stream
) end
))
1537 (loop while
(< pos index
)
1538 do
(string-output-stream-prev-buffer stream
)
1539 (setf index
(string-output-stream-index stream
)))
1540 (let ((step (- pos index
)))
1541 (incf (string-output-stream-pointer stream
) step
)
1542 (setf (string-output-stream-index stream
) pos
)))
1544 ;; We allow moving beyond the end of stream, implicitly
1545 ;; extending the output stream.
1546 (let ((next (string-output-stream-next-buffer stream
)))
1547 ;; Update after -next-buffer, INDEX is kept pointing at
1548 ;; the end of the current buffer.
1549 (setf index
(string-output-stream-index stream
))
1550 (loop while
(and next
(> pos index
))
1551 do
(setf next
(string-output-stream-next-buffer stream
)
1552 index
(string-output-stream-index stream
))))
1553 ;; Allocate new buffer if needed, or step back to
1554 ;; the desired index and set pointer and index
1556 (let ((diff (- pos index
)))
1558 (let* ((new (string-output-stream-new-buffer stream diff
))
1559 (size (length new
)))
1560 (aver (= pos
(+ index size
)))
1561 (setf (string-output-stream-pointer stream
) size
1562 (string-output-stream-index stream
) pos
))
1563 (let ((size (length (string-output-stream-buffer stream
))))
1564 (setf (string-output-stream-pointer stream
) (+ size diff
)
1565 (string-output-stream-index stream
) pos
))))))))
1567 (defun string-out-misc (stream operation
&optional arg1 arg2
)
1568 (declare (ignore arg2
))
1569 (declare (optimize speed
))
1572 ;; Keeping this first is a silly micro-optimization: FRESH-LINE
1573 ;; makes this the most common one.
1574 (/noshow0
"/string-out-misc charpos")
1575 (prog ((pointer (string-output-stream-pointer stream
))
1576 (buffer (string-output-stream-buffer stream
))
1577 (prev (string-output-stream-prev stream
))
1579 (declare (type (or null
(simple-array character
(*))) buffer
))
1581 (let ((pos (when buffer
1582 (position #\newline buffer
:from-end t
:end pointer
))))
1583 (when (or pos
(not buffer
))
1584 ;; If newline is at index I, and pointer at index I+N, charpos
1585 ;; is N-1. If there is no newline, and pointer is at index N,
1587 (return (+ base
(if pos
(- pointer pos
1) pointer
))))
1588 (setf base
(+ base pointer
)
1590 pointer
(length buffer
))
1591 (/noshow0
"/string-out-misc charpos next")
1594 (/noshow0
"/string-out-misc file-position")
1596 (set-string-output-stream-file-position stream arg1
))
1597 (string-output-stream-index stream
))
1599 (/noshow0
"/string-out-misc close")
1600 (set-closed-flame stream
))
1602 (let ((et (string-output-stream-element-type stream
)))
1603 ;; Always return a valid type-specifier
1604 (if (eq et
:default
) 'character et
)))
1605 (:element-mode
'character
)))
1607 ;;; Return a string of all the characters sent to a stream made by
1608 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1609 (defun get-output-stream-string (stream)
1610 (declare (type string-output-stream stream
))
1611 (let* ((length (max (string-output-stream-index stream
)
1612 (string-output-stream-index-cache stream
)))
1613 (element-type (string-output-stream-element-type stream
))
1614 (prev (nreverse (string-output-stream-prev stream
)))
1615 (this (string-output-stream-buffer stream
))
1616 (next (string-output-stream-next stream
))
1619 (if (eq element-type
:default
)
1622 (unless (every #'base-char-p
1623 (truly-the simple-character-string buf
))
1625 (dotimes (i (string-output-stream-pointer stream
))
1626 (unless (base-char-p (char this i
))
1633 ;; overwhelmingly common case: can be inlined
1635 ;; FIXME: If we were willing to use %SHRINK-VECTOR here,
1636 ;; and allocate new strings the size of 2 * index in
1637 ;; STRING-SOUT, we would not need to allocate one here in
1638 ;; the common case, but could just use the last one
1639 ;; allocated, and chop it down to size..
1641 ((character) (make-string length
))
1642 ;; slightly less common cases: inline it anyway
1643 ((base-char standard-char
)
1644 (make-string length
:element-type
'base-char
))
1646 (make-string length
:element-type element-type
)))))
1648 (setf (string-output-stream-index stream
) 0
1649 (string-output-stream-index-cache stream
) 0
1650 (string-output-stream-pointer stream
) 0
1651 ;; throw them away for simplicity's sake: this way the rest of the
1652 ;; implementation can assume that the greater of INDEX and INDEX-CACHE
1653 ;; is always within the last buffer.
1654 (string-output-stream-prev stream
) nil
1655 (string-output-stream-next stream
) nil
)
1657 (flet ((replace-all (fun)
1659 (declare (index start
))
1660 (dolist (buffer prev
)
1661 (funcall fun buffer start
)
1662 (incf start
(length buffer
)))
1663 (funcall fun this start
)
1664 (incf start
(length this
))
1665 (dolist (buffer next
)
1666 (funcall fun buffer start
)
1667 (incf start
(length buffer
)))
1668 ;; Hack: erase the pointers to strings, to make it less
1669 ;; likely that the conservative GC will accidentally
1670 ;; retain the buffers.
1673 (macrolet ((frob (type)
1674 `(replace-all (lambda (buffer from
)
1675 (declare (type ,type result
)
1676 (type (simple-array character
(*))
1678 (replace result buffer
:start1 from
)))))
1680 ((simple-array character
(*))
1681 (frob (simple-array character
(*))))
1683 (frob simple-base-string
))
1684 ((simple-array nil
(*))
1685 (frob (simple-array nil
(*)))))))
1689 (defun finite-base-string-ouch (stream character
)
1690 (declare (optimize (sb!c
::insert-array-bounds-checks
0)))
1691 (let ((pointer (finite-base-string-output-stream-pointer stream
))
1692 (buffer (finite-base-string-output-stream-buffer stream
)))
1693 (cond ((= pointer
(length buffer
))
1694 (bug "Should not happen"))
1696 (setf (char buffer pointer
) (truly-the base-char character
)
1697 (finite-base-string-output-stream-pointer stream
)
1698 (truly-the index
(1+ pointer
)))))))
1700 (defun finite-base-string-out-misc (stream operation
&optional arg1 arg2
)
1701 (declare (ignore stream operation arg1 arg2
))
1702 (error "finite-base-string-out-misc needs an implementation"))
1704 ;;;; fill-pointer streams
1706 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1707 ;;; the CLM, but they are required for the implementation of
1708 ;;; WITH-OUTPUT-TO-STRING.
1710 ;;; FIXME: need to support (VECTOR NIL), ideally without destroying all hope
1712 (declaim (inline vector-with-fill-pointer-p
))
1713 (defun vector-with-fill-pointer-p (x)
1715 (array-has-fill-pointer-p x
)))
1717 (deftype string-with-fill-pointer
()
1718 `(and (or (vector character
) (vector base-char
))
1719 (satisfies vector-with-fill-pointer-p
)))
1721 (defstruct (fill-pointer-output-stream
1722 (:include ansi-stream
1723 (out #'fill-pointer-ouch
)
1724 (sout #'fill-pointer-sout
)
1725 (misc #'fill-pointer-misc
))
1726 (:constructor make-fill-pointer-output-stream
(string))
1729 ;; a string with a fill pointer where we stuff the stuff we write
1730 (string (missing-arg) :type string-with-fill-pointer
:read-only t
))
1732 (declaim (freeze-type fill-pointer-output-stream
))
1734 (defun fill-pointer-ouch (stream character
)
1735 (let* ((buffer (fill-pointer-output-stream-string stream
))
1736 (current (fill-pointer buffer
))
1737 (current+1 (1+ current
)))
1738 (declare (fixnum current
))
1739 (with-array-data ((workspace buffer
) (start) (end))
1740 (string-dispatch (simple-character-string simple-base-string
) workspace
1741 (let ((offset-current (+ start current
)))
1742 (declare (fixnum offset-current
))
1743 (if (= offset-current end
)
1744 (let* ((new-length (1+ (* current
2)))
1746 (ecase (array-element-type workspace
)
1747 (character (make-string new-length
1748 :element-type
'character
))
1749 (base-char (make-string new-length
1750 :element-type
'base-char
)))))
1751 (replace new-workspace workspace
:start2 start
:end2 offset-current
)
1752 (setf workspace new-workspace
1753 offset-current current
)
1754 (set-array-header buffer workspace new-length
1755 current
+1 0 new-length nil nil
))
1756 (setf (fill-pointer buffer
) current
+1))
1757 (setf (char workspace offset-current
) character
))))
1760 (defun fill-pointer-sout (stream string start end
)
1761 (declare (fixnum start end
))
1762 (string-dispatch (simple-character-string simple-base-string
) string
1763 (let* ((buffer (fill-pointer-output-stream-string stream
))
1764 (current (fill-pointer buffer
))
1765 (string-len (- end start
))
1766 (dst-end (+ string-len current
)))
1767 (declare (fixnum current dst-end string-len
))
1768 (with-array-data ((workspace buffer
) (dst-start) (dst-length))
1769 (let ((offset-dst-end (+ dst-start dst-end
))
1770 (offset-current (+ dst-start current
)))
1771 (declare (fixnum offset-dst-end offset-current
))
1772 (if (> offset-dst-end dst-length
)
1773 (let* ((new-length (+ (the fixnum
(* current
2)) string-len
))
1775 (ecase (array-element-type workspace
)
1776 (character (make-string new-length
1777 :element-type
'character
))
1778 (base-char (make-string new-length
1779 :element-type
'base-char
)))))
1780 (replace new-workspace workspace
1781 :start2 dst-start
:end2 offset-current
)
1782 (setf workspace new-workspace
1783 offset-current current
1784 offset-dst-end dst-end
)
1785 (set-array-header buffer workspace new-length
1786 dst-end
0 new-length nil nil
))
1787 (setf (fill-pointer buffer
) dst-end
))
1788 (replace workspace string
1789 :start1 offset-current
:start2 start
:end2 end
)))
1792 (defun fill-pointer-misc (stream operation
&optional arg1 arg2
)
1793 (declare (ignore arg2
))
1796 (let ((buffer (fill-pointer-output-stream-string stream
)))
1798 (setf (fill-pointer buffer
)
1801 ;; Fill-pointer is always at fill-pointer we will
1802 ;; make :END move to the end of the actual string.
1803 (:end
(array-total-size buffer
))
1804 ;; We allow moving beyond the end of string if the
1805 ;; string is adjustable.
1806 (t (when (>= arg1
(array-total-size buffer
))
1807 (if (adjustable-array-p buffer
)
1808 (adjust-array buffer arg1
)
1809 (error "Cannot move FILE-POSITION beyond the end ~
1810 of WITH-OUTPUT-TO-STRING stream ~
1811 constructed with non-adjustable string.")))
1813 (fill-pointer buffer
))))
1815 (let* ((buffer (fill-pointer-output-stream-string stream
))
1816 (current (fill-pointer buffer
)))
1817 (with-array-data ((string buffer
) (start) (end current
))
1818 (declare (simple-string string
))
1819 (let ((found (position #\newline string
:test
#'char
=
1820 :start start
:end end
1827 (fill-pointer-output-stream-string stream
)))
1828 (:element-mode
'character
)))
1830 ;;;; case frobbing streams, used by FORMAT ~(...~)
1832 (defstruct (case-frob-stream
1833 (:include ansi-stream
1834 (misc #'case-frob-misc
))
1835 (:constructor %make-case-frob-stream
(target out sout
))
1837 (target (missing-arg) :type stream
:read-only t
))
1839 (declaim (freeze-type case-frob-stream
))
1841 (defun make-case-frob-stream (target kind
)
1842 "Return a stream that sends all output to the stream TARGET, but modifies
1843 the case of letters, depending on KIND, which should be one of:
1844 :UPCASE - convert to upper case.
1845 :DOWNCASE - convert to lower case.
1846 :CAPITALIZE - convert the first letter of words to upper case and the
1847 rest of the word to lower case.
1848 :CAPITALIZE-FIRST - convert the first letter of the first word to upper
1849 case and everything else to lower case."
1850 (declare (type stream target
)
1851 (type (member :upcase
:downcase
:capitalize
:capitalize-first
)
1854 (if (case-frob-stream-p target
)
1855 ;; If we are going to be writing to a stream that already does
1856 ;; case frobbing, why bother frobbing the case just so it can
1859 (multiple-value-bind (out sout
)
1862 (values #'case-frob-upcase-out
1863 #'case-frob-upcase-sout
))
1865 (values #'case-frob-downcase-out
1866 #'case-frob-downcase-sout
))
1868 (values #'case-frob-capitalize-out
1869 #'case-frob-capitalize-sout
))
1871 (values #'case-frob-capitalize-first-out
1872 #'case-frob-capitalize-first-sout
)))
1873 (%make-case-frob-stream target out sout
))))
1875 (defun case-frob-misc (stream op
&optional arg1 arg2
)
1876 (declare (type case-frob-stream stream
))
1879 (set-closed-flame stream
))
1880 (:element-mode
'character
)
1882 (let ((target (case-frob-stream-target stream
)))
1883 (if (ansi-stream-p target
)
1884 (funcall (ansi-stream-misc target
) target op arg1 arg2
)
1885 (stream-misc-dispatch target op arg1 arg2
))))))
1887 (defun case-frob-upcase-out (stream char
)
1888 (declare (type case-frob-stream stream
)
1889 (type character char
))
1890 (let ((target (case-frob-stream-target stream
))
1891 (char (char-upcase char
)))
1892 (if (ansi-stream-p target
)
1893 (funcall (ansi-stream-out target
) target char
)
1894 (stream-write-char target char
))))
1896 (defun case-frob-upcase-sout (stream str start end
)
1897 (declare (type case-frob-stream stream
)
1898 (type simple-string str
)
1900 (type (or index null
) end
))
1901 (let* ((target (case-frob-stream-target stream
))
1904 (string (if (and (zerop start
) (= len end
))
1906 (nstring-upcase (subseq str start end
))))
1907 (string-len (- end start
)))
1908 (if (ansi-stream-p target
)
1909 (funcall (ansi-stream-sout target
) target string
0 string-len
)
1910 (stream-write-string target string
0 string-len
))))
1912 (defun case-frob-downcase-out (stream char
)
1913 (declare (type case-frob-stream stream
)
1914 (type character char
))
1915 (let ((target (case-frob-stream-target stream
))
1916 (char (char-downcase char
)))
1917 (if (ansi-stream-p target
)
1918 (funcall (ansi-stream-out target
) target char
)
1919 (stream-write-char target char
))))
1921 (defun case-frob-downcase-sout (stream str start end
)
1922 (declare (type case-frob-stream stream
)
1923 (type simple-string str
)
1925 (type (or index null
) end
))
1926 (let* ((target (case-frob-stream-target stream
))
1929 (string (if (and (zerop start
) (= len end
))
1930 (string-downcase str
)
1931 (nstring-downcase (subseq str start end
))))
1932 (string-len (- end start
)))
1933 (if (ansi-stream-p target
)
1934 (funcall (ansi-stream-sout target
) target string
0 string-len
)
1935 (stream-write-string target string
0 string-len
))))
1937 (defun case-frob-capitalize-out (stream char
)
1938 (declare (type case-frob-stream stream
)
1939 (type character char
))
1940 (let ((target (case-frob-stream-target stream
)))
1941 (cond ((alphanumericp char
)
1942 (let ((char (char-upcase char
)))
1943 (if (ansi-stream-p target
)
1944 (funcall (ansi-stream-out target
) target char
)
1945 (stream-write-char target char
)))
1946 (setf (case-frob-stream-out stream
) #'case-frob-capitalize-aux-out
)
1947 (setf (case-frob-stream-sout stream
)
1948 #'case-frob-capitalize-aux-sout
))
1950 (if (ansi-stream-p target
)
1951 (funcall (ansi-stream-out target
) target char
)
1952 (stream-write-char target char
))))))
1954 (defun case-frob-capitalize-sout (stream str start end
)
1955 (declare (type case-frob-stream stream
)
1956 (type simple-string str
)
1958 (type (or index null
) end
))
1959 (let* ((target (case-frob-stream-target stream
))
1960 (str (subseq str start end
))
1964 (let ((char (schar str i
)))
1965 (cond ((not (alphanumericp char
))
1966 (setf inside-word nil
))
1968 (setf (schar str i
) (char-downcase char
)))
1970 (setf inside-word t
)
1971 (setf (schar str i
) (char-upcase char
))))))
1973 (setf (case-frob-stream-out stream
)
1974 #'case-frob-capitalize-aux-out
)
1975 (setf (case-frob-stream-sout stream
)
1976 #'case-frob-capitalize-aux-sout
))
1977 (if (ansi-stream-p target
)
1978 (funcall (ansi-stream-sout target
) target str
0 len
)
1979 (stream-write-string target str
0 len
))))
1981 (defun case-frob-capitalize-aux-out (stream char
)
1982 (declare (type case-frob-stream stream
)
1983 (type character char
))
1984 (let ((target (case-frob-stream-target stream
)))
1985 (cond ((alphanumericp char
)
1986 (let ((char (char-downcase char
)))
1987 (if (ansi-stream-p target
)
1988 (funcall (ansi-stream-out target
) target char
)
1989 (stream-write-char target char
))))
1991 (if (ansi-stream-p target
)
1992 (funcall (ansi-stream-out target
) target char
)
1993 (stream-write-char target char
))
1994 (setf (case-frob-stream-out stream
)
1995 #'case-frob-capitalize-out
)
1996 (setf (case-frob-stream-sout stream
)
1997 #'case-frob-capitalize-sout
)))))
1999 (defun case-frob-capitalize-aux-sout (stream str start end
)
2000 (declare (type case-frob-stream stream
)
2001 (type simple-string str
)
2003 (type (or index null
) end
))
2004 (let* ((target (case-frob-stream-target stream
))
2005 (str (subseq str start end
))
2009 (let ((char (schar str i
)))
2010 (cond ((not (alphanumericp char
))
2011 (setf inside-word nil
))
2013 (setf (schar str i
) (char-downcase char
)))
2015 (setf inside-word t
)
2016 (setf (schar str i
) (char-upcase char
))))))
2018 (setf (case-frob-stream-out stream
)
2019 #'case-frob-capitalize-out
)
2020 (setf (case-frob-stream-sout stream
)
2021 #'case-frob-capitalize-sout
))
2022 (if (ansi-stream-p target
)
2023 (funcall (ansi-stream-sout target
) target str
0 len
)
2024 (stream-write-string target str
0 len
))))
2026 (defun case-frob-capitalize-first-out (stream char
)
2027 (declare (type case-frob-stream stream
)
2028 (type character char
))
2029 (let ((target (case-frob-stream-target stream
)))
2030 (cond ((alphanumericp char
)
2031 (let ((char (char-upcase char
)))
2032 (if (ansi-stream-p target
)
2033 (funcall (ansi-stream-out target
) target char
)
2034 (stream-write-char target char
)))
2035 (setf (case-frob-stream-out stream
)
2036 #'case-frob-downcase-out
)
2037 (setf (case-frob-stream-sout stream
)
2038 #'case-frob-downcase-sout
))
2040 (if (ansi-stream-p target
)
2041 (funcall (ansi-stream-out target
) target char
)
2042 (stream-write-char target char
))))))
2044 (defun case-frob-capitalize-first-sout (stream str start end
)
2045 (declare (type case-frob-stream stream
)
2046 (type simple-string str
)
2048 (type (or index null
) end
))
2049 (let* ((target (case-frob-stream-target stream
))
2050 (str (subseq str start end
))
2053 (let ((char (schar str i
)))
2054 (when (alphanumericp char
)
2055 (setf (schar str i
) (char-upcase char
))
2056 (do ((i (1+ i
) (1+ i
)))
2058 (setf (schar str i
) (char-downcase (schar str i
))))
2059 (setf (case-frob-stream-out stream
)
2060 #'case-frob-downcase-out
)
2061 (setf (case-frob-stream-sout stream
)
2062 #'case-frob-downcase-sout
)
2064 (if (ansi-stream-p target
)
2065 (funcall (ansi-stream-sout target
) target str
0 len
)
2066 (stream-write-string target str
0 len
))))
2068 ;;;; Shared {READ,WRITE}-SEQUENCE support functions
2070 (declaim (inline stream-compute-io-function
2071 compatible-vector-and-stream-element-types-p
))
2073 (defun stream-compute-io-function (stream
2074 stream-element-mode sequence-element-type
2075 character-io binary-io bivalent-io
)
2076 (ecase stream-element-mode
2079 ((unsigned-byte signed-byte
)
2083 ((member sequence-element-type
'(nil t
))
2085 ;; Pick off common subtypes.
2086 ((eq sequence-element-type
'character
)
2088 ((or (equal sequence-element-type
'(unsigned-byte 8))
2089 (equal sequence-element-type
'(signed-byte 8)))
2091 ;; Proper subtype tests.
2092 ((subtypep sequence-element-type
'character
)
2094 ((subtypep sequence-element-type
'integer
)
2097 (error "~@<Cannot select IO functions to use for bivalent ~
2098 stream ~S and a sequence with element-type ~S.~@:>"
2099 stream sequence-element-type
))))))
2101 (defun compatible-vector-and-stream-element-types-p (vector stream
)
2102 (declare (type vector vector
)
2103 (type ansi-stream stream
))
2104 (or (and (typep vector
'(simple-array (unsigned-byte 8) (*)))
2105 (memq (stream-element-mode stream
) '(unsigned-byte :bivalent
))
2107 (and (typep vector
'(simple-array (signed-byte 8) (*)))
2108 (eq (stream-element-mode stream
) 'signed-byte
))))
2112 (defun read-sequence (seq stream
&key
(start 0) end
)
2113 "Destructively modify SEQ by reading elements from STREAM.
2114 That part of SEQ bounded by START and END is destructively modified by
2115 copying successive elements into it from STREAM. If the end of file
2116 for STREAM is reached before copying all elements of the subsequence,
2117 then the extra elements near the end of sequence are not updated, and
2118 the index of the next element is returned."
2119 (declare (type sequence seq
)
2120 (type stream stream
)
2122 (type sequence-end end
)
2124 (if (ansi-stream-p stream
)
2125 (ansi-stream-read-sequence seq stream start end
)
2126 ;; must be Gray streams FUNDAMENTAL-STREAM
2127 (stream-read-sequence stream seq start end
)))
2129 (declaim (inline read-sequence
/read-function
))
2130 (defun read-sequence/read-function
(seq stream start %end
2132 character-read-function binary-read-function
)
2133 (declare (type sequence seq
)
2134 (type stream stream
)
2136 (type sequence-end %end
)
2137 (type stream-element-mode stream-element-mode
)
2138 (type function character-read-function binary-read-function
)
2139 (values index
&optional
))
2140 (let ((end (or %end
(length seq
))))
2141 (declare (type index end
))
2142 (labels ((compute-read-function (sequence-element-type)
2143 (stream-compute-io-function
2145 stream-element-mode sequence-element-type
2146 character-read-function binary-read-function
2147 character-read-function
))
2148 (read-list (read-function)
2149 (do ((rem (nthcdr start seq
) (rest rem
))
2151 ((or (endp rem
) (>= i end
)) i
)
2152 (declare (type list rem
)
2154 (let ((el (funcall read-function stream nil
:eof nil
)))
2157 (setf (first rem
) el
))))
2158 (read-vector/fast
(data offset-start
)
2159 (let* ((numbytes (- end start
))
2160 (bytes-read (read-n-bytes
2161 stream data offset-start numbytes nil
)))
2162 (if (< bytes-read numbytes
)
2163 (+ start bytes-read
)
2165 (read-vector (read-function data offset-start offset-end
)
2166 (do ((i offset-start
(1+ i
)))
2167 ((>= i offset-end
) end
)
2168 (declare (type index i
))
2169 (let ((el (funcall read-function stream nil
:eof nil
)))
2171 (return (+ start
(- i offset-start
))))
2172 (setf (aref data i
) el
))))
2173 (read-generic-sequence (read-function)
2174 (declare (ignore read-function
))
2175 (error "~@<~A does not yet support generic sequences.~@:>"
2177 (declare (dynamic-extent #'compute-read-function
2178 #'read-list
#'read-vector
/fast
#'read-vector
2179 #'read-generic-sequence
))
2182 (read-list (compute-read-function nil
)))
2183 ((and (ansi-stream-p stream
)
2184 (ansi-stream-cin-buffer stream
)
2185 (typep seq
'simple-string
))
2186 (ansi-stream-read-string-from-frc-buffer seq stream start %end
))
2187 ((typep seq
'vector
)
2188 (with-array-data ((data seq
) (offset-start start
) (offset-end end
)
2189 :check-fill-pointer t
)
2190 (if (and (ansi-stream-p stream
)
2191 (compatible-vector-and-stream-element-types-p data stream
))
2192 (read-vector/fast data offset-start
)
2193 (read-vector (compute-read-function (array-element-type data
))
2194 data offset-start offset-end
))))
2196 (read-generic-sequence (compute-read-function nil
)))))))
2197 (declaim (notinline read-sequence
/read-function
))
2199 (defun ansi-stream-read-sequence (seq stream start %end
)
2200 (declare (type sequence seq
)
2201 (type ansi-stream stream
)
2203 (type sequence-end %end
)
2204 (values index
&optional
))
2205 (locally (declare (inline read-sequence
/read-function
))
2206 (read-sequence/read-function
2207 seq stream start %end
(stream-element-mode stream
)
2208 #'ansi-stream-read-char
#'ansi-stream-read-byte
)))
2210 (defun ansi-stream-read-string-from-frc-buffer (seq stream start %end
)
2211 (declare (type simple-string seq
)
2212 (type ansi-stream stream
)
2214 (type (or null index
) %end
))
2215 (let ((needed (- (or %end
(length seq
))
2218 (prepare-for-fast-read-char stream
2219 (declare (ignore %frc-method%
))
2220 (unless %frc-buffer%
2221 (return-from ansi-stream-read-string-from-frc-buffer nil
))
2222 (labels ((refill-buffer ()
2223 (prog1 (fast-read-char-refill stream nil
)
2224 (setf %frc-index%
(ansi-stream-in-index %frc-stream%
))))
2226 (let* ((end (length %frc-buffer%
))
2227 (len (min (- end %frc-index%
)
2229 (declare (type index end len read needed
))
2230 (string-dispatch (simple-base-string simple-character-string
)
2232 (replace seq %frc-buffer%
2233 :start1
(+ start read
)
2234 :end1
(+ start read len
)
2236 :end2
(+ %frc-index% len
)))
2238 (incf %frc-index% len
)
2239 (when (or (eql needed read
) (not (refill-buffer)))
2240 (done-with-fast-read-char)
2241 (return-from ansi-stream-read-string-from-frc-buffer
2243 (declare (inline refill-buffer
))
2244 (when (and (= %frc-index%
+ansi-stream-in-buffer-length
+)
2245 (not (refill-buffer)))
2246 ;; EOF had been reached before we read anything
2247 ;; at all. But READ-SEQUENCE never signals an EOF error.
2248 (done-with-fast-read-char)
2249 (return-from ansi-stream-read-string-from-frc-buffer start
))
2250 (loop (add-chunk))))))
2255 (defun write-sequence (seq stream
&key
(start 0) (end nil
))
2256 "Write the elements of SEQ bounded by START and END to STREAM."
2257 (declare (type sequence seq
)
2258 (type stream stream
)
2260 (type sequence-end end
)
2262 (if (ansi-stream-p stream
)
2263 (ansi-stream-write-sequence seq stream start end
)
2264 ;; must be Gray-streams FUNDAMENTAL-STREAM
2265 (stream-write-sequence stream seq start end
)))
2267 ;;; This macro allows sharing code between
2268 ;;; WRITE-SEQUENCE/WRITE-FUNCTION and SB-GRAY:STREAM-WRITE-STRING.
2269 (defmacro write-sequence
/vector
((seq type
) stream start end write-function
)
2270 (once-only ((seq seq
) (stream stream
) (start start
) (end end
)
2271 (write-function write-function
))
2273 (declare (type ,type
,seq
)
2274 (type index
,start
,end
)
2275 (type function
,write-function
))
2276 (do ((i ,start
(1+ i
)))
2278 (declare (type index i
))
2279 (funcall ,write-function
,stream
(aref ,seq i
))))))
2281 (declaim (inline write-sequence
/write-function
))
2282 (defun write-sequence/write-function
(seq stream start %end
2284 character-write-function
2285 binary-write-function
)
2286 (declare (type sequence seq
)
2287 (type stream stream
)
2289 (type sequence-end %end
)
2290 (type stream-element-mode stream-element-mode
)
2291 (type function character-write-function binary-write-function
))
2292 (let ((end (or %end
(length seq
))))
2293 (declare (type index end
))
2294 (labels ((compute-write-function (sequence-element-type)
2295 (stream-compute-io-function
2297 stream-element-mode sequence-element-type
2298 character-write-function binary-write-function
2299 #'write-element
/bivalent
))
2300 (write-element/bivalent
(stream object
)
2301 (if (characterp object
)
2302 (funcall character-write-function stream object
)
2303 (funcall binary-write-function stream object
)))
2304 (write-list (write-function)
2305 (do ((rem (nthcdr start seq
) (rest rem
))
2307 ((or (endp rem
) (>= i end
)))
2308 (declare (type list rem
)
2310 (funcall write-function stream
(first rem
))))
2311 (write-vector (data start end write-function
)
2312 (write-sequence/vector
2313 (data (simple-array * (*))) stream start end write-function
))
2314 (write-generic-sequence (write-function)
2315 (declare (ignore write-function
))
2316 (error "~@<~A does not yet support generic sequences.~@:>"
2318 (declare (dynamic-extent #'compute-write-function
2319 #'write-element
/bivalent
#'write-list
2320 #'write-vector
#'write-generic-sequence
))
2323 (write-list (compute-write-function nil
)))
2325 (if (ansi-stream-p stream
)
2326 (ansi-stream-write-string seq stream start end
)
2327 (stream-write-string stream seq start end
)))
2329 (with-array-data ((data seq
) (offset-start start
) (offset-end end
)
2330 :check-fill-pointer t
)
2331 (cond ((not (and (fd-stream-p stream
)
2332 (compatible-vector-and-stream-element-types-p data stream
)))
2333 (write-vector data offset-start offset-end
2334 (compute-write-function
2335 (array-element-type seq
))))
2336 ((eq (fd-stream-buffering stream
) :none
)
2337 (write-or-buffer-output stream data offset-start offset-end
))
2339 (buffer-output stream data offset-start offset-end
)))))
2341 (write-generic-sequence (compute-write-function nil
)))))))
2342 (declaim (notinline write-sequence
/write-function
))
2344 (defun ansi-stream-write-sequence (seq stream start %end
)
2345 (declare (type sequence seq
)
2346 (type ansi-stream stream
)
2348 (type sequence-end %end
)
2350 (inline write-sequence
/write-function
))
2351 (write-sequence/write-function
2352 seq stream start %end
(stream-element-mode stream
)
2353 (ansi-stream-out stream
) (ansi-stream-bout stream
))
2356 ;;; like FILE-POSITION, only using :FILE-LENGTH
2357 (defun file-length (stream)
2358 ;; FIXME: the FIXME following this one seems wrong on 2 counts:
2359 ;; 1. since when does cross-compiler hangup occur on undefined types?
2360 ;; 2. why is that the correct set of types to check for?
2361 ;; FIXME: The following declaration uses yet undefined types, which
2362 ;; cause cross-compiler hangup.
2364 ;; (declare (type (or file-stream synonym-stream) stream))
2366 ;; The description for FILE-LENGTH says that an error must be raised
2367 ;; for streams not associated with files (which broadcast streams
2368 ;; aren't according to the glossary). However, the behaviour of
2369 ;; FILE-LENGTH for broadcast streams is explicitly described in the
2370 ;; BROADCAST-STREAM entry.
2371 (unless (typep stream
'broadcast-stream
)
2372 (stream-must-be-associated-with-file stream
))
2373 (funcall (ansi-stream-misc stream
) stream
:file-length
))
2375 ;; Placing this definition (formerly in "toplevel") after the important
2376 ;; stream types are known produces smaller+faster code than it did before.
2377 (defun stream-output-stream (stream)
2382 (stream-output-stream
2383 (symbol-value (synonym-stream-symbol stream
))))
2385 (stream-output-stream
2386 (two-way-stream-output-stream stream
)))