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