1.0.13.48: SET-CLOSED-FLAME set ANSI-STREAM-IN twice (harmless but silly)
[sbcl/simd.git] / src / code / stream.lisp
blob272bde62d20cf16c6ba299ed68dddfe985a75b16
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 "~S is closed." 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 ;;;; file position and file length
138 ;;; Call the MISC method with the :FILE-POSITION operation.
139 #!-sb-fluid (declaim (inline ansi-stream-file-position))
140 (defun ansi-stream-file-position (stream position)
141 (declare (type stream stream))
142 (declare (type (or index (alien sb!unix:off-t) (member nil :start :end))
143 position))
144 ;; FIXME: It woud be good to comment on the stuff that is done here...
145 ;; FIXME: This doesn't look interrupt safe.
146 (cond
147 (position
148 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
149 (funcall (ansi-stream-misc stream) stream :file-position position))
151 (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
152 (when res
153 #!-sb-unicode
154 (- res
155 (- +ansi-stream-in-buffer-length+
156 (ansi-stream-in-index stream)))
157 #!+sb-unicode
158 (let* ((external-format (stream-external-format stream))
159 (ef-entry (find-external-format external-format))
160 (variable-width-p (variable-width-external-format-p ef-entry))
161 (char-len (bytes-for-char-fun ef-entry)))
162 (- res
163 (if variable-width-p
164 (loop with buffer = (ansi-stream-cin-buffer stream)
165 with start = (ansi-stream-in-index stream)
166 for i from start below +ansi-stream-in-buffer-length+
167 sum (funcall char-len (aref buffer i)))
168 (* (funcall char-len #\x) ; arbitrary argument
169 (- +ansi-stream-in-buffer-length+
170 (ansi-stream-in-index stream)))))))))))
172 (defun file-position (stream &optional position)
173 (if (ansi-stream-p stream)
174 (ansi-stream-file-position stream position)
175 (stream-file-position stream position)))
177 ;;; This is a literal translation of the ANSI glossary entry "stream
178 ;;; associated with a file".
180 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
181 ;;; file", and in particular stdin, stdout, and stderr are files, we
182 ;;; end up with this test being satisfied for weird things like
183 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
184 ;;; ANSI spec really had in mind, especially since this is used as a
185 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
186 ;;; probably thinking of something like what Unix calls block devices)
187 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
188 (defun stream-associated-with-file-p (x)
189 "Test for the ANSI concept \"stream associated with a file\"."
190 (or (typep x 'file-stream)
191 (and (synonym-stream-p x)
192 (stream-associated-with-file-p (symbol-value
193 (synonym-stream-symbol x))))))
195 (defun stream-must-be-associated-with-file (stream)
196 (declare (type stream stream))
197 (unless (stream-associated-with-file-p stream)
198 (error 'simple-type-error
199 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
200 ;; this should be TYPE-ERROR. But what then can we use for
201 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
202 ;; private predicate function..) is ugly and confusing, but
203 ;; I can't see any other way. -- WHN 2001-04-14
204 :datum stream
205 :expected-type '(satisfies stream-associated-with-file-p)
206 :format-control
207 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
208 :format-arguments (list stream))))
210 ;;; like FILE-POSITION, only using :FILE-LENGTH
211 (defun file-length (stream)
212 ;; FIXME: The following declaration uses yet undefined types, which
213 ;; cause cross-compiler hangup.
215 ;; (declare (type (or file-stream synonym-stream) stream))
217 ;; The description for FILE-LENGTH says that an error must be raised
218 ;; for streams not associated with files (which broadcast streams
219 ;; aren't according to the glossary). However, the behaviour of
220 ;; FILE-LENGTH for broadcast streams is explicitly described in the
221 ;; BROADCAST-STREAM entry.
222 (unless (typep stream 'broadcast-stream)
223 (stream-must-be-associated-with-file stream))
224 (funcall (ansi-stream-misc stream) stream :file-length))
226 (defun file-string-length (stream object)
227 (funcall (ansi-stream-misc stream) stream :file-string-length object))
229 ;;;; input functions
231 (defun ansi-stream-read-line-from-frc-buffer (stream eof-error-p eof-value)
232 (prepare-for-fast-read-char stream
233 (declare (ignore %frc-method%))
234 (let ((chunks-total-length 0)
235 (chunks nil))
236 (declare (type index chunks-total-length)
237 (list chunks))
238 (labels ((refill-buffer ()
239 (prog1
240 (fast-read-char-refill stream nil nil)
241 (setf %frc-index% (ansi-stream-in-index %frc-stream%))))
242 (newline-position ()
243 (position #\Newline (the (simple-array character (*))
244 %frc-buffer%)
245 :test #'char=
246 :start %frc-index%))
247 (make-and-return-result-string (pos)
248 (let* ((len (+ (- (or pos %frc-index%)
249 %frc-index%)
250 chunks-total-length))
251 (res (make-string len))
252 (start 0))
253 (declare (type index start))
254 (when chunks
255 (dolist (chunk (nreverse chunks))
256 (declare (type (simple-array character) chunk))
257 (replace res chunk :start1 start)
258 (incf start (length chunk))))
259 (unless (null pos)
260 (replace res %frc-buffer%
261 :start1 start
262 :start2 %frc-index% :end2 pos)
263 (setf %frc-index% (1+ pos)))
264 (done-with-fast-read-char)
265 (return-from ansi-stream-read-line-from-frc-buffer res)))
266 (add-chunk ()
267 (let* ((end (length %frc-buffer%))
268 (len (- end %frc-index%))
269 (chunk (make-string len)))
270 (replace chunk %frc-buffer% :start2 %frc-index% :end2 end)
271 (push chunk chunks)
272 (incf chunks-total-length len)
273 (when (refill-buffer)
274 (make-and-return-result-string nil)))))
275 (declare (inline make-and-return-result-string
276 refill-buffer))
277 (when (and (= %frc-index% +ansi-stream-in-buffer-length+)
278 (refill-buffer))
279 ;; EOF had been reached before we read anything
280 ;; at all. Return the EOF value or signal the error.
281 (done-with-fast-read-char)
282 (return-from ansi-stream-read-line-from-frc-buffer
283 (values (eof-or-lose stream eof-error-p eof-value) t)))
284 (loop
285 (let ((pos (newline-position)))
286 (if pos
287 (make-and-return-result-string pos)
288 (add-chunk))))))))
290 #!-sb-fluid (declaim (inline ansi-stream-read-line))
291 (defun ansi-stream-read-line (stream eof-error-p eof-value recursive-p)
292 (declare (ignore recursive-p))
293 (if (ansi-stream-cin-buffer stream)
294 ;; Stream has a fast-read-char buffer. Copy large chunks directly
295 ;; out of the buffer.
296 (ansi-stream-read-line-from-frc-buffer stream eof-error-p eof-value)
297 ;; Slow path, character by character.
298 (prepare-for-fast-read-char stream
299 (let ((res (make-string 80))
300 (len 80)
301 (index 0))
302 (loop
303 (let ((ch (fast-read-char nil nil)))
304 (cond (ch
305 (when (char= ch #\newline)
306 (done-with-fast-read-char)
307 (return (values (%shrink-vector res index) nil)))
308 (when (= index len)
309 (setq len (* len 2))
310 (let ((new (make-string len)))
311 (replace new res)
312 (setq res new)))
313 (setf (schar res index) ch)
314 (incf index))
315 ((zerop index)
316 (done-with-fast-read-char)
317 (return (values (eof-or-lose stream
318 eof-error-p
319 eof-value)
320 t)))
321 ;; Since FAST-READ-CHAR already hit the eof char, we
322 ;; shouldn't do another READ-CHAR.
324 (done-with-fast-read-char)
325 (return (values (%shrink-vector res index) t))))))))))
327 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
328 recursive-p)
329 (let ((stream (in-synonym-of stream)))
330 (if (ansi-stream-p stream)
331 (ansi-stream-read-line stream eof-error-p eof-value recursive-p)
332 ;; must be Gray streams FUNDAMENTAL-STREAM
333 (multiple-value-bind (string eof) (stream-read-line stream)
334 (if (and eof (zerop (length string)))
335 (values (eof-or-lose stream eof-error-p eof-value) t)
336 (values string eof))))))
338 ;;; We proclaim them INLINE here, then proclaim them NOTINLINE later on,
339 ;;; so, except in this file, they are not inline by default, but they can be.
340 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
342 #!-sb-fluid (declaim (inline ansi-stream-read-char))
343 (defun ansi-stream-read-char (stream eof-error-p eof-value recursive-p)
344 (declare (ignore recursive-p))
345 (prepare-for-fast-read-char stream
346 (prog1
347 (fast-read-char eof-error-p eof-value)
348 (done-with-fast-read-char))))
350 (defun read-char (&optional (stream *standard-input*)
351 (eof-error-p t)
352 eof-value
353 recursive-p)
354 (let ((stream (in-synonym-of stream)))
355 (if (ansi-stream-p stream)
356 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
357 ;; must be Gray streams FUNDAMENTAL-STREAM
358 (let ((char (stream-read-char stream)))
359 (if (eq char :eof)
360 (eof-or-lose stream eof-error-p eof-value)
361 char)))))
363 #!-sb-fluid (declaim (inline ansi-stream-unread-char))
364 (defun ansi-stream-unread-char (character stream)
365 (let ((index (1- (ansi-stream-in-index stream)))
366 (buffer (ansi-stream-cin-buffer stream)))
367 (declare (fixnum index))
368 (when (minusp index) (error "nothing to unread"))
369 (cond (buffer
370 (setf (aref buffer index) character)
371 (setf (ansi-stream-in-index stream) index))
373 (funcall (ansi-stream-misc stream) stream
374 :unread character)))))
376 (defun unread-char (character &optional (stream *standard-input*))
377 (let ((stream (in-synonym-of stream)))
378 (if (ansi-stream-p stream)
379 (ansi-stream-unread-char character stream)
380 ;; must be Gray streams FUNDAMENTAL-STREAM
381 (stream-unread-char stream character)))
382 nil)
384 #!-sb-fluid (declaim (inline ansi-stream-listen))
385 (defun ansi-stream-listen (stream)
386 (or (/= (the fixnum (ansi-stream-in-index stream))
387 +ansi-stream-in-buffer-length+)
388 ;; Handle :EOF return from misc methods specially
389 (let ((result (funcall (ansi-stream-misc stream) stream :listen)))
390 (if (eq result :eof)
392 result))))
394 (defun listen (&optional (stream *standard-input*))
395 (let ((stream (in-synonym-of stream)))
396 (if (ansi-stream-p stream)
397 (ansi-stream-listen stream)
398 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
399 (stream-listen stream))))
401 #!-sb-fluid (declaim (inline ansi-stream-read-char-no-hang))
402 (defun ansi-stream-read-char-no-hang (stream eof-error-p eof-value recursive-p)
403 (if (funcall (ansi-stream-misc stream) stream :listen)
404 ;; On T or :EOF get READ-CHAR to do the work.
405 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
406 nil))
408 (defun read-char-no-hang (&optional (stream *standard-input*)
409 (eof-error-p t)
410 eof-value
411 recursive-p)
412 (let ((stream (in-synonym-of stream)))
413 (if (ansi-stream-p stream)
414 (ansi-stream-read-char-no-hang stream eof-error-p eof-value
415 recursive-p)
416 ;; must be Gray streams FUNDAMENTAL-STREAM
417 (let ((char (stream-read-char-no-hang stream)))
418 (if (eq char :eof)
419 (eof-or-lose stream eof-error-p eof-value)
420 char)))))
422 #!-sb-fluid (declaim (inline ansi-stream-clear-input))
423 (defun ansi-stream-clear-input (stream)
424 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
425 (funcall (ansi-stream-misc stream) stream :clear-input))
427 (defun clear-input (&optional (stream *standard-input*))
428 (let ((stream (in-synonym-of stream)))
429 (if (ansi-stream-p stream)
430 (ansi-stream-clear-input stream)
431 ;; must be Gray streams FUNDAMENTAL-STREAM
432 (stream-clear-input stream)))
433 nil)
435 #!-sb-fluid (declaim (inline ansi-stream-read-byte))
436 (defun ansi-stream-read-byte (stream eof-error-p eof-value recursive-p)
437 ;; Why the "recursive-p" parameter? a-s-r-b is funcall'ed from
438 ;; a-s-read-sequence and needs a lambda list that's congruent with
439 ;; that of a-s-read-char
440 (declare (ignore recursive-p))
441 (prepare-for-fast-read-byte stream
442 (prog1
443 (fast-read-byte eof-error-p eof-value t)
444 (done-with-fast-read-byte))))
446 (defun read-byte (stream &optional (eof-error-p t) eof-value)
447 (if (ansi-stream-p stream)
448 (ansi-stream-read-byte stream eof-error-p eof-value nil)
449 ;; must be Gray streams FUNDAMENTAL-STREAM
450 (let ((char (stream-read-byte stream)))
451 (if (eq char :eof)
452 (eof-or-lose stream eof-error-p eof-value)
453 char))))
455 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
456 ;;; number of bytes read.
458 ;;; Note: CMU CL's version of this had a special interpretation of
459 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
460 ;;; case, CMU CL's version would return as soon as any data became
461 ;;; available.) This could be useful behavior for things like pipes in
462 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
463 ;;; If we ever need it, it could be added later as a new variant N-BIN
464 ;;; method (perhaps N-BIN-ASAP?) or something.
465 #!-sb-fluid (declaim (inline read-n-bytes))
466 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
467 (if (ansi-stream-p stream)
468 (ansi-stream-read-n-bytes stream buffer start numbytes eof-error-p)
469 ;; We don't need to worry about element-type size here is that
470 ;; callers are supposed to have checked everything is kosher.
471 (let* ((end (+ start numbytes))
472 (read-end (stream-read-sequence stream buffer start end)))
473 (eof-or-lose stream (and eof-error-p (< read-end end)) (- read-end start)))))
475 (defun ansi-stream-read-n-bytes (stream buffer start numbytes eof-error-p)
476 (declare (type ansi-stream stream)
477 (type index numbytes start)
478 (type (or (simple-array * (*)) system-area-pointer) buffer))
479 (let* ((stream (in-synonym-of stream ansi-stream))
480 (in-buffer (ansi-stream-in-buffer stream))
481 (index (ansi-stream-in-index stream))
482 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
483 (declare (fixnum index num-buffered))
484 (cond
485 ((not in-buffer)
486 (funcall (ansi-stream-n-bin stream)
487 stream
488 buffer
489 start
490 numbytes
491 eof-error-p))
492 ((<= numbytes num-buffered)
493 #+nil
494 (let ((copy-function (typecase buffer
495 ((simple-array * (*)) #'ub8-bash-copy)
496 (system-area-pointer #'copy-ub8-to-system-area))))
497 (funcall copy-function in-buffer index buffer start numbytes))
498 (%byte-blt in-buffer index
499 buffer start (+ start numbytes))
500 (setf (ansi-stream-in-index stream) (+ index numbytes))
501 numbytes)
503 (let ((end (+ start num-buffered)))
504 #+nil
505 (let ((copy-function (typecase buffer
506 ((simple-array * (*)) #'ub8-bash-copy)
507 (system-area-pointer #'copy-ub8-to-system-area))))
508 (funcall copy-function in-buffer index buffer start num-buffered))
509 (%byte-blt in-buffer index buffer start end)
510 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
511 (+ (funcall (ansi-stream-n-bin stream)
512 stream
513 buffer
515 (- numbytes num-buffered)
516 eof-error-p)
517 num-buffered))))))
519 ;;; the amount of space we leave at the start of the in-buffer for
520 ;;; unreading
522 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
523 (defconstant +ansi-stream-in-buffer-extra+
524 4) ; FIXME: should be symbolic constant
526 ;;; This function is called by the FAST-READ-CHAR expansion to refill
527 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
528 ;;; and hence must be an N-BIN method. It's also called by other stream
529 ;;; functions which directly peek into the frc buffer.
530 (defun fast-read-char-refill (stream eof-error-p eof-value)
531 (let* ((ibuf (ansi-stream-cin-buffer stream))
532 (count (funcall (ansi-stream-n-bin stream)
533 stream
534 ibuf
535 +ansi-stream-in-buffer-extra+
536 (- +ansi-stream-in-buffer-length+
537 +ansi-stream-in-buffer-extra+)
538 nil))
539 (start (- +ansi-stream-in-buffer-length+ count)))
540 (declare (type index start count))
541 (cond ((zerop count)
542 ;; An empty count does not necessarily mean that we reached
543 ;; the EOF, it's also possible that it's e.g. due to a
544 ;; invalid octet sequence in a multibyte stream. To handle
545 ;; the resyncing case correctly we need to call the
546 ;; single-character reading function and check whether an
547 ;; EOF was really reached. If not, we can just fill the
548 ;; buffer by one character, and hope that the next refill
549 ;; will not need to resync.
550 (let* ((value (funcall (ansi-stream-in stream) stream nil :eof))
551 (index (1- +ansi-stream-in-buffer-length+)))
552 (case value
553 ((:eof)
554 ;; Mark buffer as empty.
555 (setf (ansi-stream-in-index stream)
556 +ansi-stream-in-buffer-length+)
557 ;; EOF. Redo the read, this time with the real eof parameters.
558 (values t (funcall (ansi-stream-in stream)
559 stream eof-error-p eof-value)))
560 (otherwise
561 (setf (aref ibuf index) value)
562 (values nil (setf (ansi-stream-in-index stream) index))))))
564 (when (/= start +ansi-stream-in-buffer-extra+)
565 (#.(let* ((n-character-array-bits
566 (sb!vm:saetp-n-bits
567 (find 'character
568 sb!vm:*specialized-array-element-type-properties*
569 :key #'sb!vm:saetp-specifier)))
570 (bash-function (intern (format nil "UB~D-BASH-COPY" n-character-array-bits)
571 (find-package "SB!KERNEL"))))
572 bash-function)
573 ibuf +ansi-stream-in-buffer-extra+
574 ibuf start
575 count))
576 (values nil
577 (setf (ansi-stream-in-index stream) start))))))
579 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
580 ;;; leave room for unreading.
581 (defun fast-read-byte-refill (stream eof-error-p eof-value)
582 (let* ((ibuf (ansi-stream-in-buffer stream))
583 (count (funcall (ansi-stream-n-bin stream) stream
584 ibuf 0 +ansi-stream-in-buffer-length+
585 nil))
586 (start (- +ansi-stream-in-buffer-length+ count)))
587 (declare (type index start count))
588 (cond ((zerop count)
589 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
590 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
592 (unless (zerop start)
593 (ub8-bash-copy ibuf 0
594 ibuf start
595 count))
596 (setf (ansi-stream-in-index stream) (1+ start))
597 (aref ibuf start)))))
599 ;;; output functions
601 (defun write-char (character &optional (stream *standard-output*))
602 (with-out-stream stream (ansi-stream-out character)
603 (stream-write-char character))
604 character)
606 (defun terpri (&optional (stream *standard-output*))
607 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
608 nil)
610 #!-sb-fluid (declaim (inline ansi-stream-fresh-line))
611 (defun ansi-stream-fresh-line (stream)
612 (when (/= (or (charpos stream) 1) 0)
613 (funcall (ansi-stream-out stream) stream #\newline)
616 (defun fresh-line (&optional (stream *standard-output*))
617 (let ((stream (out-synonym-of stream)))
618 (if (ansi-stream-p stream)
619 (ansi-stream-fresh-line stream)
620 ;; must be Gray streams FUNDAMENTAL-STREAM
621 (stream-fresh-line stream))))
623 (defun write-string (string &optional (stream *standard-output*)
624 &key (start 0) end)
625 (declare (type string string))
626 ;; Note that even though you might expect, based on the behavior of
627 ;; things like AREF, that the correct upper bound here is
628 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
629 ;; "bounding index" and "length" indicate that in this case (i.e.
630 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
631 ;; (LENGTH STRING) is the required upper bound. A foolish
632 ;; consistency is the hobgoblin of lesser languages..
633 (%write-string string stream start (%check-vector-sequence-bounds
634 string start end))
635 string)
637 #!-sb-fluid (declaim (inline ansi-stream-write-string))
638 (defun ansi-stream-write-string (string stream start end)
639 (declare (type string string))
640 (declare (type ansi-stream stream))
641 (declare (type index start end))
642 (with-array-data ((data string) (offset-start start)
643 (offset-end end)
644 :check-fill-pointer t)
645 (funcall (ansi-stream-sout stream)
646 stream data offset-start offset-end))
647 string)
649 (defun %write-string (string stream start end)
650 (declare (type string string))
651 (declare (type stream-designator stream))
652 (declare (type index start end))
653 (let ((stream (out-synonym-of stream)))
654 (if(ansi-stream-p stream)
655 (ansi-stream-write-string string stream start end)
656 ;; must be Gray streams FUNDAMENTAL-STREAM
657 (stream-write-string stream string start end))))
659 ;;; A wrapper function for all those (MACROLET OUT-FUN) definitions,
660 ;;; which cannot deal with keyword arguments.
661 (declaim (inline write-string-no-key))
662 (defun write-string-no-key (string stream start end)
663 (write-string string stream :start start :end end))
665 (defun write-line (string &optional (stream *standard-output*)
666 &key (start 0) end)
667 (declare (type string string))
668 ;; FIXME: Why is there this difference between the treatments of the
669 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
670 (let ((defaulted-stream (out-synonym-of stream)))
671 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
672 string start end))
673 (write-char #\newline defaulted-stream))
674 string)
676 (defun charpos (&optional (stream *standard-output*))
677 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
679 (defun line-length (&optional (stream *standard-output*))
680 (with-out-stream stream (ansi-stream-misc :line-length)
681 (stream-line-length)))
683 (defun finish-output (&optional (stream *standard-output*))
684 (with-out-stream stream (ansi-stream-misc :finish-output)
685 (stream-finish-output))
686 nil)
688 (defun force-output (&optional (stream *standard-output*))
689 (with-out-stream stream (ansi-stream-misc :force-output)
690 (stream-force-output))
691 nil)
693 (defun clear-output (&optional (stream *standard-output*))
694 (with-out-stream stream (ansi-stream-misc :clear-output)
695 (stream-force-output))
696 nil)
698 (defun write-byte (integer stream)
699 (with-out-stream/no-synonym stream (ansi-stream-bout integer)
700 (stream-write-byte integer))
701 integer)
704 ;;; (These were inline throughout this file, but that's not appropriate
705 ;;; globally. And we must not inline them in the rest of this file if
706 ;;; dispatch to gray or simple streams is to work, since both redefine
707 ;;; these functions later.)
708 (declaim (notinline read-char unread-char read-byte listen))
710 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
711 ;;; streams to handle the misc routines and dispatch to the
712 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
713 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
714 (declare (type stream stream) (ignore arg2))
715 (ecase operation
716 (:listen
717 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
718 (let ((char (read-char-no-hang stream nil :eof)))
719 (when (characterp char)
720 (unread-char char stream))
721 char))
722 (:unread
723 (unread-char arg1 stream))
724 (:close
725 (close stream))
726 (:clear-input
727 (clear-input stream))
728 (:force-output
729 (force-output stream))
730 (:finish-output
731 (finish-output stream))
732 (:element-type
733 (stream-element-type stream))
734 (:stream-external-format
735 (stream-external-format stream))
736 (:interactive-p
737 (interactive-stream-p stream))
738 (:line-length
739 (line-length stream))
740 (:charpos
741 (charpos stream))
742 (:file-length
743 (file-length stream))
744 (:file-string-length
745 (file-string-length stream arg1))
746 (:file-position
747 (file-position stream arg1))))
749 ;;;; broadcast streams
751 (defstruct (broadcast-stream (:include ansi-stream
752 (out #'broadcast-out)
753 (bout #'broadcast-bout)
754 (sout #'broadcast-sout)
755 (misc #'broadcast-misc))
756 (:constructor %make-broadcast-stream
757 (&rest streams))
758 (:copier nil))
759 ;; a list of all the streams we broadcast to
760 (streams () :type list :read-only t))
762 (defun make-broadcast-stream (&rest streams)
763 (dolist (stream streams)
764 (unless (output-stream-p stream)
765 (error 'type-error
766 :datum stream
767 :expected-type '(satisfies output-stream-p))))
768 (apply #'%make-broadcast-stream streams))
770 (macrolet ((out-fun (name fun &rest args)
771 `(defun ,name (stream ,@args)
772 (dolist (stream (broadcast-stream-streams stream))
773 (,fun ,(car args) stream ,@(cdr args))))))
774 (out-fun broadcast-out write-char char)
775 (out-fun broadcast-bout write-byte byte)
776 (out-fun broadcast-sout write-string-no-key string start end))
778 (defun broadcast-misc (stream operation &optional arg1 arg2)
779 (let ((streams (broadcast-stream-streams stream)))
780 (case operation
781 ;; FIXME: This may not be the best place to note this, but I
782 ;; think the :CHARPOS protocol needs revision. Firstly, I think
783 ;; this is the last place where a NULL return value was possible
784 ;; (before adjusting it to be 0), so a bunch of conditionals IF
785 ;; CHARPOS can be removed; secondly, it is my belief that
786 ;; FD-STREAMS, when running FILE-POSITION, do not update the
787 ;; CHARPOS, and consequently there will be much wrongness.
789 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
790 ;; is it testing the :charpos of an input stream?
792 ;; -- CSR, 2004-02-04
793 (:charpos
794 (dolist (stream streams 0)
795 (let ((charpos (charpos stream)))
796 (if charpos (return charpos)))))
797 (:line-length
798 (let ((min nil))
799 (dolist (stream streams min)
800 (let ((res (line-length stream)))
801 (when res (setq min (if min (min res min) res)))))))
802 (:element-type
803 #+nil ; old, arguably more logical, version
804 (let (res)
805 (dolist (stream streams (if (> (length res) 1) `(and ,@res) t))
806 (pushnew (stream-element-type stream) res :test #'equal)))
807 ;; ANSI-specified version (under System Class BROADCAST-STREAM)
808 (let ((res t))
809 (do ((streams streams (cdr streams)))
810 ((null streams) res)
811 (when (null (cdr streams))
812 (setq res (stream-element-type (car streams)))))))
813 (:external-format
814 (let ((res :default))
815 (dolist (stream streams res)
816 (setq res (stream-external-format stream)))))
817 (:file-length
818 (let ((last (last streams)))
819 (if last
820 (file-length (car last))
821 0)))
822 (:file-position
823 (if arg1
824 (let ((res (or (eql arg1 :start) (eql arg1 0))))
825 (dolist (stream streams res)
826 (setq res (file-position stream arg1))))
827 (let ((res 0))
828 (dolist (stream streams res)
829 (setq res (file-position stream))))))
830 (:file-string-length
831 (let ((res 1))
832 (dolist (stream streams res)
833 (setq res (file-string-length stream arg1)))))
834 (:close
835 (set-closed-flame stream))
837 (let ((res nil))
838 (dolist (stream streams res)
839 (setq res
840 (if (ansi-stream-p stream)
841 (funcall (ansi-stream-misc stream) stream operation
842 arg1 arg2)
843 (stream-misc-dispatch stream operation arg1 arg2)))))))))
845 ;;;; synonym streams
847 (defstruct (synonym-stream (:include ansi-stream
848 (in #'synonym-in)
849 (bin #'synonym-bin)
850 (n-bin #'synonym-n-bin)
851 (out #'synonym-out)
852 (bout #'synonym-bout)
853 (sout #'synonym-sout)
854 (misc #'synonym-misc))
855 (:constructor make-synonym-stream (symbol))
856 (:copier nil))
857 ;; This is the symbol, the value of which is the stream we are synonym to.
858 (symbol nil :type symbol :read-only t))
859 (def!method print-object ((x synonym-stream) stream)
860 (print-unreadable-object (x stream :type t :identity t)
861 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
863 ;;; The output simple output methods just call the corresponding
864 ;;; function on the synonymed stream.
865 (macrolet ((out-fun (name fun &rest args)
866 `(defun ,name (stream ,@args)
867 (declare (optimize (safety 1)))
868 (let ((syn (symbol-value (synonym-stream-symbol stream))))
869 (,fun ,(car args) syn ,@(cdr args))))))
870 (out-fun synonym-out write-char ch)
871 (out-fun synonym-bout write-byte n)
872 (out-fun synonym-sout write-string-no-key string start end))
874 ;;; For the input methods, we just call the corresponding function on the
875 ;;; synonymed stream. These functions deal with getting input out of
876 ;;; the In-Buffer if there is any.
877 (macrolet ((in-fun (name fun &rest args)
878 `(defun ,name (stream ,@args)
879 (declare (optimize (safety 1)))
880 (,fun (symbol-value (synonym-stream-symbol stream))
881 ,@args))))
882 (in-fun synonym-in read-char eof-error-p eof-value)
883 (in-fun synonym-bin read-byte eof-error-p eof-value)
884 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
886 (defun synonym-misc (stream operation &optional arg1 arg2)
887 (declare (optimize (safety 1)))
888 (let ((syn (symbol-value (synonym-stream-symbol stream))))
889 (if (ansi-stream-p syn)
890 ;; We have to special-case some operations which interact with
891 ;; the in-buffer of the wrapped stream, since just calling
892 ;; ANSI-STREAM-MISC on them
893 (case operation
894 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
895 +ansi-stream-in-buffer-length+)
896 (funcall (ansi-stream-misc syn) syn :listen)))
897 (:clear-input (clear-input syn))
898 (:unread (unread-char arg1 syn))
900 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
901 (stream-misc-dispatch syn operation arg1 arg2))))
903 ;;;; two-way streams
905 (defstruct (two-way-stream
906 (:include ansi-stream
907 (in #'two-way-in)
908 (bin #'two-way-bin)
909 (n-bin #'two-way-n-bin)
910 (out #'two-way-out)
911 (bout #'two-way-bout)
912 (sout #'two-way-sout)
913 (misc #'two-way-misc))
914 (:constructor %make-two-way-stream (input-stream output-stream))
915 (:copier nil))
916 (input-stream (missing-arg) :type stream :read-only t)
917 (output-stream (missing-arg) :type stream :read-only t))
918 (defprinter (two-way-stream) input-stream output-stream)
920 (defun make-two-way-stream (input-stream output-stream)
921 #!+sb-doc
922 "Return a bidirectional stream which gets its input from INPUT-STREAM and
923 sends its output to OUTPUT-STREAM."
924 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
925 ;; should be encapsulated in a function, and used here and most of
926 ;; the other places that SYNONYM-STREAM-P appears.
927 (unless (output-stream-p output-stream)
928 (error 'type-error
929 :datum output-stream
930 :expected-type '(satisfies output-stream-p)))
931 (unless (input-stream-p input-stream)
932 (error 'type-error
933 :datum input-stream
934 :expected-type '(satisfies input-stream-p)))
935 (funcall #'%make-two-way-stream input-stream output-stream))
937 (macrolet ((out-fun (name fun &rest args)
938 `(defun ,name (stream ,@args)
939 (let ((syn (two-way-stream-output-stream stream)))
940 (,fun ,(car args) syn ,@(cdr args))))))
941 (out-fun two-way-out write-char ch)
942 (out-fun two-way-bout write-byte n)
943 (out-fun two-way-sout write-string-no-key string start end))
945 (macrolet ((in-fun (name fun &rest args)
946 `(defun ,name (stream ,@args)
947 (force-output (two-way-stream-output-stream stream))
948 (,fun (two-way-stream-input-stream stream) ,@args))))
949 (in-fun two-way-in read-char eof-error-p eof-value)
950 (in-fun two-way-bin read-byte eof-error-p eof-value)
951 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
953 (defun two-way-misc (stream operation &optional arg1 arg2)
954 (let* ((in (two-way-stream-input-stream stream))
955 (out (two-way-stream-output-stream stream))
956 (in-ansi-stream-p (ansi-stream-p in))
957 (out-ansi-stream-p (ansi-stream-p out)))
958 (case operation
959 (:listen
960 (if in-ansi-stream-p
961 (or (/= (the fixnum (ansi-stream-in-index in))
962 +ansi-stream-in-buffer-length+)
963 (funcall (ansi-stream-misc in) in :listen))
964 (listen in)))
965 ((:finish-output :force-output :clear-output)
966 (if out-ansi-stream-p
967 (funcall (ansi-stream-misc out) out operation arg1 arg2)
968 (stream-misc-dispatch out operation arg1 arg2)))
969 (:clear-input (clear-input in))
970 (:unread (unread-char arg1 in))
971 (:element-type
972 (let ((in-type (stream-element-type in))
973 (out-type (stream-element-type out)))
974 (if (equal in-type out-type)
975 in-type `(and ,in-type ,out-type))))
976 (:close
977 (set-closed-flame stream))
979 (or (if in-ansi-stream-p
980 (funcall (ansi-stream-misc in) in operation arg1 arg2)
981 (stream-misc-dispatch in operation arg1 arg2))
982 (if out-ansi-stream-p
983 (funcall (ansi-stream-misc out) out operation arg1 arg2)
984 (stream-misc-dispatch out operation arg1 arg2)))))))
986 ;;;; concatenated streams
988 (defstruct (concatenated-stream
989 (:include ansi-stream
990 (in #'concatenated-in)
991 (bin #'concatenated-bin)
992 (n-bin #'concatenated-n-bin)
993 (misc #'concatenated-misc))
994 (:constructor %make-concatenated-stream (&rest streams))
995 (:copier nil))
996 ;; The car of this is the substream we are reading from now.
997 (streams nil :type list))
998 (def!method print-object ((x concatenated-stream) stream)
999 (print-unreadable-object (x stream :type t :identity t)
1000 (format stream
1001 ":STREAMS ~S"
1002 (concatenated-stream-streams x))))
1004 (defun make-concatenated-stream (&rest streams)
1005 #!+sb-doc
1006 "Return a stream which takes its input from each of the streams in turn,
1007 going on to the next at EOF."
1008 (dolist (stream streams)
1009 (unless (input-stream-p stream)
1010 (error 'type-error
1011 :datum stream
1012 :expected-type '(satisfies input-stream-p))))
1013 (apply #'%make-concatenated-stream streams))
1015 (macrolet ((in-fun (name fun)
1016 `(defun ,name (stream eof-error-p eof-value)
1017 (do ((streams (concatenated-stream-streams stream)
1018 (cdr streams)))
1019 ((null streams)
1020 (eof-or-lose stream eof-error-p eof-value))
1021 (let* ((stream (car streams))
1022 (result (,fun stream nil nil)))
1023 (when result (return result)))
1024 (pop (concatenated-stream-streams stream))))))
1025 (in-fun concatenated-in read-char)
1026 (in-fun concatenated-bin read-byte))
1028 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
1029 (do ((streams (concatenated-stream-streams stream) (cdr streams))
1030 (current-start start)
1031 (remaining-bytes numbytes))
1032 ((null streams)
1033 (if eof-errorp
1034 (error 'end-of-file :stream stream)
1035 (- numbytes remaining-bytes)))
1036 (let* ((stream (car streams))
1037 (bytes-read (read-n-bytes stream buffer current-start
1038 remaining-bytes nil)))
1039 (incf current-start bytes-read)
1040 (decf remaining-bytes bytes-read)
1041 (when (zerop remaining-bytes) (return numbytes)))
1042 (setf (concatenated-stream-streams stream) (cdr streams))))
1044 (defun concatenated-misc (stream operation &optional arg1 arg2)
1045 (let* ((left (concatenated-stream-streams stream))
1046 (current (car left)))
1047 (case operation
1048 (:listen
1049 (unless left
1050 (return-from concatenated-misc :eof))
1051 (loop
1052 (let ((stuff (if (ansi-stream-p current)
1053 (funcall (ansi-stream-misc current) current
1054 :listen)
1055 (stream-misc-dispatch current :listen))))
1056 (cond ((eq stuff :eof)
1057 ;; Advance STREAMS, and try again.
1058 (pop (concatenated-stream-streams stream))
1059 (setf current
1060 (car (concatenated-stream-streams stream)))
1061 (unless current
1062 ;; No further streams. EOF.
1063 (return :eof)))
1064 (stuff
1065 ;; Stuff's available.
1066 (return t))
1068 ;; Nothing is available yet.
1069 (return nil))))))
1070 (:clear-input (when left (clear-input current)))
1071 (:unread (when left (unread-char arg1 current)))
1072 (:close
1073 (set-closed-flame stream))
1075 (when left
1076 (if (ansi-stream-p current)
1077 (funcall (ansi-stream-misc current) current operation arg1 arg2)
1078 (stream-misc-dispatch current operation arg1 arg2)))))))
1080 ;;;; echo streams
1082 (defstruct (echo-stream
1083 (:include two-way-stream
1084 (in #'echo-in)
1085 (bin #'echo-bin)
1086 (misc #'echo-misc)
1087 (n-bin #'echo-n-bin))
1088 (:constructor %make-echo-stream (input-stream output-stream))
1089 (:copier nil))
1090 unread-stuff)
1091 (def!method print-object ((x echo-stream) stream)
1092 (print-unreadable-object (x stream :type t :identity t)
1093 (format stream
1094 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1095 (two-way-stream-input-stream x)
1096 (two-way-stream-output-stream x))))
1098 (defun make-echo-stream (input-stream output-stream)
1099 #!+sb-doc
1100 "Return a bidirectional stream which gets its input from INPUT-STREAM and
1101 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
1102 the output stream."
1103 (unless (output-stream-p output-stream)
1104 (error 'type-error
1105 :datum output-stream
1106 :expected-type '(satisfies output-stream-p)))
1107 (unless (input-stream-p input-stream)
1108 (error 'type-error
1109 :datum input-stream
1110 :expected-type '(satisfies input-stream-p)))
1111 (funcall #'%make-echo-stream input-stream output-stream))
1113 (macrolet ((in-fun (name in-fun out-fun &rest args)
1114 `(defun ,name (stream ,@args)
1115 (or (pop (echo-stream-unread-stuff stream))
1116 (let* ((in (echo-stream-input-stream stream))
1117 (out (echo-stream-output-stream stream))
1118 (result (if eof-error-p
1119 (,in-fun in ,@args)
1120 (,in-fun in nil in))))
1121 (cond
1122 ((eql result in) eof-value)
1123 (t (,out-fun result out) result)))))))
1124 (in-fun echo-in read-char write-char eof-error-p eof-value)
1125 (in-fun echo-bin read-byte write-byte eof-error-p eof-value))
1127 (defun echo-n-bin (stream buffer start numbytes eof-error-p)
1128 (let ((new-start start)
1129 (read 0))
1130 (loop
1131 (let ((thing (pop (echo-stream-unread-stuff stream))))
1132 (cond
1133 (thing
1134 (setf (aref buffer new-start) thing)
1135 (incf new-start)
1136 (incf read)
1137 (when (= read numbytes)
1138 (return-from echo-n-bin numbytes)))
1139 (t (return nil)))))
1140 (let ((bytes-read (read-n-bytes (echo-stream-input-stream stream) buffer
1141 new-start (- numbytes read) nil)))
1142 (cond
1143 ((not eof-error-p)
1144 (write-sequence buffer (echo-stream-output-stream stream)
1145 :start new-start :end (+ new-start bytes-read))
1146 (+ bytes-read read))
1147 ((> numbytes (+ read bytes-read))
1148 (write-sequence buffer (echo-stream-output-stream stream)
1149 :start new-start :end (+ new-start bytes-read))
1150 (error 'end-of-file :stream stream))
1152 (write-sequence buffer (echo-stream-output-stream stream)
1153 :start new-start :end (+ new-start bytes-read))
1154 (aver (= numbytes (+ new-start bytes-read)))
1155 numbytes)))))
1157 ;;;; STRING-INPUT-STREAM stuff
1159 (defstruct (string-input-stream
1160 (:include ansi-stream
1161 (in #'string-inch)
1162 (bin #'ill-bin)
1163 (n-bin #'ill-bin)
1164 (misc #'string-in-misc))
1165 (:constructor internal-make-string-input-stream
1166 (string current end))
1167 (:copier nil))
1168 (string (missing-arg) :type simple-string)
1169 (current (missing-arg) :type index)
1170 (end (missing-arg) :type index))
1172 (defun string-inch (stream eof-error-p eof-value)
1173 (declare (type string-input-stream stream))
1174 (let ((string (string-input-stream-string stream))
1175 (index (string-input-stream-current stream)))
1176 (cond ((>= index (the index (string-input-stream-end stream)))
1177 (eof-or-lose stream eof-error-p eof-value))
1179 (setf (string-input-stream-current stream) (1+ index))
1180 (char string index)))))
1182 (defun string-binch (stream eof-error-p eof-value)
1183 (declare (type string-input-stream stream))
1184 (let ((string (string-input-stream-string stream))
1185 (index (string-input-stream-current stream)))
1186 (cond ((>= index (the index (string-input-stream-end stream)))
1187 (eof-or-lose stream eof-error-p eof-value))
1189 (setf (string-input-stream-current stream) (1+ index))
1190 (char-code (char string index))))))
1192 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1193 (declare (type string-input-stream stream)
1194 (type index start requested))
1195 (let* ((string (string-input-stream-string stream))
1196 (index (string-input-stream-current stream))
1197 (available (- (string-input-stream-end stream) index))
1198 (copy (min available requested)))
1199 (declare (type simple-string string))
1200 (when (plusp copy)
1201 (setf (string-input-stream-current stream)
1202 (truly-the index (+ index copy)))
1203 ;; FIXME: why are we VECTOR-SAP'ing things here? what's the point?
1204 ;; and are there SB-UNICODE issues here as well? --njf, 2005-03-24
1205 (with-pinned-objects (string buffer)
1206 (system-area-ub8-copy (vector-sap string)
1207 index
1208 (if (typep buffer 'system-area-pointer)
1209 buffer
1210 (vector-sap buffer))
1211 start
1212 copy)))
1213 (if (and (> requested copy) eof-error-p)
1214 (error 'end-of-file :stream stream)
1215 copy)))
1217 (defun string-in-misc (stream operation &optional arg1 arg2)
1218 (declare (type string-input-stream stream)
1219 (ignore arg2))
1220 (case operation
1221 (:file-position
1222 (if arg1
1223 (setf (string-input-stream-current stream)
1224 (case arg1
1225 (:start 0)
1226 (:end (string-input-stream-end stream))
1227 ;; We allow moving position beyond EOF. Errors happen
1228 ;; on read, not move -- or the user may extend the
1229 ;; input string.
1230 (t arg1)))
1231 (string-input-stream-current stream)))
1232 ;; According to ANSI: "Should signal an error of type type-error
1233 ;; if stream is not a stream associated with a file."
1234 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1235 ;; (:file-length (length (string-input-stream-string stream)))
1236 (:unread (decf (string-input-stream-current stream)))
1237 (:close (set-closed-flame stream))
1238 (:listen (or (/= (the index (string-input-stream-current stream))
1239 (the index (string-input-stream-end stream)))
1240 :eof))
1241 (:element-type (array-element-type (string-input-stream-string stream)))))
1243 (defun make-string-input-stream (string &optional (start 0) end)
1244 #!+sb-doc
1245 "Return an input stream which will supply the characters of STRING between
1246 START and END in order."
1247 (declare (type string string)
1248 (type index start)
1249 (type (or index null) end))
1250 (let* ((string (coerce string '(simple-array character (*)))))
1251 ;; FIXME: Why WITH-ARRAY-DATA, since the array is already simple?
1252 (with-array-data ((string string) (start start) (end end))
1253 (internal-make-string-input-stream
1254 string ;; now simple
1255 start
1256 end))))
1258 ;;;; STRING-OUTPUT-STREAM stuff
1259 ;;;;
1260 ;;;; FIXME: This, like almost none of the stream code is particularly
1261 ;;;; interrupt or thread-safe. While it should not be possible to
1262 ;;;; corrupt the heap here, it certainly is possible to end up with
1263 ;;;; a string-output-stream whose internal state is messed up.
1264 ;;;;
1265 ;;;; FIXME: It would be nice to support space-efficient
1266 ;;;; string-output-streams with element-type base-char. This would
1267 ;;;; mean either a separate subclass, or typecases in functions.
1269 (defparameter *string-output-stream-buffer-initial-size* 64)
1271 #!-sb-fluid
1272 (declaim (inline string-output-string-stream-buffer
1273 string-output-string-stream-pointer
1274 string-output-string-stream-index))
1275 (defstruct (string-output-stream
1276 (:include ansi-stream
1277 (out #'string-ouch)
1278 (sout #'string-sout)
1279 (misc #'string-out-misc))
1280 (:constructor make-string-output-stream
1281 (&key (element-type 'character)
1282 &aux (buffer
1283 (make-string
1284 *string-output-stream-buffer-initial-size*))))
1285 (:copier nil))
1286 ;; The string we throw stuff in.
1287 (buffer (missing-arg) :type (simple-array character (*)))
1288 ;; Chains of buffers to use
1289 (prev nil)
1290 (next nil)
1291 ;; Index of the next location to use in the current string.
1292 (pointer 0 :type index)
1293 ;; Global location in the stream
1294 (index 0 :type index)
1295 ;; Index cache: when we move backwards we save the greater of this
1296 ;; and index here, so the greater of index and this is always the
1297 ;; end of the stream.
1298 (index-cache 0 :type index)
1299 ;; Requested element type
1300 (element-type 'character))
1302 #!+sb-doc
1303 (setf (fdocumentation 'make-string-output-stream 'function)
1304 "Return an output stream which will accumulate all output given it for the
1305 benefit of the function GET-OUTPUT-STREAM-STRING.")
1307 ;;; Pushes the current segment onto the prev-list, and either pops
1308 ;;; or allocates a new one.
1309 (defun string-output-stream-new-buffer (stream size)
1310 (declare (index size))
1311 (/show0 "/string-output-stream-new-buffer")
1312 (push (string-output-stream-buffer stream)
1313 (string-output-stream-prev stream))
1314 (setf (string-output-stream-buffer stream)
1315 (or (pop (string-output-stream-next stream))
1316 ;; FIXME: This would be the correct place to detect that
1317 ;; more then FIXNUM characters are being written to the
1318 ;; stream, and do something about it.
1319 (make-string size))))
1321 ;;; Moves to the end of the next segment or the current one if there are
1322 ;;; no more segments. Returns true as long as there are next segments.
1323 (defun string-output-stream-next-buffer (stream)
1324 (/show0 "/string-output-stream-next-buffer")
1325 (let* ((old (string-output-stream-buffer stream))
1326 (new (pop (string-output-stream-next stream)))
1327 (old-size (length old))
1328 (skipped (- old-size (string-output-stream-pointer stream))))
1329 (cond (new
1330 (let ((new-size (length new)))
1331 (push old (string-output-stream-prev stream))
1332 (setf (string-output-stream-buffer stream) new
1333 (string-output-stream-pointer stream) new-size)
1334 (incf (string-output-stream-index stream) (+ skipped new-size)))
1337 (setf (string-output-stream-pointer stream) old-size)
1338 (incf (string-output-stream-index stream) skipped)
1339 nil))))
1341 ;;; Moves to the start of the previous segment or the current one if there
1342 ;;; are no more segments. Returns true as long as there are prev segments.
1343 (defun string-output-stream-prev-buffer (stream)
1344 (/show0 "/string-output-stream-prev-buffer")
1345 (let ((old (string-output-stream-buffer stream))
1346 (new (pop (string-output-stream-prev stream)))
1347 (skipped (string-output-stream-pointer stream)))
1348 (cond (new
1349 (push old (string-output-stream-next stream))
1350 (setf (string-output-stream-buffer stream) new
1351 (string-output-stream-pointer stream) 0)
1352 (decf (string-output-stream-index stream) (+ skipped (length new)))
1355 (setf (string-output-stream-pointer stream) 0)
1356 (decf (string-output-stream-index stream) skipped)
1357 nil))))
1359 (defun string-ouch (stream character)
1360 (/show0 "/string-ouch")
1361 (let ((pointer (string-output-stream-pointer stream))
1362 (buffer (string-output-stream-buffer stream))
1363 (index (string-output-stream-index stream)))
1364 (cond ((= pointer (length buffer))
1365 (setf buffer (string-output-stream-new-buffer stream index)
1366 (aref buffer 0) character
1367 (string-output-stream-pointer stream) 1))
1369 (setf (aref buffer pointer) character
1370 (string-output-stream-pointer stream) (1+ pointer))))
1371 (setf (string-output-stream-index stream) (1+ index))))
1373 (defun string-sout (stream string start end)
1374 (declare (type simple-string string)
1375 (type index start end))
1376 (let* ((full-length (- end start))
1377 (length full-length)
1378 (buffer (string-output-stream-buffer stream))
1379 (pointer (string-output-stream-pointer stream))
1380 (space (- (length buffer) pointer))
1381 (here (min space length))
1382 (stop (+ start here))
1383 (overflow (- length space)))
1384 (declare (index length space here stop full-length)
1385 (fixnum overflow)
1386 (type (simple-array character (*)) buffer))
1387 (tagbody
1388 :more
1389 (when (plusp here)
1390 (etypecase string
1391 ((simple-array character (*))
1392 (replace buffer string :start1 pointer :start2 start :end2 stop))
1393 (simple-base-string
1394 (replace buffer string :start1 pointer :start2 start :end2 stop))
1395 ((simple-array nil (*))
1396 (replace buffer string :start1 pointer :start2 start :end2 stop)))
1397 (setf (string-output-stream-pointer stream) (+ here pointer)))
1398 (when (plusp overflow)
1399 (setf start stop
1400 length (- end start)
1401 buffer (string-output-stream-new-buffer
1402 stream (max overflow (string-output-stream-index stream)))
1403 pointer 0
1404 space (length buffer)
1405 here (min space length)
1406 stop (+ start here)
1407 ;; there may be more overflow if we used a buffer
1408 ;; already allocated to the stream
1409 overflow (- length space))
1410 (go :more)))
1411 (incf (string-output-stream-index stream) full-length)))
1413 ;;; Factored out of the -misc method due to size.
1414 (defun set-string-output-stream-file-position (stream pos)
1415 (let* ((index (string-output-stream-index stream))
1416 (end (max index (string-output-stream-index-cache stream))))
1417 (declare (index index end))
1418 (setf (string-output-stream-index-cache stream) end)
1419 (cond ((eq :start pos)
1420 (loop while (string-output-stream-prev-buffer stream)))
1421 ((eq :end pos)
1422 (loop while (string-output-stream-next-buffer stream))
1423 (let ((over (- (string-output-stream-index stream) end)))
1424 (decf (string-output-stream-pointer stream) over))
1425 (setf (string-output-stream-index stream) end))
1426 ((< pos index)
1427 (loop while (< pos index)
1428 do (string-output-stream-prev-buffer stream)
1429 (setf index (string-output-stream-index stream)))
1430 (let ((step (- pos index)))
1431 (incf (string-output-stream-pointer stream) step)
1432 (setf (string-output-stream-index stream) pos)))
1433 ((> pos index)
1434 ;; We allow moving beyond the end of stream, implicitly
1435 ;; extending the output stream.
1436 (let ((next (string-output-stream-next-buffer stream)))
1437 ;; Update after -next-buffer, INDEX is kept pointing at
1438 ;; the end of the current buffer.
1439 (setf index (string-output-stream-index stream))
1440 (loop while (and next (> pos index))
1441 do (setf next (string-output-stream-next-buffer stream)
1442 index (string-output-stream-index stream))))
1443 ;; Allocate new buffer if needed, or step back to
1444 ;; the desired index and set pointer and index
1445 ;; correctly.
1446 (let ((diff (- pos index)))
1447 (if (plusp diff)
1448 (let* ((new (string-output-stream-new-buffer stream diff))
1449 (size (length new)))
1450 (aver (= pos (+ index size)))
1451 (setf (string-output-stream-pointer stream) size
1452 (string-output-stream-index stream) pos))
1453 (let ((size (length (string-output-stream-buffer stream))))
1454 (setf (string-output-stream-pointer stream) (+ size diff)
1455 (string-output-stream-index stream) pos))))))))
1457 (defun string-out-misc (stream operation &optional arg1 arg2)
1458 (declare (ignore arg2))
1459 (case operation
1460 (:charpos
1461 ;; Keeping this first is a silly micro-optimization: FRESH-LINE
1462 ;; makes this the most common one.
1463 (/show0 "/string-out-misc charpos")
1464 (prog ((pointer (string-output-stream-pointer stream))
1465 (buffer (string-output-stream-buffer stream))
1466 (prev (string-output-stream-prev stream))
1467 (base 0))
1468 :next
1469 (let ((pos (position #\newline buffer :from-end t :end pointer)))
1470 (when (or pos (not buffer))
1471 ;; If newline is at index I, and pointer at index I+N, charpos
1472 ;; is N-1. If there is no newline, and pointer is at index N,
1473 ;; charpos is N.
1474 (return (+ base (if pos (- pointer pos 1) pointer))))
1475 (setf base (+ base pointer)
1476 buffer (pop prev)
1477 pointer (length buffer))
1478 (/show0 "/string-out-misc charpos next")
1479 (go :next))))
1480 (:file-position
1481 (/show0 "/string-out-misc file-position")
1482 (when arg1
1483 (set-string-output-stream-file-position stream arg1))
1484 (string-output-stream-index stream))
1485 (:close
1486 (/show0 "/string-out-misc close")
1487 (set-closed-flame stream))
1488 (:element-type (string-output-stream-element-type stream))))
1490 ;;; Return a string of all the characters sent to a stream made by
1491 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1492 (defun get-output-stream-string (stream)
1493 (declare (type string-output-stream stream))
1494 (let* ((length (max (string-output-stream-index stream)
1495 (string-output-stream-index-cache stream)))
1496 (element-type (string-output-stream-element-type stream))
1497 (prev (string-output-stream-prev stream))
1498 (this (string-output-stream-buffer stream))
1499 (next (string-output-stream-next stream))
1500 (result
1501 (case element-type
1502 ;; overwhelmingly common case: can be inlined
1504 ;; FIXME: If we were willing to use %SHRINK-VECTOR here,
1505 ;; and allocate new strings the size of 2 * index in
1506 ;; STRING-SOUT, we would not need to allocate one here in
1507 ;; the common case, but could just use the last one
1508 ;; allocated, and chop it down to size..
1510 ((character) (make-string length))
1511 ;; slightly less common cases: inline it anyway
1512 ((base-char standard-char)
1513 (make-string length :element-type 'base-char))
1515 (make-string length :element-type element-type)))))
1517 (setf (string-output-stream-index stream) 0
1518 (string-output-stream-index-cache stream) 0
1519 (string-output-stream-pointer stream) 0
1520 ;; throw them away for simplicity's sake: this way the rest of the
1521 ;; implementation can assume that the greater of INDEX and INDEX-CACHE
1522 ;; is always within the last buffer.
1523 (string-output-stream-prev stream) nil
1524 (string-output-stream-next stream) nil)
1526 (flet ((replace-all (fun)
1527 (let ((start 0))
1528 (declare (index start))
1529 (dolist (buffer (nreverse prev))
1530 (funcall fun buffer start)
1531 (incf start (length buffer)))
1532 (funcall fun this start)
1533 (incf start (length this))
1534 (dolist (buffer next)
1535 (funcall fun buffer start)
1536 (incf start (length buffer))))))
1537 (macrolet ((frob (type)
1538 `(replace-all (lambda (buffer from)
1539 (declare (type ,type result)
1540 (type (simple-array character (*))
1541 buffer))
1542 (replace result buffer :start1 from)))))
1543 (etypecase result
1544 ((simple-array character (*))
1545 (frob (simple-array character (*))))
1546 (simple-base-string
1547 (frob simple-base-string))
1548 ((simple-array nil (*))
1549 (frob (simple-array nil (*)))))))
1551 result))
1553 ;;;; fill-pointer streams
1555 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1556 ;;; the CLM, but they are required for the implementation of
1557 ;;; WITH-OUTPUT-TO-STRING.
1559 ;;; FIXME: need to support (VECTOR BASE-CHAR) and (VECTOR NIL),
1560 ;;; ideally without destroying all hope of efficiency.
1561 (deftype string-with-fill-pointer ()
1562 '(and (vector character)
1563 (satisfies array-has-fill-pointer-p)))
1565 (defstruct (fill-pointer-output-stream
1566 (:include ansi-stream
1567 (out #'fill-pointer-ouch)
1568 (sout #'fill-pointer-sout)
1569 (misc #'fill-pointer-misc))
1570 (:constructor make-fill-pointer-output-stream (string))
1571 (:copier nil))
1572 ;; a string with a fill pointer where we stuff the stuff we write
1573 (string (missing-arg) :type string-with-fill-pointer :read-only t))
1575 (defun fill-pointer-ouch (stream character)
1576 (let* ((buffer (fill-pointer-output-stream-string stream))
1577 (current (fill-pointer buffer))
1578 (current+1 (1+ current)))
1579 (declare (fixnum current))
1580 (with-array-data ((workspace buffer) (start) (end))
1581 (declare (type (simple-array character (*)) workspace))
1582 (let ((offset-current (+ start current)))
1583 (declare (fixnum offset-current))
1584 (if (= offset-current end)
1585 (let* ((new-length (1+ (* current 2)))
1586 (new-workspace (make-string new-length)))
1587 (declare (type (simple-array character (*)) new-workspace))
1588 (replace new-workspace workspace
1589 :start2 start :end2 offset-current)
1590 (setf workspace new-workspace
1591 offset-current current)
1592 (set-array-header buffer workspace new-length
1593 current+1 0 new-length nil))
1594 (setf (fill-pointer buffer) current+1))
1595 (setf (schar workspace offset-current) character)))
1596 current+1))
1598 (defun fill-pointer-sout (stream string start end)
1599 (declare (simple-string string) (fixnum start end))
1600 (let* ((string (if (typep string '(simple-array character (*)))
1601 string
1602 (coerce string '(simple-array character (*)))))
1603 (buffer (fill-pointer-output-stream-string stream))
1604 (current (fill-pointer buffer))
1605 (string-len (- end start))
1606 (dst-end (+ string-len current)))
1607 (declare (fixnum current dst-end string-len))
1608 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1609 (declare (type (simple-array character (*)) workspace))
1610 (let ((offset-dst-end (+ dst-start dst-end))
1611 (offset-current (+ dst-start current)))
1612 (declare (fixnum offset-dst-end offset-current))
1613 (if (> offset-dst-end dst-length)
1614 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1615 (new-workspace (make-string new-length)))
1616 (declare (type (simple-array character (*)) new-workspace))
1617 (replace new-workspace workspace
1618 :start2 dst-start :end2 offset-current)
1619 (setf workspace new-workspace
1620 offset-current current
1621 offset-dst-end dst-end)
1622 (set-array-header buffer workspace new-length
1623 dst-end 0 new-length nil))
1624 (setf (fill-pointer buffer) dst-end))
1625 (replace workspace string
1626 :start1 offset-current :start2 start :end2 end)))
1627 dst-end))
1629 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1630 (declare (ignore arg2))
1631 (case operation
1632 (:file-position
1633 (let ((buffer (fill-pointer-output-stream-string stream)))
1634 (if arg1
1635 (setf (fill-pointer buffer)
1636 (case arg1
1637 (:start 0)
1638 ;; Fill-pointer is always at fill-pointer we will
1639 ;; make :END move to the end of the actual string.
1640 (:end (array-total-size buffer))
1641 ;; We allow moving beyond the end of string if the
1642 ;; string is adjustable.
1643 (t (when (>= arg1 (array-total-size buffer))
1644 (if (adjustable-array-p buffer)
1645 (adjust-array buffer arg1)
1646 (error "Cannot move FILE-POSITION beyond the end ~
1647 of WITH-OUTPUT-TO-STRING stream ~
1648 constructed with non-adjustable string.")))
1649 arg1)))
1650 (fill-pointer buffer))))
1651 (:charpos
1652 (let* ((buffer (fill-pointer-output-stream-string stream))
1653 (current (fill-pointer buffer)))
1654 (with-array-data ((string buffer) (start) (end current))
1655 (declare (simple-string string) (ignore start))
1656 (let ((found (position #\newline string :test #'char=
1657 :end end :from-end t)))
1658 (if found
1659 (- end (the fixnum found))
1660 current)))))
1661 (:element-type (array-element-type
1662 (fill-pointer-output-stream-string stream)))))
1664 ;;;; indenting streams
1666 (defstruct (indenting-stream (:include ansi-stream
1667 (out #'indenting-out)
1668 (sout #'indenting-sout)
1669 (misc #'indenting-misc))
1670 (:constructor make-indenting-stream (stream))
1671 (:copier nil))
1672 ;; the stream we're based on
1673 stream
1674 ;; how much we indent on each line
1675 (indentation 0))
1677 #!+sb-doc
1678 (setf (fdocumentation 'make-indenting-stream 'function)
1679 "Return an output stream which indents its output by some amount.")
1681 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1682 ;;; output on the given STREAM based on the specified SUB-STREAM.
1683 (defmacro indenting-indent (stream sub-stream)
1684 ;; KLUDGE: bare magic number 60
1685 `(do ((i 0 (+ i 60))
1686 (indentation (indenting-stream-indentation ,stream)))
1687 ((>= i indentation))
1688 (%write-string
1689 #.(make-string 60 :initial-element #\Space)
1690 ,sub-stream
1692 (min 60 (- indentation i)))))
1694 ;;; INDENTING-OUT writes a character to an indenting stream.
1695 (defun indenting-out (stream char)
1696 (let ((sub-stream (indenting-stream-stream stream)))
1697 (write-char char sub-stream)
1698 (if (char= char #\newline)
1699 (indenting-indent stream sub-stream))))
1701 ;;; INDENTING-SOUT writes a string to an indenting stream.
1702 (defun indenting-sout (stream string start end)
1703 (declare (simple-string string) (fixnum start end))
1704 (do ((i start)
1705 (sub-stream (indenting-stream-stream stream)))
1706 ((= i end))
1707 (let ((newline (position #\newline string :start i :end end)))
1708 (cond (newline
1709 (%write-string string sub-stream i (1+ newline))
1710 (indenting-indent stream sub-stream)
1711 (setq i (+ newline 1)))
1713 (%write-string string sub-stream i end)
1714 (setq i end))))))
1716 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1717 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1718 ;;; the base stream minus the stream's indentation.
1719 (defun indenting-misc (stream operation &optional arg1 arg2)
1720 (let ((sub-stream (indenting-stream-stream stream)))
1721 (if (ansi-stream-p sub-stream)
1722 (let ((method (ansi-stream-misc sub-stream)))
1723 (case operation
1724 (:line-length
1725 (let ((line-length (funcall method sub-stream operation)))
1726 (if line-length
1727 (- line-length (indenting-stream-indentation stream)))))
1728 (:charpos
1729 (let ((charpos (funcall method sub-stream operation)))
1730 (if charpos
1731 (- charpos (indenting-stream-indentation stream)))))
1733 (funcall method sub-stream operation arg1 arg2))))
1734 ;; must be Gray streams FUNDAMENTAL-STREAM
1735 (case operation
1736 (:line-length
1737 (let ((line-length (stream-line-length sub-stream)))
1738 (if line-length
1739 (- line-length (indenting-stream-indentation stream)))))
1740 (:charpos
1741 (let ((charpos (stream-line-column sub-stream)))
1742 (if charpos
1743 (- charpos (indenting-stream-indentation stream)))))
1745 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1747 (declaim (maybe-inline read-char unread-char read-byte listen))
1749 ;;;; case frobbing streams, used by FORMAT ~(...~)
1751 (defstruct (case-frob-stream
1752 (:include ansi-stream
1753 (misc #'case-frob-misc))
1754 (:constructor %make-case-frob-stream (target out sout))
1755 (:copier nil))
1756 (target (missing-arg) :type stream))
1758 (defun make-case-frob-stream (target kind)
1759 #!+sb-doc
1760 "Return a stream that sends all output to the stream TARGET, but modifies
1761 the case of letters, depending on KIND, which should be one of:
1762 :UPCASE - convert to upper case.
1763 :DOWNCASE - convert to lower case.
1764 :CAPITALIZE - convert the first letter of words to upper case and the
1765 rest of the word to lower case.
1766 :CAPITALIZE-FIRST - convert the first letter of the first word to upper
1767 case and everything else to lower case."
1768 (declare (type stream target)
1769 (type (member :upcase :downcase :capitalize :capitalize-first)
1770 kind)
1771 (values stream))
1772 (if (case-frob-stream-p target)
1773 ;; If we are going to be writing to a stream that already does
1774 ;; case frobbing, why bother frobbing the case just so it can
1775 ;; frob it again?
1776 target
1777 (multiple-value-bind (out sout)
1778 (ecase kind
1779 (:upcase
1780 (values #'case-frob-upcase-out
1781 #'case-frob-upcase-sout))
1782 (:downcase
1783 (values #'case-frob-downcase-out
1784 #'case-frob-downcase-sout))
1785 (:capitalize
1786 (values #'case-frob-capitalize-out
1787 #'case-frob-capitalize-sout))
1788 (:capitalize-first
1789 (values #'case-frob-capitalize-first-out
1790 #'case-frob-capitalize-first-sout)))
1791 (%make-case-frob-stream target out sout))))
1793 (defun case-frob-misc (stream op &optional arg1 arg2)
1794 (declare (type case-frob-stream stream))
1795 (case op
1796 (:close
1797 (set-closed-flame stream))
1799 (let ((target (case-frob-stream-target stream)))
1800 (if (ansi-stream-p target)
1801 (funcall (ansi-stream-misc target) target op arg1 arg2)
1802 (stream-misc-dispatch target op arg1 arg2))))))
1804 (defun case-frob-upcase-out (stream char)
1805 (declare (type case-frob-stream stream)
1806 (type character char))
1807 (let ((target (case-frob-stream-target stream))
1808 (char (char-upcase char)))
1809 (if (ansi-stream-p target)
1810 (funcall (ansi-stream-out target) target char)
1811 (stream-write-char target char))))
1813 (defun case-frob-upcase-sout (stream str start end)
1814 (declare (type case-frob-stream stream)
1815 (type simple-string str)
1816 (type index start)
1817 (type (or index null) end))
1818 (let* ((target (case-frob-stream-target stream))
1819 (len (length str))
1820 (end (or end len))
1821 (string (if (and (zerop start) (= len end))
1822 (string-upcase str)
1823 (nstring-upcase (subseq str start end))))
1824 (string-len (- end start)))
1825 (if (ansi-stream-p target)
1826 (funcall (ansi-stream-sout target) target string 0 string-len)
1827 (stream-write-string target string 0 string-len))))
1829 (defun case-frob-downcase-out (stream char)
1830 (declare (type case-frob-stream stream)
1831 (type character char))
1832 (let ((target (case-frob-stream-target stream))
1833 (char (char-downcase char)))
1834 (if (ansi-stream-p target)
1835 (funcall (ansi-stream-out target) target char)
1836 (stream-write-char target char))))
1838 (defun case-frob-downcase-sout (stream str start end)
1839 (declare (type case-frob-stream stream)
1840 (type simple-string str)
1841 (type index start)
1842 (type (or index null) end))
1843 (let* ((target (case-frob-stream-target stream))
1844 (len (length str))
1845 (end (or end len))
1846 (string (if (and (zerop start) (= len end))
1847 (string-downcase str)
1848 (nstring-downcase (subseq str start end))))
1849 (string-len (- end start)))
1850 (if (ansi-stream-p target)
1851 (funcall (ansi-stream-sout target) target string 0 string-len)
1852 (stream-write-string target string 0 string-len))))
1854 (defun case-frob-capitalize-out (stream char)
1855 (declare (type case-frob-stream stream)
1856 (type character char))
1857 (let ((target (case-frob-stream-target stream)))
1858 (cond ((alphanumericp char)
1859 (let ((char (char-upcase char)))
1860 (if (ansi-stream-p target)
1861 (funcall (ansi-stream-out target) target char)
1862 (stream-write-char target char)))
1863 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1864 (setf (case-frob-stream-sout stream)
1865 #'case-frob-capitalize-aux-sout))
1867 (if (ansi-stream-p target)
1868 (funcall (ansi-stream-out target) target char)
1869 (stream-write-char target char))))))
1871 (defun case-frob-capitalize-sout (stream str start end)
1872 (declare (type case-frob-stream stream)
1873 (type simple-string str)
1874 (type index start)
1875 (type (or index null) end))
1876 (let* ((target (case-frob-stream-target stream))
1877 (str (subseq str start end))
1878 (len (length str))
1879 (inside-word nil))
1880 (dotimes (i len)
1881 (let ((char (schar str i)))
1882 (cond ((not (alphanumericp char))
1883 (setf inside-word nil))
1884 (inside-word
1885 (setf (schar str i) (char-downcase char)))
1887 (setf inside-word t)
1888 (setf (schar str i) (char-upcase char))))))
1889 (when inside-word
1890 (setf (case-frob-stream-out stream)
1891 #'case-frob-capitalize-aux-out)
1892 (setf (case-frob-stream-sout stream)
1893 #'case-frob-capitalize-aux-sout))
1894 (if (ansi-stream-p target)
1895 (funcall (ansi-stream-sout target) target str 0 len)
1896 (stream-write-string target str 0 len))))
1898 (defun case-frob-capitalize-aux-out (stream char)
1899 (declare (type case-frob-stream stream)
1900 (type character char))
1901 (let ((target (case-frob-stream-target stream)))
1902 (cond ((alphanumericp char)
1903 (let ((char (char-downcase char)))
1904 (if (ansi-stream-p target)
1905 (funcall (ansi-stream-out target) target char)
1906 (stream-write-char target char))))
1908 (if (ansi-stream-p target)
1909 (funcall (ansi-stream-out target) target char)
1910 (stream-write-char target char))
1911 (setf (case-frob-stream-out stream)
1912 #'case-frob-capitalize-out)
1913 (setf (case-frob-stream-sout stream)
1914 #'case-frob-capitalize-sout)))))
1916 (defun case-frob-capitalize-aux-sout (stream str start end)
1917 (declare (type case-frob-stream stream)
1918 (type simple-string str)
1919 (type index start)
1920 (type (or index null) end))
1921 (let* ((target (case-frob-stream-target stream))
1922 (str (subseq str start end))
1923 (len (length str))
1924 (inside-word t))
1925 (dotimes (i len)
1926 (let ((char (schar str i)))
1927 (cond ((not (alphanumericp char))
1928 (setf inside-word nil))
1929 (inside-word
1930 (setf (schar str i) (char-downcase char)))
1932 (setf inside-word t)
1933 (setf (schar str i) (char-upcase char))))))
1934 (unless inside-word
1935 (setf (case-frob-stream-out stream)
1936 #'case-frob-capitalize-out)
1937 (setf (case-frob-stream-sout stream)
1938 #'case-frob-capitalize-sout))
1939 (if (ansi-stream-p target)
1940 (funcall (ansi-stream-sout target) target str 0 len)
1941 (stream-write-string target str 0 len))))
1943 (defun case-frob-capitalize-first-out (stream char)
1944 (declare (type case-frob-stream stream)
1945 (type character char))
1946 (let ((target (case-frob-stream-target stream)))
1947 (cond ((alphanumericp char)
1948 (let ((char (char-upcase char)))
1949 (if (ansi-stream-p target)
1950 (funcall (ansi-stream-out target) target char)
1951 (stream-write-char target char)))
1952 (setf (case-frob-stream-out stream)
1953 #'case-frob-downcase-out)
1954 (setf (case-frob-stream-sout stream)
1955 #'case-frob-downcase-sout))
1957 (if (ansi-stream-p target)
1958 (funcall (ansi-stream-out target) target char)
1959 (stream-write-char target char))))))
1961 (defun case-frob-capitalize-first-sout (stream str start end)
1962 (declare (type case-frob-stream stream)
1963 (type simple-string str)
1964 (type index start)
1965 (type (or index null) end))
1966 (let* ((target (case-frob-stream-target stream))
1967 (str (subseq str start end))
1968 (len (length str)))
1969 (dotimes (i len)
1970 (let ((char (schar str i)))
1971 (when (alphanumericp char)
1972 (setf (schar str i) (char-upcase char))
1973 (do ((i (1+ i) (1+ i)))
1974 ((= i len))
1975 (setf (schar str i) (char-downcase (schar str i))))
1976 (setf (case-frob-stream-out stream)
1977 #'case-frob-downcase-out)
1978 (setf (case-frob-stream-sout stream)
1979 #'case-frob-downcase-sout)
1980 (return))))
1981 (if (ansi-stream-p target)
1982 (funcall (ansi-stream-sout target) target str 0 len)
1983 (stream-write-string target str 0 len))))
1985 ;;;; READ-SEQUENCE
1987 (defun read-sequence (seq stream &key (start 0) end)
1988 #!+sb-doc
1989 "Destructively modify SEQ by reading elements from STREAM.
1990 That part of SEQ bounded by START and END is destructively modified by
1991 copying successive elements into it from STREAM. If the end of file
1992 for STREAM is reached before copying all elements of the subsequence,
1993 then the extra elements near the end of sequence are not updated, and
1994 the index of the next element is returned."
1995 (declare (type sequence seq)
1996 (type stream stream)
1997 (type index start)
1998 (type sequence-end end)
1999 (values index))
2000 (if (ansi-stream-p stream)
2001 (ansi-stream-read-sequence seq stream start end)
2002 ;; must be Gray streams FUNDAMENTAL-STREAM
2003 (stream-read-sequence stream seq start end)))
2005 (declaim (inline compatible-vector-and-stream-element-types-p))
2006 (defun compatible-vector-and-stream-element-types-p (vector stream)
2007 (declare (type vector vector)
2008 (type ansi-stream stream))
2009 (or (and (typep vector '(simple-array (unsigned-byte 8) (*)))
2010 (subtypep (stream-element-type stream) '(unsigned-byte 8)))
2011 (and (typep vector '(simple-array (signed-byte 8) (*)))
2012 (subtypep (stream-element-type stream) '(signed-byte 8)))))
2014 (defun ansi-stream-read-sequence (seq stream start %end)
2015 (declare (type sequence seq)
2016 (type ansi-stream stream)
2017 (type index start)
2018 (type sequence-end %end)
2019 (values index))
2020 (let ((end (or %end (length seq))))
2021 (declare (type index end))
2022 (etypecase seq
2023 (list
2024 (let ((read-function
2025 (if (subtypep (stream-element-type stream) 'character)
2026 #'ansi-stream-read-char
2027 #'ansi-stream-read-byte)))
2028 (do ((rem (nthcdr start seq) (rest rem))
2029 (i start (1+ i)))
2030 ((or (endp rem) (>= i end)) i)
2031 (declare (type list rem)
2032 (type index i))
2033 (let ((el (funcall read-function stream nil :eof nil)))
2034 (when (eq el :eof)
2035 (return i))
2036 (setf (first rem) el)))))
2037 (vector
2038 (with-array-data ((data seq) (offset-start start) (offset-end end)
2039 :check-fill-pointer t)
2040 (cond ((compatible-vector-and-stream-element-types-p data stream)
2041 (let* ((numbytes (- end start))
2042 (bytes-read (read-n-bytes stream data offset-start
2043 numbytes nil)))
2044 (if (< bytes-read numbytes)
2045 (+ start bytes-read)
2046 end)))
2047 ((and (ansi-stream-cin-buffer stream)
2048 (typep seq 'simple-string))
2049 (ansi-stream-read-string-from-frc-buffer seq stream
2050 start %end))
2052 (let ((read-function
2053 (if (subtypep (stream-element-type stream) 'character)
2054 ;; If the stream-element-type is CHARACTER,
2055 ;; this might be a bivalent stream. If the
2056 ;; sequence is a specialized unsigned-byte
2057 ;; vector, try to read use binary IO. It'll
2058 ;; signal an error if stream is an pure
2059 ;; character stream.
2060 (if (subtypep (array-element-type data)
2061 'unsigned-byte)
2062 #'ansi-stream-read-byte
2063 #'ansi-stream-read-char)
2064 #'ansi-stream-read-byte)))
2065 (do ((i offset-start (1+ i)))
2066 ((>= i offset-end) end)
2067 (declare (type index i))
2068 (let ((el (funcall read-function stream nil :eof nil)))
2069 (when (eq el :eof)
2070 (return (+ start (- i offset-start))))
2071 (setf (aref data i) el)))))))))))
2073 (defun ansi-stream-read-string-from-frc-buffer (seq stream start %end)
2074 (declare (type simple-string seq)
2075 (type ansi-stream stream)
2076 (type index start)
2077 (type (or null index) %end))
2078 (let ((needed (- (or %end (length seq))
2079 start))
2080 (read 0))
2081 (prepare-for-fast-read-char stream
2082 (declare (ignore %frc-method%))
2083 (unless %frc-buffer%
2084 (return-from ansi-stream-read-string-from-frc-buffer nil))
2085 (labels ((refill-buffer ()
2086 (prog1
2087 (fast-read-char-refill stream nil nil)
2088 (setf %frc-index% (ansi-stream-in-index %frc-stream%))))
2089 (add-chunk ()
2090 (let* ((end (length %frc-buffer%))
2091 (len (min (- end %frc-index%)
2092 (- needed read))))
2093 (declare (type index end len read needed))
2094 (string-dispatch (simple-base-string
2095 (simple-array character (*)))
2097 (replace seq %frc-buffer%
2098 :start1 (+ start read)
2099 :end1 (+ start read len)
2100 :start2 %frc-index%
2101 :end2 (+ %frc-index% len)))
2102 (incf read len)
2103 (incf %frc-index% len)
2104 (when (or (eql needed read)
2105 (refill-buffer))
2106 (done-with-fast-read-char)
2107 (return-from ansi-stream-read-string-from-frc-buffer
2108 read)))))
2109 (declare (inline refill-buffer))
2110 (when (and (= %frc-index% +ansi-stream-in-buffer-length+)
2111 (refill-buffer))
2112 ;; EOF had been reached before we read anything
2113 ;; at all. Return the EOF value or signal the error.
2114 (done-with-fast-read-char)
2115 (return-from ansi-stream-read-string-from-frc-buffer 0))
2116 (loop (add-chunk))))))
2119 ;;;; WRITE-SEQUENCE
2121 (defun write-sequence (seq stream &key (start 0) (end nil))
2122 #!+sb-doc
2123 "Write the elements of SEQ bounded by START and END to STREAM."
2124 (declare (type sequence seq)
2125 (type stream stream)
2126 (type index start)
2127 (type sequence-end end)
2128 (values sequence))
2129 (if (ansi-stream-p stream)
2130 (ansi-stream-write-sequence seq stream start end)
2131 ;; must be Gray-streams FUNDAMENTAL-STREAM
2132 (stream-write-sequence stream seq start end)))
2134 (defun ansi-stream-write-sequence (seq stream start %end)
2135 (declare (type sequence seq)
2136 (type ansi-stream stream)
2137 (type index start)
2138 (type sequence-end %end)
2139 (values sequence))
2140 (let ((end (or %end (length seq))))
2141 (declare (type index end))
2142 (etypecase seq
2143 (list
2144 (let ((write-function
2145 (if (subtypep (stream-element-type stream) 'character)
2146 (ansi-stream-out stream)
2147 (ansi-stream-bout stream))))
2148 (do ((rem (nthcdr start seq) (rest rem))
2149 (i start (1+ i)))
2150 ((or (endp rem) (>= i end)))
2151 (declare (type list rem)
2152 (type index i))
2153 (funcall write-function stream (first rem)))))
2154 (string
2155 (%write-string seq stream start end))
2156 (vector
2157 (with-array-data ((data seq) (offset-start start) (offset-end end)
2158 :check-fill-pointer t)
2159 (labels
2160 ((output-seq-in-loop ()
2161 (let ((write-function
2162 (if (subtypep (stream-element-type stream) 'character)
2163 (lambda (stream object)
2164 ;; This might be a bivalent stream, so we need
2165 ;; to dispatch on a per-element basis, rather
2166 ;; than just based on the sequence or stream
2167 ;; element types.
2168 (if (characterp object)
2169 (funcall (ansi-stream-out stream)
2170 stream object)
2171 (funcall (ansi-stream-bout stream)
2172 stream object)))
2173 (ansi-stream-bout stream))))
2174 (do ((i offset-start (1+ i)))
2175 ((>= i offset-end))
2176 (declare (type index i))
2177 (funcall write-function stream (aref data i))))))
2178 (if (and (fd-stream-p stream)
2179 (compatible-vector-and-stream-element-types-p data stream))
2180 (buffer-output stream data offset-start offset-end)
2181 (output-seq-in-loop)))))))
2182 seq)
2184 ;;;; etc.