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))
67 (when (synonym-stream-p stream)
69 (symbol-value (synonym-stream-symbol stream))))
71 (and (not (eq (ansi-stream-in stream) #'closed-flame))
72 ;;; KLUDGE: It's probably not good to have EQ tests on function
73 ;;; values like this. What if someone's redefined the function?
74 ;;; Is there a better way? (Perhaps just VALID-FOR-INPUT and
75 ;;; VALID-FOR-OUTPUT flags? -- WHN 19990902
76 (or (not (eq (ansi-stream-in stream) #'ill-in))
77 (not (eq (ansi-stream-bin stream) #'ill-bin)))))
79 (defun input-stream-p (stream)
80 (declare (type stream stream))
81 (and (ansi-stream-p stream)
82 (ansi-stream-input-stream-p stream)))
84 (declaim (inline ansi-stream-output-stream-p))
85 (defun ansi-stream-output-stream-p (stream)
86 (declare (type ansi-stream stream))
89 (when (synonym-stream-p stream)
90 (setf stream (symbol-value
91 (synonym-stream-symbol stream))))
93 (and (not (eq (ansi-stream-in stream) #'closed-flame))
94 (or (not (eq (ansi-stream-out stream) #'ill-out))
95 (not (eq (ansi-stream-bout stream) #'ill-bout)))))
97 (defun output-stream-p (stream)
98 (declare (type stream stream))
100 (and (ansi-stream-p stream)
101 (ansi-stream-output-stream-p stream)))
103 (declaim (inline ansi-stream-open-stream-p))
104 (defun ansi-stream-open-stream-p (stream)
105 (declare (type ansi-stream stream))
106 (not (eq (ansi-stream-in stream) #'closed-flame)))
108 (defun open-stream-p (stream)
109 (ansi-stream-open-stream-p stream))
111 (declaim (inline ansi-stream-element-type))
112 (defun ansi-stream-element-type (stream)
113 (declare (type ansi-stream stream))
114 (funcall (ansi-stream-misc stream) stream :element-type))
116 (defun stream-element-type (stream)
117 (ansi-stream-element-type stream))
119 (defun interactive-stream-p (stream)
120 (declare (type stream stream))
121 (funcall (ansi-stream-misc stream) stream :interactive-p))
123 (declaim (inline ansi-stream-close))
124 (defun ansi-stream-close (stream abort)
125 (declare (type ansi-stream stream))
126 (when (open-stream-p stream)
127 (funcall (ansi-stream-misc stream) stream :close abort))
130 (defun close (stream &key abort)
131 (ansi-stream-close stream abort))
133 (defun set-closed-flame (stream)
134 (setf (ansi-stream-in stream) #'closed-flame)
135 (setf (ansi-stream-bin stream) #'closed-flame)
136 (setf (ansi-stream-n-bin stream) #'closed-flame)
137 (setf (ansi-stream-in stream) #'closed-flame)
138 (setf (ansi-stream-out stream) #'closed-flame)
139 (setf (ansi-stream-bout stream) #'closed-flame)
140 (setf (ansi-stream-sout stream) #'closed-flame)
141 (setf (ansi-stream-misc stream) #'closed-flame))
143 ;;;; file position and file length
145 ;;; Call the MISC method with the :FILE-POSITION operation.
146 (defun file-position (stream &optional position)
147 (declare (type stream stream))
148 (declare (type (or index (member nil :start :end)) position))
151 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
152 (funcall (ansi-stream-misc stream) stream :file-position position))
154 (let ((res (funcall (ansi-stream-misc stream) stream :file-position nil)))
157 (- +ansi-stream-in-buffer-length+
158 (ansi-stream-in-index stream))))))))
160 ;;; This is a literal translation of the ANSI glossary entry "stream
161 ;;; associated with a file".
163 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
164 ;;; file", and in particular stdin, stdout, and stderr are files, we
165 ;;; end up with this test being satisfied for weird things like
166 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
167 ;;; ANSI spec really had in mind, especially since this is used as a
168 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
169 ;;; probably thinking of something like what Unix calls block devices)
170 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
171 (defun stream-associated-with-file-p (x)
172 "Test for the ANSI concept \"stream associated with a file\"."
173 (or (typep x 'file-stream)
174 (and (synonym-stream-p x)
175 (stream-associated-with-file-p (symbol-value
176 (synonym-stream-symbol x))))))
178 (defun stream-must-be-associated-with-file (stream)
179 (declare (type stream stream))
180 (unless (stream-associated-with-file-p stream)
181 (error 'simple-type-error
182 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
183 ;; this should be TYPE-ERROR. But what then can we use for
184 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
185 ;; private predicate function..) is ugly and confusing, but
186 ;; I can't see any other way. -- WHN 2001-04-14
187 :expected-type '(satisfies stream-associated-with-file-p)
189 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
190 :format-arguments (list stream))))
192 ;;; like FILE-POSITION, only using :FILE-LENGTH
193 (defun file-length (stream)
194 ;; FIXME: The following declaration uses yet undefined types, which
195 ;; cause cross-compiler hangup.
197 ;; (declare (type (or file-stream synonym-stream) stream))
198 (stream-must-be-associated-with-file stream)
199 (funcall (ansi-stream-misc stream) stream :file-length))
203 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
205 (declare (ignore recursive-p))
206 (let ((stream (in-synonym-of stream)))
207 (if (ansi-stream-p stream)
208 (prepare-for-fast-read-char stream
209 (let ((res (make-string 80))
213 (let ((ch (fast-read-char nil nil)))
215 (when (char= ch #\newline)
216 (done-with-fast-read-char)
217 (return (values (shrink-vector res index) nil)))
220 (let ((new (make-string len)))
223 (setf (schar res index) ch)
226 (done-with-fast-read-char)
227 (return (values (eof-or-lose stream
231 ;; Since FAST-READ-CHAR already hit the eof char, we
232 ;; shouldn't do another READ-CHAR.
234 (done-with-fast-read-char)
235 (return (values (shrink-vector res index) t))))))))
236 ;; must be Gray streams FUNDAMENTAL-STREAM
237 (multiple-value-bind (string eof) (stream-read-line stream)
238 (if (and eof (zerop (length string)))
239 (values (eof-or-lose stream eof-error-p eof-value) t)
240 (values string eof))))))
242 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
243 ;;; so, except in this file, they are not inline by default, but they can be.
244 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
246 (defun read-char (&optional (stream *standard-input*)
250 (declare (ignore recursive-p))
251 (let ((stream (in-synonym-of stream)))
252 (if (ansi-stream-p stream)
253 (prepare-for-fast-read-char stream
255 (fast-read-char eof-error-p eof-value)
256 (done-with-fast-read-char)))
257 ;; must be Gray streams FUNDAMENTAL-STREAM
258 (let ((char (stream-read-char stream)))
260 (eof-or-lose stream eof-error-p eof-value)
263 (defun unread-char (character &optional (stream *standard-input*))
264 (let ((stream (in-synonym-of stream)))
265 (if (ansi-stream-p stream)
266 (let ((index (1- (ansi-stream-in-index stream)))
267 (buffer (ansi-stream-in-buffer stream)))
268 (declare (fixnum index))
269 (when (minusp index) (error "nothing to unread"))
271 (setf (aref buffer index) (char-code character))
272 (setf (ansi-stream-in-index stream) index))
274 (funcall (ansi-stream-misc stream) stream
275 :unread character))))
276 ;; must be Gray streams FUNDAMENTAL-STREAM
277 (stream-unread-char stream character)))
281 ;;; In the interest of ``once and only once'' this macro contains the
282 ;;; framework necessary to implement a peek-char function, which has
283 ;;; two special-cases (one for gray streams and one for echo streams)
284 ;;; in addition to the normal case.
286 ;;; All arguments are forms which will be used for a specific purpose
287 ;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
288 ;;; EOF-VALUE - the eof-value argument to peek-char
289 ;;; CHAR-VAR - the variable which will be used to store the current character
290 ;;; READ-FORM - the form which will be used to read a character
291 ;;; UNREAD-FORM - ditto for unread-char
292 ;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
293 ;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
294 ;;; (this will default to CHAR-VAR)
295 (defmacro generalized-peeking-mechanism (peek-type eof-value char-var read-form unread-form &optional (skipped-char-form nil) (eof-detected-form nil))
296 `(let ((,char-var ,read-form))
297 (cond ((eql ,char-var ,eof-value)
298 ,(if eof-detected-form
301 ((characterp ,peek-type)
302 (do ((,char-var ,char-var ,read-form))
303 ((or (eql ,char-var ,eof-value)
304 (char= ,char-var ,peek-type))
305 (cond ((eql ,char-var ,eof-value)
306 ,(if eof-detected-form
313 (do ((,char-var ,char-var ,read-form))
314 ((or (eql ,char-var ,eof-value)
315 (not (whitespace-char-p ,char-var)))
316 (cond ((eql ,char-var ,eof-value)
317 ,(if eof-detected-form
327 (bug "Impossible case reached in PEEK-CHAR")))))
329 (defun peek-char (&optional (peek-type nil)
330 (stream *standard-input*)
334 (declare (ignore recursive-p))
335 ;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
336 ;; the compiler doesn't seem to be smart enough to go from there to
337 ;; imposing a type check. Figure out why (because PEEK-TYPE is an
338 ;; &OPTIONAL argument?) and fix it, and then this explicit type
339 ;; check can go away.
340 (unless (typep peek-type '(or character boolean))
341 (error 'simple-type-error
343 :expected-type '(or character boolean)
344 :format-control "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>"
345 :format-arguments (list peek-type '(or character boolean))))
346 (let ((stream (in-synonym-of stream)))
347 (cond ((typep stream 'echo-stream)
351 (list eof-error-p eof-value)))
352 ((ansi-stream-p stream)
353 (generalized-peeking-mechanism
354 peek-type eof-value char
355 (read-char stream eof-error-p eof-value)
356 (unread-char char stream)))
358 ;; by elimination, must be Gray streams FUNDAMENTAL-STREAM
359 (generalized-peeking-mechanism
362 (stream-peek-char stream)
363 (stream-read-char stream))
366 (stream-unread-char stream char))
368 (eof-or-lose stream eof-error-p eof-value))))))
370 (defun listen (&optional (stream *standard-input*))
371 (let ((stream (in-synonym-of stream)))
372 (if (ansi-stream-p stream)
373 (or (/= (the fixnum (ansi-stream-in-index stream))
374 +ansi-stream-in-buffer-length+)
375 ;; Test for T explicitly since misc methods return :EOF sometimes.
376 (eq (funcall (ansi-stream-misc stream) stream :listen) t))
377 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
378 (stream-listen stream))))
380 (defun read-char-no-hang (&optional (stream *standard-input*)
384 (declare (ignore recursive-p))
385 (let ((stream (in-synonym-of stream)))
386 (if (ansi-stream-p stream)
387 (if (funcall (ansi-stream-misc stream) stream :listen)
388 ;; On T or :EOF get READ-CHAR to do the work.
389 (read-char stream eof-error-p eof-value)
391 ;; must be Gray streams FUNDAMENTAL-STREAM
392 (let ((char (stream-read-char-no-hang stream)))
394 (eof-or-lose stream eof-error-p eof-value)
397 (defun clear-input (&optional (stream *standard-input*))
398 (let ((stream (in-synonym-of stream)))
399 (cond ((ansi-stream-p stream)
400 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
401 (funcall (ansi-stream-misc stream) stream :clear-input))
403 (stream-clear-input stream))))
406 (declaim (maybe-inline read-byte))
407 (defun read-byte (stream &optional (eof-error-p t) eof-value)
408 (let ((stream (in-synonym-of stream)))
409 (if (ansi-stream-p stream)
410 (prepare-for-fast-read-byte stream
412 (fast-read-byte eof-error-p eof-value t)
413 (done-with-fast-read-byte)))
414 ;; must be Gray streams FUNDAMENTAL-STREAM
415 (let ((char (stream-read-byte stream)))
417 (eof-or-lose stream eof-error-p eof-value)
420 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
421 ;;; number of bytes read.
423 ;;; Note: CMU CL's version of this had a special interpretation of
424 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
425 ;;; case, CMU CL's version would return as soon as any data became
426 ;;; available.) This could be useful behavior for things like pipes in
427 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
428 ;;; If we ever need it, it could be added later as a new variant N-BIN
429 ;;; method (perhaps N-BIN-ASAP?) or something.
430 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
431 (declare (type ansi-stream stream)
432 (type index numbytes start)
433 (type (or (simple-array * (*)) system-area-pointer) buffer))
434 (let* ((stream (in-synonym-of stream ansi-stream))
435 (in-buffer (ansi-stream-in-buffer stream))
436 (index (ansi-stream-in-index stream))
437 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
438 (declare (fixnum index num-buffered))
441 (funcall (ansi-stream-n-bin stream)
447 ((<= numbytes num-buffered)
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)))
454 (%byte-blt in-buffer index buffer start end)
455 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
456 (+ (funcall (ansi-stream-n-bin stream)
460 (- numbytes num-buffered)
464 ;;; the amount of space we leave at the start of the in-buffer for
467 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
468 (defconstant +ansi-stream-in-buffer-extra+
469 4) ; FIXME: should be symbolic constant
471 ;;; This function is called by the FAST-READ-CHAR expansion to refill
472 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
473 ;;; and hence must be an N-BIN method.
474 (defun fast-read-char-refill (stream eof-error-p eof-value)
475 (let* ((ibuf (ansi-stream-in-buffer stream))
476 (count (funcall (ansi-stream-n-bin stream)
479 +ansi-stream-in-buffer-extra+
480 (- +ansi-stream-in-buffer-length+
481 +ansi-stream-in-buffer-extra+)
483 (start (- +ansi-stream-in-buffer-length+ count)))
484 (declare (type index start count))
486 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
487 (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
489 (when (/= start +ansi-stream-in-buffer-extra+)
490 (bit-bash-copy ibuf (+ (* +ansi-stream-in-buffer-extra+
492 (* sb!vm:vector-data-offset
494 ibuf (+ (the index (* start sb!vm:n-byte-bits))
495 (* sb!vm:vector-data-offset
497 (* count sb!vm:n-byte-bits)))
498 (setf (ansi-stream-in-index stream) (1+ start))
499 (code-char (aref ibuf start))))))
501 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
502 ;;; leave room for unreading.
503 (defun fast-read-byte-refill (stream eof-error-p eof-value)
504 (let* ((ibuf (ansi-stream-in-buffer stream))
505 (count (funcall (ansi-stream-n-bin stream) stream
506 ibuf 0 +ansi-stream-in-buffer-length+
508 (start (- +ansi-stream-in-buffer-length+ count)))
509 (declare (type index start count))
511 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
512 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
514 (unless (zerop start)
515 (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:n-word-bits)
516 ibuf (+ (the index (* start sb!vm:n-byte-bits))
517 (* sb!vm:vector-data-offset
519 (* count sb!vm:n-byte-bits)))
520 (setf (ansi-stream-in-index stream) (1+ start))
521 (aref ibuf start)))))
525 (defun write-char (character &optional (stream *standard-output*))
526 (with-out-stream stream (ansi-stream-out character)
527 (stream-write-char character))
530 (defun terpri (&optional (stream *standard-output*))
531 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
534 (defun fresh-line (&optional (stream *standard-output*))
535 (let ((stream (out-synonym-of stream)))
536 (if (ansi-stream-p stream)
537 (when (/= (or (charpos stream) 1) 0)
538 (funcall (ansi-stream-out stream) stream #\newline)
540 ;; must be Gray streams FUNDAMENTAL-STREAM
541 (stream-fresh-line stream))))
543 (defun write-string (string &optional (stream *standard-output*)
545 (declare (type string string))
546 ;; Note that even though you might expect, based on the behavior of
547 ;; things like AREF, that the correct upper bound here is
548 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
549 ;; "bounding index" and "length" indicate that in this case (i.e.
550 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
551 ;; (LENGTH STRING) is the required upper bound. A foolish
552 ;; consistency is the hobgoblin of lesser languages..
553 (%write-string string stream start (%check-vector-sequence-bounds
557 (defun %write-string (string stream start end)
558 (declare (type string string))
559 (declare (type streamlike stream))
560 (declare (type index start end))
561 (let ((stream (out-synonym-of stream)))
562 (cond ((ansi-stream-p stream)
563 (if (array-header-p string)
564 (with-array-data ((data string) (offset-start start)
566 (funcall (ansi-stream-sout stream)
567 stream data offset-start offset-end))
568 (funcall (ansi-stream-sout stream) stream string start end))
570 (t ; must be Gray streams FUNDAMENTAL-STREAM
571 (stream-write-string stream string start end)))))
573 (defun write-line (string &optional (stream *standard-output*)
575 (declare (type string string))
576 ;; FIXME: Why is there this difference between the treatments of the
577 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
578 (let ((defaulted-stream (out-synonym-of stream)))
579 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
581 (write-char #\newline defaulted-stream))
584 (defun charpos (&optional (stream *standard-output*))
585 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
587 (defun line-length (&optional (stream *standard-output*))
588 (with-out-stream stream (ansi-stream-misc :line-length)
589 (stream-line-length)))
591 (defun finish-output (&optional (stream *standard-output*))
592 (with-out-stream stream (ansi-stream-misc :finish-output)
593 (stream-finish-output))
596 (defun force-output (&optional (stream *standard-output*))
597 (with-out-stream stream (ansi-stream-misc :force-output)
598 (stream-force-output))
601 (defun clear-output (&optional (stream *standard-output*))
602 (with-out-stream stream (ansi-stream-misc :clear-output)
603 (stream-force-output))
606 (defun write-byte (integer stream)
607 (with-out-stream stream (ansi-stream-bout integer)
608 (stream-write-byte integer))
611 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
612 ;;; streams to handle the misc routines and dispatch to the
613 ;;; appropriate Gray stream functions.
614 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
615 (declare (type fundamental-stream stream)
619 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
620 (let ((char (stream-read-char-no-hang stream)))
621 (when (characterp char)
622 (stream-unread-char stream char))
625 (stream-unread-char stream arg1))
629 (stream-clear-input stream))
631 (stream-force-output stream))
633 (stream-finish-output stream))
635 (stream-element-type stream))
637 (interactive-stream-p stream))
639 (stream-line-length stream))
641 (stream-line-column stream))
643 (file-length stream))
645 (file-position stream arg1))))
647 ;;;; broadcast streams
649 (defstruct (broadcast-stream (:include ansi-stream
650 (out #'broadcast-out)
651 (bout #'broadcast-bout)
652 (sout #'broadcast-sout)
653 (misc #'broadcast-misc))
654 (:constructor %make-broadcast-stream
657 ;; a list of all the streams we broadcast to
658 (streams () :type list :read-only t))
660 (defun make-broadcast-stream (&rest streams)
661 (dolist (stream streams)
662 (unless (or (and (synonym-stream-p stream)
663 (output-stream-p (symbol-value
664 (synonym-stream-symbol stream))))
665 (output-stream-p stream))
668 :expected-type '(satisfies output-stream-p))))
669 (apply #'%make-broadcast-stream streams))
671 (macrolet ((out-fun (fun method stream-method &rest args)
672 `(defun ,fun (stream ,@args)
673 (dolist (stream (broadcast-stream-streams stream))
674 (if (ansi-stream-p stream)
675 (funcall (,method stream) stream ,@args)
676 (,stream-method stream ,@args))))))
677 (out-fun broadcast-out ansi-stream-out stream-write-char char)
678 (out-fun broadcast-bout ansi-stream-bout stream-write-byte byte)
679 (out-fun broadcast-sout ansi-stream-sout stream-write-string
682 (defun broadcast-misc (stream operation &optional arg1 arg2)
683 (let ((streams (broadcast-stream-streams stream)))
686 (dolist (stream streams)
687 (let ((charpos (charpos stream)))
688 (if charpos (return charpos)))))
691 (dolist (stream streams min)
692 (let ((res (line-length stream)))
693 (when res (setq min (if min (min res min) res)))))))
696 (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
697 (pushnew (stream-element-type stream) res :test #'equal))))
701 (dolist (stream streams res)
703 (if (ansi-stream-p stream)
704 (funcall (ansi-stream-misc stream) stream operation
706 (stream-misc-dispatch stream operation arg1 arg2)))))))))
710 (defstruct (synonym-stream (:include ansi-stream
713 (n-bin #'synonym-n-bin)
715 (bout #'synonym-bout)
716 (sout #'synonym-sout)
717 (misc #'synonym-misc))
718 (:constructor make-synonym-stream (symbol))
720 ;; This is the symbol, the value of which is the stream we are synonym to.
721 (symbol nil :type symbol :read-only t))
722 (def!method print-object ((x synonym-stream) stream)
723 (print-unreadable-object (x stream :type t :identity t)
724 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
726 ;;; The output simple output methods just call the corresponding method
727 ;;; in the synonymed stream.
728 (macrolet ((out-fun (name slot stream-method &rest args)
729 `(defun ,name (stream ,@args)
730 (declare (optimize (safety 1)))
731 (let ((syn (symbol-value (synonym-stream-symbol stream))))
732 (if (ansi-stream-p syn)
733 (funcall (,slot syn) syn ,@args)
734 (,stream-method syn ,@args))))))
735 (out-fun synonym-out ansi-stream-out stream-write-char ch)
736 (out-fun synonym-bout ansi-stream-bout stream-write-byte n)
737 (out-fun synonym-sout ansi-stream-sout stream-write-string string start end))
739 ;;; For the input methods, we just call the corresponding function on the
740 ;;; synonymed stream. These functions deal with getting input out of
741 ;;; the In-Buffer if there is any.
742 (macrolet ((in-fun (name fun &rest args)
743 `(defun ,name (stream ,@args)
744 (declare (optimize (safety 1)))
745 (,fun (symbol-value (synonym-stream-symbol stream))
747 (in-fun synonym-in read-char eof-error-p eof-value)
748 (in-fun synonym-bin read-byte eof-error-p eof-value)
749 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
751 (defun synonym-misc (stream operation &optional arg1 arg2)
752 (declare (optimize (safety 1)))
753 (let ((syn (symbol-value (synonym-stream-symbol stream))))
754 (if (ansi-stream-p syn)
755 ;; We have to special-case some operations which interact with
756 ;; the in-buffer of the wrapped stream, since just calling
757 ;; ANSI-STREAM-MISC on them
759 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
760 +ansi-stream-in-buffer-length+)
761 (funcall (ansi-stream-misc syn) syn :listen)))
762 (:clear-input (clear-input syn))
763 (:unread (unread-char arg1 syn))
765 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
766 (stream-misc-dispatch syn operation arg1 arg2))))
770 (defstruct (two-way-stream
771 (:include ansi-stream
774 (n-bin #'two-way-n-bin)
776 (bout #'two-way-bout)
777 (sout #'two-way-sout)
778 (misc #'two-way-misc))
779 (:constructor %make-two-way-stream (input-stream output-stream))
781 (input-stream (missing-arg) :type stream :read-only t)
782 (output-stream (missing-arg) :type stream :read-only t))
783 (defprinter (two-way-stream) input-stream output-stream)
785 (defun make-two-way-stream (input-stream output-stream)
787 "Return a bidirectional stream which gets its input from INPUT-STREAM and
788 sends its output to OUTPUT-STREAM."
789 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
790 ;; should be encapsulated in a function, and used here and most of
791 ;; the other places that SYNONYM-STREAM-P appears.
792 (unless (or (and (synonym-stream-p output-stream)
793 (output-stream-p (symbol-value
794 (synonym-stream-symbol output-stream))))
795 (output-stream-p output-stream))
798 :expected-type '(satisfies output-stream-p)))
799 (unless (or (and (synonym-stream-p input-stream)
800 (input-stream-p (symbol-value
801 (synonym-stream-symbol input-stream))))
802 (input-stream-p input-stream))
805 :expected-type '(satisfies input-stream-p)))
806 (funcall #'%make-two-way-stream input-stream output-stream))
808 (macrolet ((out-fun (name slot stream-method &rest args)
809 `(defun ,name (stream ,@args)
810 (let ((syn (two-way-stream-output-stream stream)))
811 (if (ansi-stream-p syn)
812 (funcall (,slot syn) syn ,@args)
813 (,stream-method syn ,@args))))))
814 (out-fun two-way-out ansi-stream-out stream-write-char ch)
815 (out-fun two-way-bout ansi-stream-bout stream-write-byte n)
816 (out-fun two-way-sout ansi-stream-sout stream-write-string string start end))
818 (macrolet ((in-fun (name fun &rest args)
819 `(defun ,name (stream ,@args)
820 (force-output (two-way-stream-output-stream stream))
821 (,fun (two-way-stream-input-stream stream) ,@args))))
822 (in-fun two-way-in read-char eof-error-p eof-value)
823 (in-fun two-way-bin read-byte eof-error-p eof-value)
824 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
826 (defun two-way-misc (stream operation &optional arg1 arg2)
827 (let* ((in (two-way-stream-input-stream stream))
828 (out (two-way-stream-output-stream stream))
829 (in-ansi-stream-p (ansi-stream-p in))
830 (out-ansi-stream-p (ansi-stream-p out)))
834 (or (/= (the fixnum (ansi-stream-in-index in))
835 +ansi-stream-in-buffer-length+)
836 (funcall (ansi-stream-misc in) in :listen))
838 ((:finish-output :force-output :clear-output)
839 (if out-ansi-stream-p
840 (funcall (ansi-stream-misc out) out operation arg1 arg2)
841 (stream-misc-dispatch out operation arg1 arg2)))
842 (:clear-input (clear-input in))
843 (:unread (unread-char arg1 in))
845 (let ((in-type (stream-element-type in))
846 (out-type (stream-element-type out)))
847 (if (equal in-type out-type)
848 in-type `(and ,in-type ,out-type))))
850 (set-closed-flame stream))
852 (or (if in-ansi-stream-p
853 (funcall (ansi-stream-misc in) in operation arg1 arg2)
854 (stream-misc-dispatch in operation arg1 arg2))
855 (if out-ansi-stream-p
856 (funcall (ansi-stream-misc out) out operation arg1 arg2)
857 (stream-misc-dispatch out operation arg1 arg2)))))))
859 ;;;; concatenated streams
861 (defstruct (concatenated-stream
862 (:include ansi-stream
863 (in #'concatenated-in)
864 (bin #'concatenated-bin)
865 (n-bin #'concatenated-n-bin)
866 (misc #'concatenated-misc))
867 (:constructor %make-concatenated-stream
868 (&rest streams &aux (current streams)))
870 ;; The car of this is the substream we are reading from now.
872 ;; This is a list of all the substreams there ever were. We need to
873 ;; remember them so that we can close them.
875 ;; FIXME: ANSI says this is supposed to be the list of streams that
876 ;; we still have to read from. So either this needs to become a
877 ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
878 ;; around it which discards closed files from the head of the list)
879 ;; or we need to update it as we run out of files.
880 (streams nil :type list :read-only t))
881 (def!method print-object ((x concatenated-stream) stream)
882 (print-unreadable-object (x stream :type t :identity t)
885 (concatenated-stream-streams x))))
887 (defun make-concatenated-stream (&rest streams)
889 "Return a stream which takes its input from each of the streams in turn,
890 going on to the next at EOF."
891 (dolist (stream streams)
892 (unless (or (and (synonym-stream-p stream)
893 (input-stream-p (symbol-value
894 (synonym-stream-symbol stream))))
895 (input-stream-p stream))
898 :expected-type '(satisfies input-stream-p))))
899 (apply #'%make-concatenated-stream streams))
901 (macrolet ((in-fun (name fun)
902 `(defun ,name (stream eof-error-p eof-value)
903 (do ((current (concatenated-stream-current stream)
906 (eof-or-lose stream eof-error-p eof-value))
907 (let* ((stream (car current))
908 (result (,fun stream nil nil)))
909 (when result (return result)))
910 (pop (concatenated-stream-current stream))))))
911 (in-fun concatenated-in read-char)
912 (in-fun concatenated-bin read-byte))
914 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
915 (do ((current (concatenated-stream-current stream) (cdr current))
916 (current-start start)
917 (remaining-bytes numbytes))
920 (error 'end-of-file :stream stream)
921 (- numbytes remaining-bytes)))
922 (let* ((stream (car current))
923 (bytes-read (read-n-bytes stream buffer current-start
924 remaining-bytes nil)))
925 (incf current-start bytes-read)
926 (decf remaining-bytes bytes-read)
927 (when (zerop remaining-bytes) (return numbytes)))
928 (setf (concatenated-stream-current stream) (cdr current))))
930 (defun concatenated-misc (stream operation &optional arg1 arg2)
931 (let ((left (concatenated-stream-current stream)))
933 (let* ((current (car left)))
937 (let ((stuff (if (ansi-stream-p current)
938 (funcall (ansi-stream-misc current) current
940 (stream-misc-dispatch current :listen))))
941 (cond ((eq stuff :eof)
942 ;; Advance CURRENT, and try again.
943 (pop (concatenated-stream-current stream))
945 (car (concatenated-stream-current stream)))
947 ;; No further streams. EOF.
950 ;; Stuff's available.
953 ;; Nothing is available yet.
955 (:clear-input (clear-input current))
956 (:unread (unread-char arg1 current))
958 (set-closed-flame stream))
960 (if (ansi-stream-p current)
961 (funcall (ansi-stream-misc current) current operation arg1 arg2)
962 (stream-misc-dispatch current operation arg1 arg2))))))))
966 (defstruct (echo-stream
967 (:include two-way-stream
972 (:constructor %make-echo-stream (input-stream output-stream))
975 (def!method print-object ((x echo-stream) stream)
976 (print-unreadable-object (x stream :type t :identity t)
978 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
979 (two-way-stream-input-stream x)
980 (two-way-stream-output-stream x))))
982 (defun make-echo-stream (input-stream output-stream)
984 "Return a bidirectional stream which gets its input from INPUT-STREAM and
985 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
987 (unless (or (and (synonym-stream-p output-stream)
988 (output-stream-p (symbol-value
989 (synonym-stream-symbol output-stream))))
990 (output-stream-p output-stream))
993 :expected-type '(satisfies output-stream-p)))
994 (unless (or (and (synonym-stream-p input-stream)
995 (input-stream-p (symbol-value
996 (synonym-stream-symbol input-stream))))
997 (input-stream-p input-stream))
1000 :expected-type '(satisfies input-stream-p)))
1001 (funcall #'%make-echo-stream input-stream output-stream))
1003 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
1004 `(defun ,name (stream ,@args)
1005 (or (pop (echo-stream-unread-stuff stream))
1006 (let* ((in (echo-stream-input-stream stream))
1007 (out (echo-stream-output-stream stream))
1008 (result (,fun in ,@args)))
1009 (if (ansi-stream-p out)
1010 (funcall (,out-slot out) out result)
1011 (,stream-method out result))
1013 (in-fun echo-in read-char ansi-stream-out stream-write-char
1014 eof-error-p eof-value)
1015 (in-fun echo-bin read-byte ansi-stream-bout stream-write-byte
1016 eof-error-p eof-value))
1018 (defun echo-misc (stream operation &optional arg1 arg2)
1019 (let* ((in (two-way-stream-input-stream stream))
1020 (out (two-way-stream-output-stream stream)))
1023 (or (not (null (echo-stream-unread-stuff stream)))
1024 (if (ansi-stream-p in)
1025 (or (/= (the fixnum (ansi-stream-in-index in))
1026 +ansi-stream-in-buffer-length+)
1027 (funcall (ansi-stream-misc in) in :listen))
1028 (stream-misc-dispatch in :listen))))
1029 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1031 (let ((in-type (stream-element-type in))
1032 (out-type (stream-element-type out)))
1033 (if (equal in-type out-type)
1034 in-type `(and ,in-type ,out-type))))
1036 (set-closed-flame stream))
1038 ;; For the special case of peeking into an echo-stream
1039 ;; arg1 is PEEK-TYPE, arg2 is (EOF-ERROR-P EOF-VALUE)
1040 ;; returns peeked-char, eof-value, or errors end-of-file
1042 ;; Note: This code could be moved into PEEK-CHAR if desired.
1043 ;; I am unsure whether this belongs with echo-streams because it is
1044 ;; echo-stream specific, or PEEK-CHAR because it is peeking code.
1045 ;; -- mrd 2002-11-18
1047 ;; UNREAD-CHAR-P indicates whether the current character was one
1048 ;; that was previously unread. In that case, we need to ensure that
1049 ;; the semantics for UNREAD-CHAR are held; the character should
1050 ;; not be echoed again.
1051 (let ((unread-char-p nil))
1053 (unless unread-char-p
1054 (if (ansi-stream-p out)
1055 (funcall (ansi-stream-out out) out c)
1057 (stream-write-char out c))))
1059 ;; Obtain input from unread buffer or input stream,
1060 ;; and set the flag appropriately.
1061 (cond ((not (null (echo-stream-unread-stuff stream)))
1062 (setf unread-char-p t)
1063 (pop (echo-stream-unread-stuff stream)))
1065 (setf unread-char-p nil)
1066 (read-char in (first arg2) (second arg2))))))
1067 (generalized-peeking-mechanism
1068 arg1 (second arg2) char
1070 (unread-char char in)
1073 (or (if (ansi-stream-p in)
1074 (funcall (ansi-stream-misc in) in operation arg1 arg2)
1075 (stream-misc-dispatch in operation arg1 arg2))
1076 (if (ansi-stream-p out)
1077 (funcall (ansi-stream-misc out) out operation arg1 arg2)
1078 (stream-misc-dispatch out operation arg1 arg2)))))))
1080 ;;;; base STRING-STREAM stuff
1082 (defstruct (string-stream
1083 (:include ansi-stream)
1086 (string nil :type string))
1088 ;;;; STRING-INPUT-STREAM stuff
1090 (defstruct (string-input-stream
1091 (:include string-stream
1093 (bin #'string-binch)
1094 (n-bin #'string-stream-read-n-bytes)
1095 (misc #'string-in-misc)
1096 (string nil :type simple-string))
1097 (:constructor internal-make-string-input-stream
1098 (string current end))
1100 (current nil :type index)
1101 (end nil :type index))
1103 (defun string-inch (stream eof-error-p eof-value)
1104 (let ((string (string-input-stream-string stream))
1105 (index (string-input-stream-current stream)))
1106 (declare (simple-string string) (fixnum index))
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 (aref string index)))))
1113 (defun string-binch (stream eof-error-p eof-value)
1114 (let ((string (string-input-stream-string stream))
1115 (index (string-input-stream-current stream)))
1116 (declare (simple-string string)
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 (aref 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 (simple-string string)
1132 (type index index available copy))
1134 (setf (string-input-stream-current stream)
1135 (truly-the index (+ index copy)))
1136 (sb!sys:without-gcing
1137 (system-area-copy (vector-sap string)
1138 (* index sb!vm:n-byte-bits)
1139 (if (typep buffer 'system-area-pointer)
1141 (vector-sap buffer))
1142 (* start sb!vm:n-byte-bits)
1143 (* copy sb!vm:n-byte-bits))))
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 (ignore arg2))
1153 (setf (string-input-stream-current stream) arg1)
1154 (string-input-stream-current stream)))
1155 (:file-length (length (string-input-stream-string stream)))
1156 (:unread (decf (string-input-stream-current stream)))
1157 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1158 (the fixnum (string-input-stream-end stream)))
1160 (:element-type 'base-char)))
1162 (defun make-string-input-stream (string &optional
1165 "Return an input stream which will supply the characters of STRING between
1166 START and END in order."
1167 (declare (type string string)
1169 (type (or index null) end))
1171 (internal-make-string-input-stream
1172 (coerce string 'simple-string)
1174 (%check-vector-sequence-bounds string start end)))
1176 ;;;; STRING-OUTPUT-STREAM stuff
1178 (defstruct (string-output-stream
1179 (:include string-stream
1181 (sout #'string-sout)
1182 (misc #'string-out-misc)
1183 ;; The string we throw stuff in.
1184 (string (make-string 40) :type simple-string))
1185 (:constructor make-string-output-stream ())
1187 ;; Index of the next location to use.
1188 (index 0 :type fixnum))
1191 (setf (fdocumentation 'make-string-output-stream 'function)
1192 "Return an output stream which will accumulate all output given it for
1193 the benefit of the function GET-OUTPUT-STREAM-STRING.")
1195 (defun string-ouch (stream character)
1196 (let ((current (string-output-stream-index stream))
1197 (workspace (string-output-stream-string stream)))
1198 (declare (simple-string workspace) (fixnum current))
1199 (if (= current (the fixnum (length workspace)))
1200 (let ((new-workspace (make-string (* current 2))))
1201 (replace new-workspace workspace)
1202 (setf (aref new-workspace current) character)
1203 (setf (string-output-stream-string stream) new-workspace))
1204 (setf (aref workspace current) character))
1205 (setf (string-output-stream-index stream) (1+ current))))
1207 (defun string-sout (stream string start end)
1208 (declare (simple-string string) (fixnum start end))
1209 (let* ((current (string-output-stream-index stream))
1210 (length (- end start))
1211 (dst-end (+ length current))
1212 (workspace (string-output-stream-string stream)))
1213 (declare (simple-string workspace)
1214 (fixnum current length dst-end))
1215 (if (> dst-end (the fixnum (length workspace)))
1216 (let ((new-workspace (make-string (+ (* current 2) length))))
1217 (replace new-workspace workspace :end2 current)
1218 (replace new-workspace string
1219 :start1 current :end1 dst-end
1220 :start2 start :end2 end)
1221 (setf (string-output-stream-string stream) new-workspace))
1222 (replace workspace string
1223 :start1 current :end1 dst-end
1224 :start2 start :end2 end))
1225 (setf (string-output-stream-index stream) dst-end)))
1227 (defun string-out-misc (stream operation &optional arg1 arg2)
1228 (declare (ignore arg2))
1232 (string-output-stream-index stream)))
1234 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1236 (count 0 (1+ count))
1237 (string (string-output-stream-string stream)))
1239 (declare (simple-string string)
1240 (fixnum index count))
1241 (if (char= (schar string index) #\newline)
1243 (:element-type 'base-char)))
1245 ;;; Return a string of all the characters sent to a stream made by
1246 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1247 (defun get-output-stream-string (stream)
1248 (declare (type string-output-stream stream))
1249 (let* ((length (string-output-stream-index stream))
1250 (result (make-string length)))
1251 (replace result (string-output-stream-string stream))
1252 (setf (string-output-stream-index stream) 0)
1255 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1256 ;;; GET-OUTPUT-STREAM-STRING would return them.
1257 (defun dump-output-stream-string (in-stream out-stream)
1258 (%write-string (string-output-stream-string in-stream)
1261 (string-output-stream-index in-stream))
1262 (setf (string-output-stream-index in-stream) 0))
1264 ;;;; fill-pointer streams
1266 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1267 ;;; the CLM, but they are required for the implementation of
1268 ;;; WITH-OUTPUT-TO-STRING.
1270 (deftype string-with-fill-pointer ()
1272 (satisfies array-has-fill-pointer-p)))
1274 (defstruct (fill-pointer-output-stream
1275 (:include string-stream
1276 (out #'fill-pointer-ouch)
1277 (sout #'fill-pointer-sout)
1278 (misc #'fill-pointer-misc)
1279 ;; a string with a fill pointer where we stuff
1280 ;; the stuff we write
1281 (string (error "missing argument")
1282 :type string-with-fill-pointer
1284 (:constructor make-fill-pointer-output-stream (string))
1287 (defun fill-pointer-ouch (stream character)
1288 (let* ((buffer (fill-pointer-output-stream-string stream))
1289 (current (fill-pointer buffer))
1290 (current+1 (1+ current)))
1291 (declare (fixnum current))
1292 (with-array-data ((workspace buffer) (start) (end))
1293 (declare (simple-string workspace))
1294 (let ((offset-current (+ start current)))
1295 (declare (fixnum offset-current))
1296 (if (= offset-current end)
1297 (let* ((new-length (1+ (* current 2)))
1298 (new-workspace (make-string new-length)))
1299 (declare (simple-string new-workspace))
1300 (%byte-blt workspace start
1301 new-workspace 0 current)
1302 (setf workspace new-workspace)
1303 (setf offset-current current)
1304 (set-array-header buffer workspace new-length
1305 current+1 0 new-length nil))
1306 (setf (fill-pointer buffer) current+1))
1307 (setf (schar workspace offset-current) character)))
1310 (defun fill-pointer-sout (stream string start end)
1311 (declare (simple-string string) (fixnum start end))
1312 (let* ((buffer (fill-pointer-output-stream-string stream))
1313 (current (fill-pointer buffer))
1314 (string-len (- end start))
1315 (dst-end (+ string-len current)))
1316 (declare (fixnum current dst-end string-len))
1317 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1318 (declare (simple-string workspace))
1319 (let ((offset-dst-end (+ dst-start dst-end))
1320 (offset-current (+ dst-start current)))
1321 (declare (fixnum offset-dst-end offset-current))
1322 (if (> offset-dst-end dst-length)
1323 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1324 (new-workspace (make-string new-length)))
1325 (declare (simple-string new-workspace))
1326 (%byte-blt workspace dst-start
1327 new-workspace 0 current)
1328 (setf workspace new-workspace)
1329 (setf offset-current current)
1330 (setf offset-dst-end dst-end)
1331 (set-array-header buffer
1338 (setf (fill-pointer buffer) dst-end))
1339 (%byte-blt string start
1340 workspace offset-current offset-dst-end)))
1343 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1344 (declare (ignore arg1 arg2))
1347 (let* ((buffer (fill-pointer-output-stream-string stream))
1348 (current (fill-pointer buffer)))
1349 (with-array-data ((string buffer) (start) (end current))
1350 (declare (simple-string string) (ignore start))
1351 (let ((found (position #\newline string :test #'char=
1352 :end end :from-end t)))
1354 (- end (the fixnum found))
1356 (:element-type 'base-char)))
1358 ;;;; indenting streams
1360 (defstruct (indenting-stream (:include ansi-stream
1361 (out #'indenting-out)
1362 (sout #'indenting-sout)
1363 (misc #'indenting-misc))
1364 (:constructor make-indenting-stream (stream))
1366 ;; the stream we're based on
1368 ;; how much we indent on each line
1372 (setf (fdocumentation 'make-indenting-stream 'function)
1373 "Return an output stream which indents its output by some amount.")
1375 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1376 ;;; output on the given STREAM based on the specified SUB-STREAM.
1377 (defmacro indenting-indent (stream sub-stream)
1378 ;; KLUDGE: bare magic number 60
1379 `(do ((i 0 (+ i 60))
1380 (indentation (indenting-stream-indentation ,stream)))
1381 ((>= i indentation))
1386 (min 60 (- indentation i)))))
1388 ;;; INDENTING-OUT writes a character to an indenting stream.
1389 (defun indenting-out (stream char)
1390 (let ((sub-stream (indenting-stream-stream stream)))
1391 (write-char char sub-stream)
1392 (if (char= char #\newline)
1393 (indenting-indent stream sub-stream))))
1395 ;;; INDENTING-SOUT writes a string to an indenting stream.
1396 (defun indenting-sout (stream string start end)
1397 (declare (simple-string string) (fixnum start end))
1399 (sub-stream (indenting-stream-stream stream)))
1401 (let ((newline (position #\newline string :start i :end end)))
1403 (%write-string string sub-stream i (1+ newline))
1404 (indenting-indent stream sub-stream)
1405 (setq i (+ newline 1)))
1407 (%write-string string sub-stream i end)
1410 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1411 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1412 ;;; the base stream minus the stream's indentation.
1413 (defun indenting-misc (stream operation &optional arg1 arg2)
1414 (let ((sub-stream (indenting-stream-stream stream)))
1415 (if (ansi-stream-p sub-stream)
1416 (let ((method (ansi-stream-misc sub-stream)))
1419 (let ((line-length (funcall method sub-stream operation)))
1421 (- line-length (indenting-stream-indentation stream)))))
1423 (let ((charpos (funcall method sub-stream operation)))
1425 (- charpos (indenting-stream-indentation stream)))))
1427 (funcall method sub-stream operation arg1 arg2))))
1428 ;; must be Gray streams FUNDAMENTAL-STREAM
1431 (let ((line-length (stream-line-length sub-stream)))
1433 (- line-length (indenting-stream-indentation stream)))))
1435 (let ((charpos (stream-line-column sub-stream)))
1437 (- charpos (indenting-stream-indentation stream)))))
1439 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1441 (declaim (maybe-inline read-char unread-char read-byte listen))
1443 ;;;; case frobbing streams, used by FORMAT ~(...~)
1445 (defstruct (case-frob-stream
1446 (:include ansi-stream
1447 (misc #'case-frob-misc))
1448 (:constructor %make-case-frob-stream (target out sout))
1450 (target (missing-arg) :type stream))
1452 (defun make-case-frob-stream (target kind)
1454 "Return a stream that sends all output to the stream TARGET, but modifies
1455 the case of letters, depending on KIND, which should be one of:
1456 :upcase - convert to upper case.
1457 :downcase - convert to lower case.
1458 :capitalize - convert the first letter of words to upper case and the
1459 rest of the word to lower case.
1460 :capitalize-first - convert the first letter of the first word to upper
1461 case and everything else to lower case."
1462 (declare (type stream target)
1463 (type (member :upcase :downcase :capitalize :capitalize-first)
1466 (if (case-frob-stream-p target)
1467 ;; If we are going to be writing to a stream that already does
1468 ;; case frobbing, why bother frobbing the case just so it can
1471 (multiple-value-bind (out sout)
1474 (values #'case-frob-upcase-out
1475 #'case-frob-upcase-sout))
1477 (values #'case-frob-downcase-out
1478 #'case-frob-downcase-sout))
1480 (values #'case-frob-capitalize-out
1481 #'case-frob-capitalize-sout))
1483 (values #'case-frob-capitalize-first-out
1484 #'case-frob-capitalize-first-sout)))
1485 (%make-case-frob-stream target out sout))))
1487 (defun case-frob-misc (stream op &optional arg1 arg2)
1488 (declare (type case-frob-stream stream))
1492 (let ((target (case-frob-stream-target stream)))
1493 (if (ansi-stream-p target)
1494 (funcall (ansi-stream-misc target) target op arg1 arg2)
1495 (stream-misc-dispatch target op arg1 arg2))))))
1497 (defun case-frob-upcase-out (stream char)
1498 (declare (type case-frob-stream stream)
1499 (type base-char char))
1500 (let ((target (case-frob-stream-target stream))
1501 (char (char-upcase char)))
1502 (if (ansi-stream-p target)
1503 (funcall (ansi-stream-out target) target char)
1504 (stream-write-char target char))))
1506 (defun case-frob-upcase-sout (stream str start end)
1507 (declare (type case-frob-stream stream)
1508 (type simple-base-string str)
1510 (type (or index null) end))
1511 (let* ((target (case-frob-stream-target stream))
1514 (string (if (and (zerop start) (= len end))
1516 (nstring-upcase (subseq str start end))))
1517 (string-len (- end start)))
1518 (if (ansi-stream-p target)
1519 (funcall (ansi-stream-sout target) target string 0 string-len)
1520 (stream-write-string target string 0 string-len))))
1522 (defun case-frob-downcase-out (stream char)
1523 (declare (type case-frob-stream stream)
1524 (type base-char char))
1525 (let ((target (case-frob-stream-target stream))
1526 (char (char-downcase char)))
1527 (if (ansi-stream-p target)
1528 (funcall (ansi-stream-out target) target char)
1529 (stream-write-char target char))))
1531 (defun case-frob-downcase-sout (stream str start end)
1532 (declare (type case-frob-stream stream)
1533 (type simple-base-string str)
1535 (type (or index null) end))
1536 (let* ((target (case-frob-stream-target stream))
1539 (string (if (and (zerop start) (= len end))
1540 (string-downcase str)
1541 (nstring-downcase (subseq str start end))))
1542 (string-len (- end start)))
1543 (if (ansi-stream-p target)
1544 (funcall (ansi-stream-sout target) target string 0 string-len)
1545 (stream-write-string target string 0 string-len))))
1547 (defun case-frob-capitalize-out (stream char)
1548 (declare (type case-frob-stream stream)
1549 (type base-char char))
1550 (let ((target (case-frob-stream-target stream)))
1551 (cond ((alphanumericp char)
1552 (let ((char (char-upcase char)))
1553 (if (ansi-stream-p target)
1554 (funcall (ansi-stream-out target) target char)
1555 (stream-write-char target char)))
1556 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1557 (setf (case-frob-stream-sout stream)
1558 #'case-frob-capitalize-aux-sout))
1560 (if (ansi-stream-p target)
1561 (funcall (ansi-stream-out target) target char)
1562 (stream-write-char target char))))))
1564 (defun case-frob-capitalize-sout (stream str start end)
1565 (declare (type case-frob-stream stream)
1566 (type simple-base-string str)
1568 (type (or index null) end))
1569 (let* ((target (case-frob-stream-target stream))
1570 (str (subseq str start end))
1574 (let ((char (schar str i)))
1575 (cond ((not (alphanumericp char))
1576 (setf inside-word nil))
1578 (setf (schar str i) (char-downcase char)))
1580 (setf inside-word t)
1581 (setf (schar str i) (char-upcase char))))))
1583 (setf (case-frob-stream-out stream)
1584 #'case-frob-capitalize-aux-out)
1585 (setf (case-frob-stream-sout stream)
1586 #'case-frob-capitalize-aux-sout))
1587 (if (ansi-stream-p target)
1588 (funcall (ansi-stream-sout target) target str 0 len)
1589 (stream-write-string target str 0 len))))
1591 (defun case-frob-capitalize-aux-out (stream char)
1592 (declare (type case-frob-stream stream)
1593 (type base-char char))
1594 (let ((target (case-frob-stream-target stream)))
1595 (cond ((alphanumericp char)
1596 (let ((char (char-downcase char)))
1597 (if (ansi-stream-p target)
1598 (funcall (ansi-stream-out target) target char)
1599 (stream-write-char target char))))
1601 (if (ansi-stream-p target)
1602 (funcall (ansi-stream-out target) target char)
1603 (stream-write-char target char))
1604 (setf (case-frob-stream-out stream)
1605 #'case-frob-capitalize-out)
1606 (setf (case-frob-stream-sout stream)
1607 #'case-frob-capitalize-sout)))))
1609 (defun case-frob-capitalize-aux-sout (stream str start end)
1610 (declare (type case-frob-stream stream)
1611 (type simple-base-string str)
1613 (type (or index null) end))
1614 (let* ((target (case-frob-stream-target stream))
1615 (str (subseq str start end))
1619 (let ((char (schar str i)))
1620 (cond ((not (alphanumericp char))
1621 (setf inside-word nil))
1623 (setf (schar str i) (char-downcase char)))
1625 (setf inside-word t)
1626 (setf (schar str i) (char-upcase char))))))
1628 (setf (case-frob-stream-out stream)
1629 #'case-frob-capitalize-out)
1630 (setf (case-frob-stream-sout stream)
1631 #'case-frob-capitalize-sout))
1632 (if (ansi-stream-p target)
1633 (funcall (ansi-stream-sout target) target str 0 len)
1634 (stream-write-string target str 0 len))))
1636 (defun case-frob-capitalize-first-out (stream char)
1637 (declare (type case-frob-stream stream)
1638 (type base-char char))
1639 (let ((target (case-frob-stream-target stream)))
1640 (cond ((alphanumericp char)
1641 (let ((char (char-upcase char)))
1642 (if (ansi-stream-p target)
1643 (funcall (ansi-stream-out target) target char)
1644 (stream-write-char target char)))
1645 (setf (case-frob-stream-out stream)
1646 #'case-frob-downcase-out)
1647 (setf (case-frob-stream-sout stream)
1648 #'case-frob-downcase-sout))
1650 (if (ansi-stream-p target)
1651 (funcall (ansi-stream-out target) target char)
1652 (stream-write-char target char))))))
1654 (defun case-frob-capitalize-first-sout (stream str start end)
1655 (declare (type case-frob-stream stream)
1656 (type simple-base-string str)
1658 (type (or index null) end))
1659 (let* ((target (case-frob-stream-target stream))
1660 (str (subseq str start end))
1663 (let ((char (schar str i)))
1664 (when (alphanumericp char)
1665 (setf (schar str i) (char-upcase char))
1666 (do ((i (1+ i) (1+ i)))
1668 (setf (schar str i) (char-downcase (schar str i))))
1669 (setf (case-frob-stream-out stream)
1670 #'case-frob-downcase-out)
1671 (setf (case-frob-stream-sout stream)
1672 #'case-frob-downcase-sout)
1674 (if (ansi-stream-p target)
1675 (funcall (ansi-stream-sout target) target str 0 len)
1676 (stream-write-string target str 0 len))))
1680 (defun read-sequence (seq stream &key (start 0) end)
1682 "Destructively modify SEQ by reading elements from STREAM.
1683 That part of SEQ bounded by START and END is destructively modified by
1684 copying successive elements into it from STREAM. If the end of file
1685 for STREAM is reached before copying all elements of the subsequence,
1686 then the extra elements near the end of sequence are not updated, and
1687 the index of the next element is returned."
1688 (declare (type sequence seq)
1689 (type stream stream)
1691 (type sequence-end end)
1693 (if (ansi-stream-p stream)
1694 (ansi-stream-read-sequence seq stream start end)
1695 ;; must be Gray streams FUNDAMENTAL-STREAM
1696 (stream-read-sequence stream seq start end)))
1698 (defun ansi-stream-read-sequence (seq stream start %end)
1699 (declare (type sequence seq)
1700 (type ansi-stream stream)
1702 (type sequence-end %end)
1704 (let ((end (or %end (length seq))))
1705 (declare (type index end))
1708 (let ((read-function
1709 (if (subtypep (stream-element-type stream) 'character)
1712 (do ((rem (nthcdr start seq) (rest rem))
1714 ((or (endp rem) (>= i end)) i)
1715 (declare (type list rem)
1717 (let ((el (funcall read-function stream nil :eof)))
1720 (setf (first rem) el)))))
1722 (with-array-data ((data seq) (offset-start start) (offset-end end))
1724 ((or (simple-array (unsigned-byte 8) (*))
1725 (simple-array (signed-byte 8) (*))
1727 (let* ((numbytes (- end start))
1728 (bytes-read (sb!sys:read-n-bytes stream
1733 (if (< bytes-read numbytes)
1734 (+ start bytes-read)
1737 (let ((read-function
1738 (if (subtypep (stream-element-type stream) 'character)
1741 (do ((i offset-start (1+ i)))
1742 ((>= i offset-end) end)
1743 (declare (type index i))
1744 (let ((el (funcall read-function stream nil :eof)))
1746 (return (+ start (- i offset-start))))
1747 (setf (aref data i) el)))))))))))
1751 (defun write-sequence (seq stream &key (start 0) (end nil))
1753 "Write the elements of SEQ bounded by START and END to STREAM."
1754 (declare (type sequence seq)
1755 (type stream stream)
1757 (type sequence-end end)
1759 (if (ansi-stream-p stream)
1760 (ansi-stream-write-sequence seq stream start end)
1761 ;; must be Gray-streams FUNDAMENTAL-STREAM
1762 (stream-write-sequence stream seq start end)))
1764 (defun ansi-stream-write-sequence (seq stream start %end)
1765 (declare (type sequence seq)
1766 (type ansi-stream stream)
1768 (type sequence-end %end)
1770 (let ((end (or %end (length seq))))
1771 (declare (type index end))
1774 (let ((write-function
1775 (if (subtypep (stream-element-type stream) 'character)
1778 (do ((rem (nthcdr start seq) (rest rem))
1780 ((or (endp rem) (>= i end)) seq)
1781 (declare (type list rem)
1783 (funcall write-function (first rem) stream))))
1785 (%write-string seq stream start end))
1787 (let ((write-function
1788 (if (subtypep (stream-element-type stream) 'character)
1791 (do ((i start (1+ i)))
1793 (declare (type index i))
1794 (funcall write-function (aref seq i) stream)))))))
1798 ;;; (These were inline throughout this file, but that's not appropriate
1800 (declaim (maybe-inline read-char unread-char read-byte listen))