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