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