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 (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)
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))
155 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
156 (funcall (ansi-stream-misc stream) stream :file-position position))
158 (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
162 (- +ansi-stream-in-buffer-length+
163 (ansi-stream-in-index stream)))
165 (let* ((external-format (stream-external-format stream))
166 (ef-entry (find-external-format external-format))
167 (variable-width-p (variable-width-external-format-p ef-entry))
168 (char-len (bytes-for-char-fun ef-entry)))
171 (loop with buffer = (ansi-stream-cin-buffer stream)
172 with start = (ansi-stream-in-index stream)
173 for i from start below +ansi-stream-in-buffer-length+
174 sum (funcall char-len (aref buffer i)))
175 (* (funcall char-len #\x) ; arbitrary argument
176 (- +ansi-stream-in-buffer-length+
177 (ansi-stream-in-index stream)))))))))))
180 (defun file-position (stream &optional position)
181 (ansi-stream-file-position stream position))
183 ;;; This is a literal translation of the ANSI glossary entry "stream
184 ;;; associated with a file".
186 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
187 ;;; file", and in particular stdin, stdout, and stderr are files, we
188 ;;; end up with this test being satisfied for weird things like
189 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
190 ;;; ANSI spec really had in mind, especially since this is used as a
191 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
192 ;;; probably thinking of something like what Unix calls block devices)
193 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
194 (defun stream-associated-with-file-p (x)
195 "Test for the ANSI concept \"stream associated with a file\"."
196 (or (typep x 'file-stream)
197 (and (synonym-stream-p x)
198 (stream-associated-with-file-p (symbol-value
199 (synonym-stream-symbol x))))))
201 (defun stream-must-be-associated-with-file (stream)
202 (declare (type stream stream))
203 (unless (stream-associated-with-file-p stream)
204 (error 'simple-type-error
205 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
206 ;; this should be TYPE-ERROR. But what then can we use for
207 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
208 ;; private predicate function..) is ugly and confusing, but
209 ;; I can't see any other way. -- WHN 2001-04-14
211 :expected-type '(satisfies stream-associated-with-file-p)
213 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
214 :format-arguments (list stream))))
216 ;;; like FILE-POSITION, only using :FILE-LENGTH
217 (defun file-length (stream)
218 ;; FIXME: The following declaration uses yet undefined types, which
219 ;; cause cross-compiler hangup.
221 ;; (declare (type (or file-stream synonym-stream) stream))
223 ;; The description for FILE-LENGTH says that an error must be raised
224 ;; for streams not associated with files (which broadcast streams
225 ;; aren't according to the glossary). However, the behaviour of
226 ;; FILE-LENGTH for broadcast streams is explicitly described in the
227 ;; BROADCAST-STREAM entry.
228 (unless (typep stream 'broadcast-stream)
229 (stream-must-be-associated-with-file stream))
230 (funcall (ansi-stream-misc stream) stream :file-length))
232 (defun file-string-length (stream object)
233 (funcall (ansi-stream-misc stream) stream :file-string-length object))
237 #!-sb-fluid (declaim (inline ansi-stream-read-line))
238 (defun ansi-stream-read-line (stream eof-error-p eof-value recursive-p)
239 (declare (ignore recursive-p))
240 (prepare-for-fast-read-char stream
241 ;; Check whether the FAST-READ-CHAR buffer contains a newline. If it
242 ;; does, we can do things quickly by just copying the line from the
243 ;; buffer instead of doing repeated calls to FAST-READ-CHAR.
246 ;; For %FIND-POSITION transform
247 (declare (optimize (speed 2)))
248 (let ((pos (position #\Newline %frc-buffer%
250 :start %frc-index%)))
252 (let* ((len (- pos %frc-index%))
253 (res (make-string len)))
254 (replace res %frc-buffer% :start2 %frc-index% :end2 pos)
255 (setf %frc-index% (1+ pos))
256 (done-with-fast-read-char)
257 (return-from ansi-stream-read-line res))))))
258 (let ((res (make-string 80))
262 (let ((ch (fast-read-char nil nil)))
264 (when (char= ch #\newline)
265 (done-with-fast-read-char)
266 (return (values (%shrink-vector res index) nil)))
269 (let ((new (make-string len)))
272 (setf (schar res index) ch)
275 (done-with-fast-read-char)
276 (return (values (eof-or-lose stream
280 ;; Since FAST-READ-CHAR already hit the eof char, we
281 ;; shouldn't do another READ-CHAR.
283 (done-with-fast-read-char)
284 (return (values (%shrink-vector res index) t)))))))))
286 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
288 (let ((stream (in-synonym-of stream)))
289 (if (ansi-stream-p stream)
290 (ansi-stream-read-line stream eof-error-p eof-value recursive-p)
291 ;; must be Gray streams FUNDAMENTAL-STREAM
292 (multiple-value-bind (string eof) (stream-read-line stream)
293 (if (and eof (zerop (length string)))
294 (values (eof-or-lose stream eof-error-p eof-value) t)
295 (values string eof))))))
297 ;;; We proclaim them INLINE here, then proclaim them NOTINLINE later on,
298 ;;; so, except in this file, they are not inline by default, but they can be.
299 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
301 #!-sb-fluid (declaim (inline ansi-stream-read-char))
302 (defun ansi-stream-read-char (stream eof-error-p eof-value recursive-p)
303 (declare (ignore recursive-p))
304 (prepare-for-fast-read-char stream
306 (fast-read-char eof-error-p eof-value)
307 (done-with-fast-read-char))))
309 (defun read-char (&optional (stream *standard-input*)
313 (let ((stream (in-synonym-of stream)))
314 (if (ansi-stream-p stream)
315 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
316 ;; must be Gray streams FUNDAMENTAL-STREAM
317 (let ((char (stream-read-char stream)))
319 (eof-or-lose stream eof-error-p eof-value)
322 #!-sb-fluid (declaim (inline ansi-stream-unread-char))
323 (defun ansi-stream-unread-char (character stream)
324 (let ((index (1- (ansi-stream-in-index stream)))
325 (buffer (ansi-stream-cin-buffer stream)))
326 (declare (fixnum index))
327 (when (minusp index) (error "nothing to unread"))
329 (setf (aref buffer index) character)
330 (setf (ansi-stream-in-index stream) index))
332 (funcall (ansi-stream-misc stream) stream
333 :unread character)))))
335 (defun unread-char (character &optional (stream *standard-input*))
336 (let ((stream (in-synonym-of stream)))
337 (if (ansi-stream-p stream)
338 (ansi-stream-unread-char character stream)
339 ;; must be Gray streams FUNDAMENTAL-STREAM
340 (stream-unread-char stream character)))
343 #!-sb-fluid (declaim (inline ansi-stream-listen))
344 (defun ansi-stream-listen (stream)
345 (or (/= (the fixnum (ansi-stream-in-index stream))
346 +ansi-stream-in-buffer-length+)
347 ;; Handle :EOF return from misc methods specially
348 (let ((result (funcall (ansi-stream-misc stream) stream :listen)))
353 (defun listen (&optional (stream *standard-input*))
354 (let ((stream (in-synonym-of stream)))
355 (if (ansi-stream-p stream)
356 (ansi-stream-listen stream)
357 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
358 (stream-listen stream))))
360 #!-sb-fluid (declaim (inline ansi-stream-read-char-no-hang))
361 (defun ansi-stream-read-char-no-hang (stream eof-error-p eof-value recursive-p)
362 (if (funcall (ansi-stream-misc stream) stream :listen)
363 ;; On T or :EOF get READ-CHAR to do the work.
364 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
367 (defun read-char-no-hang (&optional (stream *standard-input*)
371 (let ((stream (in-synonym-of stream)))
372 (if (ansi-stream-p stream)
373 (ansi-stream-read-char-no-hang stream eof-error-p eof-value
375 ;; must be Gray streams FUNDAMENTAL-STREAM
376 (let ((char (stream-read-char-no-hang stream)))
378 (eof-or-lose stream eof-error-p eof-value)
381 #!-sb-fluid (declaim (inline ansi-stream-clear-input))
382 (defun ansi-stream-clear-input (stream)
383 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
384 (funcall (ansi-stream-misc stream) stream :clear-input))
386 (defun clear-input (&optional (stream *standard-input*))
387 (let ((stream (in-synonym-of stream)))
388 (if (ansi-stream-p stream)
389 (ansi-stream-clear-input stream)
390 ;; must be Gray streams FUNDAMENTAL-STREAM
391 (stream-clear-input stream)))
394 #!-sb-fluid (declaim (inline ansi-stream-read-byte))
395 (defun ansi-stream-read-byte (stream eof-error-p eof-value recursive-p)
396 ;; Why the "recursive-p" parameter? a-s-r-b is funcall'ed from
397 ;; a-s-read-sequence and needs a lambda list that's congruent with
398 ;; that of a-s-read-char
399 (declare (ignore recursive-p))
400 (prepare-for-fast-read-byte stream
402 (fast-read-byte eof-error-p eof-value t)
403 (done-with-fast-read-byte))))
405 (defun read-byte (stream &optional (eof-error-p t) eof-value)
406 (if (ansi-stream-p stream)
407 (ansi-stream-read-byte stream eof-error-p eof-value nil)
408 ;; must be Gray streams FUNDAMENTAL-STREAM
409 (let ((char (stream-read-byte stream)))
411 (eof-or-lose stream eof-error-p eof-value)
414 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
415 ;;; number of bytes read.
417 ;;; Note: CMU CL's version of this had a special interpretation of
418 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
419 ;;; case, CMU CL's version would return as soon as any data became
420 ;;; available.) This could be useful behavior for things like pipes in
421 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
422 ;;; If we ever need it, it could be added later as a new variant N-BIN
423 ;;; method (perhaps N-BIN-ASAP?) or something.
424 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
425 (declare (type ansi-stream stream)
426 (type index numbytes start)
427 (type (or (simple-array * (*)) system-area-pointer) buffer))
428 (let* ((stream (in-synonym-of stream ansi-stream))
429 (in-buffer (ansi-stream-in-buffer stream))
430 (index (ansi-stream-in-index stream))
431 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
432 (declare (fixnum index num-buffered))
435 (funcall (ansi-stream-n-bin stream)
441 ((<= numbytes num-buffered)
443 (let ((copy-function (typecase buffer
444 ((simple-array * (*)) #'ub8-bash-copy)
445 (system-area-pointer #'copy-ub8-to-system-area))))
446 (funcall copy-function in-buffer index buffer start numbytes))
447 (%byte-blt in-buffer index
448 buffer start (+ start numbytes))
449 (setf (ansi-stream-in-index stream) (+ index numbytes))
452 (let ((end (+ start num-buffered)))
454 (let ((copy-function (typecase buffer
455 ((simple-array * (*)) #'ub8-bash-copy)
456 (system-area-pointer #'copy-ub8-to-system-area))))
457 (funcall copy-function in-buffer index buffer start num-buffered))
458 (%byte-blt in-buffer index buffer start end)
459 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
460 (+ (funcall (ansi-stream-n-bin stream)
464 (- numbytes num-buffered)
468 ;;; the amount of space we leave at the start of the in-buffer for
471 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
472 (defconstant +ansi-stream-in-buffer-extra+
473 4) ; FIXME: should be symbolic constant
475 ;;; This function is called by the FAST-READ-CHAR expansion to refill
476 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
477 ;;; and hence must be an N-BIN method.
478 (defun fast-read-char-refill (stream eof-error-p eof-value)
479 (let* ((ibuf (ansi-stream-cin-buffer stream))
480 (count (funcall (ansi-stream-n-bin stream)
483 +ansi-stream-in-buffer-extra+
484 (- +ansi-stream-in-buffer-length+
485 +ansi-stream-in-buffer-extra+)
487 (start (- +ansi-stream-in-buffer-length+ count)))
488 (declare (type index start count))
490 (setf (ansi-stream-in-index stream)
491 +ansi-stream-in-buffer-length+)
492 (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
494 (when (/= start +ansi-stream-in-buffer-extra+)
495 (#.(let* ((n-character-array-bits
498 sb!vm:*specialized-array-element-type-properties*
499 :key #'sb!vm:saetp-specifier)))
500 (bash-function (intern (format nil "UB~D-BASH-COPY" n-character-array-bits)
501 (find-package "SB!KERNEL"))))
503 ibuf +ansi-stream-in-buffer-extra+
506 (setf (ansi-stream-in-index stream) (1+ start))
507 (aref ibuf start)))))
509 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
510 ;;; leave room for unreading.
511 (defun fast-read-byte-refill (stream eof-error-p eof-value)
512 (let* ((ibuf (ansi-stream-in-buffer stream))
513 (count (funcall (ansi-stream-n-bin stream) stream
514 ibuf 0 +ansi-stream-in-buffer-length+
516 (start (- +ansi-stream-in-buffer-length+ count)))
517 (declare (type index start count))
519 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
520 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
522 (unless (zerop start)
523 (ub8-bash-copy ibuf 0
526 (setf (ansi-stream-in-index stream) (1+ start))
527 (aref ibuf start)))))
531 (defun write-char (character &optional (stream *standard-output*))
532 (with-out-stream stream (ansi-stream-out character)
533 (stream-write-char character))
536 (defun terpri (&optional (stream *standard-output*))
537 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
540 #!-sb-fluid (declaim (inline ansi-stream-fresh-line))
541 (defun ansi-stream-fresh-line (stream)
542 (when (/= (or (charpos stream) 1) 0)
543 (funcall (ansi-stream-out stream) stream #\newline)
546 (defun fresh-line (&optional (stream *standard-output*))
547 (let ((stream (out-synonym-of stream)))
548 (if (ansi-stream-p stream)
549 (ansi-stream-fresh-line stream)
550 ;; must be Gray streams FUNDAMENTAL-STREAM
551 (stream-fresh-line stream))))
553 (defun write-string (string &optional (stream *standard-output*)
555 (declare (type string string))
556 ;; Note that even though you might expect, based on the behavior of
557 ;; things like AREF, that the correct upper bound here is
558 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
559 ;; "bounding index" and "length" indicate that in this case (i.e.
560 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
561 ;; (LENGTH STRING) is the required upper bound. A foolish
562 ;; consistency is the hobgoblin of lesser languages..
563 (%write-string string stream start (%check-vector-sequence-bounds
567 #!-sb-fluid (declaim (inline ansi-stream-write-string))
568 (defun ansi-stream-write-string (string stream start end)
569 (declare (type string string))
570 (declare (type ansi-stream stream))
571 (declare (type index start end))
572 (if (array-header-p string)
573 (with-array-data ((data string) (offset-start start)
575 (funcall (ansi-stream-sout stream)
576 stream data offset-start offset-end))
577 (funcall (ansi-stream-sout stream) stream string start end))
580 (defun %write-string (string stream start end)
581 (declare (type string string))
582 (declare (type stream-designator stream))
583 (declare (type index start end))
584 (let ((stream (out-synonym-of stream)))
585 (if(ansi-stream-p stream)
586 (ansi-stream-write-string string stream start end)
587 ;; must be Gray streams FUNDAMENTAL-STREAM
588 (stream-write-string stream string start end))))
590 ;;; A wrapper function for all those (MACROLET OUT-FUN) definitions,
591 ;;; which cannot deal with keyword arguments.
592 (declaim (inline write-string-no-key))
593 (defun write-string-no-key (string stream start end)
594 (write-string string stream :start start :end end))
596 (defun write-line (string &optional (stream *standard-output*)
598 (declare (type string string))
599 ;; FIXME: Why is there this difference between the treatments of the
600 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
601 (let ((defaulted-stream (out-synonym-of stream)))
602 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
604 (write-char #\newline defaulted-stream))
607 (defun charpos (&optional (stream *standard-output*))
608 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
610 (defun line-length (&optional (stream *standard-output*))
611 (with-out-stream stream (ansi-stream-misc :line-length)
612 (stream-line-length)))
614 (defun finish-output (&optional (stream *standard-output*))
615 (with-out-stream stream (ansi-stream-misc :finish-output)
616 (stream-finish-output))
619 (defun force-output (&optional (stream *standard-output*))
620 (with-out-stream stream (ansi-stream-misc :force-output)
621 (stream-force-output))
624 (defun clear-output (&optional (stream *standard-output*))
625 (with-out-stream stream (ansi-stream-misc :clear-output)
626 (stream-force-output))
629 (defun write-byte (integer stream)
630 (with-out-stream/no-synonym stream (ansi-stream-bout integer)
631 (stream-write-byte integer))
635 ;;; (These were inline throughout this file, but that's not appropriate
636 ;;; globally. And we must not inline them in the rest of this file if
637 ;;; dispatch to gray or simple streams is to work, since both redefine
638 ;;; these functions later.)
639 (declaim (notinline read-char unread-char read-byte listen))
641 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
642 ;;; streams to handle the misc routines and dispatch to the
643 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
644 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
645 (declare (type stream stream) (ignore arg2))
648 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
649 (let ((char (read-char-no-hang stream nil :eof)))
650 (when (characterp char)
651 (unread-char char stream))
654 (unread-char arg1 stream))
658 (clear-input stream))
660 (force-output stream))
662 (finish-output stream))
664 (stream-element-type stream))
665 (:stream-external-format
666 (stream-external-format stream))
668 (interactive-stream-p stream))
670 (line-length stream))
674 (file-length stream))
676 (file-string-length stream arg1))
678 (file-position stream arg1))))
680 ;;;; broadcast streams
682 (defstruct (broadcast-stream (:include ansi-stream
683 (out #'broadcast-out)
684 (bout #'broadcast-bout)
685 (sout #'broadcast-sout)
686 (misc #'broadcast-misc))
687 (:constructor %make-broadcast-stream
690 ;; a list of all the streams we broadcast to
691 (streams () :type list :read-only t))
693 (defun make-broadcast-stream (&rest streams)
694 (dolist (stream streams)
695 (unless (output-stream-p stream)
698 :expected-type '(satisfies output-stream-p))))
699 (apply #'%make-broadcast-stream streams))
701 (macrolet ((out-fun (name fun &rest args)
702 `(defun ,name (stream ,@args)
703 (dolist (stream (broadcast-stream-streams stream))
704 (,fun ,(car args) stream ,@(cdr args))))))
705 (out-fun broadcast-out write-char char)
706 (out-fun broadcast-bout write-byte byte)
707 (out-fun broadcast-sout write-string-no-key string start end))
709 (defun broadcast-misc (stream operation &optional arg1 arg2)
710 (let ((streams (broadcast-stream-streams stream)))
712 ;; FIXME: This may not be the best place to note this, but I
713 ;; think the :CHARPOS protocol needs revision. Firstly, I think
714 ;; this is the last place where a NULL return value was possible
715 ;; (before adjusting it to be 0), so a bunch of conditionals IF
716 ;; CHARPOS can be removed; secondly, it is my belief that
717 ;; FD-STREAMS, when running FILE-POSITION, do not update the
718 ;; CHARPOS, and consequently there will be much wrongness.
720 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
721 ;; is it testing the :charpos of an input stream?
723 ;; -- CSR, 2004-02-04
725 (dolist (stream streams 0)
726 (let ((charpos (charpos stream)))
727 (if charpos (return charpos)))))
730 (dolist (stream streams min)
731 (let ((res (line-length stream)))
732 (when res (setq min (if min (min res min) res)))))))
734 #+nil ; old, arguably more logical, version
736 (dolist (stream streams (if (> (length res) 1) `(and ,@res) t))
737 (pushnew (stream-element-type stream) res :test #'equal)))
738 ;; ANSI-specified version (under System Class BROADCAST-STREAM)
740 (do ((streams streams (cdr streams)))
742 (when (null (cdr streams))
743 (setq res (stream-element-type (car streams)))))))
745 (let ((res :default))
746 (dolist (stream streams res)
747 (setq res (stream-external-format stream)))))
749 (let ((last (last streams)))
751 (file-length (car last))
755 (let ((res (or (eql arg1 :start) (eql arg1 0))))
756 (dolist (stream streams res)
757 (setq res (file-position stream arg1))))
759 (dolist (stream streams res)
760 (setq res (file-position stream))))))
763 (dolist (stream streams res)
764 (setq res (file-string-length stream arg1)))))
766 (set-closed-flame stream))
769 (dolist (stream streams res)
771 (if (ansi-stream-p stream)
772 (funcall (ansi-stream-misc stream) stream operation
774 (stream-misc-dispatch stream operation arg1 arg2)))))))))
778 (defstruct (synonym-stream (:include ansi-stream
781 (n-bin #'synonym-n-bin)
783 (bout #'synonym-bout)
784 (sout #'synonym-sout)
785 (misc #'synonym-misc))
786 (:constructor make-synonym-stream (symbol))
788 ;; This is the symbol, the value of which is the stream we are synonym to.
789 (symbol nil :type symbol :read-only t))
790 (def!method print-object ((x synonym-stream) stream)
791 (print-unreadable-object (x stream :type t :identity t)
792 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
794 ;;; The output simple output methods just call the corresponding
795 ;;; function on the synonymed stream.
796 (macrolet ((out-fun (name fun &rest args)
797 `(defun ,name (stream ,@args)
798 (declare (optimize (safety 1)))
799 (let ((syn (symbol-value (synonym-stream-symbol stream))))
800 (,fun ,(car args) syn ,@(cdr args))))))
801 (out-fun synonym-out write-char ch)
802 (out-fun synonym-bout write-byte n)
803 (out-fun synonym-sout write-string-no-key string start end))
805 ;;; For the input methods, we just call the corresponding function on the
806 ;;; synonymed stream. These functions deal with getting input out of
807 ;;; the In-Buffer if there is any.
808 (macrolet ((in-fun (name fun &rest args)
809 `(defun ,name (stream ,@args)
810 (declare (optimize (safety 1)))
811 (,fun (symbol-value (synonym-stream-symbol stream))
813 (in-fun synonym-in read-char eof-error-p eof-value)
814 (in-fun synonym-bin read-byte eof-error-p eof-value)
815 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
817 (defun synonym-misc (stream operation &optional arg1 arg2)
818 (declare (optimize (safety 1)))
819 (let ((syn (symbol-value (synonym-stream-symbol stream))))
820 (if (ansi-stream-p syn)
821 ;; We have to special-case some operations which interact with
822 ;; the in-buffer of the wrapped stream, since just calling
823 ;; ANSI-STREAM-MISC on them
825 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
826 +ansi-stream-in-buffer-length+)
827 (funcall (ansi-stream-misc syn) syn :listen)))
828 (:clear-input (clear-input syn))
829 (:unread (unread-char arg1 syn))
831 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
832 (stream-misc-dispatch syn operation arg1 arg2))))
836 (defstruct (two-way-stream
837 (:include ansi-stream
840 (n-bin #'two-way-n-bin)
842 (bout #'two-way-bout)
843 (sout #'two-way-sout)
844 (misc #'two-way-misc))
845 (:constructor %make-two-way-stream (input-stream output-stream))
847 (input-stream (missing-arg) :type stream :read-only t)
848 (output-stream (missing-arg) :type stream :read-only t))
849 (defprinter (two-way-stream) input-stream output-stream)
851 (defun make-two-way-stream (input-stream output-stream)
853 "Return a bidirectional stream which gets its input from INPUT-STREAM and
854 sends its output to OUTPUT-STREAM."
855 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
856 ;; should be encapsulated in a function, and used here and most of
857 ;; the other places that SYNONYM-STREAM-P appears.
858 (unless (output-stream-p output-stream)
861 :expected-type '(satisfies output-stream-p)))
862 (unless (input-stream-p input-stream)
865 :expected-type '(satisfies input-stream-p)))
866 (funcall #'%make-two-way-stream input-stream output-stream))
868 (macrolet ((out-fun (name fun &rest args)
869 `(defun ,name (stream ,@args)
870 (let ((syn (two-way-stream-output-stream stream)))
871 (,fun ,(car args) syn ,@(cdr args))))))
872 (out-fun two-way-out write-char ch)
873 (out-fun two-way-bout write-byte n)
874 (out-fun two-way-sout write-string-no-key string start end))
876 (macrolet ((in-fun (name fun &rest args)
877 `(defun ,name (stream ,@args)
878 (force-output (two-way-stream-output-stream stream))
879 (,fun (two-way-stream-input-stream stream) ,@args))))
880 (in-fun two-way-in read-char eof-error-p eof-value)
881 (in-fun two-way-bin read-byte eof-error-p eof-value)
882 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
884 (defun two-way-misc (stream operation &optional arg1 arg2)
885 (let* ((in (two-way-stream-input-stream stream))
886 (out (two-way-stream-output-stream stream))
887 (in-ansi-stream-p (ansi-stream-p in))
888 (out-ansi-stream-p (ansi-stream-p out)))
892 (or (/= (the fixnum (ansi-stream-in-index in))
893 +ansi-stream-in-buffer-length+)
894 (funcall (ansi-stream-misc in) in :listen))
896 ((:finish-output :force-output :clear-output)
897 (if out-ansi-stream-p
898 (funcall (ansi-stream-misc out) out operation arg1 arg2)
899 (stream-misc-dispatch out operation arg1 arg2)))
900 (:clear-input (clear-input in))
901 (:unread (unread-char arg1 in))
903 (let ((in-type (stream-element-type in))
904 (out-type (stream-element-type out)))
905 (if (equal in-type out-type)
906 in-type `(and ,in-type ,out-type))))
908 (set-closed-flame stream))
910 (or (if in-ansi-stream-p
911 (funcall (ansi-stream-misc in) in operation arg1 arg2)
912 (stream-misc-dispatch in operation arg1 arg2))
913 (if out-ansi-stream-p
914 (funcall (ansi-stream-misc out) out operation arg1 arg2)
915 (stream-misc-dispatch out operation arg1 arg2)))))))
917 ;;;; concatenated streams
919 (defstruct (concatenated-stream
920 (:include ansi-stream
921 (in #'concatenated-in)
922 (bin #'concatenated-bin)
923 (n-bin #'concatenated-n-bin)
924 (misc #'concatenated-misc))
925 (:constructor %make-concatenated-stream (&rest streams))
927 ;; The car of this is the substream we are reading from now.
928 (streams nil :type list))
929 (def!method print-object ((x concatenated-stream) stream)
930 (print-unreadable-object (x stream :type t :identity t)
933 (concatenated-stream-streams x))))
935 (defun make-concatenated-stream (&rest streams)
937 "Return a stream which takes its input from each of the streams in turn,
938 going on to the next at EOF."
939 (dolist (stream streams)
940 (unless (input-stream-p stream)
943 :expected-type '(satisfies input-stream-p))))
944 (apply #'%make-concatenated-stream streams))
946 (macrolet ((in-fun (name fun)
947 `(defun ,name (stream eof-error-p eof-value)
948 (do ((streams (concatenated-stream-streams stream)
951 (eof-or-lose stream eof-error-p eof-value))
952 (let* ((stream (car streams))
953 (result (,fun stream nil nil)))
954 (when result (return result)))
955 (pop (concatenated-stream-streams stream))))))
956 (in-fun concatenated-in read-char)
957 (in-fun concatenated-bin read-byte))
959 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
960 (do ((streams (concatenated-stream-streams stream) (cdr streams))
961 (current-start start)
962 (remaining-bytes numbytes))
965 (error 'end-of-file :stream stream)
966 (- numbytes remaining-bytes)))
967 (let* ((stream (car streams))
968 (bytes-read (read-n-bytes stream buffer current-start
969 remaining-bytes nil)))
970 (incf current-start bytes-read)
971 (decf remaining-bytes bytes-read)
972 (when (zerop remaining-bytes) (return numbytes)))
973 (setf (concatenated-stream-streams stream) (cdr streams))))
975 (defun concatenated-misc (stream operation &optional arg1 arg2)
976 (let* ((left (concatenated-stream-streams stream))
977 (current (car left)))
981 (return-from concatenated-misc :eof))
983 (let ((stuff (if (ansi-stream-p current)
984 (funcall (ansi-stream-misc current) current
986 (stream-misc-dispatch current :listen))))
987 (cond ((eq stuff :eof)
988 ;; Advance STREAMS, and try again.
989 (pop (concatenated-stream-streams stream))
991 (car (concatenated-stream-streams stream)))
993 ;; No further streams. EOF.
996 ;; Stuff's available.
999 ;; Nothing is available yet.
1001 (:clear-input (when left (clear-input current)))
1002 (:unread (when left (unread-char arg1 current)))
1004 (set-closed-flame stream))
1007 (if (ansi-stream-p current)
1008 (funcall (ansi-stream-misc current) current operation arg1 arg2)
1009 (stream-misc-dispatch current operation arg1 arg2)))))))
1013 (defstruct (echo-stream
1014 (:include two-way-stream
1018 (n-bin #'echo-n-bin))
1019 (:constructor %make-echo-stream (input-stream output-stream))
1022 (def!method print-object ((x echo-stream) stream)
1023 (print-unreadable-object (x stream :type t :identity t)
1025 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1026 (two-way-stream-input-stream x)
1027 (two-way-stream-output-stream x))))
1029 (defun make-echo-stream (input-stream output-stream)
1031 "Return a bidirectional stream which gets its input from INPUT-STREAM and
1032 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
1034 (unless (output-stream-p output-stream)
1036 :datum output-stream
1037 :expected-type '(satisfies output-stream-p)))
1038 (unless (input-stream-p input-stream)
1041 :expected-type '(satisfies input-stream-p)))
1042 (funcall #'%make-echo-stream input-stream output-stream))
1044 (macrolet ((in-fun (name in-fun out-fun &rest args)
1045 `(defun ,name (stream ,@args)
1046 (or (pop (echo-stream-unread-stuff stream))
1047 (let* ((in (echo-stream-input-stream stream))
1048 (out (echo-stream-output-stream stream))
1049 (result (if eof-error-p
1051 (,in-fun in nil in))))
1053 ((eql result in) eof-value)
1054 (t (,out-fun result out) result)))))))
1055 (in-fun echo-in read-char write-char eof-error-p eof-value)
1056 (in-fun echo-bin read-byte write-byte eof-error-p eof-value))
1058 (defun echo-n-bin (stream buffer start numbytes eof-error-p)
1059 (let ((new-start start)
1062 (let ((thing (pop (echo-stream-unread-stuff stream))))
1065 (setf (aref buffer new-start) thing)
1068 (when (= read numbytes)
1069 (return-from echo-n-bin numbytes)))
1071 (let ((bytes-read (read-n-bytes (echo-stream-input-stream stream) buffer
1072 new-start (- numbytes read) nil)))
1075 (write-sequence buffer (echo-stream-output-stream stream)
1076 :start new-start :end (+ new-start bytes-read))
1077 (+ bytes-read read))
1078 ((> numbytes (+ read bytes-read))
1079 (write-sequence buffer (echo-stream-output-stream stream)
1080 :start new-start :end (+ new-start bytes-read))
1081 (error 'end-of-file :stream stream))
1083 (write-sequence buffer (echo-stream-output-stream stream)
1084 :start new-start :end (+ new-start bytes-read))
1085 (aver (= numbytes (+ new-start bytes-read)))
1088 ;;;; STRING-INPUT-STREAM stuff
1090 (defstruct (string-input-stream
1091 (:include ansi-stream
1095 (misc #'string-in-misc))
1096 (:constructor internal-make-string-input-stream
1097 (string current end))
1099 (string (missing-arg) :type simple-string)
1100 (current (missing-arg) :type index)
1101 (end (missing-arg) :type index))
1103 (defun string-inch (stream eof-error-p eof-value)
1104 (declare (type string-input-stream stream))
1105 (let ((string (string-input-stream-string stream))
1106 (index (string-input-stream-current stream)))
1107 (cond ((>= index (the index (string-input-stream-end stream)))
1108 (eof-or-lose stream eof-error-p eof-value))
1110 (setf (string-input-stream-current stream) (1+ index))
1111 (char string index)))))
1113 (defun string-binch (stream eof-error-p eof-value)
1114 (declare (type string-input-stream stream))
1115 (let ((string (string-input-stream-string stream))
1116 (index (string-input-stream-current stream)))
1117 (cond ((>= index (the index (string-input-stream-end stream)))
1118 (eof-or-lose stream eof-error-p eof-value))
1120 (setf (string-input-stream-current stream) (1+ index))
1121 (char-code (char string index))))))
1123 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1124 (declare (type string-input-stream stream)
1125 (type index start requested))
1126 (let* ((string (string-input-stream-string stream))
1127 (index (string-input-stream-current stream))
1128 (available (- (string-input-stream-end stream) index))
1129 (copy (min available requested)))
1130 (declare (type simple-string string))
1132 (setf (string-input-stream-current stream)
1133 (truly-the index (+ index copy)))
1134 ;; FIXME: why are we VECTOR-SAP'ing things here? what's the point?
1135 ;; and are there SB-UNICODE issues here as well? --njf, 2005-03-24
1136 (sb!sys:without-gcing
1137 (system-area-ub8-copy (vector-sap string)
1139 (if (typep buffer 'system-area-pointer)
1141 (vector-sap buffer))
1144 (if (and (> requested copy) eof-error-p)
1145 (error 'end-of-file :stream stream)
1148 (defun string-in-misc (stream operation &optional arg1 arg2)
1149 (declare (type string-input-stream stream)
1154 (setf (string-input-stream-current stream)
1157 (:end (string-input-stream-end stream))
1158 ;; We allow moving position beyond EOF. Errors happen
1159 ;; on read, not move -- or the user may extend the
1162 (string-input-stream-current stream)))
1163 ;; According to ANSI: "Should signal an error of type type-error
1164 ;; if stream is not a stream associated with a file."
1165 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1166 ;; (:file-length (length (string-input-stream-string stream)))
1167 (:unread (decf (string-input-stream-current stream)))
1168 (:close (set-closed-flame stream))
1169 (:listen (or (/= (the index (string-input-stream-current stream))
1170 (the index (string-input-stream-end stream)))
1172 (:element-type (array-element-type (string-input-stream-string stream)))))
1174 (defun make-string-input-stream (string &optional (start 0) end)
1176 "Return an input stream which will supply the characters of STRING between
1177 START and END in order."
1178 (declare (type string string)
1180 (type (or index null) end))
1181 (let* ((string (coerce string '(simple-array character (*))))
1182 (end (%check-vector-sequence-bounds string start end)))
1183 (with-array-data ((string string) (start start) (end end))
1184 (internal-make-string-input-stream
1185 string ;; now simple
1189 ;;;; STRING-OUTPUT-STREAM stuff
1191 (defstruct (string-output-stream
1192 (:include ansi-stream
1194 (sout #'string-sout)
1195 (misc #'string-out-misc))
1196 (:constructor make-string-output-stream
1197 (&key (element-type 'character)
1198 &aux (string (make-string 40))))
1200 ;; The string we throw stuff in.
1201 (string (missing-arg) :type (simple-array character (*)))
1202 ;; Index of the next location to use.
1203 (index 0 :type fixnum)
1204 ;; Index cache for string-output-stream-last-index
1205 (index-cache 0 :type fixnum)
1206 ;; Requested element type
1207 (element-type 'character))
1210 (setf (fdocumentation 'make-string-output-stream 'function)
1211 "Return an output stream which will accumulate all output given it for
1212 the benefit of the function GET-OUTPUT-STREAM-STRING.")
1214 (defun string-output-stream-last-index (stream)
1215 (max (string-output-stream-index stream)
1216 (string-output-stream-index-cache stream)))
1218 (defun string-ouch (stream character)
1219 (let ((current (string-output-stream-index stream))
1220 (workspace (string-output-stream-string stream)))
1221 (declare (type (simple-array character (*)) workspace)
1222 (type fixnum current))
1223 (if (= current (the fixnum (length workspace)))
1224 (let ((new-workspace (make-string (* current 2))))
1225 (replace new-workspace workspace)
1226 (setf (aref new-workspace current) character
1227 (string-output-stream-string stream) new-workspace))
1228 (setf (aref workspace current) character))
1229 (setf (string-output-stream-index stream) (1+ current))))
1231 (defun string-sout (stream string start end)
1232 (declare (type simple-string string)
1233 (type fixnum start end))
1234 (let* ((string (if (typep string '(simple-array character (*)))
1236 (coerce string '(simple-array character (*)))))
1237 (current (string-output-stream-index stream))
1238 (length (- end start))
1239 (dst-end (+ length current))
1240 (workspace (string-output-stream-string stream)))
1241 (declare (type (simple-array character (*)) workspace string)
1242 (type fixnum current length dst-end))
1243 (if (> dst-end (the fixnum (length workspace)))
1244 (let ((new-workspace (make-string (+ (* current 2) length))))
1245 (replace new-workspace workspace :end2 current)
1246 (replace new-workspace string
1247 :start1 current :end1 dst-end
1248 :start2 start :end2 end)
1249 (setf (string-output-stream-string stream) new-workspace))
1250 (replace workspace string
1251 :start1 current :end1 dst-end
1252 :start2 start :end2 end))
1253 (setf (string-output-stream-index stream) dst-end)))
1255 (defun string-out-misc (stream operation &optional arg1 arg2)
1256 (declare (ignore arg2))
1260 (let ((end (string-output-stream-last-index stream)))
1261 (setf (string-output-stream-index-cache stream) end
1262 (string-output-stream-index stream)
1267 ;; We allow moving beyond the end of stream,
1268 ;; implicitly extending the output stream.
1269 (let ((buffer (string-output-stream-string stream)))
1270 (when (> arg1 (length buffer))
1271 (setf (string-output-stream-string stream)
1273 arg1 :element-type (array-element-type buffer))
1274 (subseq (string-output-stream-string stream)
1276 (subseq buffer 0 end))))
1278 (string-output-stream-index stream)))
1279 (:close (set-closed-flame stream))
1281 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1283 (count 0 (1+ count))
1284 (string (string-output-stream-string stream)))
1286 (declare (type (simple-array character (*)) string)
1287 (type fixnum index count))
1288 (if (char= (schar string index) #\newline)
1290 (:element-type (array-element-type (string-output-stream-string stream)))))
1292 ;;; Return a string of all the characters sent to a stream made by
1293 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1294 (defun get-output-stream-string (stream)
1295 (declare (type string-output-stream stream))
1296 (let* ((length (string-output-stream-last-index stream))
1297 (element-type (string-output-stream-element-type stream))
1300 ;; overwhelmingly common case: can be inlined
1301 ((character) (make-string length))
1302 ;; slightly less common cases: inline it anyway
1303 ((base-char standard-char)
1304 (make-string length :element-type 'base-char))
1305 (t (make-string length :element-type element-type)))))
1306 ;; For the benefit of the REPLACE transform, let's do this, so
1307 ;; that the common case isn't ludicrously expensive.
1309 ((simple-array character (*))
1310 (replace result (string-output-stream-string stream)))
1312 (replace result (string-output-stream-string stream)))
1313 ((simple-array nil (*))
1314 (replace result (string-output-stream-string stream))))
1315 (setf (string-output-stream-index stream) 0
1316 (string-output-stream-index-cache stream) 0)
1319 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1320 ;;; GET-OUTPUT-STREAM-STRING would return them.
1321 (defun dump-output-stream-string (in-stream out-stream)
1322 (%write-string (string-output-stream-string in-stream)
1325 (string-output-stream-last-index in-stream))
1326 (setf (string-output-stream-index in-stream) 0
1327 (string-output-stream-index-cache in-stream) 0))
1329 ;;;; fill-pointer streams
1331 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1332 ;;; the CLM, but they are required for the implementation of
1333 ;;; WITH-OUTPUT-TO-STRING.
1335 ;;; FIXME: need to support (VECTOR BASE-CHAR) and (VECTOR NIL),
1336 ;;; ideally without destroying all hope of efficiency.
1337 (deftype string-with-fill-pointer ()
1338 '(and (vector character)
1339 (satisfies array-has-fill-pointer-p)))
1341 (defstruct (fill-pointer-output-stream
1342 (:include ansi-stream
1343 (out #'fill-pointer-ouch)
1344 (sout #'fill-pointer-sout)
1345 (misc #'fill-pointer-misc))
1346 (:constructor make-fill-pointer-output-stream (string))
1348 ;; a string with a fill pointer where we stuff the stuff we write
1349 (string (missing-arg) :type string-with-fill-pointer :read-only t))
1351 (defun fill-pointer-ouch (stream character)
1352 (let* ((buffer (fill-pointer-output-stream-string stream))
1353 (current (fill-pointer buffer))
1354 (current+1 (1+ current)))
1355 (declare (fixnum current))
1356 (with-array-data ((workspace buffer) (start) (end))
1357 (declare (type (simple-array character (*)) workspace))
1358 (let ((offset-current (+ start current)))
1359 (declare (fixnum offset-current))
1360 (if (= offset-current end)
1361 (let* ((new-length (1+ (* current 2)))
1362 (new-workspace (make-string new-length)))
1363 (declare (type (simple-array character (*)) new-workspace))
1364 (replace new-workspace workspace
1365 :start2 start :end2 offset-current)
1366 (setf workspace new-workspace
1367 offset-current current)
1368 (set-array-header buffer workspace new-length
1369 current+1 0 new-length nil))
1370 (setf (fill-pointer buffer) current+1))
1371 (setf (schar workspace offset-current) character)))
1374 (defun fill-pointer-sout (stream string start end)
1375 (declare (simple-string string) (fixnum start end))
1376 (let* ((string (if (typep string '(simple-array character (*)))
1378 (coerce string '(simple-array character (*)))))
1379 (buffer (fill-pointer-output-stream-string stream))
1380 (current (fill-pointer buffer))
1381 (string-len (- end start))
1382 (dst-end (+ string-len current)))
1383 (declare (fixnum current dst-end string-len))
1384 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1385 (declare (type (simple-array character (*)) workspace))
1386 (let ((offset-dst-end (+ dst-start dst-end))
1387 (offset-current (+ dst-start current)))
1388 (declare (fixnum offset-dst-end offset-current))
1389 (if (> offset-dst-end dst-length)
1390 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1391 (new-workspace (make-string new-length)))
1392 (declare (type (simple-array character (*)) new-workspace))
1393 (replace new-workspace workspace
1394 :start2 dst-start :end2 offset-current)
1395 (setf workspace new-workspace
1396 offset-current current
1397 offset-dst-end dst-end)
1398 (set-array-header buffer workspace new-length
1399 dst-end 0 new-length nil))
1400 (setf (fill-pointer buffer) dst-end))
1401 (replace workspace string
1402 :start1 offset-current :start2 start :end2 end)))
1405 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1406 (declare (ignore arg2))
1409 (let ((buffer (fill-pointer-output-stream-string stream)))
1411 (setf (fill-pointer buffer)
1414 ;; Fill-pointer is always at fill-pointer we will
1415 ;; make :END move to the end of the actual string.
1416 (:end (array-total-size buffer))
1417 ;; We allow moving beyond the end of string if the
1418 ;; string is adjustable.
1419 (t (when (>= arg1 (array-total-size buffer))
1420 (if (adjustable-array-p buffer)
1421 (adjust-array buffer arg1)
1422 (error "Cannot move FILE-POSITION beyond the end ~
1423 of WITH-OUTPUT-TO-STRING stream ~
1424 constructed with non-adjustable string.")))
1426 (fill-pointer buffer))))
1428 (let* ((buffer (fill-pointer-output-stream-string stream))
1429 (current (fill-pointer buffer)))
1430 (with-array-data ((string buffer) (start) (end current))
1431 (declare (simple-string string) (ignore start))
1432 (let ((found (position #\newline string :test #'char=
1433 :end end :from-end t)))
1435 (- end (the fixnum found))
1437 (:element-type (array-element-type
1438 (fill-pointer-output-stream-string stream)))))
1440 ;;;; indenting streams
1442 (defstruct (indenting-stream (:include ansi-stream
1443 (out #'indenting-out)
1444 (sout #'indenting-sout)
1445 (misc #'indenting-misc))
1446 (:constructor make-indenting-stream (stream))
1448 ;; the stream we're based on
1450 ;; how much we indent on each line
1454 (setf (fdocumentation 'make-indenting-stream 'function)
1455 "Return an output stream which indents its output by some amount.")
1457 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1458 ;;; output on the given STREAM based on the specified SUB-STREAM.
1459 (defmacro indenting-indent (stream sub-stream)
1460 ;; KLUDGE: bare magic number 60
1461 `(do ((i 0 (+ i 60))
1462 (indentation (indenting-stream-indentation ,stream)))
1463 ((>= i indentation))
1465 #.(make-string 60 :initial-element #\Space)
1468 (min 60 (- indentation i)))))
1470 ;;; INDENTING-OUT writes a character to an indenting stream.
1471 (defun indenting-out (stream char)
1472 (let ((sub-stream (indenting-stream-stream stream)))
1473 (write-char char sub-stream)
1474 (if (char= char #\newline)
1475 (indenting-indent stream sub-stream))))
1477 ;;; INDENTING-SOUT writes a string to an indenting stream.
1478 (defun indenting-sout (stream string start end)
1479 (declare (simple-string string) (fixnum start end))
1481 (sub-stream (indenting-stream-stream stream)))
1483 (let ((newline (position #\newline string :start i :end end)))
1485 (%write-string string sub-stream i (1+ newline))
1486 (indenting-indent stream sub-stream)
1487 (setq i (+ newline 1)))
1489 (%write-string string sub-stream i end)
1492 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1493 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1494 ;;; the base stream minus the stream's indentation.
1495 (defun indenting-misc (stream operation &optional arg1 arg2)
1496 (let ((sub-stream (indenting-stream-stream stream)))
1497 (if (ansi-stream-p sub-stream)
1498 (let ((method (ansi-stream-misc sub-stream)))
1501 (let ((line-length (funcall method sub-stream operation)))
1503 (- line-length (indenting-stream-indentation stream)))))
1505 (let ((charpos (funcall method sub-stream operation)))
1507 (- charpos (indenting-stream-indentation stream)))))
1509 (funcall method sub-stream operation arg1 arg2))))
1510 ;; must be Gray streams FUNDAMENTAL-STREAM
1513 (let ((line-length (stream-line-length sub-stream)))
1515 (- line-length (indenting-stream-indentation stream)))))
1517 (let ((charpos (stream-line-column sub-stream)))
1519 (- charpos (indenting-stream-indentation stream)))))
1521 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1523 (declaim (maybe-inline read-char unread-char read-byte listen))
1525 ;;;; case frobbing streams, used by FORMAT ~(...~)
1527 (defstruct (case-frob-stream
1528 (:include ansi-stream
1529 (misc #'case-frob-misc))
1530 (:constructor %make-case-frob-stream (target out sout))
1532 (target (missing-arg) :type stream))
1534 (defun make-case-frob-stream (target kind)
1536 "Return a stream that sends all output to the stream TARGET, but modifies
1537 the case of letters, depending on KIND, which should be one of:
1538 :UPCASE - convert to upper case.
1539 :DOWNCASE - convert to lower case.
1540 :CAPITALIZE - convert the first letter of words to upper case and the
1541 rest of the word to lower case.
1542 :CAPITALIZE-FIRST - convert the first letter of the first word to upper
1543 case and everything else to lower case."
1544 (declare (type stream target)
1545 (type (member :upcase :downcase :capitalize :capitalize-first)
1548 (if (case-frob-stream-p target)
1549 ;; If we are going to be writing to a stream that already does
1550 ;; case frobbing, why bother frobbing the case just so it can
1553 (multiple-value-bind (out sout)
1556 (values #'case-frob-upcase-out
1557 #'case-frob-upcase-sout))
1559 (values #'case-frob-downcase-out
1560 #'case-frob-downcase-sout))
1562 (values #'case-frob-capitalize-out
1563 #'case-frob-capitalize-sout))
1565 (values #'case-frob-capitalize-first-out
1566 #'case-frob-capitalize-first-sout)))
1567 (%make-case-frob-stream target out sout))))
1569 (defun case-frob-misc (stream op &optional arg1 arg2)
1570 (declare (type case-frob-stream stream))
1573 (set-closed-flame stream))
1575 (let ((target (case-frob-stream-target stream)))
1576 (if (ansi-stream-p target)
1577 (funcall (ansi-stream-misc target) target op arg1 arg2)
1578 (stream-misc-dispatch target op arg1 arg2))))))
1580 (defun case-frob-upcase-out (stream char)
1581 (declare (type case-frob-stream stream)
1582 (type character char))
1583 (let ((target (case-frob-stream-target stream))
1584 (char (char-upcase char)))
1585 (if (ansi-stream-p target)
1586 (funcall (ansi-stream-out target) target char)
1587 (stream-write-char target char))))
1589 (defun case-frob-upcase-sout (stream str start end)
1590 (declare (type case-frob-stream stream)
1591 (type simple-string str)
1593 (type (or index null) end))
1594 (let* ((target (case-frob-stream-target stream))
1597 (string (if (and (zerop start) (= len end))
1599 (nstring-upcase (subseq str start end))))
1600 (string-len (- end start)))
1601 (if (ansi-stream-p target)
1602 (funcall (ansi-stream-sout target) target string 0 string-len)
1603 (stream-write-string target string 0 string-len))))
1605 (defun case-frob-downcase-out (stream char)
1606 (declare (type case-frob-stream stream)
1607 (type character char))
1608 (let ((target (case-frob-stream-target stream))
1609 (char (char-downcase char)))
1610 (if (ansi-stream-p target)
1611 (funcall (ansi-stream-out target) target char)
1612 (stream-write-char target char))))
1614 (defun case-frob-downcase-sout (stream str start end)
1615 (declare (type case-frob-stream stream)
1616 (type simple-string str)
1618 (type (or index null) end))
1619 (let* ((target (case-frob-stream-target stream))
1622 (string (if (and (zerop start) (= len end))
1623 (string-downcase str)
1624 (nstring-downcase (subseq str start end))))
1625 (string-len (- end start)))
1626 (if (ansi-stream-p target)
1627 (funcall (ansi-stream-sout target) target string 0 string-len)
1628 (stream-write-string target string 0 string-len))))
1630 (defun case-frob-capitalize-out (stream char)
1631 (declare (type case-frob-stream stream)
1632 (type character char))
1633 (let ((target (case-frob-stream-target stream)))
1634 (cond ((alphanumericp char)
1635 (let ((char (char-upcase char)))
1636 (if (ansi-stream-p target)
1637 (funcall (ansi-stream-out target) target char)
1638 (stream-write-char target char)))
1639 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1640 (setf (case-frob-stream-sout stream)
1641 #'case-frob-capitalize-aux-sout))
1643 (if (ansi-stream-p target)
1644 (funcall (ansi-stream-out target) target char)
1645 (stream-write-char target char))))))
1647 (defun case-frob-capitalize-sout (stream str start end)
1648 (declare (type case-frob-stream stream)
1649 (type simple-string str)
1651 (type (or index null) end))
1652 (let* ((target (case-frob-stream-target stream))
1653 (str (subseq str start end))
1657 (let ((char (schar str i)))
1658 (cond ((not (alphanumericp char))
1659 (setf inside-word nil))
1661 (setf (schar str i) (char-downcase char)))
1663 (setf inside-word t)
1664 (setf (schar str i) (char-upcase char))))))
1666 (setf (case-frob-stream-out stream)
1667 #'case-frob-capitalize-aux-out)
1668 (setf (case-frob-stream-sout stream)
1669 #'case-frob-capitalize-aux-sout))
1670 (if (ansi-stream-p target)
1671 (funcall (ansi-stream-sout target) target str 0 len)
1672 (stream-write-string target str 0 len))))
1674 (defun case-frob-capitalize-aux-out (stream char)
1675 (declare (type case-frob-stream stream)
1676 (type character char))
1677 (let ((target (case-frob-stream-target stream)))
1678 (cond ((alphanumericp char)
1679 (let ((char (char-downcase char)))
1680 (if (ansi-stream-p target)
1681 (funcall (ansi-stream-out target) target char)
1682 (stream-write-char target char))))
1684 (if (ansi-stream-p target)
1685 (funcall (ansi-stream-out target) target char)
1686 (stream-write-char target char))
1687 (setf (case-frob-stream-out stream)
1688 #'case-frob-capitalize-out)
1689 (setf (case-frob-stream-sout stream)
1690 #'case-frob-capitalize-sout)))))
1692 (defun case-frob-capitalize-aux-sout (stream str start end)
1693 (declare (type case-frob-stream stream)
1694 (type simple-string str)
1696 (type (or index null) end))
1697 (let* ((target (case-frob-stream-target stream))
1698 (str (subseq str start end))
1702 (let ((char (schar str i)))
1703 (cond ((not (alphanumericp char))
1704 (setf inside-word nil))
1706 (setf (schar str i) (char-downcase char)))
1708 (setf inside-word t)
1709 (setf (schar str i) (char-upcase char))))))
1711 (setf (case-frob-stream-out stream)
1712 #'case-frob-capitalize-out)
1713 (setf (case-frob-stream-sout stream)
1714 #'case-frob-capitalize-sout))
1715 (if (ansi-stream-p target)
1716 (funcall (ansi-stream-sout target) target str 0 len)
1717 (stream-write-string target str 0 len))))
1719 (defun case-frob-capitalize-first-out (stream char)
1720 (declare (type case-frob-stream stream)
1721 (type character char))
1722 (let ((target (case-frob-stream-target stream)))
1723 (cond ((alphanumericp char)
1724 (let ((char (char-upcase char)))
1725 (if (ansi-stream-p target)
1726 (funcall (ansi-stream-out target) target char)
1727 (stream-write-char target char)))
1728 (setf (case-frob-stream-out stream)
1729 #'case-frob-downcase-out)
1730 (setf (case-frob-stream-sout stream)
1731 #'case-frob-downcase-sout))
1733 (if (ansi-stream-p target)
1734 (funcall (ansi-stream-out target) target char)
1735 (stream-write-char target char))))))
1737 (defun case-frob-capitalize-first-sout (stream str start end)
1738 (declare (type case-frob-stream stream)
1739 (type simple-string str)
1741 (type (or index null) end))
1742 (let* ((target (case-frob-stream-target stream))
1743 (str (subseq str start end))
1746 (let ((char (schar str i)))
1747 (when (alphanumericp char)
1748 (setf (schar str i) (char-upcase char))
1749 (do ((i (1+ i) (1+ i)))
1751 (setf (schar str i) (char-downcase (schar str i))))
1752 (setf (case-frob-stream-out stream)
1753 #'case-frob-downcase-out)
1754 (setf (case-frob-stream-sout stream)
1755 #'case-frob-downcase-sout)
1757 (if (ansi-stream-p target)
1758 (funcall (ansi-stream-sout target) target str 0 len)
1759 (stream-write-string target str 0 len))))
1763 (defun read-sequence (seq stream &key (start 0) end)
1765 "Destructively modify SEQ by reading elements from STREAM.
1766 That part of SEQ bounded by START and END is destructively modified by
1767 copying successive elements into it from STREAM. If the end of file
1768 for STREAM is reached before copying all elements of the subsequence,
1769 then the extra elements near the end of sequence are not updated, and
1770 the index of the next element is returned."
1771 (declare (type sequence seq)
1772 (type stream stream)
1774 (type sequence-end end)
1776 (if (ansi-stream-p stream)
1777 (ansi-stream-read-sequence seq stream start end)
1778 ;; must be Gray streams FUNDAMENTAL-STREAM
1779 (stream-read-sequence stream seq start end)))
1781 (declaim (inline compatible-vector-and-stream-element-types-p))
1782 (defun compatible-vector-and-stream-element-types-p (vector stream)
1783 (declare (type vector vector)
1784 (type ansi-stream stream))
1785 (or (and (typep vector '(simple-array (unsigned-byte 8) (*)))
1786 (subtypep (stream-element-type stream) '(unsigned-byte 8)))
1787 (and (typep vector '(simple-array (signed-byte 8) (*)))
1788 (subtypep (stream-element-type stream) '(signed-byte 8)))))
1790 (defun ansi-stream-read-sequence (seq stream start %end)
1791 (declare (type sequence seq)
1792 (type ansi-stream stream)
1794 (type sequence-end %end)
1796 (let ((end (or %end (length seq))))
1797 (declare (type index end))
1800 (let ((read-function
1801 (if (subtypep (stream-element-type stream) 'character)
1802 #'ansi-stream-read-char
1803 #'ansi-stream-read-byte)))
1804 (do ((rem (nthcdr start seq) (rest rem))
1806 ((or (endp rem) (>= i end)) i)
1807 (declare (type list rem)
1809 (let ((el (funcall read-function stream nil :eof nil)))
1812 (setf (first rem) el)))))
1814 (with-array-data ((data seq) (offset-start start) (offset-end end))
1815 (if (compatible-vector-and-stream-element-types-p data stream)
1816 (let* ((numbytes (- end start))
1817 (bytes-read (read-n-bytes stream data offset-start
1819 (if (< bytes-read numbytes)
1820 (+ start bytes-read)
1822 (let ((read-function
1823 (if (subtypep (array-element-type data) 'character)
1824 #'ansi-stream-read-char
1825 #'ansi-stream-read-byte)))
1826 (do ((i offset-start (1+ i)))
1827 ((>= i offset-end) end)
1828 (declare (type index i))
1829 (let ((el (funcall read-function stream nil :eof nil)))
1831 (return (+ start (- i offset-start))))
1832 (setf (aref data i) el))))))))))
1836 (defun write-sequence (seq stream &key (start 0) (end nil))
1838 "Write the elements of SEQ bounded by START and END to STREAM."
1839 (declare (type sequence seq)
1840 (type stream stream)
1842 (type sequence-end end)
1844 (if (ansi-stream-p stream)
1845 (ansi-stream-write-sequence seq stream start end)
1846 ;; must be Gray-streams FUNDAMENTAL-STREAM
1847 (stream-write-sequence stream seq start end)))
1849 (defun ansi-stream-write-sequence (seq stream start %end)
1850 (declare (type sequence seq)
1851 (type ansi-stream stream)
1853 (type sequence-end %end)
1855 (let ((end (or %end (length seq))))
1856 (declare (type index end))
1859 (let ((write-function
1860 (if (subtypep (stream-element-type stream) 'character)
1861 (ansi-stream-out stream)
1862 (ansi-stream-bout stream))))
1863 (do ((rem (nthcdr start seq) (rest rem))
1865 ((or (endp rem) (>= i end)))
1866 (declare (type list rem)
1868 (funcall write-function stream (first rem)))))
1870 (%write-string seq stream start end))
1872 (with-array-data ((data seq) (offset-start start) (offset-end end))
1874 ((output-seq-in-loop ()
1875 (let ((write-function
1876 (if (subtypep (array-element-type data) 'character)
1877 (ansi-stream-out stream)
1878 (ansi-stream-bout stream))))
1879 (do ((i offset-start (1+ i)))
1881 (declare (type index i))
1882 (funcall write-function stream (aref data i))))))
1883 (if (and (fd-stream-p stream)
1884 (compatible-vector-and-stream-element-types-p data stream))
1885 (output-raw-bytes stream data offset-start offset-end)
1886 (output-seq-in-loop)))))))