1 ;;;; os-independent stream functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
16 ;;; The initialization of these streams is performed by
17 ;;; STREAM-COLD-INIT-OR-RESET.
18 (defvar *terminal-io
* () #!+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
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
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
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
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 ;; STREAM-PATHNAME, STREAM-TRUENAME, STREAM-ALTNAME, STREAM-OWNER-PID
137 ;; and STREAM-AFTER-CLOSE are implement pieces of the protocol we use
138 ;; for stream internals in both FD-STREAMS and SB-SIMPLE-STREAMS. All
139 ;; of these are redefined as generic functions in SB-SIMPLE-STREAMS,
140 ;; and methods will be added to them there.
141 (defun stream-pathname (stream)
142 (fd-stream-pathname stream
))
144 (defun stream-truename (stream)
145 (let ((truename (fd-stream-truename stream
)))
148 ;; This is a flaw in our system: not all FD-STREAMs should be
149 ;; FILE-STREAMs, Unix propaganda notwithstanding.
150 (error 'type-error
:datum stream
:expected-type
'file-stream
))))
152 (defun (setf stream-truename
) (new-name stream
)
153 ;; Same KLUDGE as for ANSI-STREAM-PATHNAME.
154 (setf (fd-stream-truename stream
) new-name
))
156 (defun stream-altname (stream)
157 (fd-stream-altname stream
))
159 (defun (setf stream-altname
) (new-name stream
)
160 (setf (fd-stream-altname stream
) new-name
))
162 ;; FIXME: this needs to be exported for the user.
163 (defun stream-owner-pid (stream)
164 (fd-stream-owner-pid stream
))
166 (defun (setf stream-owner-pid
) (value stream
)
167 (setf (fd-stream-owner-pid stream
) value
))
169 (defun stream-after-close (stream)
170 (fd-stream-after-close stream
))
172 (defun (setf stream-after-close
) (value stream
)
173 (setf (fd-stream-after-close stream
) value
))
175 ;;;; file position and file length
176 (defun external-format-char-size (external-format)
177 (let ((ef-entry (find-external-format external-format
)))
178 (if (variable-width-external-format-p ef-entry
)
179 (bytes-for-char-fun ef-entry
)
180 (funcall (bytes-for-char-fun ef-entry
) #\x
))))
182 ;;; Call the MISC method with the :FILE-POSITION operation.
183 #!-sb-fluid
(declaim (inline ansi-stream-file-position
))
184 (defun ansi-stream-file-position (stream position
)
185 (declare (type stream stream
))
186 (declare (type (or index
(alien sb
!unix
:off-t
) (member nil
:start
:end
))
188 ;; FIXME: It woud be good to comment on the stuff that is done here...
189 ;; FIXME: This doesn't look interrupt safe.
192 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
193 (funcall (ansi-stream-misc stream
) stream
:file-position position
))
195 (let ((res (funcall (ansi-stream-misc stream
) stream
:file-position nil
)))
199 (- +ansi-stream-in-buffer-length
+
200 (ansi-stream-in-index stream
)))
202 (let ((char-size (if (fd-stream-p stream
)
203 (fd-stream-char-size stream
)
204 (external-format-char-size (stream-external-format stream
)))))
208 (loop with buffer
= (ansi-stream-cin-buffer stream
)
209 with start
= (ansi-stream-in-index stream
)
210 for i from start below
+ansi-stream-in-buffer-length
+
211 sum
(funcall char-size
(aref buffer i
))))
214 (- +ansi-stream-in-buffer-length
+
215 (ansi-stream-in-index stream
))))))))))))
217 (defun file-position (stream &optional position
)
218 (if (ansi-stream-p stream
)
219 (ansi-stream-file-position stream position
)
220 (stream-file-position stream position
)))
222 ;;; This is a literal translation of the ANSI glossary entry "stream
223 ;;; associated with a file".
225 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
226 ;;; file", and in particular stdin, stdout, and stderr are files, we
227 ;;; end up with this test being satisfied for weird things like
228 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
229 ;;; ANSI spec really had in mind, especially since this is used as a
230 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
231 ;;; probably thinking of something like what Unix calls block devices)
232 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
233 (defun stream-associated-with-file-p (x)
234 "Test for the ANSI concept \"stream associated with a file\"."
235 (or (typep x
'file-stream
)
236 (and (synonym-stream-p x
)
237 (stream-associated-with-file-p (symbol-value
238 (synonym-stream-symbol x
))))))
240 (defun stream-must-be-associated-with-file (stream)
241 (declare (type stream stream
))
242 (unless (stream-associated-with-file-p stream
)
243 (error 'simple-type-error
244 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
245 ;; this should be TYPE-ERROR. But what then can we use for
246 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
247 ;; private predicate function..) is ugly and confusing, but
248 ;; I can't see any other way. -- WHN 2001-04-14
250 :expected-type
'(satisfies stream-associated-with-file-p
)
252 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
253 :format-arguments
(list stream
))))
255 ;;; like FILE-POSITION, only using :FILE-LENGTH
256 (defun file-length (stream)
257 ;; FIXME: The following declaration uses yet undefined types, which
258 ;; cause cross-compiler hangup.
260 ;; (declare (type (or file-stream synonym-stream) stream))
262 ;; The description for FILE-LENGTH says that an error must be raised
263 ;; for streams not associated with files (which broadcast streams
264 ;; aren't according to the glossary). However, the behaviour of
265 ;; FILE-LENGTH for broadcast streams is explicitly described in the
266 ;; BROADCAST-STREAM entry.
267 (unless (typep stream
'broadcast-stream
)
268 (stream-must-be-associated-with-file stream
))
269 (funcall (ansi-stream-misc stream
) stream
:file-length
))
271 (defun file-string-length (stream object
)
272 (funcall (ansi-stream-misc stream
) stream
:file-string-length object
))
276 (defun ansi-stream-read-line-from-frc-buffer (stream eof-error-p eof-value
)
277 (prepare-for-fast-read-char stream
278 (declare (ignore %frc-method%
))
279 (let ((chunks-total-length 0)
281 (declare (type index chunks-total-length
)
283 (labels ((refill-buffer ()
285 (fast-read-char-refill stream nil nil
)
286 (setf %frc-index%
(ansi-stream-in-index %frc-stream%
))))
288 (position #\Newline
(the (simple-array character
(*))
292 (make-and-return-result-string (pos)
293 (let* ((len (+ (- (or pos %frc-index%
)
295 chunks-total-length
))
296 (res (make-string len
))
298 (declare (type index start
))
300 (dolist (chunk (nreverse chunks
))
301 (declare (type (simple-array character
) chunk
))
302 (replace res chunk
:start1 start
)
303 (incf start
(length chunk
))))
305 (replace res %frc-buffer%
307 :start2 %frc-index%
:end2 pos
)
308 (setf %frc-index%
(1+ pos
)))
309 (done-with-fast-read-char)
310 (return-from ansi-stream-read-line-from-frc-buffer res
)))
312 (let* ((end (length %frc-buffer%
))
313 (len (- end %frc-index%
))
314 (chunk (make-string len
)))
315 (replace chunk %frc-buffer%
:start2 %frc-index%
:end2 end
)
317 (incf chunks-total-length len
)
318 (when (refill-buffer)
319 (make-and-return-result-string nil
)))))
320 (declare (inline make-and-return-result-string
322 (when (and (= %frc-index%
+ansi-stream-in-buffer-length
+)
324 ;; EOF had been reached before we read anything
325 ;; at all. Return the EOF value or signal the error.
326 (done-with-fast-read-char)
327 (return-from ansi-stream-read-line-from-frc-buffer
328 (values (eof-or-lose stream eof-error-p eof-value
) t
)))
330 (let ((pos (newline-position)))
332 (make-and-return-result-string pos
)
335 #!-sb-fluid
(declaim (inline ansi-stream-read-line
))
336 (defun ansi-stream-read-line (stream eof-error-p eof-value recursive-p
)
337 (declare (ignore recursive-p
))
338 (if (ansi-stream-cin-buffer stream
)
339 ;; Stream has a fast-read-char buffer. Copy large chunks directly
340 ;; out of the buffer.
341 (ansi-stream-read-line-from-frc-buffer stream eof-error-p eof-value
)
342 ;; Slow path, character by character.
343 (prepare-for-fast-read-char stream
344 (let ((res (make-string 80))
348 (let ((ch (fast-read-char nil nil
)))
350 (when (char= ch
#\newline
)
351 (done-with-fast-read-char)
352 (return (values (%shrink-vector res index
) nil
)))
355 (let ((new (make-string len
)))
358 (setf (schar res index
) ch
)
361 (done-with-fast-read-char)
362 (return (values (eof-or-lose stream
366 ;; Since FAST-READ-CHAR already hit the eof char, we
367 ;; shouldn't do another READ-CHAR.
369 (done-with-fast-read-char)
370 (return (values (%shrink-vector res index
) t
))))))))))
372 (defun read-line (&optional
(stream *standard-input
*) (eof-error-p t
) eof-value
374 (let ((stream (in-synonym-of stream
)))
375 (if (ansi-stream-p stream
)
376 (ansi-stream-read-line stream eof-error-p eof-value recursive-p
)
377 ;; must be Gray streams FUNDAMENTAL-STREAM
378 (multiple-value-bind (string eof
) (stream-read-line stream
)
379 (if (and eof
(zerop (length string
)))
380 (values (eof-or-lose stream eof-error-p eof-value
) t
)
381 (values string eof
))))))
383 ;;; We proclaim them INLINE here, then proclaim them NOTINLINE later on,
384 ;;; so, except in this file, they are not inline by default, but they can be.
385 #!-sb-fluid
(declaim (inline read-char unread-char read-byte listen
))
387 #!-sb-fluid
(declaim (inline ansi-stream-read-char
))
388 (defun ansi-stream-read-char (stream eof-error-p eof-value recursive-p
)
389 (declare (ignore recursive-p
))
390 (prepare-for-fast-read-char stream
392 (fast-read-char eof-error-p eof-value
)
393 (done-with-fast-read-char))))
395 (defun read-char (&optional
(stream *standard-input
*)
399 (let ((stream (in-synonym-of stream
)))
400 (if (ansi-stream-p stream
)
401 (ansi-stream-read-char stream eof-error-p eof-value recursive-p
)
402 ;; must be Gray streams FUNDAMENTAL-STREAM
403 (let ((char (stream-read-char stream
)))
405 (eof-or-lose stream eof-error-p eof-value
)
408 #!-sb-fluid
(declaim (inline ansi-stream-unread-char
))
409 (defun ansi-stream-unread-char (character stream
)
410 (let ((index (1- (ansi-stream-in-index stream
)))
411 (buffer (ansi-stream-cin-buffer stream
)))
412 (declare (fixnum index
))
413 (when (minusp index
) (error "nothing to unread"))
415 (setf (aref buffer index
) character
)
416 (setf (ansi-stream-in-index stream
) index
))
418 (funcall (ansi-stream-misc stream
) stream
419 :unread character
)))))
421 (defun unread-char (character &optional
(stream *standard-input
*))
422 (let ((stream (in-synonym-of stream
)))
423 (if (ansi-stream-p stream
)
424 (ansi-stream-unread-char character stream
)
425 ;; must be Gray streams FUNDAMENTAL-STREAM
426 (stream-unread-char stream character
)))
429 #!-sb-fluid
(declaim (inline ansi-stream-listen
))
430 (defun ansi-stream-listen (stream)
431 (or (/= (the fixnum
(ansi-stream-in-index stream
))
432 +ansi-stream-in-buffer-length
+)
433 ;; Handle :EOF return from misc methods specially
434 (let ((result (funcall (ansi-stream-misc stream
) stream
:listen
)))
439 (defun listen (&optional
(stream *standard-input
*))
440 (let ((stream (in-synonym-of stream
)))
441 (if (ansi-stream-p stream
)
442 (ansi-stream-listen stream
)
443 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
444 (stream-listen stream
))))
446 #!-sb-fluid
(declaim (inline ansi-stream-read-char-no-hang
))
447 (defun ansi-stream-read-char-no-hang (stream eof-error-p eof-value recursive-p
)
448 (if (funcall (ansi-stream-misc stream
) stream
:listen
)
449 ;; On T or :EOF get READ-CHAR to do the work.
450 (ansi-stream-read-char stream eof-error-p eof-value recursive-p
)
453 (defun read-char-no-hang (&optional
(stream *standard-input
*)
457 (let ((stream (in-synonym-of stream
)))
458 (if (ansi-stream-p stream
)
459 (ansi-stream-read-char-no-hang stream eof-error-p eof-value
461 ;; must be Gray streams FUNDAMENTAL-STREAM
462 (let ((char (stream-read-char-no-hang stream
)))
464 (eof-or-lose stream eof-error-p eof-value
)
467 #!-sb-fluid
(declaim (inline ansi-stream-clear-input
))
468 (defun ansi-stream-clear-input (stream)
469 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
470 (funcall (ansi-stream-misc stream
) stream
:clear-input
))
472 (defun clear-input (&optional
(stream *standard-input
*))
473 (let ((stream (in-synonym-of stream
)))
474 (if (ansi-stream-p stream
)
475 (ansi-stream-clear-input stream
)
476 ;; must be Gray streams FUNDAMENTAL-STREAM
477 (stream-clear-input stream
)))
480 #!-sb-fluid
(declaim (inline ansi-stream-read-byte
))
481 (defun ansi-stream-read-byte (stream eof-error-p eof-value recursive-p
)
482 ;; Why the "recursive-p" parameter? a-s-r-b is funcall'ed from
483 ;; a-s-read-sequence and needs a lambda list that's congruent with
484 ;; that of a-s-read-char
485 (declare (ignore recursive-p
))
486 (prepare-for-fast-read-byte stream
488 (fast-read-byte eof-error-p eof-value t
)
489 (done-with-fast-read-byte))))
491 (defun read-byte (stream &optional
(eof-error-p t
) eof-value
)
492 (if (ansi-stream-p stream
)
493 (ansi-stream-read-byte stream eof-error-p eof-value nil
)
494 ;; must be Gray streams FUNDAMENTAL-STREAM
495 (let ((char (stream-read-byte stream
)))
497 (eof-or-lose stream eof-error-p eof-value
)
500 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
501 ;;; number of bytes read.
503 ;;; Note: CMU CL's version of this had a special interpretation of
504 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
505 ;;; case, CMU CL's version would return as soon as any data became
506 ;;; available.) This could be useful behavior for things like pipes in
507 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
508 ;;; If we ever need it, it could be added later as a new variant N-BIN
509 ;;; method (perhaps N-BIN-ASAP?) or something.
510 #!-sb-fluid
(declaim (inline read-n-bytes
))
511 (defun read-n-bytes (stream buffer start numbytes
&optional
(eof-error-p t
))
512 (if (ansi-stream-p stream
)
513 (ansi-stream-read-n-bytes stream buffer start numbytes eof-error-p
)
514 ;; We don't need to worry about element-type size here is that
515 ;; callers are supposed to have checked everything is kosher.
516 (let* ((end (+ start numbytes
))
517 (read-end (stream-read-sequence stream buffer start end
)))
518 (eof-or-lose stream
(and eof-error-p
(< read-end end
)) (- read-end start
)))))
520 (defun ansi-stream-read-n-bytes (stream buffer start numbytes eof-error-p
)
521 (declare (type ansi-stream stream
)
522 (type index numbytes start
)
523 (type (or (simple-array * (*)) system-area-pointer
) buffer
))
524 (let* ((stream (in-synonym-of stream ansi-stream
))
525 (in-buffer (ansi-stream-in-buffer stream
))
526 (index (ansi-stream-in-index stream
))
527 (num-buffered (- +ansi-stream-in-buffer-length
+ index
)))
528 (declare (fixnum index num-buffered
))
531 (funcall (ansi-stream-n-bin stream
)
537 ((<= numbytes num-buffered
)
539 (let ((copy-function (typecase buffer
540 ((simple-array * (*)) #'ub8-bash-copy
)
541 (system-area-pointer #'copy-ub8-to-system-area
))))
542 (funcall copy-function in-buffer index buffer start numbytes
))
543 (%byte-blt in-buffer index
544 buffer start
(+ start numbytes
))
545 (setf (ansi-stream-in-index stream
) (+ index numbytes
))
548 (let ((end (+ start num-buffered
)))
550 (let ((copy-function (typecase buffer
551 ((simple-array * (*)) #'ub8-bash-copy
)
552 (system-area-pointer #'copy-ub8-to-system-area
))))
553 (funcall copy-function in-buffer index buffer start num-buffered
))
554 (%byte-blt in-buffer index buffer start end
)
555 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
556 (+ (funcall (ansi-stream-n-bin stream
)
560 (- numbytes num-buffered
)
564 ;;; the amount of space we leave at the start of the in-buffer for
567 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
568 (defconstant +ansi-stream-in-buffer-extra
+
569 4) ; FIXME: should be symbolic constant
571 ;;; This function is called by the FAST-READ-CHAR expansion to refill
572 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
573 ;;; and hence must be an N-BIN method. It's also called by other stream
574 ;;; functions which directly peek into the frc buffer.
575 (defun fast-read-char-refill (stream eof-error-p eof-value
)
576 (let* ((ibuf (ansi-stream-cin-buffer stream
))
577 (count (funcall (ansi-stream-n-bin stream
)
580 +ansi-stream-in-buffer-extra
+
581 (- +ansi-stream-in-buffer-length
+
582 +ansi-stream-in-buffer-extra
+)
584 (start (- +ansi-stream-in-buffer-length
+ count
)))
585 (declare (type index start count
))
587 ;; An empty count does not necessarily mean that we reached
588 ;; the EOF, it's also possible that it's e.g. due to a
589 ;; invalid octet sequence in a multibyte stream. To handle
590 ;; the resyncing case correctly we need to call the
591 ;; single-character reading function and check whether an
592 ;; EOF was really reached. If not, we can just fill the
593 ;; buffer by one character, and hope that the next refill
594 ;; will not need to resync.
595 (let* ((value (funcall (ansi-stream-in stream
) stream nil
:eof
))
596 (index (1- +ansi-stream-in-buffer-length
+)))
599 ;; Mark buffer as empty.
600 (setf (ansi-stream-in-index stream
)
601 +ansi-stream-in-buffer-length
+)
602 ;; EOF. Redo the read, this time with the real eof parameters.
603 (values t
(funcall (ansi-stream-in stream
)
604 stream eof-error-p eof-value
)))
606 (setf (aref ibuf index
) value
)
607 (values nil
(setf (ansi-stream-in-index stream
) index
))))))
609 (when (/= start
+ansi-stream-in-buffer-extra
+)
610 (#.
(let* ((n-character-array-bits
613 sb
!vm
:*specialized-array-element-type-properties
*
614 :key
#'sb
!vm
:saetp-specifier
)))
615 (bash-function (intern (format nil
"UB~D-BASH-COPY" n-character-array-bits
)
616 (find-package "SB!KERNEL"))))
618 ibuf
+ansi-stream-in-buffer-extra
+
622 (setf (ansi-stream-in-index stream
) start
))))))
624 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
625 ;;; leave room for unreading.
626 (defun fast-read-byte-refill (stream eof-error-p eof-value
)
627 (let* ((ibuf (ansi-stream-in-buffer stream
))
628 (count (funcall (ansi-stream-n-bin stream
) stream
629 ibuf
0 +ansi-stream-in-buffer-length
+
631 (start (- +ansi-stream-in-buffer-length
+ count
)))
632 (declare (type index start count
))
634 (setf (ansi-stream-in-index stream
) +ansi-stream-in-buffer-length
+)
635 (funcall (ansi-stream-bin stream
) stream eof-error-p eof-value
))
637 (unless (zerop start
)
638 (ub8-bash-copy ibuf
0
641 (setf (ansi-stream-in-index stream
) (1+ start
))
642 (aref ibuf start
)))))
646 (defun write-char (character &optional
(stream *standard-output
*))
647 (with-out-stream stream
(ansi-stream-out character
)
648 (stream-write-char character
))
651 (defun terpri (&optional
(stream *standard-output
*))
652 (with-out-stream stream
(ansi-stream-out #\newline
) (stream-terpri))
655 #!-sb-fluid
(declaim (inline ansi-stream-fresh-line
))
656 (defun ansi-stream-fresh-line (stream)
657 (when (/= (or (charpos stream
) 1) 0)
658 (funcall (ansi-stream-out stream
) stream
#\newline
)
661 (defun fresh-line (&optional
(stream *standard-output
*))
662 (let ((stream (out-synonym-of stream
)))
663 (if (ansi-stream-p stream
)
664 (ansi-stream-fresh-line stream
)
665 ;; must be Gray streams FUNDAMENTAL-STREAM
666 (stream-fresh-line stream
))))
668 (defun write-string (string &optional
(stream *standard-output
*)
670 (declare (type string string
))
671 ;; Note that even though you might expect, based on the behavior of
672 ;; things like AREF, that the correct upper bound here is
673 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
674 ;; "bounding index" and "length" indicate that in this case (i.e.
675 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
676 ;; (LENGTH STRING) is the required upper bound. A foolish
677 ;; consistency is the hobgoblin of lesser languages..
678 (%write-string string stream start
(%check-vector-sequence-bounds
682 #!-sb-fluid
(declaim (inline ansi-stream-write-string
))
683 (defun ansi-stream-write-string (string stream start end
)
684 (declare (type string string
))
685 (declare (type ansi-stream stream
))
686 (declare (type index start end
))
687 (with-array-data ((data string
) (offset-start start
)
689 :check-fill-pointer t
)
690 (funcall (ansi-stream-sout stream
)
691 stream data offset-start offset-end
))
694 (defun %write-string
(string stream start end
)
695 (declare (type string string
))
696 (declare (type stream-designator stream
))
697 (declare (type index start end
))
698 (let ((stream (out-synonym-of stream
)))
699 (if(ansi-stream-p stream
)
700 (ansi-stream-write-string string stream start end
)
701 ;; must be Gray streams FUNDAMENTAL-STREAM
702 (stream-write-string stream string start end
))))
704 ;;; A wrapper function for all those (MACROLET OUT-FUN) definitions,
705 ;;; which cannot deal with keyword arguments.
706 (declaim (inline write-string-no-key
))
707 (defun write-string-no-key (string stream start end
)
708 (write-string string stream
:start start
:end end
))
710 (defun write-line (string &optional
(stream *standard-output
*)
712 (declare (type string string
))
713 ;; FIXME: Why is there this difference between the treatments of the
714 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
715 (let ((defaulted-stream (out-synonym-of stream
)))
716 (%write-string string defaulted-stream start
(%check-vector-sequence-bounds
718 (write-char #\newline defaulted-stream
))
721 (defun charpos (&optional
(stream *standard-output
*))
722 (with-out-stream stream
(ansi-stream-misc :charpos
) (stream-line-column)))
724 (defun line-length (&optional
(stream *standard-output
*))
725 (with-out-stream stream
(ansi-stream-misc :line-length
)
726 (stream-line-length)))
728 (defun finish-output (&optional
(stream *standard-output
*))
729 (with-out-stream stream
(ansi-stream-misc :finish-output
)
730 (stream-finish-output))
733 (defun force-output (&optional
(stream *standard-output
*))
734 (with-out-stream stream
(ansi-stream-misc :force-output
)
735 (stream-force-output))
738 (defun clear-output (&optional
(stream *standard-output
*))
739 (with-out-stream stream
(ansi-stream-misc :clear-output
)
740 (stream-force-output))
743 (defun write-byte (integer stream
)
744 (with-out-stream/no-synonym stream
(ansi-stream-bout integer
)
745 (stream-write-byte integer
))
749 ;;; (These were inline throughout this file, but that's not appropriate
750 ;;; globally. And we must not inline them in the rest of this file if
751 ;;; dispatch to gray or simple streams is to work, since both redefine
752 ;;; these functions later.)
753 (declaim (notinline read-char unread-char read-byte listen
))
755 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
756 ;;; streams to handle the misc routines and dispatch to the
757 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
758 (defun stream-misc-dispatch (stream operation
&optional arg1 arg2
)
759 (declare (type stream stream
) (ignore arg2
))
762 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
763 (let ((char (read-char-no-hang stream nil
:eof
)))
764 (when (characterp char
)
765 (unread-char char stream
))
768 (unread-char arg1 stream
))
772 (clear-input stream
))
774 (force-output stream
))
776 (finish-output stream
))
778 (stream-element-type stream
))
779 (:stream-external-format
780 (stream-external-format stream
))
782 (interactive-stream-p stream
))
784 (line-length stream
))
788 (file-length stream
))
790 (file-string-length stream arg1
))
792 (file-position stream arg1
))))
794 ;;;; broadcast streams
796 (defstruct (broadcast-stream (:include ansi-stream
797 (out #'broadcast-out
)
798 (bout #'broadcast-bout
)
799 (sout #'broadcast-sout
)
800 (misc #'broadcast-misc
))
801 (:constructor %make-broadcast-stream
804 ;; a list of all the streams we broadcast to
805 (streams () :type list
:read-only t
))
807 (defun make-broadcast-stream (&rest streams
)
808 (dolist (stream streams
)
809 (unless (output-stream-p stream
)
812 :expected-type
'(satisfies output-stream-p
))))
813 (apply #'%make-broadcast-stream streams
))
815 (macrolet ((out-fun (name fun
&rest args
)
816 `(defun ,name
(stream ,@args
)
817 (dolist (stream (broadcast-stream-streams stream
))
818 (,fun
,(car args
) stream
,@(cdr args
))))))
819 (out-fun broadcast-out write-char char
)
820 (out-fun broadcast-bout write-byte byte
)
821 (out-fun broadcast-sout write-string-no-key string start end
))
823 (defun broadcast-misc (stream operation
&optional arg1 arg2
)
824 (let ((streams (broadcast-stream-streams stream
)))
826 ;; FIXME: This may not be the best place to note this, but I
827 ;; think the :CHARPOS protocol needs revision. Firstly, I think
828 ;; this is the last place where a NULL return value was possible
829 ;; (before adjusting it to be 0), so a bunch of conditionals IF
830 ;; CHARPOS can be removed; secondly, it is my belief that
831 ;; FD-STREAMS, when running FILE-POSITION, do not update the
832 ;; CHARPOS, and consequently there will be much wrongness.
834 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
835 ;; is it testing the :charpos of an input stream?
837 ;; -- CSR, 2004-02-04
839 (dolist (stream streams
0)
840 (let ((charpos (charpos stream
)))
841 (if charpos
(return charpos
)))))
844 (dolist (stream streams min
)
845 (let ((res (line-length stream
)))
846 (when res
(setq min
(if min
(min res min
) res
)))))))
848 #+nil
; old, arguably more logical, version
850 (dolist (stream streams
(if (> (length res
) 1) `(and ,@res
) t
))
851 (pushnew (stream-element-type stream
) res
:test
#'equal
)))
852 ;; ANSI-specified version (under System Class BROADCAST-STREAM)
854 (do ((streams streams
(cdr streams
)))
856 (when (null (cdr streams
))
857 (setq res
(stream-element-type (car streams
)))))))
859 (let ((res :default
))
860 (dolist (stream streams res
)
861 (setq res
(stream-external-format stream
)))))
863 (let ((last (last streams
)))
865 (file-length (car last
))
869 (let ((res (or (eql arg1
:start
) (eql arg1
0))))
870 (dolist (stream streams res
)
871 (setq res
(file-position stream arg1
))))
873 (dolist (stream streams res
)
874 (setq res
(file-position stream
))))))
877 (dolist (stream streams res
)
878 (setq res
(file-string-length stream arg1
)))))
880 (set-closed-flame stream
))
883 (dolist (stream streams res
)
885 (if (ansi-stream-p stream
)
886 (funcall (ansi-stream-misc stream
) stream operation
888 (stream-misc-dispatch stream operation arg1 arg2
)))))))))
892 (defstruct (synonym-stream (:include ansi-stream
895 (n-bin #'synonym-n-bin
)
897 (bout #'synonym-bout
)
898 (sout #'synonym-sout
)
899 (misc #'synonym-misc
))
900 (:constructor make-synonym-stream
(symbol))
902 ;; This is the symbol, the value of which is the stream we are synonym to.
903 (symbol nil
:type symbol
:read-only t
))
904 (def!method print-object
((x synonym-stream
) stream
)
905 (print-unreadable-object (x stream
:type t
:identity t
)
906 (format stream
":SYMBOL ~S" (synonym-stream-symbol x
))))
908 ;;; The output simple output methods just call the corresponding
909 ;;; function on the synonymed stream.
910 (macrolet ((out-fun (name fun
&rest args
)
911 `(defun ,name
(stream ,@args
)
912 (declare (optimize (safety 1)))
913 (let ((syn (symbol-value (synonym-stream-symbol stream
))))
914 (,fun
,(car args
) syn
,@(cdr args
))))))
915 (out-fun synonym-out write-char ch
)
916 (out-fun synonym-bout write-byte n
)
917 (out-fun synonym-sout write-string-no-key string start end
))
919 ;;; For the input methods, we just call the corresponding function on the
920 ;;; synonymed stream. These functions deal with getting input out of
921 ;;; the In-Buffer if there is any.
922 (macrolet ((in-fun (name fun
&rest args
)
923 `(defun ,name
(stream ,@args
)
924 (declare (optimize (safety 1)))
925 (,fun
(symbol-value (synonym-stream-symbol stream
))
927 (in-fun synonym-in read-char eof-error-p eof-value
)
928 (in-fun synonym-bin read-byte eof-error-p eof-value
)
929 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p
))
931 (defun synonym-misc (stream operation
&optional arg1 arg2
)
932 (declare (optimize (safety 1)))
933 (let ((syn (symbol-value (synonym-stream-symbol stream
))))
934 (if (ansi-stream-p syn
)
935 ;; We have to special-case some operations which interact with
936 ;; the in-buffer of the wrapped stream, since just calling
937 ;; ANSI-STREAM-MISC on them
939 (:listen
(or (/= (the fixnum
(ansi-stream-in-index syn
))
940 +ansi-stream-in-buffer-length
+)
941 (funcall (ansi-stream-misc syn
) syn
:listen
)))
942 (:clear-input
(clear-input syn
))
943 (:unread
(unread-char arg1 syn
))
945 (funcall (ansi-stream-misc syn
) syn operation arg1 arg2
)))
946 (stream-misc-dispatch syn operation arg1 arg2
))))
950 (defstruct (two-way-stream
951 (:include ansi-stream
954 (n-bin #'two-way-n-bin
)
956 (bout #'two-way-bout
)
957 (sout #'two-way-sout
)
958 (misc #'two-way-misc
))
959 (:constructor %make-two-way-stream
(input-stream output-stream
))
961 (input-stream (missing-arg) :type stream
:read-only t
)
962 (output-stream (missing-arg) :type stream
:read-only t
))
963 (defprinter (two-way-stream) input-stream output-stream
)
965 (defun make-two-way-stream (input-stream output-stream
)
967 "Return a bidirectional stream which gets its input from INPUT-STREAM and
968 sends its output to OUTPUT-STREAM."
969 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
970 ;; should be encapsulated in a function, and used here and most of
971 ;; the other places that SYNONYM-STREAM-P appears.
972 (unless (output-stream-p output-stream
)
975 :expected-type
'(satisfies output-stream-p
)))
976 (unless (input-stream-p input-stream
)
979 :expected-type
'(satisfies input-stream-p
)))
980 (funcall #'%make-two-way-stream input-stream output-stream
))
982 (macrolet ((out-fun (name fun
&rest args
)
983 `(defun ,name
(stream ,@args
)
984 (let ((syn (two-way-stream-output-stream stream
)))
985 (,fun
,(car args
) syn
,@(cdr args
))))))
986 (out-fun two-way-out write-char ch
)
987 (out-fun two-way-bout write-byte n
)
988 (out-fun two-way-sout write-string-no-key string start end
))
990 (macrolet ((in-fun (name fun
&rest args
)
991 `(defun ,name
(stream ,@args
)
992 (force-output (two-way-stream-output-stream stream
))
993 (,fun
(two-way-stream-input-stream stream
) ,@args
))))
994 (in-fun two-way-in read-char eof-error-p eof-value
)
995 (in-fun two-way-bin read-byte eof-error-p eof-value
)
996 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p
))
998 (defun two-way-misc (stream operation
&optional arg1 arg2
)
999 (let* ((in (two-way-stream-input-stream stream
))
1000 (out (two-way-stream-output-stream stream
))
1001 (in-ansi-stream-p (ansi-stream-p in
))
1002 (out-ansi-stream-p (ansi-stream-p out
)))
1005 (if in-ansi-stream-p
1006 (or (/= (the fixnum
(ansi-stream-in-index in
))
1007 +ansi-stream-in-buffer-length
+)
1008 (funcall (ansi-stream-misc in
) in
:listen
))
1010 ((:finish-output
:force-output
:clear-output
)
1011 (if out-ansi-stream-p
1012 (funcall (ansi-stream-misc out
) out operation arg1 arg2
)
1013 (stream-misc-dispatch out operation arg1 arg2
)))
1014 (:clear-input
(clear-input in
))
1015 (:unread
(unread-char arg1 in
))
1017 (let ((in-type (stream-element-type in
))
1018 (out-type (stream-element-type out
)))
1019 (if (equal in-type out-type
)
1020 in-type
`(and ,in-type
,out-type
))))
1022 (set-closed-flame stream
))
1024 (or (if in-ansi-stream-p
1025 (funcall (ansi-stream-misc in
) in operation arg1 arg2
)
1026 (stream-misc-dispatch in operation arg1 arg2
))
1027 (if out-ansi-stream-p
1028 (funcall (ansi-stream-misc out
) out operation arg1 arg2
)
1029 (stream-misc-dispatch out operation arg1 arg2
)))))))
1031 ;;;; concatenated streams
1033 (defstruct (concatenated-stream
1034 (:include ansi-stream
1035 (in #'concatenated-in
)
1036 (bin #'concatenated-bin
)
1037 (n-bin #'concatenated-n-bin
)
1038 (misc #'concatenated-misc
))
1039 (:constructor %make-concatenated-stream
(&rest streams
))
1041 ;; The car of this is the substream we are reading from now.
1042 (streams nil
:type list
))
1043 (def!method print-object
((x concatenated-stream
) stream
)
1044 (print-unreadable-object (x stream
:type t
:identity t
)
1047 (concatenated-stream-streams x
))))
1049 (defun make-concatenated-stream (&rest streams
)
1051 "Return a stream which takes its input from each of the streams in turn,
1052 going on to the next at EOF."
1053 (dolist (stream streams
)
1054 (unless (input-stream-p stream
)
1057 :expected-type
'(satisfies input-stream-p
))))
1058 (apply #'%make-concatenated-stream streams
))
1060 (macrolet ((in-fun (name fun
)
1061 `(defun ,name
(stream eof-error-p eof-value
)
1062 (do ((streams (concatenated-stream-streams stream
)
1065 (eof-or-lose stream eof-error-p eof-value
))
1066 (let* ((stream (car streams
))
1067 (result (,fun stream nil nil
)))
1068 (when result
(return result
)))
1069 (pop (concatenated-stream-streams stream
))))))
1070 (in-fun concatenated-in read-char
)
1071 (in-fun concatenated-bin read-byte
))
1073 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp
)
1074 (do ((streams (concatenated-stream-streams stream
) (cdr streams
))
1075 (current-start start
)
1076 (remaining-bytes numbytes
))
1079 (error 'end-of-file
:stream stream
)
1080 (- numbytes remaining-bytes
)))
1081 (let* ((stream (car streams
))
1082 (bytes-read (read-n-bytes stream buffer current-start
1083 remaining-bytes nil
)))
1084 (incf current-start bytes-read
)
1085 (decf remaining-bytes bytes-read
)
1086 (when (zerop remaining-bytes
) (return numbytes
)))
1087 (setf (concatenated-stream-streams stream
) (cdr streams
))))
1089 (defun concatenated-misc (stream operation
&optional arg1 arg2
)
1090 (let* ((left (concatenated-stream-streams stream
))
1091 (current (car left
)))
1095 (return-from concatenated-misc
:eof
))
1097 (let ((stuff (if (ansi-stream-p current
)
1098 (funcall (ansi-stream-misc current
) current
1100 (stream-misc-dispatch current
:listen
))))
1101 (cond ((eq stuff
:eof
)
1102 ;; Advance STREAMS, and try again.
1103 (pop (concatenated-stream-streams stream
))
1105 (car (concatenated-stream-streams stream
)))
1107 ;; No further streams. EOF.
1110 ;; Stuff's available.
1113 ;; Nothing is available yet.
1115 (:clear-input
(when left
(clear-input current
)))
1116 (:unread
(when left
(unread-char arg1 current
)))
1118 (set-closed-flame stream
))
1121 (if (ansi-stream-p current
)
1122 (funcall (ansi-stream-misc current
) current operation arg1 arg2
)
1123 (stream-misc-dispatch current operation arg1 arg2
)))))))
1127 (defstruct (echo-stream
1128 (:include two-way-stream
1132 (n-bin #'echo-n-bin
))
1133 (:constructor %make-echo-stream
(input-stream output-stream
))
1136 (def!method print-object
((x echo-stream
) stream
)
1137 (print-unreadable-object (x stream
:type t
:identity t
)
1139 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1140 (two-way-stream-input-stream x
)
1141 (two-way-stream-output-stream x
))))
1143 (defun make-echo-stream (input-stream output-stream
)
1145 "Return a bidirectional stream which gets its input from INPUT-STREAM and
1146 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
1148 (unless (output-stream-p output-stream
)
1150 :datum output-stream
1151 :expected-type
'(satisfies output-stream-p
)))
1152 (unless (input-stream-p input-stream
)
1155 :expected-type
'(satisfies input-stream-p
)))
1156 (funcall #'%make-echo-stream input-stream output-stream
))
1158 (macrolet ((in-fun (name in-fun out-fun
&rest args
)
1159 `(defun ,name
(stream ,@args
)
1160 (or (pop (echo-stream-unread-stuff stream
))
1161 (let* ((in (echo-stream-input-stream stream
))
1162 (out (echo-stream-output-stream stream
))
1163 (result (if eof-error-p
1165 (,in-fun in nil in
))))
1167 ((eql result in
) eof-value
)
1168 (t (,out-fun result out
) result
)))))))
1169 (in-fun echo-in read-char write-char eof-error-p eof-value
)
1170 (in-fun echo-bin read-byte write-byte eof-error-p eof-value
))
1172 (defun echo-n-bin (stream buffer start numbytes eof-error-p
)
1173 (let ((new-start start
)
1176 (let ((thing (pop (echo-stream-unread-stuff stream
))))
1179 (setf (aref buffer new-start
) thing
)
1182 (when (= read numbytes
)
1183 (return-from echo-n-bin numbytes
)))
1185 (let ((bytes-read (read-n-bytes (echo-stream-input-stream stream
) buffer
1186 new-start
(- numbytes read
) nil
)))
1189 (write-sequence buffer
(echo-stream-output-stream stream
)
1190 :start new-start
:end
(+ new-start bytes-read
))
1191 (+ bytes-read read
))
1192 ((> numbytes
(+ read bytes-read
))
1193 (write-sequence buffer
(echo-stream-output-stream stream
)
1194 :start new-start
:end
(+ new-start bytes-read
))
1195 (error 'end-of-file
:stream stream
))
1197 (write-sequence buffer
(echo-stream-output-stream stream
)
1198 :start new-start
:end
(+ new-start bytes-read
))
1199 (aver (= numbytes
(+ new-start bytes-read
)))
1202 ;;;; STRING-INPUT-STREAM stuff
1204 (defstruct (string-input-stream
1205 (:include ansi-stream
1209 (misc #'string-in-misc
))
1210 (:constructor internal-make-string-input-stream
1211 (string current end
))
1213 (string (missing-arg) :type simple-string
)
1214 (current (missing-arg) :type index
)
1215 (end (missing-arg) :type index
))
1217 (defun string-inch (stream eof-error-p eof-value
)
1218 (declare (type string-input-stream stream
))
1219 (let ((string (string-input-stream-string stream
))
1220 (index (string-input-stream-current stream
)))
1221 (cond ((>= index
(the index
(string-input-stream-end stream
)))
1222 (eof-or-lose stream eof-error-p eof-value
))
1224 (setf (string-input-stream-current stream
) (1+ index
))
1225 (char string index
)))))
1227 (defun string-binch (stream eof-error-p eof-value
)
1228 (declare (type string-input-stream stream
))
1229 (let ((string (string-input-stream-string stream
))
1230 (index (string-input-stream-current stream
)))
1231 (cond ((>= index
(the index
(string-input-stream-end stream
)))
1232 (eof-or-lose stream eof-error-p eof-value
))
1234 (setf (string-input-stream-current stream
) (1+ index
))
1235 (char-code (char string index
))))))
1237 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p
)
1238 (declare (type string-input-stream stream
)
1239 (type index start requested
))
1240 (let* ((string (string-input-stream-string stream
))
1241 (index (string-input-stream-current stream
))
1242 (available (- (string-input-stream-end stream
) index
))
1243 (copy (min available requested
)))
1244 (declare (type simple-string string
))
1246 (setf (string-input-stream-current stream
)
1247 (truly-the index
(+ index copy
)))
1248 ;; FIXME: why are we VECTOR-SAP'ing things here? what's the point?
1249 ;; and are there SB-UNICODE issues here as well? --njf, 2005-03-24
1250 (with-pinned-objects (string buffer
)
1251 (system-area-ub8-copy (vector-sap string
)
1253 (if (typep buffer
'system-area-pointer
)
1255 (vector-sap buffer
))
1258 (if (and (> requested copy
) eof-error-p
)
1259 (error 'end-of-file
:stream stream
)
1262 (defun string-in-misc (stream operation
&optional arg1 arg2
)
1263 (declare (type string-input-stream stream
)
1268 (setf (string-input-stream-current stream
)
1271 (:end
(string-input-stream-end stream
))
1272 ;; We allow moving position beyond EOF. Errors happen
1273 ;; on read, not move -- or the user may extend the
1276 (string-input-stream-current stream
)))
1277 ;; According to ANSI: "Should signal an error of type type-error
1278 ;; if stream is not a stream associated with a file."
1279 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1280 ;; (:file-length (length (string-input-stream-string stream)))
1281 (:unread
(decf (string-input-stream-current stream
)))
1282 (:close
(set-closed-flame stream
))
1283 (:listen
(or (/= (the index
(string-input-stream-current stream
))
1284 (the index
(string-input-stream-end stream
)))
1286 (:element-type
(array-element-type (string-input-stream-string stream
)))))
1288 (defun make-string-input-stream (string &optional
(start 0) end
)
1290 "Return an input stream which will supply the characters of STRING between
1291 START and END in order."
1292 (declare (type string string
)
1294 (type (or index null
) end
))
1295 (let* ((string (coerce string
'(simple-array character
(*)))))
1296 ;; FIXME: Why WITH-ARRAY-DATA, since the array is already simple?
1297 (with-array-data ((string string
) (start start
) (end end
))
1298 (internal-make-string-input-stream
1299 string
;; now simple
1303 ;;;; STRING-OUTPUT-STREAM stuff
1305 ;;;; FIXME: This, like almost none of the stream code is particularly
1306 ;;;; interrupt or thread-safe. While it should not be possible to
1307 ;;;; corrupt the heap here, it certainly is possible to end up with
1308 ;;;; a string-output-stream whose internal state is messed up.
1310 ;;;; FIXME: It would be nice to support space-efficient
1311 ;;;; string-output-streams with element-type base-char. This would
1312 ;;;; mean either a separate subclass, or typecases in functions.
1314 (defparameter *string-output-stream-buffer-initial-size
* 64)
1317 (declaim (inline string-output-string-stream-buffer
1318 string-output-string-stream-pointer
1319 string-output-string-stream-index
))
1320 (defstruct (string-output-stream
1321 (:include ansi-stream
1323 (sout #'string-sout
)
1324 (misc #'string-out-misc
))
1325 (:constructor make-string-output-stream
1326 (&key
(element-type 'character
)
1329 *string-output-stream-buffer-initial-size
*))))
1331 ;; The string we throw stuff in.
1332 (buffer (missing-arg) :type
(simple-array character
(*)))
1333 ;; Chains of buffers to use
1336 ;; Index of the next location to use in the current string.
1337 (pointer 0 :type index
)
1338 ;; Global location in the stream
1339 (index 0 :type index
)
1340 ;; Index cache: when we move backwards we save the greater of this
1341 ;; and index here, so the greater of index and this is always the
1342 ;; end of the stream.
1343 (index-cache 0 :type index
)
1344 ;; Requested element type
1345 (element-type 'character
))
1348 (setf (fdocumentation 'make-string-output-stream
'function
)
1349 "Return an output stream which will accumulate all output given it for the
1350 benefit of the function GET-OUTPUT-STREAM-STRING.")
1352 ;;; Pushes the current segment onto the prev-list, and either pops
1353 ;;; or allocates a new one.
1354 (defun string-output-stream-new-buffer (stream size
)
1355 (declare (index size
))
1356 (/show0
"/string-output-stream-new-buffer")
1357 (push (string-output-stream-buffer stream
)
1358 (string-output-stream-prev stream
))
1359 (setf (string-output-stream-buffer stream
)
1360 (or (pop (string-output-stream-next stream
))
1361 ;; FIXME: This would be the correct place to detect that
1362 ;; more then FIXNUM characters are being written to the
1363 ;; stream, and do something about it.
1364 (make-string size
))))
1366 ;;; Moves to the end of the next segment or the current one if there are
1367 ;;; no more segments. Returns true as long as there are next segments.
1368 (defun string-output-stream-next-buffer (stream)
1369 (/show0
"/string-output-stream-next-buffer")
1370 (let* ((old (string-output-stream-buffer stream
))
1371 (new (pop (string-output-stream-next stream
)))
1372 (old-size (length old
))
1373 (skipped (- old-size
(string-output-stream-pointer stream
))))
1375 (let ((new-size (length new
)))
1376 (push old
(string-output-stream-prev stream
))
1377 (setf (string-output-stream-buffer stream
) new
1378 (string-output-stream-pointer stream
) new-size
)
1379 (incf (string-output-stream-index stream
) (+ skipped new-size
)))
1382 (setf (string-output-stream-pointer stream
) old-size
)
1383 (incf (string-output-stream-index stream
) skipped
)
1386 ;;; Moves to the start of the previous segment or the current one if there
1387 ;;; are no more segments. Returns true as long as there are prev segments.
1388 (defun string-output-stream-prev-buffer (stream)
1389 (/show0
"/string-output-stream-prev-buffer")
1390 (let ((old (string-output-stream-buffer stream
))
1391 (new (pop (string-output-stream-prev stream
)))
1392 (skipped (string-output-stream-pointer stream
)))
1394 (push old
(string-output-stream-next stream
))
1395 (setf (string-output-stream-buffer stream
) new
1396 (string-output-stream-pointer stream
) 0)
1397 (decf (string-output-stream-index stream
) (+ skipped
(length new
)))
1400 (setf (string-output-stream-pointer stream
) 0)
1401 (decf (string-output-stream-index stream
) skipped
)
1404 (defun string-ouch (stream character
)
1405 (/show0
"/string-ouch")
1406 (let ((pointer (string-output-stream-pointer stream
))
1407 (buffer (string-output-stream-buffer stream
))
1408 (index (string-output-stream-index stream
)))
1409 (cond ((= pointer
(length buffer
))
1410 (setf buffer
(string-output-stream-new-buffer stream index
)
1411 (aref buffer
0) character
1412 (string-output-stream-pointer stream
) 1))
1414 (setf (aref buffer pointer
) character
1415 (string-output-stream-pointer stream
) (1+ pointer
))))
1416 (setf (string-output-stream-index stream
) (1+ index
))))
1418 (defun string-sout (stream string start end
)
1419 (declare (type simple-string string
)
1420 (type index start end
))
1421 (let* ((full-length (- end start
))
1422 (length full-length
)
1423 (buffer (string-output-stream-buffer stream
))
1424 (pointer (string-output-stream-pointer stream
))
1425 (space (- (length buffer
) pointer
))
1426 (here (min space length
))
1427 (stop (+ start here
))
1428 (overflow (- length space
)))
1429 (declare (index length space here stop full-length
)
1431 (type (simple-array character
(*)) buffer
))
1436 ((simple-array character
(*))
1437 (replace buffer string
:start1 pointer
:start2 start
:end2 stop
))
1439 (replace buffer string
:start1 pointer
:start2 start
:end2 stop
))
1440 ((simple-array nil
(*))
1441 (replace buffer string
:start1 pointer
:start2 start
:end2 stop
)))
1442 (setf (string-output-stream-pointer stream
) (+ here pointer
)))
1443 (when (plusp overflow
)
1445 length
(- end start
)
1446 buffer
(string-output-stream-new-buffer
1447 stream
(max overflow
(string-output-stream-index stream
)))
1449 space
(length buffer
)
1450 here
(min space length
)
1452 ;; there may be more overflow if we used a buffer
1453 ;; already allocated to the stream
1454 overflow
(- length space
))
1456 (incf (string-output-stream-index stream
) full-length
)))
1458 ;;; Factored out of the -misc method due to size.
1459 (defun set-string-output-stream-file-position (stream pos
)
1460 (let* ((index (string-output-stream-index stream
))
1461 (end (max index
(string-output-stream-index-cache stream
))))
1462 (declare (index index end
))
1463 (setf (string-output-stream-index-cache stream
) end
)
1464 (cond ((eq :start pos
)
1465 (loop while
(string-output-stream-prev-buffer stream
)))
1467 (loop while
(string-output-stream-next-buffer stream
))
1468 (let ((over (- (string-output-stream-index stream
) end
)))
1469 (decf (string-output-stream-pointer stream
) over
))
1470 (setf (string-output-stream-index stream
) end
))
1472 (loop while
(< pos index
)
1473 do
(string-output-stream-prev-buffer stream
)
1474 (setf index
(string-output-stream-index stream
)))
1475 (let ((step (- pos index
)))
1476 (incf (string-output-stream-pointer stream
) step
)
1477 (setf (string-output-stream-index stream
) pos
)))
1479 ;; We allow moving beyond the end of stream, implicitly
1480 ;; extending the output stream.
1481 (let ((next (string-output-stream-next-buffer stream
)))
1482 ;; Update after -next-buffer, INDEX is kept pointing at
1483 ;; the end of the current buffer.
1484 (setf index
(string-output-stream-index stream
))
1485 (loop while
(and next
(> pos index
))
1486 do
(setf next
(string-output-stream-next-buffer stream
)
1487 index
(string-output-stream-index stream
))))
1488 ;; Allocate new buffer if needed, or step back to
1489 ;; the desired index and set pointer and index
1491 (let ((diff (- pos index
)))
1493 (let* ((new (string-output-stream-new-buffer stream diff
))
1494 (size (length new
)))
1495 (aver (= pos
(+ index size
)))
1496 (setf (string-output-stream-pointer stream
) size
1497 (string-output-stream-index stream
) pos
))
1498 (let ((size (length (string-output-stream-buffer stream
))))
1499 (setf (string-output-stream-pointer stream
) (+ size diff
)
1500 (string-output-stream-index stream
) pos
))))))))
1502 (defun string-out-misc (stream operation
&optional arg1 arg2
)
1503 (declare (ignore arg2
))
1506 ;; Keeping this first is a silly micro-optimization: FRESH-LINE
1507 ;; makes this the most common one.
1508 (/show0
"/string-out-misc charpos")
1509 (prog ((pointer (string-output-stream-pointer stream
))
1510 (buffer (string-output-stream-buffer stream
))
1511 (prev (string-output-stream-prev stream
))
1514 (let ((pos (position #\newline buffer
:from-end t
:end pointer
)))
1515 (when (or pos
(not buffer
))
1516 ;; If newline is at index I, and pointer at index I+N, charpos
1517 ;; is N-1. If there is no newline, and pointer is at index N,
1519 (return (+ base
(if pos
(- pointer pos
1) pointer
))))
1520 (setf base
(+ base pointer
)
1522 pointer
(length buffer
))
1523 (/show0
"/string-out-misc charpos next")
1526 (/show0
"/string-out-misc file-position")
1528 (set-string-output-stream-file-position stream arg1
))
1529 (string-output-stream-index stream
))
1531 (/show0
"/string-out-misc close")
1532 (set-closed-flame stream
))
1533 (:element-type
(string-output-stream-element-type stream
))))
1535 ;;; Return a string of all the characters sent to a stream made by
1536 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1537 (defun get-output-stream-string (stream)
1538 (declare (type string-output-stream stream
))
1539 (let* ((length (max (string-output-stream-index stream
)
1540 (string-output-stream-index-cache stream
)))
1541 (element-type (string-output-stream-element-type stream
))
1542 (prev (string-output-stream-prev stream
))
1543 (this (string-output-stream-buffer stream
))
1544 (next (string-output-stream-next stream
))
1547 ;; overwhelmingly common case: can be inlined
1549 ;; FIXME: If we were willing to use %SHRINK-VECTOR here,
1550 ;; and allocate new strings the size of 2 * index in
1551 ;; STRING-SOUT, we would not need to allocate one here in
1552 ;; the common case, but could just use the last one
1553 ;; allocated, and chop it down to size..
1555 ((character) (make-string length
))
1556 ;; slightly less common cases: inline it anyway
1557 ((base-char standard-char
)
1558 (make-string length
:element-type
'base-char
))
1560 (make-string length
:element-type element-type
)))))
1562 (setf (string-output-stream-index stream
) 0
1563 (string-output-stream-index-cache stream
) 0
1564 (string-output-stream-pointer stream
) 0
1565 ;; throw them away for simplicity's sake: this way the rest of the
1566 ;; implementation can assume that the greater of INDEX and INDEX-CACHE
1567 ;; is always within the last buffer.
1568 (string-output-stream-prev stream
) nil
1569 (string-output-stream-next stream
) nil
)
1571 (flet ((replace-all (fun)
1573 (declare (index start
))
1574 (dolist (buffer (nreverse prev
))
1575 (funcall fun buffer start
)
1576 (incf start
(length buffer
)))
1577 (funcall fun this start
)
1578 (incf start
(length this
))
1579 (dolist (buffer next
)
1580 (funcall fun buffer start
)
1581 (incf start
(length buffer
))))))
1582 (macrolet ((frob (type)
1583 `(replace-all (lambda (buffer from
)
1584 (declare (type ,type result
)
1585 (type (simple-array character
(*))
1587 (replace result buffer
:start1 from
)))))
1589 ((simple-array character
(*))
1590 (frob (simple-array character
(*))))
1592 (frob simple-base-string
))
1593 ((simple-array nil
(*))
1594 (frob (simple-array nil
(*)))))))
1598 ;;;; fill-pointer streams
1600 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1601 ;;; the CLM, but they are required for the implementation of
1602 ;;; WITH-OUTPUT-TO-STRING.
1604 ;;; FIXME: need to support (VECTOR BASE-CHAR) and (VECTOR NIL),
1605 ;;; ideally without destroying all hope of efficiency.
1606 (deftype string-with-fill-pointer
()
1607 '(and (vector character
)
1608 (satisfies array-has-fill-pointer-p
)))
1610 (defstruct (fill-pointer-output-stream
1611 (:include ansi-stream
1612 (out #'fill-pointer-ouch
)
1613 (sout #'fill-pointer-sout
)
1614 (misc #'fill-pointer-misc
))
1615 (:constructor make-fill-pointer-output-stream
(string))
1617 ;; a string with a fill pointer where we stuff the stuff we write
1618 (string (missing-arg) :type string-with-fill-pointer
:read-only t
))
1620 (defun fill-pointer-ouch (stream character
)
1621 (let* ((buffer (fill-pointer-output-stream-string stream
))
1622 (current (fill-pointer buffer
))
1623 (current+1 (1+ current
)))
1624 (declare (fixnum current
))
1625 (with-array-data ((workspace buffer
) (start) (end))
1626 (declare (type (simple-array character
(*)) workspace
))
1627 (let ((offset-current (+ start current
)))
1628 (declare (fixnum offset-current
))
1629 (if (= offset-current end
)
1630 (let* ((new-length (1+ (* current
2)))
1631 (new-workspace (make-string new-length
)))
1632 (declare (type (simple-array character
(*)) new-workspace
))
1633 (replace new-workspace workspace
1634 :start2 start
:end2 offset-current
)
1635 (setf workspace new-workspace
1636 offset-current current
)
1637 (set-array-header buffer workspace new-length
1638 current
+1 0 new-length nil
))
1639 (setf (fill-pointer buffer
) current
+1))
1640 (setf (schar workspace offset-current
) character
)))
1643 (defun fill-pointer-sout (stream string start end
)
1644 (declare (simple-string string
) (fixnum start end
))
1645 (let* ((string (if (typep string
'(simple-array character
(*)))
1647 (coerce string
'(simple-array character
(*)))))
1648 (buffer (fill-pointer-output-stream-string stream
))
1649 (current (fill-pointer buffer
))
1650 (string-len (- end start
))
1651 (dst-end (+ string-len current
)))
1652 (declare (fixnum current dst-end string-len
))
1653 (with-array-data ((workspace buffer
) (dst-start) (dst-length))
1654 (declare (type (simple-array character
(*)) workspace
))
1655 (let ((offset-dst-end (+ dst-start dst-end
))
1656 (offset-current (+ dst-start current
)))
1657 (declare (fixnum offset-dst-end offset-current
))
1658 (if (> offset-dst-end dst-length
)
1659 (let* ((new-length (+ (the fixnum
(* current
2)) string-len
))
1660 (new-workspace (make-string new-length
)))
1661 (declare (type (simple-array character
(*)) new-workspace
))
1662 (replace new-workspace workspace
1663 :start2 dst-start
:end2 offset-current
)
1664 (setf workspace new-workspace
1665 offset-current current
1666 offset-dst-end dst-end
)
1667 (set-array-header buffer workspace new-length
1668 dst-end
0 new-length nil
))
1669 (setf (fill-pointer buffer
) dst-end
))
1670 (replace workspace string
1671 :start1 offset-current
:start2 start
:end2 end
)))
1674 (defun fill-pointer-misc (stream operation
&optional arg1 arg2
)
1675 (declare (ignore arg2
))
1678 (let ((buffer (fill-pointer-output-stream-string stream
)))
1680 (setf (fill-pointer buffer
)
1683 ;; Fill-pointer is always at fill-pointer we will
1684 ;; make :END move to the end of the actual string.
1685 (:end
(array-total-size buffer
))
1686 ;; We allow moving beyond the end of string if the
1687 ;; string is adjustable.
1688 (t (when (>= arg1
(array-total-size buffer
))
1689 (if (adjustable-array-p buffer
)
1690 (adjust-array buffer arg1
)
1691 (error "Cannot move FILE-POSITION beyond the end ~
1692 of WITH-OUTPUT-TO-STRING stream ~
1693 constructed with non-adjustable string.")))
1695 (fill-pointer buffer
))))
1697 (let* ((buffer (fill-pointer-output-stream-string stream
))
1698 (current (fill-pointer buffer
)))
1699 (with-array-data ((string buffer
) (start) (end current
))
1700 (declare (simple-string string
) (ignore start
))
1701 (let ((found (position #\newline string
:test
#'char
=
1702 :end end
:from-end t
)))
1704 (- end
(the fixnum found
))
1706 (:element-type
(array-element-type
1707 (fill-pointer-output-stream-string stream
)))))
1709 ;;;; indenting streams
1711 (defstruct (indenting-stream (:include ansi-stream
1712 (out #'indenting-out
)
1713 (sout #'indenting-sout
)
1714 (misc #'indenting-misc
))
1715 (:constructor make-indenting-stream
(stream))
1717 ;; the stream we're based on
1719 ;; how much we indent on each line
1723 (setf (fdocumentation 'make-indenting-stream
'function
)
1724 "Return an output stream which indents its output by some amount.")
1726 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1727 ;;; output on the given STREAM based on the specified SUB-STREAM.
1728 (defmacro indenting-indent
(stream sub-stream
)
1729 ;; KLUDGE: bare magic number 60
1730 `(do ((i 0 (+ i
60))
1731 (indentation (indenting-stream-indentation ,stream
)))
1732 ((>= i indentation
))
1734 #.
(make-string 60 :initial-element
#\Space
)
1737 (min 60 (- indentation i
)))))
1739 ;;; INDENTING-OUT writes a character to an indenting stream.
1740 (defun indenting-out (stream char
)
1741 (let ((sub-stream (indenting-stream-stream stream
)))
1742 (write-char char sub-stream
)
1743 (if (char= char
#\newline
)
1744 (indenting-indent stream sub-stream
))))
1746 ;;; INDENTING-SOUT writes a string to an indenting stream.
1747 (defun indenting-sout (stream string start end
)
1748 (declare (simple-string string
) (fixnum start end
))
1750 (sub-stream (indenting-stream-stream stream
)))
1752 (let ((newline (position #\newline string
:start i
:end end
)))
1754 (%write-string string sub-stream i
(1+ newline
))
1755 (indenting-indent stream sub-stream
)
1756 (setq i
(+ newline
1)))
1758 (%write-string string sub-stream i end
)
1761 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1762 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1763 ;;; the base stream minus the stream's indentation.
1764 (defun indenting-misc (stream operation
&optional arg1 arg2
)
1765 (let ((sub-stream (indenting-stream-stream stream
)))
1766 (if (ansi-stream-p sub-stream
)
1767 (let ((method (ansi-stream-misc sub-stream
)))
1770 (let ((line-length (funcall method sub-stream operation
)))
1772 (- line-length
(indenting-stream-indentation stream
)))))
1774 (let ((charpos (funcall method sub-stream operation
)))
1776 (- charpos
(indenting-stream-indentation stream
)))))
1778 (funcall method sub-stream operation arg1 arg2
))))
1779 ;; must be Gray streams FUNDAMENTAL-STREAM
1782 (let ((line-length (stream-line-length sub-stream
)))
1784 (- line-length
(indenting-stream-indentation stream
)))))
1786 (let ((charpos (stream-line-column sub-stream
)))
1788 (- charpos
(indenting-stream-indentation stream
)))))
1790 (stream-misc-dispatch sub-stream operation arg1 arg2
))))))
1792 (declaim (maybe-inline read-char unread-char read-byte listen
))
1794 ;;;; case frobbing streams, used by FORMAT ~(...~)
1796 (defstruct (case-frob-stream
1797 (:include ansi-stream
1798 (misc #'case-frob-misc
))
1799 (:constructor %make-case-frob-stream
(target out sout
))
1801 (target (missing-arg) :type stream
))
1803 (defun make-case-frob-stream (target kind
)
1805 "Return a stream that sends all output to the stream TARGET, but modifies
1806 the case of letters, depending on KIND, which should be one of:
1807 :UPCASE - convert to upper case.
1808 :DOWNCASE - convert to lower case.
1809 :CAPITALIZE - convert the first letter of words to upper case and the
1810 rest of the word to lower case.
1811 :CAPITALIZE-FIRST - convert the first letter of the first word to upper
1812 case and everything else to lower case."
1813 (declare (type stream target
)
1814 (type (member :upcase
:downcase
:capitalize
:capitalize-first
)
1817 (if (case-frob-stream-p target
)
1818 ;; If we are going to be writing to a stream that already does
1819 ;; case frobbing, why bother frobbing the case just so it can
1822 (multiple-value-bind (out sout
)
1825 (values #'case-frob-upcase-out
1826 #'case-frob-upcase-sout
))
1828 (values #'case-frob-downcase-out
1829 #'case-frob-downcase-sout
))
1831 (values #'case-frob-capitalize-out
1832 #'case-frob-capitalize-sout
))
1834 (values #'case-frob-capitalize-first-out
1835 #'case-frob-capitalize-first-sout
)))
1836 (%make-case-frob-stream target out sout
))))
1838 (defun case-frob-misc (stream op
&optional arg1 arg2
)
1839 (declare (type case-frob-stream stream
))
1842 (set-closed-flame stream
))
1844 (let ((target (case-frob-stream-target stream
)))
1845 (if (ansi-stream-p target
)
1846 (funcall (ansi-stream-misc target
) target op arg1 arg2
)
1847 (stream-misc-dispatch target op arg1 arg2
))))))
1849 (defun case-frob-upcase-out (stream char
)
1850 (declare (type case-frob-stream stream
)
1851 (type character char
))
1852 (let ((target (case-frob-stream-target stream
))
1853 (char (char-upcase char
)))
1854 (if (ansi-stream-p target
)
1855 (funcall (ansi-stream-out target
) target char
)
1856 (stream-write-char target char
))))
1858 (defun case-frob-upcase-sout (stream str start end
)
1859 (declare (type case-frob-stream stream
)
1860 (type simple-string str
)
1862 (type (or index null
) end
))
1863 (let* ((target (case-frob-stream-target stream
))
1866 (string (if (and (zerop start
) (= len end
))
1868 (nstring-upcase (subseq str start end
))))
1869 (string-len (- end start
)))
1870 (if (ansi-stream-p target
)
1871 (funcall (ansi-stream-sout target
) target string
0 string-len
)
1872 (stream-write-string target string
0 string-len
))))
1874 (defun case-frob-downcase-out (stream char
)
1875 (declare (type case-frob-stream stream
)
1876 (type character char
))
1877 (let ((target (case-frob-stream-target stream
))
1878 (char (char-downcase char
)))
1879 (if (ansi-stream-p target
)
1880 (funcall (ansi-stream-out target
) target char
)
1881 (stream-write-char target char
))))
1883 (defun case-frob-downcase-sout (stream str start end
)
1884 (declare (type case-frob-stream stream
)
1885 (type simple-string str
)
1887 (type (or index null
) end
))
1888 (let* ((target (case-frob-stream-target stream
))
1891 (string (if (and (zerop start
) (= len end
))
1892 (string-downcase str
)
1893 (nstring-downcase (subseq str start end
))))
1894 (string-len (- end start
)))
1895 (if (ansi-stream-p target
)
1896 (funcall (ansi-stream-sout target
) target string
0 string-len
)
1897 (stream-write-string target string
0 string-len
))))
1899 (defun case-frob-capitalize-out (stream char
)
1900 (declare (type case-frob-stream stream
)
1901 (type character char
))
1902 (let ((target (case-frob-stream-target stream
)))
1903 (cond ((alphanumericp char
)
1904 (let ((char (char-upcase char
)))
1905 (if (ansi-stream-p target
)
1906 (funcall (ansi-stream-out target
) target char
)
1907 (stream-write-char target char
)))
1908 (setf (case-frob-stream-out stream
) #'case-frob-capitalize-aux-out
)
1909 (setf (case-frob-stream-sout stream
)
1910 #'case-frob-capitalize-aux-sout
))
1912 (if (ansi-stream-p target
)
1913 (funcall (ansi-stream-out target
) target char
)
1914 (stream-write-char target char
))))))
1916 (defun case-frob-capitalize-sout (stream str start end
)
1917 (declare (type case-frob-stream stream
)
1918 (type simple-string str
)
1920 (type (or index null
) end
))
1921 (let* ((target (case-frob-stream-target stream
))
1922 (str (subseq str start end
))
1926 (let ((char (schar str i
)))
1927 (cond ((not (alphanumericp char
))
1928 (setf inside-word nil
))
1930 (setf (schar str i
) (char-downcase char
)))
1932 (setf inside-word t
)
1933 (setf (schar str i
) (char-upcase char
))))))
1935 (setf (case-frob-stream-out stream
)
1936 #'case-frob-capitalize-aux-out
)
1937 (setf (case-frob-stream-sout stream
)
1938 #'case-frob-capitalize-aux-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-aux-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-downcase char
)))
1949 (if (ansi-stream-p target
)
1950 (funcall (ansi-stream-out target
) target char
)
1951 (stream-write-char target char
))))
1953 (if (ansi-stream-p target
)
1954 (funcall (ansi-stream-out target
) target char
)
1955 (stream-write-char target char
))
1956 (setf (case-frob-stream-out stream
)
1957 #'case-frob-capitalize-out
)
1958 (setf (case-frob-stream-sout stream
)
1959 #'case-frob-capitalize-sout
)))))
1961 (defun case-frob-capitalize-aux-sout (stream str start end
)
1962 (declare (type case-frob-stream stream
)
1963 (type simple-string str
)
1965 (type (or index null
) end
))
1966 (let* ((target (case-frob-stream-target stream
))
1967 (str (subseq str start end
))
1971 (let ((char (schar str i
)))
1972 (cond ((not (alphanumericp char
))
1973 (setf inside-word nil
))
1975 (setf (schar str i
) (char-downcase char
)))
1977 (setf inside-word t
)
1978 (setf (schar str i
) (char-upcase char
))))))
1980 (setf (case-frob-stream-out stream
)
1981 #'case-frob-capitalize-out
)
1982 (setf (case-frob-stream-sout stream
)
1983 #'case-frob-capitalize-sout
))
1984 (if (ansi-stream-p target
)
1985 (funcall (ansi-stream-sout target
) target str
0 len
)
1986 (stream-write-string target str
0 len
))))
1988 (defun case-frob-capitalize-first-out (stream char
)
1989 (declare (type case-frob-stream stream
)
1990 (type character char
))
1991 (let ((target (case-frob-stream-target stream
)))
1992 (cond ((alphanumericp char
)
1993 (let ((char (char-upcase char
)))
1994 (if (ansi-stream-p target
)
1995 (funcall (ansi-stream-out target
) target char
)
1996 (stream-write-char target char
)))
1997 (setf (case-frob-stream-out stream
)
1998 #'case-frob-downcase-out
)
1999 (setf (case-frob-stream-sout stream
)
2000 #'case-frob-downcase-sout
))
2002 (if (ansi-stream-p target
)
2003 (funcall (ansi-stream-out target
) target char
)
2004 (stream-write-char target char
))))))
2006 (defun case-frob-capitalize-first-sout (stream str start end
)
2007 (declare (type case-frob-stream stream
)
2008 (type simple-string str
)
2010 (type (or index null
) end
))
2011 (let* ((target (case-frob-stream-target stream
))
2012 (str (subseq str start end
))
2015 (let ((char (schar str i
)))
2016 (when (alphanumericp char
)
2017 (setf (schar str i
) (char-upcase char
))
2018 (do ((i (1+ i
) (1+ i
)))
2020 (setf (schar str i
) (char-downcase (schar str i
))))
2021 (setf (case-frob-stream-out stream
)
2022 #'case-frob-downcase-out
)
2023 (setf (case-frob-stream-sout stream
)
2024 #'case-frob-downcase-sout
)
2026 (if (ansi-stream-p target
)
2027 (funcall (ansi-stream-sout target
) target str
0 len
)
2028 (stream-write-string target str
0 len
))))
2032 (defun read-sequence (seq stream
&key
(start 0) end
)
2034 "Destructively modify SEQ by reading elements from STREAM.
2035 That part of SEQ bounded by START and END is destructively modified by
2036 copying successive elements into it from STREAM. If the end of file
2037 for STREAM is reached before copying all elements of the subsequence,
2038 then the extra elements near the end of sequence are not updated, and
2039 the index of the next element is returned."
2040 (declare (type sequence seq
)
2041 (type stream stream
)
2043 (type sequence-end end
)
2045 (if (ansi-stream-p stream
)
2046 (ansi-stream-read-sequence seq stream start end
)
2047 ;; must be Gray streams FUNDAMENTAL-STREAM
2048 (stream-read-sequence stream seq start end
)))
2050 (declaim (inline compatible-vector-and-stream-element-types-p
))
2051 (defun compatible-vector-and-stream-element-types-p (vector stream
)
2052 (declare (type vector vector
)
2053 (type ansi-stream stream
))
2054 (or (and (typep vector
'(simple-array (unsigned-byte 8) (*)))
2055 (subtypep (stream-element-type stream
) '(unsigned-byte 8)))
2056 (and (typep vector
'(simple-array (signed-byte 8) (*)))
2057 (subtypep (stream-element-type stream
) '(signed-byte 8)))))
2059 (defun ansi-stream-read-sequence (seq stream start %end
)
2060 (declare (type sequence seq
)
2061 (type ansi-stream stream
)
2063 (type sequence-end %end
)
2065 (let ((end (or %end
(length seq
))))
2066 (declare (type index end
))
2069 (let ((read-function
2070 (if (subtypep (stream-element-type stream
) 'character
)
2071 #'ansi-stream-read-char
2072 #'ansi-stream-read-byte
)))
2073 (do ((rem (nthcdr start seq
) (rest rem
))
2075 ((or (endp rem
) (>= i end
)) i
)
2076 (declare (type list rem
)
2078 (let ((el (funcall read-function stream nil
:eof nil
)))
2081 (setf (first rem
) el
)))))
2083 (with-array-data ((data seq
) (offset-start start
) (offset-end end
)
2084 :check-fill-pointer t
)
2085 (cond ((compatible-vector-and-stream-element-types-p data stream
)
2086 (let* ((numbytes (- end start
))
2087 (bytes-read (read-n-bytes stream data offset-start
2089 (if (< bytes-read numbytes
)
2090 (+ start bytes-read
)
2092 ((and (ansi-stream-cin-buffer stream
)
2093 (typep seq
'simple-string
))
2094 (ansi-stream-read-string-from-frc-buffer seq stream
2097 (let ((read-function
2098 (if (subtypep (stream-element-type stream
) 'character
)
2099 ;; If the stream-element-type is CHARACTER,
2100 ;; this might be a bivalent stream. If the
2101 ;; sequence is a specialized unsigned-byte
2102 ;; vector, try to read use binary IO. It'll
2103 ;; signal an error if stream is an pure
2104 ;; character stream.
2105 (if (subtypep (array-element-type data
)
2107 #'ansi-stream-read-byte
2108 #'ansi-stream-read-char
)
2109 #'ansi-stream-read-byte
)))
2110 (do ((i offset-start
(1+ i
)))
2111 ((>= i offset-end
) end
)
2112 (declare (type index i
))
2113 (let ((el (funcall read-function stream nil
:eof nil
)))
2115 (return (+ start
(- i offset-start
))))
2116 (setf (aref data i
) el
)))))))))))
2118 (defun ansi-stream-read-string-from-frc-buffer (seq stream start %end
)
2119 (declare (type simple-string seq
)
2120 (type ansi-stream stream
)
2122 (type (or null index
) %end
))
2123 (let ((needed (- (or %end
(length seq
))
2126 (prepare-for-fast-read-char stream
2127 (declare (ignore %frc-method%
))
2128 (unless %frc-buffer%
2129 (return-from ansi-stream-read-string-from-frc-buffer nil
))
2130 (labels ((refill-buffer ()
2132 (fast-read-char-refill stream nil nil
)
2133 (setf %frc-index%
(ansi-stream-in-index %frc-stream%
))))
2135 (let* ((end (length %frc-buffer%
))
2136 (len (min (- end %frc-index%
)
2138 (declare (type index end len read needed
))
2139 (string-dispatch (simple-base-string
2140 (simple-array character
(*)))
2142 (replace seq %frc-buffer%
2143 :start1
(+ start read
)
2144 :end1
(+ start read len
)
2146 :end2
(+ %frc-index% len
)))
2148 (incf %frc-index% len
)
2149 (when (or (eql needed read
)
2151 (done-with-fast-read-char)
2152 (return-from ansi-stream-read-string-from-frc-buffer
2154 (declare (inline refill-buffer
))
2155 (when (and (= %frc-index%
+ansi-stream-in-buffer-length
+)
2157 ;; EOF had been reached before we read anything
2158 ;; at all. Return the EOF value or signal the error.
2159 (done-with-fast-read-char)
2160 (return-from ansi-stream-read-string-from-frc-buffer
0))
2161 (loop (add-chunk))))))
2166 (defun write-sequence (seq stream
&key
(start 0) (end nil
))
2168 "Write the elements of SEQ bounded by START and END to STREAM."
2169 (declare (type sequence seq
)
2170 (type stream stream
)
2172 (type sequence-end end
)
2174 (if (ansi-stream-p stream
)
2175 (ansi-stream-write-sequence seq stream start end
)
2176 ;; must be Gray-streams FUNDAMENTAL-STREAM
2177 (stream-write-sequence stream seq start end
)))
2179 (defun ansi-stream-write-sequence (seq stream start %end
)
2180 (declare (type sequence seq
)
2181 (type ansi-stream stream
)
2183 (type sequence-end %end
)
2185 (let ((end (or %end
(length seq
))))
2186 (declare (type index end
))
2189 (let ((write-function
2190 (if (subtypep (stream-element-type stream
) 'character
)
2191 (ansi-stream-out stream
)
2192 (ansi-stream-bout stream
))))
2193 (do ((rem (nthcdr start seq
) (rest rem
))
2195 ((or (endp rem
) (>= i end
)))
2196 (declare (type list rem
)
2198 (funcall write-function stream
(first rem
)))))
2200 (%write-string seq stream start end
))
2202 (with-array-data ((data seq
) (offset-start start
) (offset-end end
)
2203 :check-fill-pointer t
)
2205 ((output-seq-in-loop ()
2206 (let ((write-function
2207 (if (subtypep (stream-element-type stream
) 'character
)
2208 (lambda (stream object
)
2209 ;; This might be a bivalent stream, so we need
2210 ;; to dispatch on a per-element basis, rather
2211 ;; than just based on the sequence or stream
2213 (if (characterp object
)
2214 (funcall (ansi-stream-out stream
)
2216 (funcall (ansi-stream-bout stream
)
2218 (ansi-stream-bout stream
))))
2219 (do ((i offset-start
(1+ i
)))
2221 (declare (type index i
))
2222 (funcall write-function stream
(aref data i
))))))
2223 (if (and (fd-stream-p stream
)
2224 (compatible-vector-and-stream-element-types-p data stream
))
2225 (buffer-output stream data offset-start offset-end
)
2226 (output-seq-in-loop)))))))