Fix STREAM-ELEMENT-MODE for non-fd ANSI-STREAMs
[sbcl.git] / src / code / stream.lisp
blobafa1c28c2b6d4c14b99e752f89438254b7bbed28
1 ;;;; os-independent stream functions
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.
12 (in-package "SB!IMPL")
14 ;;;; standard streams
16 ;;; The initialization of these streams is performed by
17 ;;; STREAM-COLD-INIT-OR-RESET.
18 (defvar *terminal-io* () #!+sb-doc "terminal I/O stream")
19 (defvar *standard-input* () #!+sb-doc "default input stream")
20 (defvar *standard-output* () #!+sb-doc "default output stream")
21 (defvar *error-output* () #!+sb-doc "error output stream")
22 (defvar *query-io* () #!+sb-doc "query I/O stream")
23 (defvar *trace-output* () #!+sb-doc "trace output stream")
24 (defvar *debug-io* () #!+sb-doc "interactive debugging stream")
26 (defun stream-element-type-stream-element-mode (element-type)
27 (when (eq element-type :default)
28 (return-from stream-element-type-stream-element-mode :bivalent))
30 (unless (valid-type-specifier-p element-type)
31 (return-from stream-element-type-stream-element-mode :bivalent))
33 (let* ((characterp (subtypep element-type 'character))
34 (unsigned-byte-p (subtypep element-type 'unsigned-byte))
35 ;; Every UNSIGNED-BYTE subtype is a SIGNED-BYTE
36 ;; subtype. Therefore explicitly check for intersection with
37 ;; the negative integers.
38 (signed-byte-p (and (subtypep element-type 'signed-byte)
39 (not (subtypep `(and ,element-type (integer * -1))
40 nil)))))
41 (cond
42 ((and characterp (not unsigned-byte-p) (not signed-byte-p))
43 'character)
44 ((and (not characterp) unsigned-byte-p (not signed-byte-p))
45 'unsigned-byte)
46 ((and (not characterp) (not unsigned-byte-p) signed-byte-p)
47 'signed-byte)
49 :bivalent))))
51 (defun ill-in (stream &rest ignore)
52 (declare (ignore ignore))
53 (error 'simple-type-error
54 :datum stream
55 :expected-type '(satisfies input-stream-p)
56 :format-control "~S is not a character input stream."
57 :format-arguments (list stream)))
58 (defun ill-out (stream &rest ignore)
59 (declare (ignore ignore))
60 (error 'simple-type-error
61 :datum stream
62 :expected-type '(satisfies output-stream-p)
63 :format-control "~S is not a character output stream."
64 :format-arguments (list stream)))
65 (defun ill-bin (stream &rest ignore)
66 (declare (ignore ignore))
67 (error 'simple-type-error
68 :datum stream
69 :expected-type '(satisfies input-stream-p)
70 :format-control "~S is not a binary input stream."
71 :format-arguments (list stream)))
72 (defun ill-bout (stream &rest ignore)
73 (declare (ignore ignore))
74 (error 'simple-type-error
75 :datum stream
76 :expected-type '(satisfies output-stream-p)
77 :format-control "~S is not a binary output stream."
78 :format-arguments (list stream)))
79 (defun closed-flame (stream &rest ignore)
80 (declare (ignore ignore))
81 (error 'closed-stream-error :stream stream))
82 (defun no-op-placeholder (&rest ignore)
83 (declare (ignore ignore)))
85 ;;; stream manipulation functions
87 ;;; SYNONYM-STREAM type is needed by ANSI-STREAM-{INPUT,OUTPUT}-STREAM-P
88 (defstruct (synonym-stream (:include ansi-stream
89 (in #'synonym-in)
90 (bin #'synonym-bin)
91 (n-bin #'synonym-n-bin)
92 (out #'synonym-out)
93 (bout #'synonym-bout)
94 (sout #'synonym-sout)
95 (misc #'synonym-misc))
96 (:constructor make-synonym-stream (symbol))
97 (:copier nil))
98 ;; This is the symbol, the value of which is the stream we are synonym to.
99 (symbol nil :type symbol :read-only t))
100 (declaim (freeze-type synonym-stream))
102 (defun ansi-stream-input-stream-p (stream)
103 (declare (type ansi-stream stream))
104 (if (synonym-stream-p stream)
105 (input-stream-p (symbol-value (synonym-stream-symbol stream)))
106 (and (not (eq (ansi-stream-in stream) #'closed-flame))
107 ;;; KLUDGE: It's probably not good to have EQ tests on function
108 ;;; values like this. What if someone's redefined the function?
109 ;;; Is there a better way? (Perhaps just VALID-FOR-INPUT and
110 ;;; VALID-FOR-OUTPUT flags? -- WHN 19990902
111 (or (not (eq (ansi-stream-in stream) #'ill-in))
112 (not (eq (ansi-stream-bin stream) #'ill-bin))))))
114 ;;; Temporary definition that gets overwritten by pcl/gray-streams
115 (defun input-stream-p (stream)
116 (declare (type stream stream))
117 (and (ansi-stream-p stream)
118 (ansi-stream-input-stream-p stream)))
120 (defun ansi-stream-output-stream-p (stream)
121 (declare (type ansi-stream stream))
122 (if (synonym-stream-p stream)
123 (output-stream-p (symbol-value (synonym-stream-symbol stream)))
124 (and (not (eq (ansi-stream-in stream) #'closed-flame))
125 (or (not (eq (ansi-stream-out stream) #'ill-out))
126 (not (eq (ansi-stream-bout stream) #'ill-bout))))))
128 ;;; Temporary definition that gets overwritten by pcl/gray-streams
129 (defun output-stream-p (stream)
130 (declare (type stream stream))
131 (and (ansi-stream-p stream)
132 (ansi-stream-output-stream-p stream)))
134 (declaim (inline ansi-stream-open-stream-p))
135 (defun ansi-stream-open-stream-p (stream)
136 (declare (type ansi-stream stream))
137 ;; CLHS 22.1.4 lets us not worry about synonym streams here.
138 (not (eq (ansi-stream-in stream) #'closed-flame)))
140 (defun open-stream-p (stream)
141 (ansi-stream-open-stream-p stream))
143 (declaim (inline ansi-stream-element-type))
144 (defun ansi-stream-element-type (stream)
145 (declare (type ansi-stream stream))
146 (funcall (ansi-stream-misc stream) stream :element-type))
148 (defun stream-element-type (stream)
149 (ansi-stream-element-type stream))
151 (defun stream-external-format (stream)
152 (funcall (ansi-stream-misc stream) stream :external-format))
154 (defun interactive-stream-p (stream)
155 (declare (type stream stream))
156 (funcall (ansi-stream-misc stream) stream :interactive-p))
158 (declaim (inline ansi-stream-close))
159 (defun ansi-stream-close (stream abort)
160 (declare (type ansi-stream stream))
161 (when (open-stream-p stream)
162 (funcall (ansi-stream-misc stream) stream :close abort))
165 (defun close (stream &key abort)
166 (ansi-stream-close stream abort))
168 (defun set-closed-flame (stream)
169 (setf (ansi-stream-in stream) #'closed-flame)
170 (setf (ansi-stream-bin stream) #'closed-flame)
171 (setf (ansi-stream-n-bin stream) #'closed-flame)
172 (setf (ansi-stream-out stream) #'closed-flame)
173 (setf (ansi-stream-bout stream) #'closed-flame)
174 (setf (ansi-stream-sout stream) #'closed-flame)
175 (setf (ansi-stream-misc stream) #'closed-flame))
177 ;;;; for file position and file length
178 (defun external-format-char-size (external-format)
179 (ef-char-size (get-external-format external-format)))
181 ;;; Call the MISC method with the :FILE-POSITION operation.
182 #!-sb-fluid (declaim (inline ansi-stream-file-position))
183 (defun ansi-stream-file-position (stream position)
184 (declare (type stream stream))
185 (declare (type (or index (alien sb!unix:unix-offset) (member nil :start :end))
186 position))
187 ;; FIXME: It would be good to comment on the stuff that is done here...
188 ;; FIXME: This doesn't look interrupt safe.
189 (cond
190 (position
191 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
192 (funcall (ansi-stream-misc stream) stream :file-position position))
194 (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
195 (when res
196 #!-sb-unicode
197 (- res
198 (- +ansi-stream-in-buffer-length+
199 (ansi-stream-in-index stream)))
200 #!+sb-unicode
201 (let ((char-size (if (fd-stream-p stream)
202 (fd-stream-char-size stream)
203 (external-format-char-size (stream-external-format stream)))))
204 (- res
205 (etypecase char-size
206 (function
207 (loop with buffer = (ansi-stream-cin-buffer stream)
208 with start = (ansi-stream-in-index stream)
209 for i from start below +ansi-stream-in-buffer-length+
210 sum (funcall char-size (aref buffer i))))
211 (fixnum
212 (* char-size
213 (- +ansi-stream-in-buffer-length+
214 (ansi-stream-in-index stream))))))))))))
216 (defun file-position (stream &optional position)
217 (if (ansi-stream-p stream)
218 (ansi-stream-file-position stream position)
219 (stream-file-position stream position)))
221 ;;; This is a literal translation of the ANSI glossary entry "stream
222 ;;; associated with a file".
224 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
225 ;;; file", and in particular stdin, stdout, and stderr are files, we
226 ;;; end up with this test being satisfied for weird things like
227 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
228 ;;; ANSI spec really had in mind, especially since this is used as a
229 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
230 ;;; probably thinking of something like what Unix calls block devices)
231 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
232 (defun stream-associated-with-file-p (x)
233 #!+sb-doc
234 "Test for the ANSI concept \"stream associated with a file\"."
235 (or (typep x 'file-stream)
236 (and (synonym-stream-p x)
237 (stream-associated-with-file-p (symbol-value
238 (synonym-stream-symbol x))))))
240 (defun stream-must-be-associated-with-file (stream)
241 (declare (type stream stream))
242 (unless (stream-associated-with-file-p stream)
243 (error 'simple-type-error
244 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
245 ;; this should be TYPE-ERROR. But what then can we use for
246 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
247 ;; private predicate function..) is ugly and confusing, but
248 ;; I can't see any other way. -- WHN 2001-04-14
249 :datum stream
250 :expected-type '(satisfies stream-associated-with-file-p)
251 :format-control
252 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
253 :format-arguments (list stream))))
255 (defun file-string-length (stream object)
256 (funcall (ansi-stream-misc stream) stream :file-string-length object))
258 ;;;; input functions
260 (defun ansi-stream-read-line-from-frc-buffer (stream eof-error-p eof-value)
261 (prepare-for-fast-read-char stream
262 (declare (ignore %frc-method%))
263 (declare (type ansi-stream-cin-buffer %frc-buffer%))
264 (let ((chunks-total-length 0)
265 (chunks nil))
266 (declare (type index chunks-total-length)
267 (list chunks))
268 (labels ((refill-buffer ()
269 (prog1 (fast-read-char-refill stream nil)
270 (setf %frc-index% (ansi-stream-in-index %frc-stream%))))
271 (build-result (pos n-more-chars)
272 (let ((res (make-string (+ chunks-total-length n-more-chars)))
273 (start1 chunks-total-length))
274 (declare (type index start1))
275 (when (>= pos 0)
276 (replace res %frc-buffer%
277 :start1 start1 :start2 %frc-index% :end2 pos)
278 (setf %frc-index% (1+ pos)))
279 (done-with-fast-read-char)
280 (dolist (chunk chunks res)
281 (declare (type (simple-array character (*)) chunk))
282 (decf start1 (length chunk))
283 (replace res chunk :start1 start1)))))
284 (declare (inline refill-buffer))
285 (if (or (< %frc-index% +ansi-stream-in-buffer-length+) (refill-buffer))
286 (loop
287 (let ((pos (position #\Newline %frc-buffer%
288 :test #'char= :start %frc-index%)))
289 (when pos
290 (return (values (build-result pos (- pos %frc-index%)) nil)))
291 (let ((chunk (subseq %frc-buffer% %frc-index%)))
292 (incf chunks-total-length (length chunk))
293 (push chunk chunks))
294 (unless (refill-buffer)
295 (return (values (build-result -1 0) t)))))
296 ;; EOF had been reached before we read anything
297 ;; at all. Return the EOF value or signal the error.
298 (progn (done-with-fast-read-char)
299 (eof-or-lose stream eof-error-p (values eof-value t))))))))
301 #!-sb-fluid (declaim (inline ansi-stream-read-line))
302 (defun ansi-stream-read-line (stream eof-error-p eof-value recursive-p)
303 (declare (ignore recursive-p))
304 (if (ansi-stream-cin-buffer stream)
305 ;; Stream has a fast-read-char buffer. Copy large chunks directly
306 ;; out of the buffer.
307 (ansi-stream-read-line-from-frc-buffer stream eof-error-p eof-value)
308 ;; Slow path, character by character.
309 (prepare-for-fast-read-char stream
310 (let ((res (make-string 80))
311 (len 80)
312 (index 0))
313 (loop
314 (let ((ch (fast-read-char nil nil)))
315 (cond (ch
316 (when (char= ch #\newline)
317 (done-with-fast-read-char)
318 (return (values (%shrink-vector res index) nil)))
319 (when (= index len)
320 (setq len (* len 2))
321 (let ((new (make-string len)))
322 (replace new res)
323 (setq res new)))
324 (setf (schar res index) ch)
325 (incf index))
326 ((zerop index)
327 (done-with-fast-read-char)
328 (return (values (eof-or-lose stream
329 eof-error-p
330 eof-value)
331 t)))
332 ;; Since FAST-READ-CHAR already hit the eof char, we
333 ;; shouldn't do another READ-CHAR.
335 (done-with-fast-read-char)
336 (return (values (%shrink-vector res index) t))))))))))
338 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
339 recursive-p)
340 (declare (explicit-check))
341 (let ((stream (in-synonym-of stream)))
342 (if (ansi-stream-p stream)
343 (ansi-stream-read-line stream eof-error-p eof-value recursive-p)
344 ;; must be Gray streams FUNDAMENTAL-STREAM
345 (multiple-value-bind (string eof) (stream-read-line stream)
346 (if (and eof (zerop (length string)))
347 (values (eof-or-lose stream eof-error-p eof-value) t)
348 (values string eof))))))
350 ;;; We proclaim them INLINE here, then proclaim them NOTINLINE later on,
351 ;;; so, except in this file, they are not inline by default, but they can be.
352 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
354 #!-sb-fluid (declaim (inline ansi-stream-read-char))
355 (defun ansi-stream-read-char (stream eof-error-p eof-value recursive-p)
356 (declare (ignore recursive-p))
357 (prepare-for-fast-read-char stream
358 (prog1
359 (fast-read-char eof-error-p eof-value)
360 (done-with-fast-read-char))))
362 (defun read-char (&optional (stream *standard-input*)
363 (eof-error-p t)
364 eof-value
365 recursive-p)
366 (declare (explicit-check))
367 (let ((stream (in-synonym-of stream)))
368 (if (ansi-stream-p stream)
369 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
370 ;; must be Gray streams FUNDAMENTAL-STREAM
371 (let ((char (stream-read-char stream)))
372 (if (eq char :eof)
373 (eof-or-lose stream eof-error-p eof-value)
374 (the character char))))))
376 #!-sb-fluid (declaim (inline ansi-stream-unread-char))
377 (defun ansi-stream-unread-char (character stream)
378 (let ((index (1- (ansi-stream-in-index stream)))
379 (buffer (ansi-stream-cin-buffer stream)))
380 (declare (fixnum index))
381 (when (minusp index) (error "nothing to unread"))
382 (cond (buffer
383 (setf (aref buffer index) character)
384 (setf (ansi-stream-in-index stream) index)
385 ;; Ugh. an ANSI-STREAM with a char buffer never gives a chance to
386 ;; the stream's misc routine to handle the UNREAD operation.
387 (when (ansi-stream-input-char-pos stream)
388 (decf (ansi-stream-input-char-pos stream))))
390 (funcall (ansi-stream-misc stream) stream
391 :unread character)))))
393 (defun unread-char (character &optional (stream *standard-input*))
394 (declare (explicit-check))
395 (let ((stream (in-synonym-of stream)))
396 (if (ansi-stream-p stream)
397 (ansi-stream-unread-char character stream)
398 ;; must be Gray streams FUNDAMENTAL-STREAM
399 (stream-unread-char stream character)))
400 nil)
402 #!-sb-fluid (declaim (inline ansi-stream-listen))
403 (defun ansi-stream-listen (stream)
404 (or (/= (the fixnum (ansi-stream-in-index stream))
405 +ansi-stream-in-buffer-length+)
406 ;; Handle :EOF return from misc methods specially
407 (let ((result (funcall (ansi-stream-misc stream) stream :listen)))
408 (if (eq result :eof)
410 result))))
412 (defun listen (&optional (stream *standard-input*))
413 (declare (explicit-check))
414 (let ((stream (in-synonym-of stream)))
415 (if (ansi-stream-p stream)
416 (ansi-stream-listen stream)
417 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
418 (stream-listen stream))))
420 #!-sb-fluid (declaim (inline ansi-stream-read-char-no-hang))
421 (defun ansi-stream-read-char-no-hang (stream eof-error-p eof-value recursive-p)
422 (if (funcall (ansi-stream-misc stream) stream :listen)
423 ;; On T or :EOF get READ-CHAR to do the work.
424 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
425 nil))
427 (defun read-char-no-hang (&optional (stream *standard-input*)
428 (eof-error-p t)
429 eof-value
430 recursive-p)
431 (declare (explicit-check))
432 (let ((stream (in-synonym-of stream)))
433 (if (ansi-stream-p stream)
434 (ansi-stream-read-char-no-hang stream eof-error-p eof-value
435 recursive-p)
436 ;; must be Gray streams FUNDAMENTAL-STREAM
437 (let ((char (stream-read-char-no-hang stream)))
438 (if (eq char :eof)
439 (eof-or-lose stream eof-error-p eof-value)
440 (the (or character null) char))))))
442 #!-sb-fluid (declaim (inline ansi-stream-clear-input))
443 (defun ansi-stream-clear-input (stream)
444 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
445 (funcall (ansi-stream-misc stream) stream :clear-input))
447 (defun clear-input (&optional (stream *standard-input*))
448 (declare (explicit-check))
449 (let ((stream (in-synonym-of stream)))
450 (if (ansi-stream-p stream)
451 (ansi-stream-clear-input stream)
452 ;; must be Gray streams FUNDAMENTAL-STREAM
453 (stream-clear-input stream)))
454 nil)
456 #!-sb-fluid (declaim (inline ansi-stream-read-byte))
457 (defun ansi-stream-read-byte (stream eof-error-p eof-value recursive-p)
458 ;; Why the "recursive-p" parameter? a-s-r-b is funcall'ed from
459 ;; a-s-read-sequence and needs a lambda list that's congruent with
460 ;; that of a-s-read-char
461 (declare (ignore recursive-p))
462 (with-fast-read-byte (t stream eof-error-p eof-value)
463 ;; FIXME: the overhead of the UNWIND-PROTECT inserted by
464 ;; WITH-FAST-READ-BYTE significantly impacts the time taken
465 ;; by single byte reads. For this use-case we could
466 ;; probably just change it to a PROG1.
467 (fast-read-byte)))
469 (defun read-byte (stream &optional (eof-error-p t) eof-value)
470 (declare (explicit-check))
471 (if (ansi-stream-p stream)
472 (ansi-stream-read-byte stream eof-error-p eof-value nil)
473 ;; must be Gray streams FUNDAMENTAL-STREAM
474 (let ((byte (stream-read-byte stream)))
475 (if (eq byte :eof)
476 (eof-or-lose stream eof-error-p eof-value)
477 (the integer byte)))))
479 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
480 ;;; number of bytes read.
482 ;;; Note: CMU CL's version of this had a special interpretation of
483 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
484 ;;; case, CMU CL's version would return as soon as any data became
485 ;;; available.) This could be useful behavior for things like pipes in
486 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
487 ;;; If we ever need it, it could be added later as a new variant N-BIN
488 ;;; method (perhaps N-BIN-ASAP?) or something.
489 #!-sb-fluid (declaim (inline read-n-bytes))
490 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
491 (if (ansi-stream-p stream)
492 (ansi-stream-read-n-bytes stream buffer start numbytes eof-error-p)
493 ;; We don't need to worry about element-type size here is that
494 ;; callers are supposed to have checked everything is kosher.
495 (let* ((end (+ start numbytes))
496 (read-end (stream-read-sequence stream buffer start end)))
497 (eof-or-lose stream (and eof-error-p (< read-end end)) (- read-end start)))))
499 (defun ansi-stream-read-n-bytes (stream buffer start numbytes eof-error-p)
500 (declare (type ansi-stream stream)
501 (type index numbytes start)
502 (type (or (simple-array * (*)) system-area-pointer) buffer))
503 (let* ((in-buffer (ansi-stream-in-buffer stream))
504 (index (ansi-stream-in-index stream))
505 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
506 (declare (fixnum index num-buffered))
507 (cond
508 ((not in-buffer)
509 (funcall (ansi-stream-n-bin stream)
510 stream
511 buffer
512 start
513 numbytes
514 eof-error-p))
515 ((<= numbytes num-buffered)
516 #+nil
517 (let ((copy-function (typecase buffer
518 ((simple-array * (*)) #'ub8-bash-copy)
519 (system-area-pointer #'copy-ub8-to-system-area))))
520 (funcall copy-function in-buffer index buffer start numbytes))
521 (%byte-blt in-buffer index
522 buffer start (+ start numbytes))
523 (setf (ansi-stream-in-index stream) (+ index numbytes))
524 numbytes)
526 (let ((end (+ start num-buffered)))
527 #+nil
528 (let ((copy-function (typecase buffer
529 ((simple-array * (*)) #'ub8-bash-copy)
530 (system-area-pointer #'copy-ub8-to-system-area))))
531 (funcall copy-function in-buffer index buffer start num-buffered))
532 (%byte-blt in-buffer index buffer start end)
533 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
534 (+ (funcall (ansi-stream-n-bin stream)
535 stream
536 buffer
538 (- numbytes num-buffered)
539 eof-error-p)
540 num-buffered))))))
542 ;;; the amount of space we leave at the start of the in-buffer for
543 ;;; unreading
545 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
546 (defconstant +ansi-stream-in-buffer-extra+
547 4) ; FIXME: should be symbolic constant
549 ;;; This function is called by the FAST-READ-CHAR expansion to refill
550 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
551 ;;; and hence must be an N-BIN method. It's also called by other stream
552 ;;; functions which directly peek into the frc buffer.
553 ;;; If EOF is hit and EOF-ERROR-P is false, then return NIL,
554 ;;; otherwise return the new index into CIN-BUFFER.
555 (defun fast-read-char-refill (stream eof-error-p)
556 (when (ansi-stream-input-char-pos stream)
557 ;; Characters between (ANSI-STREAM-IN-INDEX %FRC-STREAM%)
558 ;; and +ANSI-STREAM-IN-BUFFER-LENGTH+ have to be re-scanned.
559 (update-input-char-pos stream))
560 (let* ((ibuf (ansi-stream-cin-buffer stream))
561 (count (funcall (ansi-stream-n-bin stream)
562 stream
563 ibuf
564 +ansi-stream-in-buffer-extra+
565 (- +ansi-stream-in-buffer-length+
566 +ansi-stream-in-buffer-extra+)
567 nil))
568 (start (- +ansi-stream-in-buffer-length+ count)))
569 (declare (type index start count))
570 (cond ((zerop count)
571 ;; An empty count does not necessarily mean that we reached
572 ;; the EOF, it's also possible that it's e.g. due to a
573 ;; invalid octet sequence in a multibyte stream. To handle
574 ;; the resyncing case correctly we need to call the reading
575 ;; function and check whether an EOF was really reached. If
576 ;; not, we can just fill the buffer by one character, and
577 ;; hope that the next refill will not need to resync.
579 ;; KLUDGE: we can't use FD-STREAM functions (which are the
580 ;; only ones which will give us decoding errors) here,
581 ;; because this code is generic. We can't call the N-BIN
582 ;; function, because near the end of a real file that can
583 ;; legitimately bounce us to the IN function. So we have
584 ;; to call ANSI-STREAM-IN.
585 (let* ((index (1- +ansi-stream-in-buffer-length+))
586 (value (funcall (ansi-stream-in stream) stream nil :eof)))
587 (cond
588 ;; When not signaling an error, it is important that IN-INDEX
589 ;; be set to +ANSI-STREAM-IN-BUFFER-LENGTH+ here, even though
590 ;; DONE-WITH-FAST-READ-CHAR will do the same, thereby writing
591 ;; the caller's %FRC-INDEX% (= +ANSI-STREAM-IN-BUFFER-LENGTH+)
592 ;; into the slot. But because we've already bumped INPUT-CHAR-POS
593 ;; and scanned characters between the original %FRC-INDEX%
594 ;; and the buffer end (above), we must *not* do that again.
595 ((eql value :eof)
596 ;; definitely EOF now
597 (setf (ansi-stream-in-index stream)
598 +ansi-stream-in-buffer-length+)
599 (eof-or-lose stream eof-error-p nil))
600 ;; we resynced or were given something instead
602 (setf (aref ibuf index) value)
603 (setf (ansi-stream-in-index stream) index)))))
605 (when (/= start +ansi-stream-in-buffer-extra+)
606 (#.(let* ((n-character-array-bits
607 (sb!vm:saetp-n-bits
608 (find 'character
609 sb!vm:*specialized-array-element-type-properties*
610 :key #'sb!vm:saetp-specifier)))
611 (bash-function (intern (format nil "UB~D-BASH-COPY" n-character-array-bits)
612 (find-package "SB!KERNEL"))))
613 bash-function)
614 ibuf +ansi-stream-in-buffer-extra+
615 ibuf start
616 count))
617 (setf (ansi-stream-in-index stream) start)))))
619 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
620 ;;; leave room for unreading.
621 (defun fast-read-byte-refill (stream eof-error-p eof-value)
622 (let* ((ibuf (ansi-stream-in-buffer stream))
623 (count (funcall (ansi-stream-n-bin stream) stream
624 ibuf 0 +ansi-stream-in-buffer-length+
625 nil))
626 (start (- +ansi-stream-in-buffer-length+ count)))
627 (declare (type index start count))
628 (cond ((zerop count)
629 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
630 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
632 (unless (zerop start)
633 (ub8-bash-copy ibuf 0
634 ibuf start
635 count))
636 (setf (ansi-stream-in-index stream) (1+ start))
637 (aref ibuf start)))))
639 ;;; output functions
641 (defun write-char (character &optional (stream *standard-output*))
642 (declare (explicit-check))
643 (with-out-stream stream (ansi-stream-out character)
644 (stream-write-char character))
645 character)
647 (defun terpri (&optional (stream *standard-output*))
648 (declare (explicit-check))
649 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
650 nil)
652 #!-sb-fluid (declaim (inline ansi-stream-fresh-line))
653 (defun ansi-stream-fresh-line (stream)
654 (when (/= (or (charpos stream) 1) 0)
655 (funcall (ansi-stream-out stream) stream #\newline)
658 (defun fresh-line (&optional (stream *standard-output*))
659 (declare (explicit-check))
660 (let ((stream (out-synonym-of stream)))
661 (if (ansi-stream-p stream)
662 (ansi-stream-fresh-line stream)
663 ;; must be Gray streams FUNDAMENTAL-STREAM
664 (stream-fresh-line stream))))
666 #!-sb-fluid (declaim (inline ansi-stream-write-string))
667 (defun ansi-stream-write-string (string stream start end)
668 (with-array-data ((data string) (offset-start start)
669 (offset-end end)
670 :check-fill-pointer t)
671 (funcall (ansi-stream-sout stream)
672 stream data offset-start offset-end)))
674 (defun %write-string (string stream start end)
675 (let ((stream (out-synonym-of stream)))
676 (if (ansi-stream-p stream)
677 (ansi-stream-write-string string stream start end)
678 ;; must be Gray streams FUNDAMENTAL-STREAM
679 (stream-write-string stream string start end)))
680 string)
682 (defun write-string (string &optional (stream *standard-output*)
683 &key (start 0) end)
684 (declare (type string string))
685 (declare (type stream-designator stream))
686 (declare (explicit-check))
687 (%write-string string stream start end))
689 (defun write-line (string &optional (stream *standard-output*)
690 &key (start 0) end)
691 (declare (type string string))
692 (declare (type stream-designator stream))
693 (declare (explicit-check))
694 (let ((stream (out-synonym-of stream)))
695 (cond ((ansi-stream-p stream)
696 (ansi-stream-write-string string stream start end)
697 (funcall (ansi-stream-out stream) stream #\newline))
699 (stream-write-string stream string start end)
700 (stream-write-char stream #\newline))))
701 string)
703 (defun charpos (&optional (stream *standard-output*))
704 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
706 (defun line-length (&optional (stream *standard-output*))
707 (with-out-stream stream (ansi-stream-misc :line-length)
708 (stream-line-length)))
710 (defun finish-output (&optional (stream *standard-output*))
711 (declare (explicit-check))
712 (with-out-stream stream (ansi-stream-misc :finish-output)
713 (stream-finish-output))
714 nil)
716 (defun force-output (&optional (stream *standard-output*))
717 (declare (explicit-check))
718 (with-out-stream stream (ansi-stream-misc :force-output)
719 (stream-force-output))
720 nil)
722 (defun clear-output (&optional (stream *standard-output*))
723 (declare (explicit-check))
724 (with-out-stream stream (ansi-stream-misc :clear-output)
725 (stream-clear-output))
726 nil)
728 (defun write-byte (integer stream)
729 (declare (explicit-check))
730 (with-out-stream/no-synonym stream (ansi-stream-bout integer)
731 (stream-write-byte integer))
732 integer)
735 ;;; Meta: the following comment is mostly true, but gray stream support
736 ;;; is already incorporated into the definitions within this file.
737 ;;; But these need to redefinable, otherwise the relative order of
738 ;;; loading sb-simple-streams and any user-defined code which executes
739 ;;; (F #'read-char ...) is sensitive to the order in which those
740 ;;; are loaded, though insensitive at compile-time.
741 ;;; (These were inline throughout this file, but that's not appropriate
742 ;;; globally. And we must not inline them in the rest of this file if
743 ;;; dispatch to gray or simple streams is to work, since both redefine
744 ;;; these functions later.)
745 (declaim (notinline read-char unread-char read-byte listen))
747 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
748 ;;; streams to handle the misc routines and dispatch to the
749 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
750 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
751 (declare (type stream stream) (ignore arg2))
752 (ecase operation
753 (:listen
754 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
755 (let ((char (read-char-no-hang stream nil :eof)))
756 (when (characterp char)
757 (unread-char char stream))
758 char))
759 (:unread
760 (unread-char arg1 stream))
761 (:close
762 (close stream))
763 (:clear-input
764 (clear-input stream))
765 (:force-output
766 (force-output stream))
767 (:finish-output
768 (finish-output stream))
769 (:element-type
770 (stream-element-type stream))
771 (:element-mode
772 (stream-element-type-stream-element-mode
773 (stream-element-type stream)))
774 (:stream-external-format
775 (stream-external-format stream))
776 (:interactive-p
777 (interactive-stream-p stream))
778 (:line-length
779 (line-length stream))
780 (:charpos
781 (charpos stream))
782 (:file-length
783 (file-length stream))
784 (:file-string-length
785 (file-string-length stream arg1))
786 (:file-position
787 (file-position stream arg1))))
789 ;;;; broadcast streams
791 (defstruct (broadcast-stream (:include ansi-stream
792 (out #'broadcast-out)
793 (bout #'broadcast-bout)
794 (sout #'broadcast-sout)
795 (misc #'broadcast-misc))
796 (:constructor %make-broadcast-stream
797 (streams))
798 (:copier nil)
799 (:predicate nil))
800 ;; a list of all the streams we broadcast to
801 (streams () :type list :read-only t))
803 (declaim (freeze-type broadcast-stream))
805 (defun make-broadcast-stream (&rest streams)
806 (dolist (stream streams)
807 (unless (output-stream-p stream)
808 (error 'type-error
809 :datum stream
810 :expected-type '(satisfies output-stream-p))))
811 (%make-broadcast-stream streams))
813 (macrolet ((out-fun (name fun &rest args)
814 `(defun ,name (stream ,@args)
815 (dolist (stream (broadcast-stream-streams stream))
816 (,fun ,(car args) stream ,@(cdr args))))))
817 (out-fun broadcast-out write-char char)
818 (out-fun broadcast-bout write-byte byte)
819 (out-fun broadcast-sout %write-string string start end))
821 (defun broadcast-misc (stream operation &optional arg1 arg2)
822 (let ((streams (broadcast-stream-streams stream)))
823 (case operation
824 ;; FIXME: This may not be the best place to note this, but I
825 ;; think the :CHARPOS protocol needs revision. Firstly, I think
826 ;; this is the last place where a NULL return value was possible
827 ;; (before adjusting it to be 0), so a bunch of conditionals IF
828 ;; CHARPOS can be removed; secondly, it is my belief that
829 ;; FD-STREAMS, when running FILE-POSITION, do not update the
830 ;; CHARPOS, and consequently there will be much wrongness.
832 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
833 ;; is it testing the :charpos of an input stream?
835 ;; -- CSR, 2004-02-04
836 (:charpos
837 (dolist (stream streams 0)
838 (let ((charpos (charpos stream)))
839 (when charpos
840 (return charpos)))))
841 (:line-length
842 (let ((min nil))
843 (dolist (stream streams min)
844 (let ((res (line-length stream)))
845 (when res (setq min (if min (min res min) res)))))))
846 (:element-type
847 (let ((last (last streams)))
848 (if last
849 (stream-element-type (car last))
850 t)))
851 (:element-mode
852 (awhen (last streams)
853 (stream-element-mode (car it))))
854 (:external-format
855 (let ((last (last streams)))
856 (if last
857 (stream-external-format (car last))
858 :default)))
859 (:file-length
860 (let ((last (last streams)))
861 (if last
862 (file-length (car last))
863 0)))
864 (:file-position
865 (if arg1
866 (let ((res (or (eql arg1 :start) (eql arg1 0))))
867 (dolist (stream streams res)
868 (setq res (file-position stream arg1))))
869 (let ((last (last streams)))
870 (if last
871 (file-position (car last))
872 0))))
873 (:file-string-length
874 (let ((last (last streams)))
875 (if last
876 (file-string-length (car last) arg1)
877 1)))
878 (:close
879 (set-closed-flame stream))
881 (let ((res nil))
882 (dolist (stream streams res)
883 (setq res
884 (if (ansi-stream-p stream)
885 (funcall (ansi-stream-misc stream) stream operation
886 arg1 arg2)
887 (stream-misc-dispatch stream operation arg1 arg2)))))))))
889 ;;;; synonym streams
891 (defmethod print-object ((x synonym-stream) stream)
892 (print-unreadable-object (x stream :type t :identity t)
893 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
895 ;;; The output simple output methods just call the corresponding
896 ;;; function on the synonymed stream.
897 (macrolet ((out-fun (name fun &rest args)
898 `(defun ,name (stream ,@args)
899 (declare (optimize (safety 1)))
900 (let ((syn (symbol-value (synonym-stream-symbol stream))))
901 (,fun ,(car args) syn ,@(cdr args))))))
902 (out-fun synonym-out write-char ch)
903 (out-fun synonym-bout write-byte n)
904 (out-fun synonym-sout %write-string string start end))
906 ;;; For the input methods, we just call the corresponding function on the
907 ;;; synonymed stream. These functions deal with getting input out of
908 ;;; the In-Buffer if there is any.
909 (macrolet ((in-fun (name fun &rest args)
910 `(defun ,name (stream ,@args)
911 (declare (optimize (safety 1)))
912 (,fun (symbol-value (synonym-stream-symbol stream))
913 ,@args))))
914 (in-fun synonym-in read-char eof-error-p eof-value)
915 (in-fun synonym-bin read-byte eof-error-p eof-value)
916 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
918 (defun synonym-misc (stream operation &optional arg1 arg2)
919 (declare (optimize (safety 1)))
920 (let ((syn (symbol-value (synonym-stream-symbol stream))))
921 (if (ansi-stream-p syn)
922 ;; We have to special-case some operations which interact with
923 ;; the in-buffer of the wrapped stream, since just calling
924 ;; ANSI-STREAM-MISC on them
925 (case operation
926 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
927 +ansi-stream-in-buffer-length+)
928 (funcall (ansi-stream-misc syn) syn :listen)))
929 (:clear-input (clear-input syn))
930 (:unread (unread-char arg1 syn))
932 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
933 (stream-misc-dispatch syn operation arg1 arg2))))
935 ;;;; two-way streams
937 (defstruct (two-way-stream
938 (:include ansi-stream
939 (in #'two-way-in)
940 (bin #'two-way-bin)
941 (n-bin #'two-way-n-bin)
942 (out #'two-way-out)
943 (bout #'two-way-bout)
944 (sout #'two-way-sout)
945 (misc #'two-way-misc))
946 (:constructor %make-two-way-stream (input-stream output-stream))
947 (:copier nil)
948 (:predicate nil))
949 (input-stream (missing-arg) :type stream :read-only t)
950 (output-stream (missing-arg) :type stream :read-only t))
952 (defprinter (two-way-stream) input-stream output-stream)
954 (defun make-two-way-stream (input-stream output-stream)
955 #!+sb-doc
956 "Return a bidirectional stream which gets its input from INPUT-STREAM and
957 sends its output to OUTPUT-STREAM."
958 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
959 ;; should be encapsulated in a function, and used here and most of
960 ;; the other places that SYNONYM-STREAM-P appears.
961 (unless (output-stream-p output-stream)
962 (error 'type-error
963 :datum output-stream
964 :expected-type '(satisfies output-stream-p)))
965 (unless (input-stream-p input-stream)
966 (error 'type-error
967 :datum input-stream
968 :expected-type '(satisfies input-stream-p)))
969 (%make-two-way-stream input-stream output-stream))
971 (macrolet ((out-fun (name fun &rest args)
972 `(defun ,name (stream ,@args)
973 (let ((syn (two-way-stream-output-stream stream)))
974 (,fun ,(car args) syn ,@(cdr args))))))
975 (out-fun two-way-out write-char ch)
976 (out-fun two-way-bout write-byte n)
977 (out-fun two-way-sout %write-string string start end))
979 (macrolet ((in-fun (name fun &rest args)
980 `(defun ,name (stream ,@args)
981 (,fun (two-way-stream-input-stream stream) ,@args))))
982 (in-fun two-way-in read-char eof-error-p eof-value)
983 (in-fun two-way-bin read-byte eof-error-p eof-value)
984 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
986 (defun two-way-misc (stream operation &optional arg1 arg2)
987 (let* ((in (two-way-stream-input-stream stream))
988 (out (two-way-stream-output-stream stream))
989 (in-ansi-stream-p (ansi-stream-p in))
990 (out-ansi-stream-p (ansi-stream-p out)))
991 (case operation
992 (:listen
993 (if in-ansi-stream-p
994 (or (/= (the fixnum (ansi-stream-in-index in))
995 +ansi-stream-in-buffer-length+)
996 (funcall (ansi-stream-misc in) in :listen))
997 (listen in)))
998 ((:finish-output :force-output :clear-output)
999 (if out-ansi-stream-p
1000 (funcall (ansi-stream-misc out) out operation arg1 arg2)
1001 (stream-misc-dispatch out operation arg1 arg2)))
1002 (:clear-input (clear-input in))
1003 (:unread (unread-char arg1 in))
1004 (:element-type
1005 (let ((in-type (stream-element-type in))
1006 (out-type (stream-element-type out)))
1007 (if (equal in-type out-type)
1008 in-type
1009 `(and ,in-type ,out-type))))
1010 (:element-mode
1011 (let ((in-mode (stream-element-mode in))
1012 (out-mode (stream-element-mode out)))
1013 (when (equal in-mode out-mode)
1014 in-mode)))
1015 (:close
1016 (set-closed-flame stream))
1018 (or (if in-ansi-stream-p
1019 (funcall (ansi-stream-misc in) in operation arg1 arg2)
1020 (stream-misc-dispatch in operation arg1 arg2))
1021 (if out-ansi-stream-p
1022 (funcall (ansi-stream-misc out) out operation arg1 arg2)
1023 (stream-misc-dispatch out operation arg1 arg2)))))))
1025 ;;;; concatenated streams
1027 (defstruct (concatenated-stream
1028 (:include ansi-stream
1029 (in #'concatenated-in)
1030 (bin #'concatenated-bin)
1031 (n-bin #'concatenated-n-bin)
1032 (misc #'concatenated-misc))
1033 (:constructor %make-concatenated-stream (streams))
1034 (:copier nil)
1035 (:predicate nil))
1036 ;; The car of this is the substream we are reading from now.
1037 (streams nil :type list))
1039 (declaim (freeze-type concatenated-stream))
1041 (defmethod print-object ((x concatenated-stream) stream)
1042 (print-unreadable-object (x stream :type t :identity t)
1043 (format stream
1044 ":STREAMS ~S"
1045 (concatenated-stream-streams x))))
1047 (defun make-concatenated-stream (&rest streams)
1048 #!+sb-doc
1049 "Return a stream which takes its input from each of the streams in turn,
1050 going on to the next at EOF."
1051 (dolist (stream streams)
1052 (unless (input-stream-p stream)
1053 (error 'type-error
1054 :datum stream
1055 :expected-type '(satisfies input-stream-p))))
1056 (%make-concatenated-stream streams))
1058 (macrolet ((in-fun (name fun)
1059 `(defun ,name (stream eof-error-p eof-value)
1060 (do ((streams (concatenated-stream-streams stream)
1061 (cdr streams)))
1062 ((null streams)
1063 (eof-or-lose stream eof-error-p eof-value))
1064 (let* ((stream (car streams))
1065 (result (,fun stream nil nil)))
1066 (when result (return result)))
1067 (pop (concatenated-stream-streams stream))))))
1068 (in-fun concatenated-in read-char)
1069 (in-fun concatenated-bin read-byte))
1071 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
1072 (do ((streams (concatenated-stream-streams stream) (cdr streams))
1073 (current-start start)
1074 (remaining-bytes numbytes))
1075 ((null streams)
1076 (if eof-errorp
1077 (error 'end-of-file :stream stream)
1078 (- numbytes remaining-bytes)))
1079 (let* ((stream (car streams))
1080 (bytes-read (read-n-bytes stream buffer current-start
1081 remaining-bytes nil)))
1082 (incf current-start bytes-read)
1083 (decf remaining-bytes bytes-read)
1084 (when (zerop remaining-bytes) (return numbytes)))
1085 (setf (concatenated-stream-streams stream) (cdr streams))))
1087 (defun concatenated-misc (stream operation &optional arg1 arg2)
1088 (let* ((left (concatenated-stream-streams stream))
1089 (current (car left)))
1090 (case operation
1091 (:listen
1092 (unless left
1093 (return-from concatenated-misc :eof))
1094 (loop
1095 (let ((stuff (if (ansi-stream-p current)
1096 (funcall (ansi-stream-misc current) current
1097 :listen)
1098 (stream-misc-dispatch current :listen))))
1099 (cond ((eq stuff :eof)
1100 ;; Advance STREAMS, and try again.
1101 (pop (concatenated-stream-streams stream))
1102 (setf current
1103 (car (concatenated-stream-streams stream)))
1104 (unless current
1105 ;; No further streams. EOF.
1106 (return :eof)))
1107 (stuff
1108 ;; Stuff's available.
1109 (return t))
1111 ;; Nothing is available yet.
1112 (return nil))))))
1113 (:clear-input (when left (clear-input current)))
1114 (:unread (when left (unread-char arg1 current)))
1115 (:close
1116 (set-closed-flame stream))
1118 (when left
1119 (if (ansi-stream-p current)
1120 (funcall (ansi-stream-misc current) current operation arg1 arg2)
1121 (stream-misc-dispatch current operation arg1 arg2)))))))
1123 ;;;; echo streams
1125 (defstruct (echo-stream
1126 (:include two-way-stream
1127 (in #'echo-in)
1128 (bin #'echo-bin)
1129 (misc #'echo-misc)
1130 (n-bin #'echo-n-bin))
1131 (:constructor %make-echo-stream (input-stream output-stream))
1132 (:copier nil)
1133 (:predicate nil))
1134 (unread-stuff nil :type boolean))
1136 (declaim (freeze-type echo-stream))
1138 (defmethod print-object ((x echo-stream) stream)
1139 (print-unreadable-object (x stream :type t :identity t)
1140 (format stream
1141 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1142 (two-way-stream-input-stream x)
1143 (two-way-stream-output-stream x))))
1145 (defun make-echo-stream (input-stream output-stream)
1146 #!+sb-doc
1147 "Return a bidirectional stream which gets its input from INPUT-STREAM and
1148 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
1149 the output stream."
1150 (unless (output-stream-p output-stream)
1151 (error 'type-error
1152 :datum output-stream
1153 :expected-type '(satisfies output-stream-p)))
1154 (unless (input-stream-p input-stream)
1155 (error 'type-error
1156 :datum input-stream
1157 :expected-type '(satisfies input-stream-p)))
1158 (%make-echo-stream input-stream output-stream))
1160 (macrolet ((in-fun (name in-fun out-fun &rest args)
1161 `(defun ,name (stream ,@args)
1162 (let* ((unread-stuff-p (echo-stream-unread-stuff stream))
1163 (in (echo-stream-input-stream stream))
1164 (out (echo-stream-output-stream stream))
1165 (result (if eof-error-p
1166 (,in-fun in ,@args)
1167 (,in-fun in nil in))))
1168 (setf (echo-stream-unread-stuff stream) nil)
1169 (cond
1170 ((eql result in) eof-value)
1171 ;; If unread-stuff was true, the character read
1172 ;; from the input stream was previously echoed.
1173 (t (unless unread-stuff-p (,out-fun result out)) result))))))
1174 (in-fun echo-in read-char write-char eof-error-p eof-value)
1175 (in-fun echo-bin read-byte write-byte eof-error-p eof-value))
1177 (defun echo-n-bin (stream buffer start numbytes eof-error-p)
1178 (let ((bytes-read 0))
1179 ;; Note: before ca 1.0.27.18, the logic for handling unread
1180 ;; characters never could have worked, so probably nobody has ever
1181 ;; tried doing bivalent block I/O through an echo stream; this may
1182 ;; not work either.
1183 (when (echo-stream-unread-stuff stream)
1184 (let* ((char (read-char stream))
1185 (octets (string-to-octets
1186 (string char)
1187 :external-format
1188 (stream-external-format
1189 (echo-stream-input-stream stream))))
1190 (octet-count (length octets))
1191 (blt-count (min octet-count numbytes)))
1192 (replace buffer octets :start1 start :end1 (+ start blt-count))
1193 (incf start blt-count)
1194 (decf numbytes blt-count)))
1195 (incf bytes-read (read-n-bytes (echo-stream-input-stream stream) buffer
1196 start numbytes nil))
1197 (cond
1198 ((not eof-error-p)
1199 (write-sequence buffer (echo-stream-output-stream stream)
1200 :start start :end (+ start bytes-read))
1201 bytes-read)
1202 ((> numbytes bytes-read)
1203 (write-sequence buffer (echo-stream-output-stream stream)
1204 :start start :end (+ start bytes-read))
1205 (error 'end-of-file :stream stream))
1207 (write-sequence buffer (echo-stream-output-stream stream)
1208 :start start :end (+ start bytes-read))
1209 (aver (= numbytes (+ start bytes-read)))
1210 numbytes))))
1212 ;;;; STRING-INPUT-STREAM stuff
1214 (defstruct (string-input-stream
1215 (:include ansi-stream
1216 (in #'string-inch)
1217 (misc #'string-in-misc))
1218 (:constructor %make-string-input-stream
1219 (string current end))
1220 (:copier nil)
1221 (:predicate nil))
1222 (string (missing-arg) :type simple-string :read-only t)
1223 (current (missing-arg) :type index)
1224 (end (missing-arg) :type index))
1226 (declaim (freeze-type string-input-stream))
1228 (defun string-inch (stream eof-error-p eof-value)
1229 (declare (type string-input-stream stream))
1230 (let ((string (string-input-stream-string stream))
1231 (index (string-input-stream-current stream)))
1232 (cond ((>= index (the index (string-input-stream-end stream)))
1233 (eof-or-lose stream eof-error-p eof-value))
1235 (setf (string-input-stream-current stream) (1+ index))
1236 (char string index)))))
1238 (defun string-binch (stream eof-error-p eof-value)
1239 (declare (type string-input-stream stream))
1240 (let ((string (string-input-stream-string stream))
1241 (index (string-input-stream-current stream)))
1242 (cond ((>= index (the index (string-input-stream-end stream)))
1243 (eof-or-lose stream eof-error-p eof-value))
1245 (setf (string-input-stream-current stream) (1+ index))
1246 (char-code (char string index))))))
1248 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1249 (declare (type string-input-stream stream)
1250 (type index start requested))
1251 (let* ((string (string-input-stream-string stream))
1252 (index (string-input-stream-current stream))
1253 (available (- (string-input-stream-end stream) index))
1254 (copy (min available requested)))
1255 (declare (type simple-string string))
1256 (when (plusp copy)
1257 (setf (string-input-stream-current stream)
1258 (truly-the index (+ index copy)))
1259 ;; FIXME: why are we VECTOR-SAP'ing things here? what's the point?
1260 ;; and are there SB-UNICODE issues here as well? --njf, 2005-03-24
1261 (with-pinned-objects (string buffer)
1262 (system-area-ub8-copy (vector-sap string)
1263 index
1264 (if (typep buffer 'system-area-pointer)
1265 buffer
1266 (vector-sap buffer))
1267 start
1268 copy)))
1269 (if (and (> requested copy) eof-error-p)
1270 (error 'end-of-file :stream stream)
1271 copy)))
1273 (defun string-in-misc (stream operation &optional arg1 arg2)
1274 (declare (type string-input-stream stream)
1275 (ignore arg2))
1276 (case operation
1277 (:file-position
1278 (if arg1
1279 (setf (string-input-stream-current stream)
1280 (case arg1
1281 (:start 0)
1282 (:end (string-input-stream-end stream))
1283 ;; We allow moving position beyond EOF. Errors happen
1284 ;; on read, not move.
1285 (t arg1)))
1286 (string-input-stream-current stream)))
1287 ;; According to ANSI: "Should signal an error of type type-error
1288 ;; if stream is not a stream associated with a file."
1289 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1290 ;; (:file-length (length (string-input-stream-string stream)))
1291 (:unread (decf (string-input-stream-current stream)))
1292 (:close (set-closed-flame stream))
1293 (:listen (or (/= (the index (string-input-stream-current stream))
1294 (the index (string-input-stream-end stream)))
1295 :eof))
1296 (:element-type (array-element-type (string-input-stream-string stream)))))
1298 (defun make-string-input-stream (string &optional (start 0) end)
1299 #!+sb-doc
1300 "Return an input stream which will supply the characters of STRING between
1301 START and END in order."
1302 (declare (type string string)
1303 (type index start)
1304 (type (or index null) end))
1305 ;; FIXME: very inefficient if the input string is, say a 100000-character
1306 ;; adjustable string but (- END START) is 100 characters. We should use
1307 ;; SUBSEQ instead of coercing the whole string. And if STRING is non-simple
1308 ;; but has element type CHARACTER, wouldn't it work to just use the
1309 ;; underlying simple-string since %MAKE-STRING-INPUT-STREAM accepts bounding
1310 ;; indices that can be fudged to deal with any offset?
1311 ;; And (for unicode builds) if the input is BASE-STRING, we should use
1312 ;; MAKE-ARRAY and REPLACE to coerce just the specified piece.
1313 (let* ((string (coerce string '(simple-array character (*)))))
1314 ;; Why WITH-ARRAY-DATA, since the array is already simple?
1315 ;; because it's a nice abstract way to check the START and END.
1316 (with-array-data ((string string) (start start) (end end))
1317 (%make-string-input-stream
1318 string ;; now simple
1319 start end))))
1321 ;;;; STRING-OUTPUT-STREAM stuff
1322 ;;;;
1323 ;;;; FIXME: This, like almost none of the stream code is particularly
1324 ;;;; interrupt or thread-safe. While it should not be possible to
1325 ;;;; corrupt the heap here, it certainly is possible to end up with
1326 ;;;; a string-output-stream whose internal state is messed up.
1327 ;;;;
1328 ;;;; FIXME: It would be nice to support space-efficient
1329 ;;;; string-output-streams with element-type base-char. This would
1330 ;;;; mean either a separate subclass, or typecases in functions.
1332 (defparameter *string-output-stream-buffer-initial-size* 64)
1334 (defstruct (string-output-stream
1335 (:include ansi-stream
1336 (out #'string-ouch)
1337 (sout #'string-sout)
1338 (misc #'string-out-misc))
1339 (:constructor %make-string-output-stream (element-type))
1340 (:copier nil)
1341 (:predicate nil))
1342 ;; The string we throw stuff in.
1343 (buffer (make-string
1344 *string-output-stream-buffer-initial-size*)
1345 :type (simple-array character (*)))
1346 ;; Chains of buffers to use
1347 (prev nil :type list)
1348 (next nil :type list)
1349 ;; Index of the next location to use in the current string.
1350 (pointer 0 :type index)
1351 ;; Global location in the stream
1352 (index 0 :type index)
1353 ;; Index cache: when we move backwards we save the greater of this
1354 ;; and index here, so the greater of index and this is always the
1355 ;; end of the stream.
1356 (index-cache 0 :type index)
1357 ;; Requested element type
1358 ;; FIXME: there seems to be no way to skip the type-check in the ctor,
1359 ;; which is redundant with the check in MAKE-STRING-OUTPUT-STREAM.
1360 (element-type 'character :type type-specifier
1361 :read-only t))
1363 (declaim (freeze-type string-output-stream))
1364 (defun make-string-output-stream (&key (element-type 'character))
1365 #!+sb-doc
1366 "Return an output stream which will accumulate all output given it for the
1367 benefit of the function GET-OUTPUT-STREAM-STRING."
1368 (declare (explicit-check))
1369 (if (csubtypep (specifier-type element-type) (specifier-type 'character))
1370 (%make-string-output-stream element-type)
1371 (error "~S is not a subtype of CHARACTER" element-type)))
1373 ;;; Pushes the current segment onto the prev-list, and either pops
1374 ;;; or allocates a new one.
1375 (defun string-output-stream-new-buffer (stream size)
1376 (declare (index size))
1377 (/noshow0 "/string-output-stream-new-buffer")
1378 (push (string-output-stream-buffer stream)
1379 (string-output-stream-prev stream))
1380 (setf (string-output-stream-buffer stream)
1381 (or (pop (string-output-stream-next stream))
1382 ;; FIXME: This would be the correct place to detect that
1383 ;; more than FIXNUM characters are being written to the
1384 ;; stream, and do something about it.
1385 (make-string size))))
1387 ;;; Moves to the end of the next segment or the current one if there are
1388 ;;; no more segments. Returns true as long as there are next segments.
1389 (defun string-output-stream-next-buffer (stream)
1390 (/noshow0 "/string-output-stream-next-buffer")
1391 (let* ((old (string-output-stream-buffer stream))
1392 (new (pop (string-output-stream-next stream)))
1393 (old-size (length old))
1394 (skipped (- old-size (string-output-stream-pointer stream))))
1395 (cond (new
1396 (let ((new-size (length new)))
1397 (push old (string-output-stream-prev stream))
1398 (setf (string-output-stream-buffer stream) new
1399 (string-output-stream-pointer stream) new-size)
1400 (incf (string-output-stream-index stream) (+ skipped new-size)))
1403 (setf (string-output-stream-pointer stream) old-size)
1404 (incf (string-output-stream-index stream) skipped)
1405 nil))))
1407 ;;; Moves to the start of the previous segment or the current one if there
1408 ;;; are no more segments. Returns true as long as there are prev segments.
1409 (defun string-output-stream-prev-buffer (stream)
1410 (/noshow0 "/string-output-stream-prev-buffer")
1411 (let ((old (string-output-stream-buffer stream))
1412 (new (pop (string-output-stream-prev stream)))
1413 (skipped (string-output-stream-pointer stream)))
1414 (cond (new
1415 (push old (string-output-stream-next stream))
1416 (setf (string-output-stream-buffer stream) new
1417 (string-output-stream-pointer stream) 0)
1418 (decf (string-output-stream-index stream) (+ skipped (length new)))
1421 (setf (string-output-stream-pointer stream) 0)
1422 (decf (string-output-stream-index stream) skipped)
1423 nil))))
1425 (defun string-ouch (stream character)
1426 (/noshow0 "/string-ouch")
1427 (let ((pointer (string-output-stream-pointer stream))
1428 (buffer (string-output-stream-buffer stream))
1429 (index (string-output-stream-index stream)))
1430 (cond ((= pointer (length buffer))
1431 (setf buffer (string-output-stream-new-buffer stream index)
1432 (aref buffer 0) character
1433 (string-output-stream-pointer stream) 1))
1435 (setf (aref buffer pointer) character
1436 (string-output-stream-pointer stream) (1+ pointer))))
1437 (setf (string-output-stream-index stream) (1+ index))))
1439 (defun string-sout (stream string start end)
1440 (declare (type simple-string string)
1441 (type index start end))
1442 (let* ((full-length (- end start))
1443 (length full-length)
1444 (buffer (string-output-stream-buffer stream))
1445 (pointer (string-output-stream-pointer stream))
1446 (space (- (length buffer) pointer))
1447 (here (min space length))
1448 (stop (+ start here))
1449 (overflow (- length space)))
1450 (declare (index length space here stop full-length)
1451 (fixnum overflow)
1452 (type (simple-array character (*)) buffer))
1453 (tagbody
1454 :more
1455 (when (plusp here)
1456 (etypecase string
1457 ((simple-array character (*))
1458 (replace buffer string :start1 pointer :start2 start :end2 stop))
1459 (simple-base-string
1460 (replace buffer string :start1 pointer :start2 start :end2 stop))
1461 ((simple-array nil (*))
1462 (replace buffer string :start1 pointer :start2 start :end2 stop)))
1463 (setf (string-output-stream-pointer stream) (+ here pointer)))
1464 (when (plusp overflow)
1465 (setf start stop
1466 length (- end start)
1467 buffer (string-output-stream-new-buffer
1468 stream (max overflow (string-output-stream-index stream)))
1469 pointer 0
1470 space (length buffer)
1471 here (min space length)
1472 stop (+ start here)
1473 ;; there may be more overflow if we used a buffer
1474 ;; already allocated to the stream
1475 overflow (- length space))
1476 (go :more)))
1477 (incf (string-output-stream-index stream) full-length)))
1479 ;;; Factored out of the -misc method due to size.
1480 (defun set-string-output-stream-file-position (stream pos)
1481 (let* ((index (string-output-stream-index stream))
1482 (end (max index (string-output-stream-index-cache stream))))
1483 (declare (index index end))
1484 (setf (string-output-stream-index-cache stream) end)
1485 (cond ((eq :start pos)
1486 (loop while (string-output-stream-prev-buffer stream)))
1487 ((eq :end pos)
1488 (loop while (string-output-stream-next-buffer stream))
1489 (let ((over (- (string-output-stream-index stream) end)))
1490 (decf (string-output-stream-pointer stream) over))
1491 (setf (string-output-stream-index stream) end))
1492 ((< pos index)
1493 (loop while (< pos index)
1494 do (string-output-stream-prev-buffer stream)
1495 (setf index (string-output-stream-index stream)))
1496 (let ((step (- pos index)))
1497 (incf (string-output-stream-pointer stream) step)
1498 (setf (string-output-stream-index stream) pos)))
1499 ((> pos index)
1500 ;; We allow moving beyond the end of stream, implicitly
1501 ;; extending the output stream.
1502 (let ((next (string-output-stream-next-buffer stream)))
1503 ;; Update after -next-buffer, INDEX is kept pointing at
1504 ;; the end of the current buffer.
1505 (setf index (string-output-stream-index stream))
1506 (loop while (and next (> pos index))
1507 do (setf next (string-output-stream-next-buffer stream)
1508 index (string-output-stream-index stream))))
1509 ;; Allocate new buffer if needed, or step back to
1510 ;; the desired index and set pointer and index
1511 ;; correctly.
1512 (let ((diff (- pos index)))
1513 (if (plusp diff)
1514 (let* ((new (string-output-stream-new-buffer stream diff))
1515 (size (length new)))
1516 (aver (= pos (+ index size)))
1517 (setf (string-output-stream-pointer stream) size
1518 (string-output-stream-index stream) pos))
1519 (let ((size (length (string-output-stream-buffer stream))))
1520 (setf (string-output-stream-pointer stream) (+ size diff)
1521 (string-output-stream-index stream) pos))))))))
1523 (defun string-out-misc (stream operation &optional arg1 arg2)
1524 (declare (ignore arg2))
1525 (declare (optimize speed))
1526 (case operation
1527 (:charpos
1528 ;; Keeping this first is a silly micro-optimization: FRESH-LINE
1529 ;; makes this the most common one.
1530 (/noshow0 "/string-out-misc charpos")
1531 (prog ((pointer (string-output-stream-pointer stream))
1532 (buffer (string-output-stream-buffer stream))
1533 (prev (string-output-stream-prev stream))
1534 (base 0))
1535 (declare (type (or null (simple-array character (*))) buffer))
1536 :next
1537 (let ((pos (when buffer
1538 (position #\newline buffer :from-end t :end pointer))))
1539 (when (or pos (not buffer))
1540 ;; If newline is at index I, and pointer at index I+N, charpos
1541 ;; is N-1. If there is no newline, and pointer is at index N,
1542 ;; charpos is N.
1543 (return (+ base (if pos (- pointer pos 1) pointer))))
1544 (setf base (+ base pointer)
1545 buffer (pop prev)
1546 pointer (length buffer))
1547 (/noshow0 "/string-out-misc charpos next")
1548 (go :next))))
1549 (:file-position
1550 (/noshow0 "/string-out-misc file-position")
1551 (when arg1
1552 (set-string-output-stream-file-position stream arg1))
1553 (string-output-stream-index stream))
1554 (:close
1555 (/noshow0 "/string-out-misc close")
1556 (set-closed-flame stream))
1557 (:element-type (string-output-stream-element-type stream))))
1559 ;;; Return a string of all the characters sent to a stream made by
1560 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1561 (defun get-output-stream-string (stream)
1562 (declare (type string-output-stream stream))
1563 (let* ((length (max (string-output-stream-index stream)
1564 (string-output-stream-index-cache stream)))
1565 (element-type (string-output-stream-element-type stream))
1566 (prev (nreverse (string-output-stream-prev stream)))
1567 (this (string-output-stream-buffer stream))
1568 (next (string-output-stream-next stream))
1569 (result
1570 (case element-type
1571 ;; overwhelmingly common case: can be inlined
1573 ;; FIXME: If we were willing to use %SHRINK-VECTOR here,
1574 ;; and allocate new strings the size of 2 * index in
1575 ;; STRING-SOUT, we would not need to allocate one here in
1576 ;; the common case, but could just use the last one
1577 ;; allocated, and chop it down to size..
1579 ((character) (make-string length))
1580 ;; slightly less common cases: inline it anyway
1581 ((base-char standard-char)
1582 (make-string length :element-type 'base-char))
1584 (make-string length :element-type element-type)))))
1586 (setf (string-output-stream-index stream) 0
1587 (string-output-stream-index-cache stream) 0
1588 (string-output-stream-pointer stream) 0
1589 ;; throw them away for simplicity's sake: this way the rest of the
1590 ;; implementation can assume that the greater of INDEX and INDEX-CACHE
1591 ;; is always within the last buffer.
1592 (string-output-stream-prev stream) nil
1593 (string-output-stream-next stream) nil)
1595 (flet ((replace-all (fun)
1596 (let ((start 0))
1597 (declare (index start))
1598 (dolist (buffer prev)
1599 (funcall fun buffer start)
1600 (incf start (length buffer)))
1601 (funcall fun this start)
1602 (incf start (length this))
1603 (dolist (buffer next)
1604 (funcall fun buffer start)
1605 (incf start (length buffer)))
1606 ;; Hack: erase the pointers to strings, to make it less
1607 ;; likely that the conservative GC will accidentally
1608 ;; retain the buffers.
1609 (fill prev nil)
1610 (fill next nil))))
1611 (macrolet ((frob (type)
1612 `(replace-all (lambda (buffer from)
1613 (declare (type ,type result)
1614 (type (simple-array character (*))
1615 buffer))
1616 (replace result buffer :start1 from)))))
1617 (etypecase result
1618 ((simple-array character (*))
1619 (frob (simple-array character (*))))
1620 (simple-base-string
1621 (frob simple-base-string))
1622 ((simple-array nil (*))
1623 (frob (simple-array nil (*)))))))
1625 result))
1627 ;;;; fill-pointer streams
1629 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1630 ;;; the CLM, but they are required for the implementation of
1631 ;;; WITH-OUTPUT-TO-STRING.
1633 ;;; FIXME: need to support (VECTOR NIL), ideally without destroying all hope
1634 ;;; of efficiency.
1635 (declaim (inline vector-with-fill-pointer-p))
1636 (defun vector-with-fill-pointer-p (x)
1637 (and (vectorp x)
1638 (array-has-fill-pointer-p x)))
1640 (deftype string-with-fill-pointer ()
1641 `(and (or (vector character) (vector base-char))
1642 (satisfies vector-with-fill-pointer-p)))
1644 (defstruct (fill-pointer-output-stream
1645 (:include ansi-stream
1646 (out #'fill-pointer-ouch)
1647 (sout #'fill-pointer-sout)
1648 (misc #'fill-pointer-misc))
1649 (:constructor make-fill-pointer-output-stream (string))
1650 (:copier nil)
1651 (:predicate nil))
1652 ;; a string with a fill pointer where we stuff the stuff we write
1653 (string (missing-arg) :type string-with-fill-pointer :read-only t))
1655 (declaim (freeze-type fill-pointer-output-stream))
1657 (defun fill-pointer-ouch (stream character)
1658 (let* ((buffer (fill-pointer-output-stream-string stream))
1659 (current (fill-pointer buffer))
1660 (current+1 (1+ current)))
1661 (declare (fixnum current))
1662 (with-array-data ((workspace buffer) (start) (end))
1663 (string-dispatch
1664 ((simple-array character (*))
1665 (simple-array base-char (*)))
1666 workspace
1667 (let ((offset-current (+ start current)))
1668 (declare (fixnum offset-current))
1669 (if (= offset-current end)
1670 (let* ((new-length (1+ (* current 2)))
1671 (new-workspace
1672 (ecase (array-element-type workspace)
1673 (character (make-string new-length
1674 :element-type 'character))
1675 (base-char (make-string new-length
1676 :element-type 'base-char)))))
1677 (replace new-workspace workspace :start2 start :end2 offset-current)
1678 (setf workspace new-workspace
1679 offset-current current)
1680 (set-array-header buffer workspace new-length
1681 current+1 0 new-length nil nil))
1682 (setf (fill-pointer buffer) current+1))
1683 (setf (char workspace offset-current) character))))
1684 current+1))
1686 (defun fill-pointer-sout (stream string start end)
1687 (declare (fixnum start end))
1688 (string-dispatch
1689 ((simple-array character (*))
1690 (simple-array base-char (*)))
1691 string
1692 (let* ((buffer (fill-pointer-output-stream-string stream))
1693 (current (fill-pointer buffer))
1694 (string-len (- end start))
1695 (dst-end (+ string-len current)))
1696 (declare (fixnum current dst-end string-len))
1697 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1698 (let ((offset-dst-end (+ dst-start dst-end))
1699 (offset-current (+ dst-start current)))
1700 (declare (fixnum offset-dst-end offset-current))
1701 (if (> offset-dst-end dst-length)
1702 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1703 (new-workspace
1704 (ecase (array-element-type workspace)
1705 (character (make-string new-length
1706 :element-type 'character))
1707 (base-char (make-string new-length
1708 :element-type 'base-char)))))
1709 (replace new-workspace workspace
1710 :start2 dst-start :end2 offset-current)
1711 (setf workspace new-workspace
1712 offset-current current
1713 offset-dst-end dst-end)
1714 (set-array-header buffer workspace new-length
1715 dst-end 0 new-length nil nil))
1716 (setf (fill-pointer buffer) dst-end))
1717 (replace workspace string
1718 :start1 offset-current :start2 start :end2 end)))
1719 dst-end)))
1721 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1722 (declare (ignore arg2))
1723 (case operation
1724 (:file-position
1725 (let ((buffer (fill-pointer-output-stream-string stream)))
1726 (if arg1
1727 (setf (fill-pointer buffer)
1728 (case arg1
1729 (:start 0)
1730 ;; Fill-pointer is always at fill-pointer we will
1731 ;; make :END move to the end of the actual string.
1732 (:end (array-total-size buffer))
1733 ;; We allow moving beyond the end of string if the
1734 ;; string is adjustable.
1735 (t (when (>= arg1 (array-total-size buffer))
1736 (if (adjustable-array-p buffer)
1737 (adjust-array buffer arg1)
1738 (error "Cannot move FILE-POSITION beyond the end ~
1739 of WITH-OUTPUT-TO-STRING stream ~
1740 constructed with non-adjustable string.")))
1741 arg1)))
1742 (fill-pointer buffer))))
1743 (:charpos
1744 (let* ((buffer (fill-pointer-output-stream-string stream))
1745 (current (fill-pointer buffer)))
1746 (with-array-data ((string buffer) (start) (end current))
1747 (declare (simple-string string) (ignore start))
1748 (let ((found (position #\newline string :test #'char=
1749 :end end :from-end t)))
1750 (if found
1751 (- end (the fixnum found))
1752 current)))))
1753 (:element-type
1754 (array-element-type
1755 (fill-pointer-output-stream-string stream)))))
1757 ;;;; case frobbing streams, used by FORMAT ~(...~)
1759 (defstruct (case-frob-stream
1760 (:include ansi-stream
1761 (misc #'case-frob-misc))
1762 (:constructor %make-case-frob-stream (target out sout))
1763 (:copier nil))
1764 (target (missing-arg) :type stream :read-only t))
1766 (declaim (freeze-type case-frob-stream))
1768 (defun make-case-frob-stream (target kind)
1769 #!+sb-doc
1770 "Return a stream that sends all output to the stream TARGET, but modifies
1771 the case of letters, depending on KIND, which should be one of:
1772 :UPCASE - convert to upper case.
1773 :DOWNCASE - convert to lower case.
1774 :CAPITALIZE - convert the first letter of words to upper case and the
1775 rest of the word to lower case.
1776 :CAPITALIZE-FIRST - convert the first letter of the first word to upper
1777 case and everything else to lower case."
1778 (declare (type stream target)
1779 (type (member :upcase :downcase :capitalize :capitalize-first)
1780 kind)
1781 (values stream))
1782 (if (case-frob-stream-p target)
1783 ;; If we are going to be writing to a stream that already does
1784 ;; case frobbing, why bother frobbing the case just so it can
1785 ;; frob it again?
1786 target
1787 (multiple-value-bind (out sout)
1788 (ecase kind
1789 (:upcase
1790 (values #'case-frob-upcase-out
1791 #'case-frob-upcase-sout))
1792 (:downcase
1793 (values #'case-frob-downcase-out
1794 #'case-frob-downcase-sout))
1795 (:capitalize
1796 (values #'case-frob-capitalize-out
1797 #'case-frob-capitalize-sout))
1798 (:capitalize-first
1799 (values #'case-frob-capitalize-first-out
1800 #'case-frob-capitalize-first-sout)))
1801 (%make-case-frob-stream target out sout))))
1803 (defun case-frob-misc (stream op &optional arg1 arg2)
1804 (declare (type case-frob-stream stream))
1805 (case op
1806 (:close
1807 (set-closed-flame stream))
1809 (let ((target (case-frob-stream-target stream)))
1810 (if (ansi-stream-p target)
1811 (funcall (ansi-stream-misc target) target op arg1 arg2)
1812 (stream-misc-dispatch target op arg1 arg2))))))
1814 (defun case-frob-upcase-out (stream char)
1815 (declare (type case-frob-stream stream)
1816 (type character char))
1817 (let ((target (case-frob-stream-target stream))
1818 (char (char-upcase char)))
1819 (if (ansi-stream-p target)
1820 (funcall (ansi-stream-out target) target char)
1821 (stream-write-char target char))))
1823 (defun case-frob-upcase-sout (stream str start end)
1824 (declare (type case-frob-stream stream)
1825 (type simple-string str)
1826 (type index start)
1827 (type (or index null) end))
1828 (let* ((target (case-frob-stream-target stream))
1829 (len (length str))
1830 (end (or end len))
1831 (string (if (and (zerop start) (= len end))
1832 (string-upcase str)
1833 (nstring-upcase (subseq str start end))))
1834 (string-len (- end start)))
1835 (if (ansi-stream-p target)
1836 (funcall (ansi-stream-sout target) target string 0 string-len)
1837 (stream-write-string target string 0 string-len))))
1839 (defun case-frob-downcase-out (stream char)
1840 (declare (type case-frob-stream stream)
1841 (type character char))
1842 (let ((target (case-frob-stream-target stream))
1843 (char (char-downcase char)))
1844 (if (ansi-stream-p target)
1845 (funcall (ansi-stream-out target) target char)
1846 (stream-write-char target char))))
1848 (defun case-frob-downcase-sout (stream str start end)
1849 (declare (type case-frob-stream stream)
1850 (type simple-string str)
1851 (type index start)
1852 (type (or index null) end))
1853 (let* ((target (case-frob-stream-target stream))
1854 (len (length str))
1855 (end (or end len))
1856 (string (if (and (zerop start) (= len end))
1857 (string-downcase str)
1858 (nstring-downcase (subseq str start end))))
1859 (string-len (- end start)))
1860 (if (ansi-stream-p target)
1861 (funcall (ansi-stream-sout target) target string 0 string-len)
1862 (stream-write-string target string 0 string-len))))
1864 (defun case-frob-capitalize-out (stream char)
1865 (declare (type case-frob-stream stream)
1866 (type character char))
1867 (let ((target (case-frob-stream-target stream)))
1868 (cond ((alphanumericp char)
1869 (let ((char (char-upcase char)))
1870 (if (ansi-stream-p target)
1871 (funcall (ansi-stream-out target) target char)
1872 (stream-write-char target char)))
1873 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1874 (setf (case-frob-stream-sout stream)
1875 #'case-frob-capitalize-aux-sout))
1877 (if (ansi-stream-p target)
1878 (funcall (ansi-stream-out target) target char)
1879 (stream-write-char target char))))))
1881 (defun case-frob-capitalize-sout (stream str start end)
1882 (declare (type case-frob-stream stream)
1883 (type simple-string str)
1884 (type index start)
1885 (type (or index null) end))
1886 (let* ((target (case-frob-stream-target stream))
1887 (str (subseq str start end))
1888 (len (length str))
1889 (inside-word nil))
1890 (dotimes (i len)
1891 (let ((char (schar str i)))
1892 (cond ((not (alphanumericp char))
1893 (setf inside-word nil))
1894 (inside-word
1895 (setf (schar str i) (char-downcase char)))
1897 (setf inside-word t)
1898 (setf (schar str i) (char-upcase char))))))
1899 (when inside-word
1900 (setf (case-frob-stream-out stream)
1901 #'case-frob-capitalize-aux-out)
1902 (setf (case-frob-stream-sout stream)
1903 #'case-frob-capitalize-aux-sout))
1904 (if (ansi-stream-p target)
1905 (funcall (ansi-stream-sout target) target str 0 len)
1906 (stream-write-string target str 0 len))))
1908 (defun case-frob-capitalize-aux-out (stream char)
1909 (declare (type case-frob-stream stream)
1910 (type character char))
1911 (let ((target (case-frob-stream-target stream)))
1912 (cond ((alphanumericp char)
1913 (let ((char (char-downcase char)))
1914 (if (ansi-stream-p target)
1915 (funcall (ansi-stream-out target) target char)
1916 (stream-write-char target char))))
1918 (if (ansi-stream-p target)
1919 (funcall (ansi-stream-out target) target char)
1920 (stream-write-char target char))
1921 (setf (case-frob-stream-out stream)
1922 #'case-frob-capitalize-out)
1923 (setf (case-frob-stream-sout stream)
1924 #'case-frob-capitalize-sout)))))
1926 (defun case-frob-capitalize-aux-sout (stream str start end)
1927 (declare (type case-frob-stream stream)
1928 (type simple-string str)
1929 (type index start)
1930 (type (or index null) end))
1931 (let* ((target (case-frob-stream-target stream))
1932 (str (subseq str start end))
1933 (len (length str))
1934 (inside-word t))
1935 (dotimes (i len)
1936 (let ((char (schar str i)))
1937 (cond ((not (alphanumericp char))
1938 (setf inside-word nil))
1939 (inside-word
1940 (setf (schar str i) (char-downcase char)))
1942 (setf inside-word t)
1943 (setf (schar str i) (char-upcase char))))))
1944 (unless inside-word
1945 (setf (case-frob-stream-out stream)
1946 #'case-frob-capitalize-out)
1947 (setf (case-frob-stream-sout stream)
1948 #'case-frob-capitalize-sout))
1949 (if (ansi-stream-p target)
1950 (funcall (ansi-stream-sout target) target str 0 len)
1951 (stream-write-string target str 0 len))))
1953 (defun case-frob-capitalize-first-out (stream char)
1954 (declare (type case-frob-stream stream)
1955 (type character char))
1956 (let ((target (case-frob-stream-target stream)))
1957 (cond ((alphanumericp char)
1958 (let ((char (char-upcase char)))
1959 (if (ansi-stream-p target)
1960 (funcall (ansi-stream-out target) target char)
1961 (stream-write-char target char)))
1962 (setf (case-frob-stream-out stream)
1963 #'case-frob-downcase-out)
1964 (setf (case-frob-stream-sout stream)
1965 #'case-frob-downcase-sout))
1967 (if (ansi-stream-p target)
1968 (funcall (ansi-stream-out target) target char)
1969 (stream-write-char target char))))))
1971 (defun case-frob-capitalize-first-sout (stream str start end)
1972 (declare (type case-frob-stream stream)
1973 (type simple-string str)
1974 (type index start)
1975 (type (or index null) end))
1976 (let* ((target (case-frob-stream-target stream))
1977 (str (subseq str start end))
1978 (len (length str)))
1979 (dotimes (i len)
1980 (let ((char (schar str i)))
1981 (when (alphanumericp char)
1982 (setf (schar str i) (char-upcase char))
1983 (do ((i (1+ i) (1+ i)))
1984 ((= i len))
1985 (setf (schar str i) (char-downcase (schar str i))))
1986 (setf (case-frob-stream-out stream)
1987 #'case-frob-downcase-out)
1988 (setf (case-frob-stream-sout stream)
1989 #'case-frob-downcase-sout)
1990 (return))))
1991 (if (ansi-stream-p target)
1992 (funcall (ansi-stream-sout target) target str 0 len)
1993 (stream-write-string target str 0 len))))
1995 ;;;; Shared {READ,WRITE}-SEQUENCE support functions
1997 (declaim (inline stream-element-mode
1998 stream-compute-io-function
1999 compatible-vector-and-stream-element-types-p))
2001 (defun stream-element-mode (stream)
2002 (declare (type stream stream))
2003 (cond
2004 ((fd-stream-p stream)
2005 (fd-stream-element-mode stream))
2006 ((and (ansi-stream-p stream)
2007 (funcall (ansi-stream-misc stream) stream :element-mode)))
2009 (stream-element-type-stream-element-mode
2010 (stream-element-type stream)))))
2012 (defun stream-compute-io-function (stream
2013 stream-element-mode sequence-element-type
2014 character-io binary-io bivalent-io)
2015 (ecase stream-element-mode
2016 (character
2017 character-io)
2018 ((unsigned-byte signed-byte)
2019 binary-io)
2020 (:bivalent
2021 (cond
2022 ((member sequence-element-type '(nil t))
2023 bivalent-io)
2024 ;; Pick off common subtypes.
2025 ((eq sequence-element-type 'character)
2026 character-io)
2027 ((or (equal sequence-element-type '(unsigned-byte 8))
2028 (equal sequence-element-type '(signed-byte 8)))
2029 binary-io)
2030 ;; Proper subtype tests.
2031 ((subtypep sequence-element-type 'character)
2032 character-io)
2033 ((subtypep sequence-element-type 'integer)
2034 binary-io)
2036 (error "~@<Cannot select IO functions to use for bivalent ~
2037 stream ~S and a sequence with element-type ~S.~@:>"
2038 stream sequence-element-type))))))
2040 (defun compatible-vector-and-stream-element-types-p (vector stream)
2041 (declare (type vector vector)
2042 (type ansi-stream stream))
2043 (or (and (typep vector '(simple-array (unsigned-byte 8) (*)))
2044 (eq (stream-element-mode stream) 'unsigned-byte))
2045 (and (typep vector '(simple-array (signed-byte 8) (*)))
2046 (eq (stream-element-mode stream) 'signed-byte))))
2048 ;;;; READ-SEQUENCE
2050 (defun read-sequence (seq stream &key (start 0) end)
2051 #!+sb-doc
2052 "Destructively modify SEQ by reading elements from STREAM.
2053 That part of SEQ bounded by START and END is destructively modified by
2054 copying successive elements into it from STREAM. If the end of file
2055 for STREAM is reached before copying all elements of the subsequence,
2056 then the extra elements near the end of sequence are not updated, and
2057 the index of the next element is returned."
2058 (declare (type sequence seq)
2059 (type stream stream)
2060 (type index start)
2061 (type sequence-end end)
2062 (values index))
2063 (if (ansi-stream-p stream)
2064 (ansi-stream-read-sequence seq stream start end)
2065 ;; must be Gray streams FUNDAMENTAL-STREAM
2066 (stream-read-sequence stream seq start end)))
2068 (declaim (inline read-sequence/read-function))
2069 (defun read-sequence/read-function (seq stream start %end
2070 stream-element-mode
2071 character-read-function binary-read-function)
2072 (declare (type sequence seq)
2073 (type stream stream)
2074 (type index start)
2075 (type sequence-end %end)
2076 (type stream-element-mode stream-element-mode)
2077 (type function character-read-function binary-read-function)
2078 (values index &optional))
2079 (let ((end (or %end (length seq))))
2080 (declare (type index end))
2081 (labels ((compute-read-function (sequence-element-type)
2082 (stream-compute-io-function
2083 stream
2084 stream-element-mode sequence-element-type
2085 character-read-function binary-read-function
2086 character-read-function))
2087 (read-list (read-function)
2088 (do ((rem (nthcdr start seq) (rest rem))
2089 (i start (1+ i)))
2090 ((or (endp rem) (>= i end)) i)
2091 (declare (type list rem)
2092 (type index i))
2093 (let ((el (funcall read-function stream nil :eof nil)))
2094 (when (eq el :eof)
2095 (return i))
2096 (setf (first rem) el))))
2097 (read-vector/fast (data offset-start)
2098 (let* ((numbytes (- end start))
2099 (bytes-read (read-n-bytes
2100 stream data offset-start numbytes nil)))
2101 (if (< bytes-read numbytes)
2102 (+ start bytes-read)
2103 end)))
2104 (read-vector (read-function data offset-start offset-end)
2105 (do ((i offset-start (1+ i)))
2106 ((>= i offset-end) end)
2107 (declare (type index i))
2108 (let ((el (funcall read-function stream nil :eof nil)))
2109 (when (eq el :eof)
2110 (return (+ start (- i offset-start))))
2111 (setf (aref data i) el))))
2112 (read-generic-sequence (read-function)
2113 (declare (ignore read-function))
2114 (error "~@<~A does not yet support generic sequences.~@:>"
2115 'read-sequence)))
2116 (declare (dynamic-extent #'compute-read-function
2117 #'read-list #'read-vector/fast #'read-vector
2118 #'read-generic-sequence))
2119 (cond
2120 ((typep seq 'list)
2121 (read-list (compute-read-function nil)))
2122 ((and (ansi-stream-p stream)
2123 (ansi-stream-cin-buffer stream)
2124 (typep seq 'simple-string))
2125 (ansi-stream-read-string-from-frc-buffer seq stream start %end))
2126 ((typep seq 'vector)
2127 (with-array-data ((data seq) (offset-start start) (offset-end end)
2128 :check-fill-pointer t)
2129 (if (and (ansi-stream-p stream)
2130 (compatible-vector-and-stream-element-types-p data stream))
2131 (read-vector/fast data offset-start)
2132 (read-vector (compute-read-function (array-element-type data))
2133 data offset-start offset-end))))
2135 (read-generic-sequence (compute-read-function nil)))))))
2136 (declaim (notinline read-sequence/read-function))
2138 (defun ansi-stream-read-sequence (seq stream start %end)
2139 (declare (type sequence seq)
2140 (type ansi-stream stream)
2141 (type index start)
2142 (type sequence-end %end)
2143 (values index &optional))
2144 (locally (declare (inline read-sequence/read-function))
2145 (read-sequence/read-function
2146 seq stream start %end (stream-element-mode stream)
2147 #'ansi-stream-read-char #'ansi-stream-read-byte)))
2149 (defun ansi-stream-read-string-from-frc-buffer (seq stream start %end)
2150 (declare (type simple-string seq)
2151 (type ansi-stream stream)
2152 (type index start)
2153 (type (or null index) %end))
2154 (let ((needed (- (or %end (length seq))
2155 start))
2156 (read 0))
2157 (prepare-for-fast-read-char stream
2158 (declare (ignore %frc-method%))
2159 (unless %frc-buffer%
2160 (return-from ansi-stream-read-string-from-frc-buffer nil))
2161 (labels ((refill-buffer ()
2162 (prog1 (fast-read-char-refill stream nil)
2163 (setf %frc-index% (ansi-stream-in-index %frc-stream%))))
2164 (add-chunk ()
2165 (let* ((end (length %frc-buffer%))
2166 (len (min (- end %frc-index%)
2167 (- needed read))))
2168 (declare (type index end len read needed))
2169 (string-dispatch (simple-base-string
2170 (simple-array character (*)))
2172 (replace seq %frc-buffer%
2173 :start1 (+ start read)
2174 :end1 (+ start read len)
2175 :start2 %frc-index%
2176 :end2 (+ %frc-index% len)))
2177 (incf read len)
2178 (incf %frc-index% len)
2179 (when (or (eql needed read) (not (refill-buffer)))
2180 (done-with-fast-read-char)
2181 (return-from ansi-stream-read-string-from-frc-buffer
2182 (+ start read))))))
2183 (declare (inline refill-buffer))
2184 (when (and (= %frc-index% +ansi-stream-in-buffer-length+)
2185 (not (refill-buffer)))
2186 ;; EOF had been reached before we read anything
2187 ;; at all. But READ-SEQUENCE never signals an EOF error.
2188 (done-with-fast-read-char)
2189 (return-from ansi-stream-read-string-from-frc-buffer start))
2190 (loop (add-chunk))))))
2193 ;;;; WRITE-SEQUENCE
2195 (defun write-sequence (seq stream &key (start 0) (end nil))
2196 #!+sb-doc
2197 "Write the elements of SEQ bounded by START and END to STREAM."
2198 (declare (type sequence seq)
2199 (type stream stream)
2200 (type index start)
2201 (type sequence-end end)
2202 (values sequence))
2203 (if (ansi-stream-p stream)
2204 (ansi-stream-write-sequence seq stream start end)
2205 ;; must be Gray-streams FUNDAMENTAL-STREAM
2206 (stream-write-sequence stream seq start end)))
2208 ;;; This macro allows sharing code between
2209 ;;; WRITE-SEQUENCE/WRITE-FUNCTION and SB-GRAY:STREAM-WRITE-STRING.
2210 (defmacro write-sequence/vector ((seq type) stream start end write-function)
2211 (once-only ((seq seq) (stream stream) (start start) (end end)
2212 (write-function write-function))
2213 `(locally
2214 (declare (type ,type ,seq)
2215 (type index ,start ,end)
2216 (type function ,write-function))
2217 (do ((i ,start (1+ i)))
2218 ((>= i ,end))
2219 (declare (type index i))
2220 (funcall ,write-function ,stream (aref ,seq i))))))
2222 (declaim (inline write-sequence/write-function))
2223 (defun write-sequence/write-function (seq stream start %end
2224 stream-element-mode
2225 character-write-function
2226 binary-write-function)
2227 (declare (type sequence seq)
2228 (type stream stream)
2229 (type index start)
2230 (type sequence-end %end)
2231 (type stream-element-mode stream-element-mode)
2232 (type function character-write-function binary-write-function))
2233 (let ((end (or %end (length seq))))
2234 (declare (type index end))
2235 (labels ((compute-write-function (sequence-element-type)
2236 (stream-compute-io-function
2237 stream
2238 stream-element-mode sequence-element-type
2239 character-write-function binary-write-function
2240 #'write-element/bivalent))
2241 (write-element/bivalent (stream object)
2242 (if (characterp object)
2243 (funcall character-write-function stream object)
2244 (funcall binary-write-function stream object)))
2245 (write-list (write-function)
2246 (do ((rem (nthcdr start seq) (rest rem))
2247 (i start (1+ i)))
2248 ((or (endp rem) (>= i end)))
2249 (declare (type list rem)
2250 (type index i))
2251 (funcall write-function stream (first rem))))
2252 (write-vector (data start end write-function)
2253 (write-sequence/vector
2254 (data (simple-array * (*))) stream start end write-function))
2255 (write-generic-sequence (write-function)
2256 (declare (ignore write-function))
2257 (error "~@<~A does not yet support generic sequences.~@:>"
2258 'write-sequence)))
2259 (declare (dynamic-extent #'compute-write-function
2260 #'write-element/bivalent #'write-list
2261 #'write-vector #'write-generic-sequence))
2262 (etypecase seq
2263 (list
2264 (write-list (compute-write-function nil)))
2265 (string
2266 (if (ansi-stream-p stream)
2267 (ansi-stream-write-string seq stream start end)
2268 (stream-write-string stream seq start end)))
2269 (vector
2270 (with-array-data ((data seq) (offset-start start) (offset-end end)
2271 :check-fill-pointer t)
2272 (if (and (fd-stream-p stream)
2273 (compatible-vector-and-stream-element-types-p data stream))
2274 (buffer-output stream data offset-start offset-end)
2275 (write-vector data offset-start offset-end
2276 (compute-write-function
2277 (array-element-type seq))))))
2278 (sequence
2279 (write-generic-sequence (compute-write-function nil)))))))
2280 (declaim (notinline write-sequence/write-function))
2282 (defun ansi-stream-write-sequence (seq stream start %end)
2283 (declare (type sequence seq)
2284 (type ansi-stream stream)
2285 (type index start)
2286 (type sequence-end %end)
2287 (values sequence))
2288 (locally (declare (inline write-sequence/write-function))
2289 (write-sequence/write-function
2290 seq stream start %end (stream-element-mode stream)
2291 (ansi-stream-out stream) (ansi-stream-bout stream)))
2292 seq)
2294 ;;; like FILE-POSITION, only using :FILE-LENGTH
2295 (defun file-length (stream)
2296 ;; FIXME: the FIXME following this one seems wrong on 2 counts:
2297 ;; 1. since when does cross-compiler hangup occur on undefined types?
2298 ;; 2. why is that the correct set of types to check for?
2299 ;; FIXME: The following declaration uses yet undefined types, which
2300 ;; cause cross-compiler hangup.
2302 ;; (declare (type (or file-stream synonym-stream) stream))
2304 ;; The description for FILE-LENGTH says that an error must be raised
2305 ;; for streams not associated with files (which broadcast streams
2306 ;; aren't according to the glossary). However, the behaviour of
2307 ;; FILE-LENGTH for broadcast streams is explicitly described in the
2308 ;; BROADCAST-STREAM entry.
2309 (unless (typep stream 'broadcast-stream)
2310 (stream-must-be-associated-with-file stream))
2311 (funcall (ansi-stream-misc stream) stream :file-length))
2313 ;; Placing this definition (formerly in "toplevel") after the important
2314 ;; stream types are known produces smaller+faster code than it did before.
2315 (defun stream-output-stream (stream)
2316 (typecase stream
2317 (fd-stream
2318 stream)
2319 (synonym-stream
2320 (stream-output-stream
2321 (symbol-value (synonym-stream-symbol stream))))
2322 (two-way-stream
2323 (stream-output-stream
2324 (two-way-stream-output-stream stream)))
2326 stream)))
2328 ;;;; etc.