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)))))))))))
179 (defun file-position (stream &optional position)
180 (if (ansi-stream-p stream)
181 (ansi-stream-file-position stream position)
182 (stream-file-position stream position)))
184 ;;; This is a literal translation of the ANSI glossary entry "stream
185 ;;; associated with a file".
187 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
188 ;;; file", and in particular stdin, stdout, and stderr are files, we
189 ;;; end up with this test being satisfied for weird things like
190 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
191 ;;; ANSI spec really had in mind, especially since this is used as a
192 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
193 ;;; probably thinking of something like what Unix calls block devices)
194 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
195 (defun stream-associated-with-file-p (x)
196 "Test for the ANSI concept \"stream associated with a file\"."
197 (or (typep x 'file-stream)
198 (and (synonym-stream-p x)
199 (stream-associated-with-file-p (symbol-value
200 (synonym-stream-symbol x))))))
202 (defun stream-must-be-associated-with-file (stream)
203 (declare (type stream stream))
204 (unless (stream-associated-with-file-p stream)
205 (error 'simple-type-error
206 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
207 ;; this should be TYPE-ERROR. But what then can we use for
208 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
209 ;; private predicate function..) is ugly and confusing, but
210 ;; I can't see any other way. -- WHN 2001-04-14
212 :expected-type '(satisfies stream-associated-with-file-p)
214 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
215 :format-arguments (list stream))))
217 ;;; like FILE-POSITION, only using :FILE-LENGTH
218 (defun file-length (stream)
219 ;; FIXME: The following declaration uses yet undefined types, which
220 ;; cause cross-compiler hangup.
222 ;; (declare (type (or file-stream synonym-stream) stream))
224 ;; The description for FILE-LENGTH says that an error must be raised
225 ;; for streams not associated with files (which broadcast streams
226 ;; aren't according to the glossary). However, the behaviour of
227 ;; FILE-LENGTH for broadcast streams is explicitly described in the
228 ;; BROADCAST-STREAM entry.
229 (unless (typep stream 'broadcast-stream)
230 (stream-must-be-associated-with-file stream))
231 (funcall (ansi-stream-misc stream) stream :file-length))
233 (defun file-string-length (stream object)
234 (funcall (ansi-stream-misc stream) stream :file-string-length object))
238 #!-sb-fluid (declaim (inline ansi-stream-read-line))
239 (defun ansi-stream-read-line (stream eof-error-p eof-value recursive-p)
240 (declare (ignore recursive-p))
241 (prepare-for-fast-read-char stream
242 ;; Check whether the FAST-READ-CHAR buffer contains a newline. If it
243 ;; does, we can do things quickly by just copying the line from the
244 ;; buffer instead of doing repeated calls to FAST-READ-CHAR.
247 ;; For %FIND-POSITION transform
248 (declare (optimize (speed 2)))
249 (let ((pos (position #\Newline %frc-buffer%
251 :start %frc-index%)))
253 (let* ((len (- pos %frc-index%))
254 (res (make-string len)))
255 (replace res %frc-buffer% :start2 %frc-index% :end2 pos)
256 (setf %frc-index% (1+ pos))
257 (done-with-fast-read-char)
258 (return-from ansi-stream-read-line res))))))
259 (let ((res (make-string 80))
263 (let ((ch (fast-read-char nil nil)))
265 (when (char= ch #\newline)
266 (done-with-fast-read-char)
267 (return (values (%shrink-vector res index) nil)))
270 (let ((new (make-string len)))
273 (setf (schar res index) ch)
276 (done-with-fast-read-char)
277 (return (values (eof-or-lose stream
281 ;; Since FAST-READ-CHAR already hit the eof char, we
282 ;; shouldn't do another READ-CHAR.
284 (done-with-fast-read-char)
285 (return (values (%shrink-vector res index) t)))))))))
287 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
289 (let ((stream (in-synonym-of stream)))
290 (if (ansi-stream-p stream)
291 (ansi-stream-read-line stream eof-error-p eof-value recursive-p)
292 ;; must be Gray streams FUNDAMENTAL-STREAM
293 (multiple-value-bind (string eof) (stream-read-line stream)
294 (if (and eof (zerop (length string)))
295 (values (eof-or-lose stream eof-error-p eof-value) t)
296 (values string eof))))))
298 ;;; We proclaim them INLINE here, then proclaim them NOTINLINE later on,
299 ;;; so, except in this file, they are not inline by default, but they can be.
300 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
302 #!-sb-fluid (declaim (inline ansi-stream-read-char))
303 (defun ansi-stream-read-char (stream eof-error-p eof-value recursive-p)
304 (declare (ignore recursive-p))
305 (prepare-for-fast-read-char stream
307 (fast-read-char eof-error-p eof-value)
308 (done-with-fast-read-char))))
310 (defun read-char (&optional (stream *standard-input*)
314 (let ((stream (in-synonym-of stream)))
315 (if (ansi-stream-p stream)
316 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
317 ;; must be Gray streams FUNDAMENTAL-STREAM
318 (let ((char (stream-read-char stream)))
320 (eof-or-lose stream eof-error-p eof-value)
323 #!-sb-fluid (declaim (inline ansi-stream-unread-char))
324 (defun ansi-stream-unread-char (character stream)
325 (let ((index (1- (ansi-stream-in-index stream)))
326 (buffer (ansi-stream-cin-buffer stream)))
327 (declare (fixnum index))
328 (when (minusp index) (error "nothing to unread"))
330 (setf (aref buffer index) character)
331 (setf (ansi-stream-in-index stream) index))
333 (funcall (ansi-stream-misc stream) stream
334 :unread character)))))
336 (defun unread-char (character &optional (stream *standard-input*))
337 (let ((stream (in-synonym-of stream)))
338 (if (ansi-stream-p stream)
339 (ansi-stream-unread-char character stream)
340 ;; must be Gray streams FUNDAMENTAL-STREAM
341 (stream-unread-char stream character)))
344 #!-sb-fluid (declaim (inline ansi-stream-listen))
345 (defun ansi-stream-listen (stream)
346 (or (/= (the fixnum (ansi-stream-in-index stream))
347 +ansi-stream-in-buffer-length+)
348 ;; Handle :EOF return from misc methods specially
349 (let ((result (funcall (ansi-stream-misc stream) stream :listen)))
354 (defun listen (&optional (stream *standard-input*))
355 (let ((stream (in-synonym-of stream)))
356 (if (ansi-stream-p stream)
357 (ansi-stream-listen stream)
358 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
359 (stream-listen stream))))
361 #!-sb-fluid (declaim (inline ansi-stream-read-char-no-hang))
362 (defun ansi-stream-read-char-no-hang (stream eof-error-p eof-value recursive-p)
363 (if (funcall (ansi-stream-misc stream) stream :listen)
364 ;; On T or :EOF get READ-CHAR to do the work.
365 (ansi-stream-read-char stream eof-error-p eof-value recursive-p)
368 (defun read-char-no-hang (&optional (stream *standard-input*)
372 (let ((stream (in-synonym-of stream)))
373 (if (ansi-stream-p stream)
374 (ansi-stream-read-char-no-hang stream eof-error-p eof-value
376 ;; must be Gray streams FUNDAMENTAL-STREAM
377 (let ((char (stream-read-char-no-hang stream)))
379 (eof-or-lose stream eof-error-p eof-value)
382 #!-sb-fluid (declaim (inline ansi-stream-clear-input))
383 (defun ansi-stream-clear-input (stream)
384 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
385 (funcall (ansi-stream-misc stream) stream :clear-input))
387 (defun clear-input (&optional (stream *standard-input*))
388 (let ((stream (in-synonym-of stream)))
389 (if (ansi-stream-p stream)
390 (ansi-stream-clear-input stream)
391 ;; must be Gray streams FUNDAMENTAL-STREAM
392 (stream-clear-input stream)))
395 #!-sb-fluid (declaim (inline ansi-stream-read-byte))
396 (defun ansi-stream-read-byte (stream eof-error-p eof-value recursive-p)
397 ;; Why the "recursive-p" parameter? a-s-r-b is funcall'ed from
398 ;; a-s-read-sequence and needs a lambda list that's congruent with
399 ;; that of a-s-read-char
400 (declare (ignore recursive-p))
401 (prepare-for-fast-read-byte stream
403 (fast-read-byte eof-error-p eof-value t)
404 (done-with-fast-read-byte))))
406 (defun read-byte (stream &optional (eof-error-p t) eof-value)
407 (if (ansi-stream-p stream)
408 (ansi-stream-read-byte stream eof-error-p eof-value nil)
409 ;; must be Gray streams FUNDAMENTAL-STREAM
410 (let ((char (stream-read-byte stream)))
412 (eof-or-lose stream eof-error-p eof-value)
415 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
416 ;;; number of bytes read.
418 ;;; Note: CMU CL's version of this had a special interpretation of
419 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
420 ;;; case, CMU CL's version would return as soon as any data became
421 ;;; available.) This could be useful behavior for things like pipes in
422 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
423 ;;; If we ever need it, it could be added later as a new variant N-BIN
424 ;;; method (perhaps N-BIN-ASAP?) or something.
425 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
426 (declare (type ansi-stream stream)
427 (type index numbytes start)
428 (type (or (simple-array * (*)) system-area-pointer) buffer))
429 (let* ((stream (in-synonym-of stream ansi-stream))
430 (in-buffer (ansi-stream-in-buffer stream))
431 (index (ansi-stream-in-index stream))
432 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
433 (declare (fixnum index num-buffered))
436 (funcall (ansi-stream-n-bin stream)
442 ((<= numbytes num-buffered)
444 (let ((copy-function (typecase buffer
445 ((simple-array * (*)) #'ub8-bash-copy)
446 (system-area-pointer #'copy-ub8-to-system-area))))
447 (funcall copy-function in-buffer index buffer start numbytes))
448 (%byte-blt in-buffer index
449 buffer start (+ start numbytes))
450 (setf (ansi-stream-in-index stream) (+ index numbytes))
453 (let ((end (+ start num-buffered)))
455 (let ((copy-function (typecase buffer
456 ((simple-array * (*)) #'ub8-bash-copy)
457 (system-area-pointer #'copy-ub8-to-system-area))))
458 (funcall copy-function in-buffer index buffer start num-buffered))
459 (%byte-blt in-buffer index buffer start end)
460 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
461 (+ (funcall (ansi-stream-n-bin stream)
465 (- numbytes num-buffered)
469 ;;; the amount of space we leave at the start of the in-buffer for
472 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
473 (defconstant +ansi-stream-in-buffer-extra+
474 4) ; FIXME: should be symbolic constant
476 ;;; This function is called by the FAST-READ-CHAR expansion to refill
477 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
478 ;;; and hence must be an N-BIN method.
479 (defun fast-read-char-refill (stream eof-error-p eof-value)
480 (let* ((ibuf (ansi-stream-cin-buffer stream))
481 (count (funcall (ansi-stream-n-bin stream)
484 +ansi-stream-in-buffer-extra+
485 (- +ansi-stream-in-buffer-length+
486 +ansi-stream-in-buffer-extra+)
488 (start (- +ansi-stream-in-buffer-length+ count)))
489 (declare (type index start count))
491 (setf (ansi-stream-in-index stream)
492 +ansi-stream-in-buffer-length+)
493 (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
495 (when (/= start +ansi-stream-in-buffer-extra+)
496 (#.(let* ((n-character-array-bits
499 sb!vm:*specialized-array-element-type-properties*
500 :key #'sb!vm:saetp-specifier)))
501 (bash-function (intern (format nil "UB~D-BASH-COPY" n-character-array-bits)
502 (find-package "SB!KERNEL"))))
504 ibuf +ansi-stream-in-buffer-extra+
507 (setf (ansi-stream-in-index stream) (1+ start))
508 (aref ibuf start)))))
510 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
511 ;;; leave room for unreading.
512 (defun fast-read-byte-refill (stream eof-error-p eof-value)
513 (let* ((ibuf (ansi-stream-in-buffer stream))
514 (count (funcall (ansi-stream-n-bin stream) stream
515 ibuf 0 +ansi-stream-in-buffer-length+
517 (start (- +ansi-stream-in-buffer-length+ count)))
518 (declare (type index start count))
520 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
521 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
523 (unless (zerop start)
524 (ub8-bash-copy ibuf 0
527 (setf (ansi-stream-in-index stream) (1+ start))
528 (aref ibuf start)))))
532 (defun write-char (character &optional (stream *standard-output*))
533 (with-out-stream stream (ansi-stream-out character)
534 (stream-write-char character))
537 (defun terpri (&optional (stream *standard-output*))
538 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
541 #!-sb-fluid (declaim (inline ansi-stream-fresh-line))
542 (defun ansi-stream-fresh-line (stream)
543 (when (/= (or (charpos stream) 1) 0)
544 (funcall (ansi-stream-out stream) stream #\newline)
547 (defun fresh-line (&optional (stream *standard-output*))
548 (let ((stream (out-synonym-of stream)))
549 (if (ansi-stream-p stream)
550 (ansi-stream-fresh-line stream)
551 ;; must be Gray streams FUNDAMENTAL-STREAM
552 (stream-fresh-line stream))))
554 (defun write-string (string &optional (stream *standard-output*)
556 (declare (type string string))
557 ;; Note that even though you might expect, based on the behavior of
558 ;; things like AREF, that the correct upper bound here is
559 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
560 ;; "bounding index" and "length" indicate that in this case (i.e.
561 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
562 ;; (LENGTH STRING) is the required upper bound. A foolish
563 ;; consistency is the hobgoblin of lesser languages..
564 (%write-string string stream start (%check-vector-sequence-bounds
568 #!-sb-fluid (declaim (inline ansi-stream-write-string))
569 (defun ansi-stream-write-string (string stream start end)
570 (declare (type string string))
571 (declare (type ansi-stream stream))
572 (declare (type index start end))
573 (if (array-header-p string)
574 (with-array-data ((data string) (offset-start start)
576 (funcall (ansi-stream-sout stream)
577 stream data offset-start offset-end))
578 (funcall (ansi-stream-sout stream) stream string start end))
581 (defun %write-string (string stream start end)
582 (declare (type string string))
583 (declare (type stream-designator stream))
584 (declare (type index start end))
585 (let ((stream (out-synonym-of stream)))
586 (if(ansi-stream-p stream)
587 (ansi-stream-write-string string stream start end)
588 ;; must be Gray streams FUNDAMENTAL-STREAM
589 (stream-write-string stream string start end))))
591 ;;; A wrapper function for all those (MACROLET OUT-FUN) definitions,
592 ;;; which cannot deal with keyword arguments.
593 (declaim (inline write-string-no-key))
594 (defun write-string-no-key (string stream start end)
595 (write-string string stream :start start :end end))
597 (defun write-line (string &optional (stream *standard-output*)
599 (declare (type string string))
600 ;; FIXME: Why is there this difference between the treatments of the
601 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
602 (let ((defaulted-stream (out-synonym-of stream)))
603 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
605 (write-char #\newline defaulted-stream))
608 (defun charpos (&optional (stream *standard-output*))
609 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
611 (defun line-length (&optional (stream *standard-output*))
612 (with-out-stream stream (ansi-stream-misc :line-length)
613 (stream-line-length)))
615 (defun finish-output (&optional (stream *standard-output*))
616 (with-out-stream stream (ansi-stream-misc :finish-output)
617 (stream-finish-output))
620 (defun force-output (&optional (stream *standard-output*))
621 (with-out-stream stream (ansi-stream-misc :force-output)
622 (stream-force-output))
625 (defun clear-output (&optional (stream *standard-output*))
626 (with-out-stream stream (ansi-stream-misc :clear-output)
627 (stream-force-output))
630 (defun write-byte (integer stream)
631 (with-out-stream/no-synonym stream (ansi-stream-bout integer)
632 (stream-write-byte integer))
636 ;;; (These were inline throughout this file, but that's not appropriate
637 ;;; globally. And we must not inline them in the rest of this file if
638 ;;; dispatch to gray or simple streams is to work, since both redefine
639 ;;; these functions later.)
640 (declaim (notinline read-char unread-char read-byte listen))
642 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
643 ;;; streams to handle the misc routines and dispatch to the
644 ;;; appropriate SIMPLE- or FUNDAMENTAL-STREAM functions.
645 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
646 (declare (type stream stream) (ignore arg2))
649 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
650 (let ((char (read-char-no-hang stream nil :eof)))
651 (when (characterp char)
652 (unread-char char stream))
655 (unread-char arg1 stream))
659 (clear-input stream))
661 (force-output stream))
663 (finish-output stream))
665 (stream-element-type stream))
666 (:stream-external-format
667 (stream-external-format stream))
669 (interactive-stream-p stream))
671 (line-length stream))
675 (file-length stream))
677 (file-string-length stream arg1))
679 (file-position stream arg1))))
681 ;;;; broadcast streams
683 (defstruct (broadcast-stream (:include ansi-stream
684 (out #'broadcast-out)
685 (bout #'broadcast-bout)
686 (sout #'broadcast-sout)
687 (misc #'broadcast-misc))
688 (:constructor %make-broadcast-stream
691 ;; a list of all the streams we broadcast to
692 (streams () :type list :read-only t))
694 (defun make-broadcast-stream (&rest streams)
695 (dolist (stream streams)
696 (unless (output-stream-p stream)
699 :expected-type '(satisfies output-stream-p))))
700 (apply #'%make-broadcast-stream streams))
702 (macrolet ((out-fun (name fun &rest args)
703 `(defun ,name (stream ,@args)
704 (dolist (stream (broadcast-stream-streams stream))
705 (,fun ,(car args) stream ,@(cdr args))))))
706 (out-fun broadcast-out write-char char)
707 (out-fun broadcast-bout write-byte byte)
708 (out-fun broadcast-sout write-string-no-key string start end))
710 (defun broadcast-misc (stream operation &optional arg1 arg2)
711 (let ((streams (broadcast-stream-streams stream)))
713 ;; FIXME: This may not be the best place to note this, but I
714 ;; think the :CHARPOS protocol needs revision. Firstly, I think
715 ;; this is the last place where a NULL return value was possible
716 ;; (before adjusting it to be 0), so a bunch of conditionals IF
717 ;; CHARPOS can be removed; secondly, it is my belief that
718 ;; FD-STREAMS, when running FILE-POSITION, do not update the
719 ;; CHARPOS, and consequently there will be much wrongness.
721 ;; FIXME: see also TWO-WAY-STREAM treatment of :CHARPOS -- why
722 ;; is it testing the :charpos of an input stream?
724 ;; -- CSR, 2004-02-04
726 (dolist (stream streams 0)
727 (let ((charpos (charpos stream)))
728 (if charpos (return charpos)))))
731 (dolist (stream streams min)
732 (let ((res (line-length stream)))
733 (when res (setq min (if min (min res min) res)))))))
735 #+nil ; old, arguably more logical, version
737 (dolist (stream streams (if (> (length res) 1) `(and ,@res) t))
738 (pushnew (stream-element-type stream) res :test #'equal)))
739 ;; ANSI-specified version (under System Class BROADCAST-STREAM)
741 (do ((streams streams (cdr streams)))
743 (when (null (cdr streams))
744 (setq res (stream-element-type (car streams)))))))
746 (let ((res :default))
747 (dolist (stream streams res)
748 (setq res (stream-external-format stream)))))
750 (let ((last (last streams)))
752 (file-length (car last))
756 (let ((res (or (eql arg1 :start) (eql arg1 0))))
757 (dolist (stream streams res)
758 (setq res (file-position stream arg1))))
760 (dolist (stream streams res)
761 (setq res (file-position stream))))))
764 (dolist (stream streams res)
765 (setq res (file-string-length stream arg1)))))
767 (set-closed-flame stream))
770 (dolist (stream streams res)
772 (if (ansi-stream-p stream)
773 (funcall (ansi-stream-misc stream) stream operation
775 (stream-misc-dispatch stream operation arg1 arg2)))))))))
779 (defstruct (synonym-stream (:include ansi-stream
782 (n-bin #'synonym-n-bin)
784 (bout #'synonym-bout)
785 (sout #'synonym-sout)
786 (misc #'synonym-misc))
787 (:constructor make-synonym-stream (symbol))
789 ;; This is the symbol, the value of which is the stream we are synonym to.
790 (symbol nil :type symbol :read-only t))
791 (def!method print-object ((x synonym-stream) stream)
792 (print-unreadable-object (x stream :type t :identity t)
793 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
795 ;;; The output simple output methods just call the corresponding
796 ;;; function on the synonymed stream.
797 (macrolet ((out-fun (name fun &rest args)
798 `(defun ,name (stream ,@args)
799 (declare (optimize (safety 1)))
800 (let ((syn (symbol-value (synonym-stream-symbol stream))))
801 (,fun ,(car args) syn ,@(cdr args))))))
802 (out-fun synonym-out write-char ch)
803 (out-fun synonym-bout write-byte n)
804 (out-fun synonym-sout write-string-no-key string start end))
806 ;;; For the input methods, we just call the corresponding function on the
807 ;;; synonymed stream. These functions deal with getting input out of
808 ;;; the In-Buffer if there is any.
809 (macrolet ((in-fun (name fun &rest args)
810 `(defun ,name (stream ,@args)
811 (declare (optimize (safety 1)))
812 (,fun (symbol-value (synonym-stream-symbol stream))
814 (in-fun synonym-in read-char eof-error-p eof-value)
815 (in-fun synonym-bin read-byte eof-error-p eof-value)
816 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
818 (defun synonym-misc (stream operation &optional arg1 arg2)
819 (declare (optimize (safety 1)))
820 (let ((syn (symbol-value (synonym-stream-symbol stream))))
821 (if (ansi-stream-p syn)
822 ;; We have to special-case some operations which interact with
823 ;; the in-buffer of the wrapped stream, since just calling
824 ;; ANSI-STREAM-MISC on them
826 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
827 +ansi-stream-in-buffer-length+)
828 (funcall (ansi-stream-misc syn) syn :listen)))
829 (:clear-input (clear-input syn))
830 (:unread (unread-char arg1 syn))
832 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
833 (stream-misc-dispatch syn operation arg1 arg2))))
837 (defstruct (two-way-stream
838 (:include ansi-stream
841 (n-bin #'two-way-n-bin)
843 (bout #'two-way-bout)
844 (sout #'two-way-sout)
845 (misc #'two-way-misc))
846 (:constructor %make-two-way-stream (input-stream output-stream))
848 (input-stream (missing-arg) :type stream :read-only t)
849 (output-stream (missing-arg) :type stream :read-only t))
850 (defprinter (two-way-stream) input-stream output-stream)
852 (defun make-two-way-stream (input-stream output-stream)
854 "Return a bidirectional stream which gets its input from INPUT-STREAM and
855 sends its output to OUTPUT-STREAM."
856 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
857 ;; should be encapsulated in a function, and used here and most of
858 ;; the other places that SYNONYM-STREAM-P appears.
859 (unless (output-stream-p output-stream)
862 :expected-type '(satisfies output-stream-p)))
863 (unless (input-stream-p input-stream)
866 :expected-type '(satisfies input-stream-p)))
867 (funcall #'%make-two-way-stream input-stream output-stream))
869 (macrolet ((out-fun (name fun &rest args)
870 `(defun ,name (stream ,@args)
871 (let ((syn (two-way-stream-output-stream stream)))
872 (,fun ,(car args) syn ,@(cdr args))))))
873 (out-fun two-way-out write-char ch)
874 (out-fun two-way-bout write-byte n)
875 (out-fun two-way-sout write-string-no-key string start end))
877 (macrolet ((in-fun (name fun &rest args)
878 `(defun ,name (stream ,@args)
879 (force-output (two-way-stream-output-stream stream))
880 (,fun (two-way-stream-input-stream stream) ,@args))))
881 (in-fun two-way-in read-char eof-error-p eof-value)
882 (in-fun two-way-bin read-byte eof-error-p eof-value)
883 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
885 (defun two-way-misc (stream operation &optional arg1 arg2)
886 (let* ((in (two-way-stream-input-stream stream))
887 (out (two-way-stream-output-stream stream))
888 (in-ansi-stream-p (ansi-stream-p in))
889 (out-ansi-stream-p (ansi-stream-p out)))
893 (or (/= (the fixnum (ansi-stream-in-index in))
894 +ansi-stream-in-buffer-length+)
895 (funcall (ansi-stream-misc in) in :listen))
897 ((:finish-output :force-output :clear-output)
898 (if out-ansi-stream-p
899 (funcall (ansi-stream-misc out) out operation arg1 arg2)
900 (stream-misc-dispatch out operation arg1 arg2)))
901 (:clear-input (clear-input in))
902 (:unread (unread-char arg1 in))
904 (let ((in-type (stream-element-type in))
905 (out-type (stream-element-type out)))
906 (if (equal in-type out-type)
907 in-type `(and ,in-type ,out-type))))
909 (set-closed-flame stream))
911 (or (if in-ansi-stream-p
912 (funcall (ansi-stream-misc in) in operation arg1 arg2)
913 (stream-misc-dispatch in operation arg1 arg2))
914 (if out-ansi-stream-p
915 (funcall (ansi-stream-misc out) out operation arg1 arg2)
916 (stream-misc-dispatch out operation arg1 arg2)))))))
918 ;;;; concatenated streams
920 (defstruct (concatenated-stream
921 (:include ansi-stream
922 (in #'concatenated-in)
923 (bin #'concatenated-bin)
924 (n-bin #'concatenated-n-bin)
925 (misc #'concatenated-misc))
926 (:constructor %make-concatenated-stream (&rest streams))
928 ;; The car of this is the substream we are reading from now.
929 (streams nil :type list))
930 (def!method print-object ((x concatenated-stream) stream)
931 (print-unreadable-object (x stream :type t :identity t)
934 (concatenated-stream-streams x))))
936 (defun make-concatenated-stream (&rest streams)
938 "Return a stream which takes its input from each of the streams in turn,
939 going on to the next at EOF."
940 (dolist (stream streams)
941 (unless (input-stream-p stream)
944 :expected-type '(satisfies input-stream-p))))
945 (apply #'%make-concatenated-stream streams))
947 (macrolet ((in-fun (name fun)
948 `(defun ,name (stream eof-error-p eof-value)
949 (do ((streams (concatenated-stream-streams stream)
952 (eof-or-lose stream eof-error-p eof-value))
953 (let* ((stream (car streams))
954 (result (,fun stream nil nil)))
955 (when result (return result)))
956 (pop (concatenated-stream-streams stream))))))
957 (in-fun concatenated-in read-char)
958 (in-fun concatenated-bin read-byte))
960 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
961 (do ((streams (concatenated-stream-streams stream) (cdr streams))
962 (current-start start)
963 (remaining-bytes numbytes))
966 (error 'end-of-file :stream stream)
967 (- numbytes remaining-bytes)))
968 (let* ((stream (car streams))
969 (bytes-read (read-n-bytes stream buffer current-start
970 remaining-bytes nil)))
971 (incf current-start bytes-read)
972 (decf remaining-bytes bytes-read)
973 (when (zerop remaining-bytes) (return numbytes)))
974 (setf (concatenated-stream-streams stream) (cdr streams))))
976 (defun concatenated-misc (stream operation &optional arg1 arg2)
977 (let* ((left (concatenated-stream-streams stream))
978 (current (car left)))
982 (return-from concatenated-misc :eof))
984 (let ((stuff (if (ansi-stream-p current)
985 (funcall (ansi-stream-misc current) current
987 (stream-misc-dispatch current :listen))))
988 (cond ((eq stuff :eof)
989 ;; Advance STREAMS, and try again.
990 (pop (concatenated-stream-streams stream))
992 (car (concatenated-stream-streams stream)))
994 ;; No further streams. EOF.
997 ;; Stuff's available.
1000 ;; Nothing is available yet.
1002 (:clear-input (when left (clear-input current)))
1003 (:unread (when left (unread-char arg1 current)))
1005 (set-closed-flame stream))
1008 (if (ansi-stream-p current)
1009 (funcall (ansi-stream-misc current) current operation arg1 arg2)
1010 (stream-misc-dispatch current operation arg1 arg2)))))))
1014 (defstruct (echo-stream
1015 (:include two-way-stream
1019 (n-bin #'echo-n-bin))
1020 (:constructor %make-echo-stream (input-stream output-stream))
1023 (def!method print-object ((x echo-stream) stream)
1024 (print-unreadable-object (x stream :type t :identity t)
1026 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1027 (two-way-stream-input-stream x)
1028 (two-way-stream-output-stream x))))
1030 (defun make-echo-stream (input-stream output-stream)
1032 "Return a bidirectional stream which gets its input from INPUT-STREAM and
1033 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
1035 (unless (output-stream-p output-stream)
1037 :datum output-stream
1038 :expected-type '(satisfies output-stream-p)))
1039 (unless (input-stream-p input-stream)
1042 :expected-type '(satisfies input-stream-p)))
1043 (funcall #'%make-echo-stream input-stream output-stream))
1045 (macrolet ((in-fun (name in-fun out-fun &rest args)
1046 `(defun ,name (stream ,@args)
1047 (or (pop (echo-stream-unread-stuff stream))
1048 (let* ((in (echo-stream-input-stream stream))
1049 (out (echo-stream-output-stream stream))
1050 (result (if eof-error-p
1052 (,in-fun in nil in))))
1054 ((eql result in) eof-value)
1055 (t (,out-fun result out) result)))))))
1056 (in-fun echo-in read-char write-char eof-error-p eof-value)
1057 (in-fun echo-bin read-byte write-byte eof-error-p eof-value))
1059 (defun echo-n-bin (stream buffer start numbytes eof-error-p)
1060 (let ((new-start start)
1063 (let ((thing (pop (echo-stream-unread-stuff stream))))
1066 (setf (aref buffer new-start) thing)
1069 (when (= read numbytes)
1070 (return-from echo-n-bin numbytes)))
1072 (let ((bytes-read (read-n-bytes (echo-stream-input-stream stream) buffer
1073 new-start (- numbytes read) nil)))
1076 (write-sequence buffer (echo-stream-output-stream stream)
1077 :start new-start :end (+ new-start bytes-read))
1078 (+ bytes-read read))
1079 ((> numbytes (+ read bytes-read))
1080 (write-sequence buffer (echo-stream-output-stream stream)
1081 :start new-start :end (+ new-start bytes-read))
1082 (error 'end-of-file :stream stream))
1084 (write-sequence buffer (echo-stream-output-stream stream)
1085 :start new-start :end (+ new-start bytes-read))
1086 (aver (= numbytes (+ new-start bytes-read)))
1089 ;;;; STRING-INPUT-STREAM stuff
1091 (defstruct (string-input-stream
1092 (:include ansi-stream
1096 (misc #'string-in-misc))
1097 (:constructor internal-make-string-input-stream
1098 (string current end))
1100 (string (missing-arg) :type simple-string)
1101 (current (missing-arg) :type index)
1102 (end (missing-arg) :type index))
1104 (defun string-inch (stream eof-error-p eof-value)
1105 (declare (type string-input-stream stream))
1106 (let ((string (string-input-stream-string stream))
1107 (index (string-input-stream-current stream)))
1108 (cond ((>= index (the index (string-input-stream-end stream)))
1109 (eof-or-lose stream eof-error-p eof-value))
1111 (setf (string-input-stream-current stream) (1+ index))
1112 (char string index)))))
1114 (defun string-binch (stream eof-error-p eof-value)
1115 (declare (type string-input-stream stream))
1116 (let ((string (string-input-stream-string stream))
1117 (index (string-input-stream-current stream)))
1118 (cond ((>= index (the index (string-input-stream-end stream)))
1119 (eof-or-lose stream eof-error-p eof-value))
1121 (setf (string-input-stream-current stream) (1+ index))
1122 (char-code (char string index))))))
1124 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1125 (declare (type string-input-stream stream)
1126 (type index start requested))
1127 (let* ((string (string-input-stream-string stream))
1128 (index (string-input-stream-current stream))
1129 (available (- (string-input-stream-end stream) index))
1130 (copy (min available requested)))
1131 (declare (type simple-string string))
1133 (setf (string-input-stream-current stream)
1134 (truly-the index (+ index copy)))
1135 ;; FIXME: why are we VECTOR-SAP'ing things here? what's the point?
1136 ;; and are there SB-UNICODE issues here as well? --njf, 2005-03-24
1137 (sb!sys:without-gcing
1138 (system-area-ub8-copy (vector-sap string)
1140 (if (typep buffer 'system-area-pointer)
1142 (vector-sap buffer))
1145 (if (and (> requested copy) eof-error-p)
1146 (error 'end-of-file :stream stream)
1149 (defun string-in-misc (stream operation &optional arg1 arg2)
1150 (declare (type string-input-stream stream)
1155 (setf (string-input-stream-current stream)
1158 (:end (string-input-stream-end stream))
1159 ;; We allow moving position beyond EOF. Errors happen
1160 ;; on read, not move -- or the user may extend the
1163 (string-input-stream-current stream)))
1164 ;; According to ANSI: "Should signal an error of type type-error
1165 ;; if stream is not a stream associated with a file."
1166 ;; This is checked by FILE-LENGTH, so no need to do it here either.
1167 ;; (:file-length (length (string-input-stream-string stream)))
1168 (:unread (decf (string-input-stream-current stream)))
1169 (:close (set-closed-flame stream))
1170 (:listen (or (/= (the index (string-input-stream-current stream))
1171 (the index (string-input-stream-end stream)))
1173 (:element-type (array-element-type (string-input-stream-string stream)))))
1175 (defun make-string-input-stream (string &optional (start 0) end)
1177 "Return an input stream which will supply the characters of STRING between
1178 START and END in order."
1179 (declare (type string string)
1181 (type (or index null) end))
1182 (let* ((string (coerce string '(simple-array character (*))))
1183 (end (%check-vector-sequence-bounds string start end)))
1184 (with-array-data ((string string) (start start) (end end))
1185 (internal-make-string-input-stream
1186 string ;; now simple
1190 ;;;; STRING-OUTPUT-STREAM stuff
1192 (defstruct (string-output-stream
1193 (:include ansi-stream
1195 (sout #'string-sout)
1196 (misc #'string-out-misc))
1197 (:constructor make-string-output-stream
1198 (&key (element-type 'character)
1199 &aux (string (make-string 40))))
1201 ;; The string we throw stuff in.
1202 (string (missing-arg) :type (simple-array character (*)))
1203 ;; Index of the next location to use.
1204 (index 0 :type fixnum)
1205 ;; Index cache for string-output-stream-last-index
1206 (index-cache 0 :type fixnum)
1207 ;; Requested element type
1208 (element-type 'character))
1211 (setf (fdocumentation 'make-string-output-stream 'function)
1212 "Return an output stream which will accumulate all output given it for
1213 the benefit of the function GET-OUTPUT-STREAM-STRING.")
1215 (defun string-output-stream-last-index (stream)
1216 (max (string-output-stream-index stream)
1217 (string-output-stream-index-cache stream)))
1219 (defun string-ouch (stream character)
1220 (let ((current (string-output-stream-index stream))
1221 (workspace (string-output-stream-string stream)))
1222 (declare (type (simple-array character (*)) workspace)
1223 (type fixnum current))
1224 (if (= current (the fixnum (length workspace)))
1225 (let ((new-workspace (make-string (* current 2))))
1226 (replace new-workspace workspace)
1227 (setf (aref new-workspace current) character
1228 (string-output-stream-string stream) new-workspace))
1229 (setf (aref workspace current) character))
1230 (setf (string-output-stream-index stream) (1+ current))))
1232 (defun string-sout (stream string start end)
1233 (declare (type simple-string string)
1234 (type fixnum start end))
1235 (let* ((string (if (typep string '(simple-array character (*)))
1237 (coerce string '(simple-array character (*)))))
1238 (current (string-output-stream-index stream))
1239 (length (- end start))
1240 (dst-end (+ length current))
1241 (workspace (string-output-stream-string stream)))
1242 (declare (type (simple-array character (*)) workspace string)
1243 (type fixnum current length dst-end))
1244 (if (> dst-end (the fixnum (length workspace)))
1245 (let ((new-workspace (make-string (+ (* current 2) length))))
1246 (replace new-workspace workspace :end2 current)
1247 (replace new-workspace string
1248 :start1 current :end1 dst-end
1249 :start2 start :end2 end)
1250 (setf (string-output-stream-string stream) new-workspace))
1251 (replace workspace string
1252 :start1 current :end1 dst-end
1253 :start2 start :end2 end))
1254 (setf (string-output-stream-index stream) dst-end)))
1256 (defun string-out-misc (stream operation &optional arg1 arg2)
1257 (declare (ignore arg2))
1261 (let ((end (string-output-stream-last-index stream)))
1262 (setf (string-output-stream-index-cache stream) end
1263 (string-output-stream-index stream)
1268 ;; We allow moving beyond the end of stream,
1269 ;; implicitly extending the output stream.
1270 (let ((buffer (string-output-stream-string stream)))
1271 (when (> arg1 (length buffer))
1272 (setf (string-output-stream-string stream)
1274 arg1 :element-type (array-element-type buffer))
1275 (subseq (string-output-stream-string stream)
1277 (subseq buffer 0 end))))
1279 (string-output-stream-index stream)))
1280 (:close (set-closed-flame stream))
1282 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1284 (count 0 (1+ count))
1285 (string (string-output-stream-string stream)))
1287 (declare (type (simple-array character (*)) string)
1288 (type fixnum index count))
1289 (if (char= (schar string index) #\newline)
1291 (:element-type (array-element-type (string-output-stream-string stream)))))
1293 ;;; Return a string of all the characters sent to a stream made by
1294 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1295 (defun get-output-stream-string (stream)
1296 (declare (type string-output-stream stream))
1297 (let* ((length (string-output-stream-last-index stream))
1298 (element-type (string-output-stream-element-type stream))
1301 ;; overwhelmingly common case: can be inlined
1302 ((character) (make-string length))
1303 ;; slightly less common cases: inline it anyway
1304 ((base-char standard-char)
1305 (make-string length :element-type 'base-char))
1306 (t (make-string length :element-type element-type)))))
1307 ;; For the benefit of the REPLACE transform, let's do this, so
1308 ;; that the common case isn't ludicrously expensive.
1310 ((simple-array character (*))
1311 (replace result (string-output-stream-string stream)))
1313 (replace result (string-output-stream-string stream)))
1314 ((simple-array nil (*))
1315 (replace result (string-output-stream-string stream))))
1316 (setf (string-output-stream-index stream) 0
1317 (string-output-stream-index-cache stream) 0)
1320 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1321 ;;; GET-OUTPUT-STREAM-STRING would return them.
1322 (defun dump-output-stream-string (in-stream out-stream)
1323 (%write-string (string-output-stream-string in-stream)
1326 (string-output-stream-last-index in-stream))
1327 (setf (string-output-stream-index in-stream) 0
1328 (string-output-stream-index-cache in-stream) 0))
1330 ;;;; fill-pointer streams
1332 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1333 ;;; the CLM, but they are required for the implementation of
1334 ;;; WITH-OUTPUT-TO-STRING.
1336 ;;; FIXME: need to support (VECTOR BASE-CHAR) and (VECTOR NIL),
1337 ;;; ideally without destroying all hope of efficiency.
1338 (deftype string-with-fill-pointer ()
1339 '(and (vector character)
1340 (satisfies array-has-fill-pointer-p)))
1342 (defstruct (fill-pointer-output-stream
1343 (:include ansi-stream
1344 (out #'fill-pointer-ouch)
1345 (sout #'fill-pointer-sout)
1346 (misc #'fill-pointer-misc))
1347 (:constructor make-fill-pointer-output-stream (string))
1349 ;; a string with a fill pointer where we stuff the stuff we write
1350 (string (missing-arg) :type string-with-fill-pointer :read-only t))
1352 (defun fill-pointer-ouch (stream character)
1353 (let* ((buffer (fill-pointer-output-stream-string stream))
1354 (current (fill-pointer buffer))
1355 (current+1 (1+ current)))
1356 (declare (fixnum current))
1357 (with-array-data ((workspace buffer) (start) (end))
1358 (declare (type (simple-array character (*)) workspace))
1359 (let ((offset-current (+ start current)))
1360 (declare (fixnum offset-current))
1361 (if (= offset-current end)
1362 (let* ((new-length (1+ (* current 2)))
1363 (new-workspace (make-string new-length)))
1364 (declare (type (simple-array character (*)) new-workspace))
1365 (replace new-workspace workspace
1366 :start2 start :end2 offset-current)
1367 (setf workspace new-workspace
1368 offset-current current)
1369 (set-array-header buffer workspace new-length
1370 current+1 0 new-length nil))
1371 (setf (fill-pointer buffer) current+1))
1372 (setf (schar workspace offset-current) character)))
1375 (defun fill-pointer-sout (stream string start end)
1376 (declare (simple-string string) (fixnum start end))
1377 (let* ((string (if (typep string '(simple-array character (*)))
1379 (coerce string '(simple-array character (*)))))
1380 (buffer (fill-pointer-output-stream-string stream))
1381 (current (fill-pointer buffer))
1382 (string-len (- end start))
1383 (dst-end (+ string-len current)))
1384 (declare (fixnum current dst-end string-len))
1385 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1386 (declare (type (simple-array character (*)) workspace))
1387 (let ((offset-dst-end (+ dst-start dst-end))
1388 (offset-current (+ dst-start current)))
1389 (declare (fixnum offset-dst-end offset-current))
1390 (if (> offset-dst-end dst-length)
1391 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1392 (new-workspace (make-string new-length)))
1393 (declare (type (simple-array character (*)) new-workspace))
1394 (replace new-workspace workspace
1395 :start2 dst-start :end2 offset-current)
1396 (setf workspace new-workspace
1397 offset-current current
1398 offset-dst-end dst-end)
1399 (set-array-header buffer workspace new-length
1400 dst-end 0 new-length nil))
1401 (setf (fill-pointer buffer) dst-end))
1402 (replace workspace string
1403 :start1 offset-current :start2 start :end2 end)))
1406 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1407 (declare (ignore arg2))
1410 (let ((buffer (fill-pointer-output-stream-string stream)))
1412 (setf (fill-pointer buffer)
1415 ;; Fill-pointer is always at fill-pointer we will
1416 ;; make :END move to the end of the actual string.
1417 (:end (array-total-size buffer))
1418 ;; We allow moving beyond the end of string if the
1419 ;; string is adjustable.
1420 (t (when (>= arg1 (array-total-size buffer))
1421 (if (adjustable-array-p buffer)
1422 (adjust-array buffer arg1)
1423 (error "Cannot move FILE-POSITION beyond the end ~
1424 of WITH-OUTPUT-TO-STRING stream ~
1425 constructed with non-adjustable string.")))
1427 (fill-pointer buffer))))
1429 (let* ((buffer (fill-pointer-output-stream-string stream))
1430 (current (fill-pointer buffer)))
1431 (with-array-data ((string buffer) (start) (end current))
1432 (declare (simple-string string) (ignore start))
1433 (let ((found (position #\newline string :test #'char=
1434 :end end :from-end t)))
1436 (- end (the fixnum found))
1438 (:element-type (array-element-type
1439 (fill-pointer-output-stream-string stream)))))
1441 ;;;; indenting streams
1443 (defstruct (indenting-stream (:include ansi-stream
1444 (out #'indenting-out)
1445 (sout #'indenting-sout)
1446 (misc #'indenting-misc))
1447 (:constructor make-indenting-stream (stream))
1449 ;; the stream we're based on
1451 ;; how much we indent on each line
1455 (setf (fdocumentation 'make-indenting-stream 'function)
1456 "Return an output stream which indents its output by some amount.")
1458 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1459 ;;; output on the given STREAM based on the specified SUB-STREAM.
1460 (defmacro indenting-indent (stream sub-stream)
1461 ;; KLUDGE: bare magic number 60
1462 `(do ((i 0 (+ i 60))
1463 (indentation (indenting-stream-indentation ,stream)))
1464 ((>= i indentation))
1466 #.(make-string 60 :initial-element #\Space)
1469 (min 60 (- indentation i)))))
1471 ;;; INDENTING-OUT writes a character to an indenting stream.
1472 (defun indenting-out (stream char)
1473 (let ((sub-stream (indenting-stream-stream stream)))
1474 (write-char char sub-stream)
1475 (if (char= char #\newline)
1476 (indenting-indent stream sub-stream))))
1478 ;;; INDENTING-SOUT writes a string to an indenting stream.
1479 (defun indenting-sout (stream string start end)
1480 (declare (simple-string string) (fixnum start end))
1482 (sub-stream (indenting-stream-stream stream)))
1484 (let ((newline (position #\newline string :start i :end end)))
1486 (%write-string string sub-stream i (1+ newline))
1487 (indenting-indent stream sub-stream)
1488 (setq i (+ newline 1)))
1490 (%write-string string sub-stream i end)
1493 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1494 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1495 ;;; the base stream minus the stream's indentation.
1496 (defun indenting-misc (stream operation &optional arg1 arg2)
1497 (let ((sub-stream (indenting-stream-stream stream)))
1498 (if (ansi-stream-p sub-stream)
1499 (let ((method (ansi-stream-misc sub-stream)))
1502 (let ((line-length (funcall method sub-stream operation)))
1504 (- line-length (indenting-stream-indentation stream)))))
1506 (let ((charpos (funcall method sub-stream operation)))
1508 (- charpos (indenting-stream-indentation stream)))))
1510 (funcall method sub-stream operation arg1 arg2))))
1511 ;; must be Gray streams FUNDAMENTAL-STREAM
1514 (let ((line-length (stream-line-length sub-stream)))
1516 (- line-length (indenting-stream-indentation stream)))))
1518 (let ((charpos (stream-line-column sub-stream)))
1520 (- charpos (indenting-stream-indentation stream)))))
1522 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1524 (declaim (maybe-inline read-char unread-char read-byte listen))
1526 ;;;; case frobbing streams, used by FORMAT ~(...~)
1528 (defstruct (case-frob-stream
1529 (:include ansi-stream
1530 (misc #'case-frob-misc))
1531 (:constructor %make-case-frob-stream (target out sout))
1533 (target (missing-arg) :type stream))
1535 (defun make-case-frob-stream (target kind)
1537 "Return a stream that sends all output to the stream TARGET, but modifies
1538 the case of letters, depending on KIND, which should be one of:
1539 :UPCASE - convert to upper case.
1540 :DOWNCASE - convert to lower case.
1541 :CAPITALIZE - convert the first letter of words to upper case and the
1542 rest of the word to lower case.
1543 :CAPITALIZE-FIRST - convert the first letter of the first word to upper
1544 case and everything else to lower case."
1545 (declare (type stream target)
1546 (type (member :upcase :downcase :capitalize :capitalize-first)
1549 (if (case-frob-stream-p target)
1550 ;; If we are going to be writing to a stream that already does
1551 ;; case frobbing, why bother frobbing the case just so it can
1554 (multiple-value-bind (out sout)
1557 (values #'case-frob-upcase-out
1558 #'case-frob-upcase-sout))
1560 (values #'case-frob-downcase-out
1561 #'case-frob-downcase-sout))
1563 (values #'case-frob-capitalize-out
1564 #'case-frob-capitalize-sout))
1566 (values #'case-frob-capitalize-first-out
1567 #'case-frob-capitalize-first-sout)))
1568 (%make-case-frob-stream target out sout))))
1570 (defun case-frob-misc (stream op &optional arg1 arg2)
1571 (declare (type case-frob-stream stream))
1574 (set-closed-flame stream))
1576 (let ((target (case-frob-stream-target stream)))
1577 (if (ansi-stream-p target)
1578 (funcall (ansi-stream-misc target) target op arg1 arg2)
1579 (stream-misc-dispatch target op arg1 arg2))))))
1581 (defun case-frob-upcase-out (stream char)
1582 (declare (type case-frob-stream stream)
1583 (type character char))
1584 (let ((target (case-frob-stream-target stream))
1585 (char (char-upcase char)))
1586 (if (ansi-stream-p target)
1587 (funcall (ansi-stream-out target) target char)
1588 (stream-write-char target char))))
1590 (defun case-frob-upcase-sout (stream str start end)
1591 (declare (type case-frob-stream stream)
1592 (type simple-string str)
1594 (type (or index null) end))
1595 (let* ((target (case-frob-stream-target stream))
1598 (string (if (and (zerop start) (= len end))
1600 (nstring-upcase (subseq str start end))))
1601 (string-len (- end start)))
1602 (if (ansi-stream-p target)
1603 (funcall (ansi-stream-sout target) target string 0 string-len)
1604 (stream-write-string target string 0 string-len))))
1606 (defun case-frob-downcase-out (stream char)
1607 (declare (type case-frob-stream stream)
1608 (type character char))
1609 (let ((target (case-frob-stream-target stream))
1610 (char (char-downcase char)))
1611 (if (ansi-stream-p target)
1612 (funcall (ansi-stream-out target) target char)
1613 (stream-write-char target char))))
1615 (defun case-frob-downcase-sout (stream str start end)
1616 (declare (type case-frob-stream stream)
1617 (type simple-string str)
1619 (type (or index null) end))
1620 (let* ((target (case-frob-stream-target stream))
1623 (string (if (and (zerop start) (= len end))
1624 (string-downcase str)
1625 (nstring-downcase (subseq str start end))))
1626 (string-len (- end start)))
1627 (if (ansi-stream-p target)
1628 (funcall (ansi-stream-sout target) target string 0 string-len)
1629 (stream-write-string target string 0 string-len))))
1631 (defun case-frob-capitalize-out (stream char)
1632 (declare (type case-frob-stream stream)
1633 (type character char))
1634 (let ((target (case-frob-stream-target stream)))
1635 (cond ((alphanumericp char)
1636 (let ((char (char-upcase char)))
1637 (if (ansi-stream-p target)
1638 (funcall (ansi-stream-out target) target char)
1639 (stream-write-char target char)))
1640 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1641 (setf (case-frob-stream-sout stream)
1642 #'case-frob-capitalize-aux-sout))
1644 (if (ansi-stream-p target)
1645 (funcall (ansi-stream-out target) target char)
1646 (stream-write-char target char))))))
1648 (defun case-frob-capitalize-sout (stream str start end)
1649 (declare (type case-frob-stream stream)
1650 (type simple-string str)
1652 (type (or index null) end))
1653 (let* ((target (case-frob-stream-target stream))
1654 (str (subseq str start end))
1658 (let ((char (schar str i)))
1659 (cond ((not (alphanumericp char))
1660 (setf inside-word nil))
1662 (setf (schar str i) (char-downcase char)))
1664 (setf inside-word t)
1665 (setf (schar str i) (char-upcase char))))))
1667 (setf (case-frob-stream-out stream)
1668 #'case-frob-capitalize-aux-out)
1669 (setf (case-frob-stream-sout stream)
1670 #'case-frob-capitalize-aux-sout))
1671 (if (ansi-stream-p target)
1672 (funcall (ansi-stream-sout target) target str 0 len)
1673 (stream-write-string target str 0 len))))
1675 (defun case-frob-capitalize-aux-out (stream char)
1676 (declare (type case-frob-stream stream)
1677 (type character char))
1678 (let ((target (case-frob-stream-target stream)))
1679 (cond ((alphanumericp char)
1680 (let ((char (char-downcase char)))
1681 (if (ansi-stream-p target)
1682 (funcall (ansi-stream-out target) target char)
1683 (stream-write-char target char))))
1685 (if (ansi-stream-p target)
1686 (funcall (ansi-stream-out target) target char)
1687 (stream-write-char target char))
1688 (setf (case-frob-stream-out stream)
1689 #'case-frob-capitalize-out)
1690 (setf (case-frob-stream-sout stream)
1691 #'case-frob-capitalize-sout)))))
1693 (defun case-frob-capitalize-aux-sout (stream str start end)
1694 (declare (type case-frob-stream stream)
1695 (type simple-string str)
1697 (type (or index null) end))
1698 (let* ((target (case-frob-stream-target stream))
1699 (str (subseq str start end))
1703 (let ((char (schar str i)))
1704 (cond ((not (alphanumericp char))
1705 (setf inside-word nil))
1707 (setf (schar str i) (char-downcase char)))
1709 (setf inside-word t)
1710 (setf (schar str i) (char-upcase char))))))
1712 (setf (case-frob-stream-out stream)
1713 #'case-frob-capitalize-out)
1714 (setf (case-frob-stream-sout stream)
1715 #'case-frob-capitalize-sout))
1716 (if (ansi-stream-p target)
1717 (funcall (ansi-stream-sout target) target str 0 len)
1718 (stream-write-string target str 0 len))))
1720 (defun case-frob-capitalize-first-out (stream char)
1721 (declare (type case-frob-stream stream)
1722 (type character char))
1723 (let ((target (case-frob-stream-target stream)))
1724 (cond ((alphanumericp char)
1725 (let ((char (char-upcase char)))
1726 (if (ansi-stream-p target)
1727 (funcall (ansi-stream-out target) target char)
1728 (stream-write-char target char)))
1729 (setf (case-frob-stream-out stream)
1730 #'case-frob-downcase-out)
1731 (setf (case-frob-stream-sout stream)
1732 #'case-frob-downcase-sout))
1734 (if (ansi-stream-p target)
1735 (funcall (ansi-stream-out target) target char)
1736 (stream-write-char target char))))))
1738 (defun case-frob-capitalize-first-sout (stream str start end)
1739 (declare (type case-frob-stream stream)
1740 (type simple-string str)
1742 (type (or index null) end))
1743 (let* ((target (case-frob-stream-target stream))
1744 (str (subseq str start end))
1747 (let ((char (schar str i)))
1748 (when (alphanumericp char)
1749 (setf (schar str i) (char-upcase char))
1750 (do ((i (1+ i) (1+ i)))
1752 (setf (schar str i) (char-downcase (schar str i))))
1753 (setf (case-frob-stream-out stream)
1754 #'case-frob-downcase-out)
1755 (setf (case-frob-stream-sout stream)
1756 #'case-frob-downcase-sout)
1758 (if (ansi-stream-p target)
1759 (funcall (ansi-stream-sout target) target str 0 len)
1760 (stream-write-string target str 0 len))))
1764 (defun read-sequence (seq stream &key (start 0) end)
1766 "Destructively modify SEQ by reading elements from STREAM.
1767 That part of SEQ bounded by START and END is destructively modified by
1768 copying successive elements into it from STREAM. If the end of file
1769 for STREAM is reached before copying all elements of the subsequence,
1770 then the extra elements near the end of sequence are not updated, and
1771 the index of the next element is returned."
1772 (declare (type sequence seq)
1773 (type stream stream)
1775 (type sequence-end end)
1777 (if (ansi-stream-p stream)
1778 (ansi-stream-read-sequence seq stream start end)
1779 ;; must be Gray streams FUNDAMENTAL-STREAM
1780 (stream-read-sequence stream seq start end)))
1782 (declaim (inline compatible-vector-and-stream-element-types-p))
1783 (defun compatible-vector-and-stream-element-types-p (vector stream)
1784 (declare (type vector vector)
1785 (type ansi-stream stream))
1786 (or (and (typep vector '(simple-array (unsigned-byte 8) (*)))
1787 (subtypep (stream-element-type stream) '(unsigned-byte 8)))
1788 (and (typep vector '(simple-array (signed-byte 8) (*)))
1789 (subtypep (stream-element-type stream) '(signed-byte 8)))))
1791 (defun ansi-stream-read-sequence (seq stream start %end)
1792 (declare (type sequence seq)
1793 (type ansi-stream stream)
1795 (type sequence-end %end)
1797 (let ((end (or %end (length seq))))
1798 (declare (type index end))
1801 (let ((read-function
1802 (if (subtypep (stream-element-type stream) 'character)
1803 #'ansi-stream-read-char
1804 #'ansi-stream-read-byte)))
1805 (do ((rem (nthcdr start seq) (rest rem))
1807 ((or (endp rem) (>= i end)) i)
1808 (declare (type list rem)
1810 (let ((el (funcall read-function stream nil :eof nil)))
1813 (setf (first rem) el)))))
1815 (with-array-data ((data seq) (offset-start start) (offset-end end))
1816 (if (compatible-vector-and-stream-element-types-p data stream)
1817 (let* ((numbytes (- end start))
1818 (bytes-read (read-n-bytes stream data offset-start
1820 (if (< bytes-read numbytes)
1821 (+ start bytes-read)
1823 (let ((read-function
1824 (if (subtypep (stream-element-type stream) 'character)
1825 ;; If the stream-element-type is CHARACTER,
1826 ;; this might be a bivalent stream. If the
1827 ;; sequence is a specialized unsigned-byte
1828 ;; vector, try to read use binary IO. It'll
1829 ;; signal an error if stream is an pure
1830 ;; character stream.
1831 (if (subtypep (array-element-type data)
1833 #'ansi-stream-read-byte
1834 #'ansi-stream-read-char)
1835 #'ansi-stream-read-byte)))
1836 (do ((i offset-start (1+ i)))
1837 ((>= i offset-end) end)
1838 (declare (type index i))
1839 (let ((el (funcall read-function stream nil :eof nil)))
1841 (return (+ start (- i offset-start))))
1842 (setf (aref data i) el))))))))))
1846 (defun write-sequence (seq stream &key (start 0) (end nil))
1848 "Write the elements of SEQ bounded by START and END to STREAM."
1849 (declare (type sequence seq)
1850 (type stream stream)
1852 (type sequence-end end)
1854 (if (ansi-stream-p stream)
1855 (ansi-stream-write-sequence seq stream start end)
1856 ;; must be Gray-streams FUNDAMENTAL-STREAM
1857 (stream-write-sequence stream seq start end)))
1859 (defun ansi-stream-write-sequence (seq stream start %end)
1860 (declare (type sequence seq)
1861 (type ansi-stream stream)
1863 (type sequence-end %end)
1865 (let ((end (or %end (length seq))))
1866 (declare (type index end))
1869 (let ((write-function
1870 (if (subtypep (stream-element-type stream) 'character)
1871 (ansi-stream-out stream)
1872 (ansi-stream-bout stream))))
1873 (do ((rem (nthcdr start seq) (rest rem))
1875 ((or (endp rem) (>= i end)))
1876 (declare (type list rem)
1878 (funcall write-function stream (first rem)))))
1880 (%write-string seq stream start end))
1882 (with-array-data ((data seq) (offset-start start) (offset-end end))
1884 ((output-seq-in-loop ()
1885 (let ((write-function
1886 (if (subtypep (stream-element-type stream) 'character)
1887 (lambda (stream object)
1888 ;; This might be a bivalent stream, so we need
1889 ;; to dispatch on a per-element basis, rather
1890 ;; than just based on the sequence or stream
1892 (if (characterp object)
1893 (funcall (ansi-stream-out stream)
1895 (funcall (ansi-stream-bout stream)
1897 (ansi-stream-bout stream))))
1898 (do ((i offset-start (1+ i)))
1900 (declare (type index i))
1901 (funcall write-function stream (aref data i))))))
1902 (if (and (fd-stream-p stream)
1903 (compatible-vector-and-stream-element-types-p data stream))
1904 (output-raw-bytes stream data offset-start offset-end)
1905 (output-seq-in-loop)))))))