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 (declare (type (or file-stream synonym-stream) stream))
195 (stream-must-be-associated-with-file stream)
196 (funcall (ansi-stream-misc stream) stream :file-length))
200 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
202 (declare (ignore recursive-p))
203 (let ((stream (in-synonym-of stream)))
204 (if (ansi-stream-p stream)
205 (prepare-for-fast-read-char stream
206 (let ((res (make-string 80))
210 (let ((ch (fast-read-char nil nil)))
212 (when (char= ch #\newline)
213 (done-with-fast-read-char)
214 (return (values (shrink-vector res index) nil)))
217 (let ((new (make-string len)))
220 (setf (schar res index) ch)
223 (done-with-fast-read-char)
224 (return (values (eof-or-lose stream
228 ;; Since FAST-READ-CHAR already hit the eof char, we
229 ;; shouldn't do another READ-CHAR.
231 (done-with-fast-read-char)
232 (return (values (shrink-vector res index) t))))))))
233 ;; must be Gray streams FUNDAMENTAL-STREAM
234 (multiple-value-bind (string eof) (stream-read-line stream)
235 (if (and eof (zerop (length string)))
236 (values (eof-or-lose stream eof-error-p eof-value) t)
237 (values string eof))))))
239 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
240 ;;; so, except in this file, they are not inline by default, but they can be.
241 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
243 (defun read-char (&optional (stream *standard-input*)
247 (declare (ignore recursive-p))
248 (let ((stream (in-synonym-of stream)))
249 (if (ansi-stream-p stream)
250 (prepare-for-fast-read-char stream
252 (fast-read-char eof-error-p eof-value)
253 (done-with-fast-read-char)))
254 ;; must be Gray streams FUNDAMENTAL-STREAM
255 (let ((char (stream-read-char stream)))
257 (eof-or-lose stream eof-error-p eof-value)
260 (defun unread-char (character &optional (stream *standard-input*))
261 (let ((stream (in-synonym-of stream)))
262 (if (ansi-stream-p stream)
263 (let ((index (1- (ansi-stream-in-index stream)))
264 (buffer (ansi-stream-in-buffer stream)))
265 (declare (fixnum index))
266 (when (minusp index) (error "nothing to unread"))
268 (setf (aref buffer index) (char-code character))
269 (setf (ansi-stream-in-index stream) index))
271 (funcall (ansi-stream-misc stream) stream
272 :unread character))))
273 ;; must be Gray streams FUNDAMENTAL-STREAM
274 (stream-unread-char stream character)))
278 ;;; In the interest of ``once and only once'' this macro contains the
279 ;;; framework necessary to implement a peek-char function, which has
280 ;;; two special-cases (one for gray streams and one for echo streams)
281 ;;; in addition to the normal case.
283 ;;; All arguments are forms which will be used for a specific purpose
284 ;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
285 ;;; EOF-VALUE - the eof-value argument to peek-char
286 ;;; CHAR-VAR - the variable which will be used to store the current character
287 ;;; READ-FORM - the form which will be used to read a character
288 ;;; UNREAD-FORM - ditto for unread-char
289 ;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
290 ;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
291 ;;; (this will default to CHAR-VAR)
292 (defmacro generalized-peeking-mechanism (peek-type eof-value char-var read-form unread-form &optional (skipped-char-form nil) (eof-detected-form nil))
293 `(let ((,char-var ,read-form))
294 (cond ((eql ,char-var ,eof-value)
295 ,(if eof-detected-form
298 ((characterp ,peek-type)
299 (do ((,char-var ,char-var ,read-form))
300 ((or (eql ,char-var ,eof-value)
301 (char= ,char-var ,peek-type))
302 (cond ((eql ,char-var ,eof-value)
303 ,(if eof-detected-form
310 (do ((,char-var ,char-var ,read-form))
311 ((or (eql ,char-var ,eof-value)
312 (not (whitespace-char-p ,char-var)))
313 (cond ((eql ,char-var ,eof-value)
314 ,(if eof-detected-form
324 (bug "Impossible case reached in PEEK-CHAR")))))
326 (defun peek-char (&optional (peek-type nil)
327 (stream *standard-input*)
331 (declare (ignore recursive-p))
332 ;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
333 ;; the compiler doesn't seem to be smart enough to go from there to
334 ;; imposing a type check. Figure out why (because PEEK-TYPE is an
335 ;; &OPTIONAL argument?) and fix it, and then this explicit type
336 ;; check can go away.
337 (unless (typep peek-type '(or character boolean))
338 (error 'simple-type-error
340 :expected-type '(or character boolean)
341 :format-control "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>"
342 :format-arguments (list peek-type '(or character boolean))))
343 (let ((stream (in-synonym-of stream)))
344 (cond ((typep stream 'echo-stream)
348 (list eof-error-p eof-value)))
349 ((ansi-stream-p stream)
350 (generalized-peeking-mechanism
351 peek-type eof-value char
352 (read-char stream eof-error-p eof-value)
353 (unread-char char stream)))
355 ;; by elimination, must be Gray streams FUNDAMENTAL-STREAM
356 (generalized-peeking-mechanism
359 (stream-peek-char stream)
360 (stream-read-char stream))
363 (stream-unread-char stream char))
365 (eof-or-lose stream eof-error-p eof-value))))))
367 (defun listen (&optional (stream *standard-input*))
368 (let ((stream (in-synonym-of stream)))
369 (if (ansi-stream-p stream)
370 (or (/= (the fixnum (ansi-stream-in-index stream))
371 +ansi-stream-in-buffer-length+)
372 ;; Test for T explicitly since misc methods return :EOF sometimes.
373 (eq (funcall (ansi-stream-misc stream) stream :listen) t))
374 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
375 (stream-listen stream))))
377 (defun read-char-no-hang (&optional (stream *standard-input*)
381 (declare (ignore recursive-p))
382 (let ((stream (in-synonym-of stream)))
383 (if (ansi-stream-p stream)
384 (if (funcall (ansi-stream-misc stream) stream :listen)
385 ;; On T or :EOF get READ-CHAR to do the work.
386 (read-char stream eof-error-p eof-value)
388 ;; must be Gray streams FUNDAMENTAL-STREAM
389 (let ((char (stream-read-char-no-hang stream)))
391 (eof-or-lose stream eof-error-p eof-value)
394 (defun clear-input (&optional (stream *standard-input*))
395 (let ((stream (in-synonym-of stream)))
396 (cond ((ansi-stream-p stream)
397 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
398 (funcall (ansi-stream-misc stream) stream :clear-input))
400 (stream-clear-input stream))))
403 (declaim (maybe-inline read-byte))
404 (defun read-byte (stream &optional (eof-error-p t) eof-value)
405 (let ((stream (in-synonym-of stream)))
406 (if (ansi-stream-p stream)
407 (prepare-for-fast-read-byte stream
409 (fast-read-byte eof-error-p eof-value t)
410 (done-with-fast-read-byte)))
411 ;; must be Gray streams FUNDAMENTAL-STREAM
412 (let ((char (stream-read-byte stream)))
414 (eof-or-lose stream eof-error-p eof-value)
417 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
418 ;;; number of bytes read.
420 ;;; Note: CMU CL's version of this had a special interpretation of
421 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
422 ;;; case, CMU CL's version would return as soon as any data became
423 ;;; available.) This could be useful behavior for things like pipes in
424 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
425 ;;; If we ever need it, it could be added later as a new variant N-BIN
426 ;;; method (perhaps N-BIN-ASAP?) or something.
427 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
428 (declare (type ansi-stream stream)
429 (type index numbytes start)
430 (type (or (simple-array * (*)) system-area-pointer) buffer))
431 (let* ((stream (in-synonym-of stream ansi-stream))
432 (in-buffer (ansi-stream-in-buffer stream))
433 (index (ansi-stream-in-index stream))
434 (num-buffered (- +ansi-stream-in-buffer-length+ index)))
435 (declare (fixnum index num-buffered))
438 (funcall (ansi-stream-n-bin stream)
444 ((<= numbytes num-buffered)
445 (%byte-blt in-buffer index
446 buffer start (+ start numbytes))
447 (setf (ansi-stream-in-index stream) (+ index numbytes))
450 (let ((end (+ start num-buffered)))
451 (%byte-blt in-buffer index buffer start end)
452 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
453 (+ (funcall (ansi-stream-n-bin stream)
457 (- numbytes num-buffered)
461 ;;; the amount of space we leave at the start of the in-buffer for
464 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
465 (defconstant +ansi-stream-in-buffer-extra+
466 4) ; FIXME: should be symbolic constant
468 ;;; This function is called by the FAST-READ-CHAR expansion to refill
469 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
470 ;;; and hence must be an N-BIN method.
471 (defun fast-read-char-refill (stream eof-error-p eof-value)
472 (let* ((ibuf (ansi-stream-in-buffer stream))
473 (count (funcall (ansi-stream-n-bin stream)
476 +ansi-stream-in-buffer-extra+
477 (- +ansi-stream-in-buffer-length+
478 +ansi-stream-in-buffer-extra+)
480 (start (- +ansi-stream-in-buffer-length+ count)))
481 (declare (type index start count))
483 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
484 (funcall (ansi-stream-in stream) stream eof-error-p eof-value))
486 (when (/= start +ansi-stream-in-buffer-extra+)
487 (bit-bash-copy ibuf (+ (* +ansi-stream-in-buffer-extra+
489 (* sb!vm:vector-data-offset
491 ibuf (+ (the index (* start sb!vm:n-byte-bits))
492 (* sb!vm:vector-data-offset
494 (* count sb!vm:n-byte-bits)))
495 (setf (ansi-stream-in-index stream) (1+ start))
496 (code-char (aref ibuf start))))))
498 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
499 ;;; leave room for unreading.
500 (defun fast-read-byte-refill (stream eof-error-p eof-value)
501 (let* ((ibuf (ansi-stream-in-buffer stream))
502 (count (funcall (ansi-stream-n-bin stream) stream
503 ibuf 0 +ansi-stream-in-buffer-length+
505 (start (- +ansi-stream-in-buffer-length+ count)))
506 (declare (type index start count))
508 (setf (ansi-stream-in-index stream) +ansi-stream-in-buffer-length+)
509 (funcall (ansi-stream-bin stream) stream eof-error-p eof-value))
511 (unless (zerop start)
512 (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:n-word-bits)
513 ibuf (+ (the index (* start sb!vm:n-byte-bits))
514 (* sb!vm:vector-data-offset
516 (* count sb!vm:n-byte-bits)))
517 (setf (ansi-stream-in-index stream) (1+ start))
518 (aref ibuf start)))))
522 (defun write-char (character &optional (stream *standard-output*))
523 (with-out-stream stream (ansi-stream-out character)
524 (stream-write-char character))
527 (defun terpri (&optional (stream *standard-output*))
528 (with-out-stream stream (ansi-stream-out #\newline) (stream-terpri))
531 (defun fresh-line (&optional (stream *standard-output*))
532 (let ((stream (out-synonym-of stream)))
533 (if (ansi-stream-p stream)
534 (when (/= (or (charpos stream) 1) 0)
535 (funcall (ansi-stream-out stream) stream #\newline)
537 ;; must be Gray streams FUNDAMENTAL-STREAM
538 (stream-fresh-line stream))))
540 (defun write-string (string &optional (stream *standard-output*)
542 (declare (type string string))
543 ;; Note that even though you might expect, based on the behavior of
544 ;; things like AREF, that the correct upper bound here is
545 ;; (ARRAY-DIMENSION STRING 0), the ANSI glossary definitions for
546 ;; "bounding index" and "length" indicate that in this case (i.e.
547 ;; for the ANSI-specified functions WRITE-STRING [and WRITE-LINE]),
548 ;; (LENGTH STRING) is the required upper bound. A foolish
549 ;; consistency is the hobgoblin of lesser languages..
550 (%write-string string stream start (%check-vector-sequence-bounds
554 (defun %write-string (string stream start end)
555 (declare (type string string))
556 (declare (type streamlike stream))
557 (declare (type index start end))
558 (let ((stream (out-synonym-of stream)))
559 (cond ((ansi-stream-p stream)
560 (if (array-header-p string)
561 (with-array-data ((data string) (offset-start start)
563 (funcall (ansi-stream-sout stream)
564 stream data offset-start offset-end))
565 (funcall (ansi-stream-sout stream) stream string start end))
567 (t ; must be Gray streams FUNDAMENTAL-STREAM
568 (stream-write-string stream string start end)))))
570 (defun write-line (string &optional (stream *standard-output*)
572 (declare (type string string))
573 ;; FIXME: Why is there this difference between the treatments of the
574 ;; STREAM argument in WRITE-STRING and WRITE-LINE?
575 (let ((defaulted-stream (out-synonym-of stream)))
576 (%write-string string defaulted-stream start (%check-vector-sequence-bounds
578 (write-char #\newline defaulted-stream))
581 (defun charpos (&optional (stream *standard-output*))
582 (with-out-stream stream (ansi-stream-misc :charpos) (stream-line-column)))
584 (defun line-length (&optional (stream *standard-output*))
585 (with-out-stream stream (ansi-stream-misc :line-length)
586 (stream-line-length)))
588 (defun finish-output (&optional (stream *standard-output*))
589 (with-out-stream stream (ansi-stream-misc :finish-output)
590 (stream-finish-output))
593 (defun force-output (&optional (stream *standard-output*))
594 (with-out-stream stream (ansi-stream-misc :force-output)
595 (stream-force-output))
598 (defun clear-output (&optional (stream *standard-output*))
599 (with-out-stream stream (ansi-stream-misc :clear-output)
600 (stream-force-output))
603 (defun write-byte (integer stream)
604 (with-out-stream stream (ansi-stream-bout integer)
605 (stream-write-byte integer))
608 ;;; This is called from ANSI-STREAM routines that encapsulate CLOS
609 ;;; streams to handle the misc routines and dispatch to the
610 ;;; appropriate Gray stream functions.
611 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
612 (declare (type fundamental-stream stream)
616 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
617 (let ((char (stream-read-char-no-hang stream)))
618 (when (characterp char)
619 (stream-unread-char stream char))
622 (stream-unread-char stream arg1))
626 (stream-clear-input stream))
628 (stream-force-output stream))
630 (stream-finish-output stream))
632 (stream-element-type stream))
634 (interactive-stream-p stream))
636 (stream-line-length stream))
638 (stream-line-column stream))
640 (file-length stream))
642 (file-position stream arg1))))
644 ;;;; broadcast streams
646 (defstruct (broadcast-stream (:include ansi-stream
647 (out #'broadcast-out)
648 (bout #'broadcast-bout)
649 (sout #'broadcast-sout)
650 (misc #'broadcast-misc))
651 (:constructor %make-broadcast-stream
654 ;; a list of all the streams we broadcast to
655 (streams () :type list :read-only t))
657 (defun make-broadcast-stream (&rest streams)
658 (dolist (stream streams)
659 (unless (or (and (synonym-stream-p stream)
660 (output-stream-p (symbol-value
661 (synonym-stream-symbol stream))))
662 (output-stream-p stream))
665 :expected-type '(satisfies output-stream-p))))
666 (apply #'%make-broadcast-stream streams))
668 (macrolet ((out-fun (fun method stream-method &rest args)
669 `(defun ,fun (stream ,@args)
670 (dolist (stream (broadcast-stream-streams stream))
671 (if (ansi-stream-p stream)
672 (funcall (,method stream) stream ,@args)
673 (,stream-method stream ,@args))))))
674 (out-fun broadcast-out ansi-stream-out stream-write-char char)
675 (out-fun broadcast-bout ansi-stream-bout stream-write-byte byte)
676 (out-fun broadcast-sout ansi-stream-sout stream-write-string
679 (defun broadcast-misc (stream operation &optional arg1 arg2)
680 (let ((streams (broadcast-stream-streams stream)))
683 (dolist (stream streams)
684 (let ((charpos (charpos stream)))
685 (if charpos (return charpos)))))
688 (dolist (stream streams min)
689 (let ((res (line-length stream)))
690 (when res (setq min (if min (min res min) res)))))))
693 (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
694 (pushnew (stream-element-type stream) res :test #'equal))))
698 (dolist (stream streams res)
700 (if (ansi-stream-p stream)
701 (funcall (ansi-stream-misc stream) stream operation
703 (stream-misc-dispatch stream operation arg1 arg2)))))))))
707 (defstruct (synonym-stream (:include ansi-stream
710 (n-bin #'synonym-n-bin)
712 (bout #'synonym-bout)
713 (sout #'synonym-sout)
714 (misc #'synonym-misc))
715 (:constructor make-synonym-stream (symbol))
717 ;; This is the symbol, the value of which is the stream we are synonym to.
718 (symbol nil :type symbol :read-only t))
719 (def!method print-object ((x synonym-stream) stream)
720 (print-unreadable-object (x stream :type t :identity t)
721 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
723 ;;; The output simple output methods just call the corresponding method
724 ;;; in the synonymed stream.
725 (macrolet ((out-fun (name slot stream-method &rest args)
726 `(defun ,name (stream ,@args)
727 (declare (optimize (safety 1)))
728 (let ((syn (symbol-value (synonym-stream-symbol stream))))
729 (if (ansi-stream-p syn)
730 (funcall (,slot syn) syn ,@args)
731 (,stream-method syn ,@args))))))
732 (out-fun synonym-out ansi-stream-out stream-write-char ch)
733 (out-fun synonym-bout ansi-stream-bout stream-write-byte n)
734 (out-fun synonym-sout ansi-stream-sout stream-write-string string start end))
736 ;;; For the input methods, we just call the corresponding function on the
737 ;;; synonymed stream. These functions deal with getting input out of
738 ;;; the In-Buffer if there is any.
739 (macrolet ((in-fun (name fun &rest args)
740 `(defun ,name (stream ,@args)
741 (declare (optimize (safety 1)))
742 (,fun (symbol-value (synonym-stream-symbol stream))
744 (in-fun synonym-in read-char eof-error-p eof-value)
745 (in-fun synonym-bin read-byte eof-error-p eof-value)
746 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
748 (defun synonym-misc (stream operation &optional arg1 arg2)
749 (declare (optimize (safety 1)))
750 (let ((syn (symbol-value (synonym-stream-symbol stream))))
751 (if (ansi-stream-p syn)
752 ;; We have to special-case some operations which interact with
753 ;; the in-buffer of the wrapped stream, since just calling
754 ;; ANSI-STREAM-MISC on them
756 (:listen (or (/= (the fixnum (ansi-stream-in-index syn))
757 +ansi-stream-in-buffer-length+)
758 (funcall (ansi-stream-misc syn) syn :listen)))
759 (:clear-input (clear-input syn))
760 (:unread (unread-char arg1 syn))
762 (funcall (ansi-stream-misc syn) syn operation arg1 arg2)))
763 (stream-misc-dispatch syn operation arg1 arg2))))
767 (defstruct (two-way-stream
768 (:include ansi-stream
771 (n-bin #'two-way-n-bin)
773 (bout #'two-way-bout)
774 (sout #'two-way-sout)
775 (misc #'two-way-misc))
776 (:constructor %make-two-way-stream (input-stream output-stream))
778 (input-stream (missing-arg) :type stream :read-only t)
779 (output-stream (missing-arg) :type stream :read-only t))
780 (defprinter (two-way-stream) input-stream output-stream)
782 (defun make-two-way-stream (input-stream output-stream)
784 "Return a bidirectional stream which gets its input from INPUT-STREAM and
785 sends its output to OUTPUT-STREAM."
786 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
787 ;; should be encapsulated in a function, and used here and most of
788 ;; the other places that SYNONYM-STREAM-P appears.
789 (unless (or (and (synonym-stream-p output-stream)
790 (output-stream-p (symbol-value
791 (synonym-stream-symbol output-stream))))
792 (output-stream-p output-stream))
795 :expected-type '(satisfies output-stream-p)))
796 (unless (or (and (synonym-stream-p input-stream)
797 (input-stream-p (symbol-value
798 (synonym-stream-symbol input-stream))))
799 (input-stream-p input-stream))
802 :expected-type '(satisfies input-stream-p)))
803 (funcall #'%make-two-way-stream input-stream output-stream))
805 (macrolet ((out-fun (name slot stream-method &rest args)
806 `(defun ,name (stream ,@args)
807 (let ((syn (two-way-stream-output-stream stream)))
808 (if (ansi-stream-p syn)
809 (funcall (,slot syn) syn ,@args)
810 (,stream-method syn ,@args))))))
811 (out-fun two-way-out ansi-stream-out stream-write-char ch)
812 (out-fun two-way-bout ansi-stream-bout stream-write-byte n)
813 (out-fun two-way-sout ansi-stream-sout stream-write-string string start end))
815 (macrolet ((in-fun (name fun &rest args)
816 `(defun ,name (stream ,@args)
817 (force-output (two-way-stream-output-stream stream))
818 (,fun (two-way-stream-input-stream stream) ,@args))))
819 (in-fun two-way-in read-char eof-error-p eof-value)
820 (in-fun two-way-bin read-byte eof-error-p eof-value)
821 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
823 (defun two-way-misc (stream operation &optional arg1 arg2)
824 (let* ((in (two-way-stream-input-stream stream))
825 (out (two-way-stream-output-stream stream))
826 (in-ansi-stream-p (ansi-stream-p in))
827 (out-ansi-stream-p (ansi-stream-p out)))
831 (or (/= (the fixnum (ansi-stream-in-index in))
832 +ansi-stream-in-buffer-length+)
833 (funcall (ansi-stream-misc in) in :listen))
835 ((:finish-output :force-output :clear-output)
836 (if out-ansi-stream-p
837 (funcall (ansi-stream-misc out) out operation arg1 arg2)
838 (stream-misc-dispatch out operation arg1 arg2)))
839 (:clear-input (clear-input in))
840 (:unread (unread-char arg1 in))
842 (let ((in-type (stream-element-type in))
843 (out-type (stream-element-type out)))
844 (if (equal in-type out-type)
845 in-type `(and ,in-type ,out-type))))
847 (set-closed-flame stream))
849 (or (if in-ansi-stream-p
850 (funcall (ansi-stream-misc in) in operation arg1 arg2)
851 (stream-misc-dispatch in operation arg1 arg2))
852 (if out-ansi-stream-p
853 (funcall (ansi-stream-misc out) out operation arg1 arg2)
854 (stream-misc-dispatch out operation arg1 arg2)))))))
856 ;;;; concatenated streams
858 (defstruct (concatenated-stream
859 (:include ansi-stream
860 (in #'concatenated-in)
861 (bin #'concatenated-bin)
862 (n-bin #'concatenated-n-bin)
863 (misc #'concatenated-misc))
864 (:constructor %make-concatenated-stream
865 (&rest streams &aux (current streams)))
867 ;; The car of this is the substream we are reading from now.
869 ;; This is a list of all the substreams there ever were. We need to
870 ;; remember them so that we can close them.
872 ;; FIXME: ANSI says this is supposed to be the list of streams that
873 ;; we still have to read from. So either this needs to become a
874 ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
875 ;; around it which discards closed files from the head of the list)
876 ;; or we need to update it as we run out of files.
877 (streams nil :type list :read-only t))
878 (def!method print-object ((x concatenated-stream) stream)
879 (print-unreadable-object (x stream :type t :identity t)
882 (concatenated-stream-streams x))))
884 (defun make-concatenated-stream (&rest streams)
886 "Return a stream which takes its input from each of the streams in turn,
887 going on to the next at EOF."
888 (dolist (stream streams)
889 (unless (or (and (synonym-stream-p stream)
890 (input-stream-p (symbol-value
891 (synonym-stream-symbol stream))))
892 (input-stream-p stream))
895 :expected-type '(satisfies input-stream-p))))
896 (apply #'%make-concatenated-stream streams))
898 (macrolet ((in-fun (name fun)
899 `(defun ,name (stream eof-error-p eof-value)
900 (do ((current (concatenated-stream-current stream)
903 (eof-or-lose stream eof-error-p eof-value))
904 (let* ((stream (car current))
905 (result (,fun stream nil nil)))
906 (when result (return result)))
907 (pop (concatenated-stream-current stream))))))
908 (in-fun concatenated-in read-char)
909 (in-fun concatenated-bin read-byte))
911 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
912 (do ((current (concatenated-stream-current stream) (cdr current))
913 (current-start start)
914 (remaining-bytes numbytes))
917 (error 'end-of-file :stream stream)
918 (- numbytes remaining-bytes)))
919 (let* ((stream (car current))
920 (bytes-read (read-n-bytes stream buffer current-start
921 remaining-bytes nil)))
922 (incf current-start bytes-read)
923 (decf remaining-bytes bytes-read)
924 (when (zerop remaining-bytes) (return numbytes)))
925 (setf (concatenated-stream-current stream) (cdr current))))
927 (defun concatenated-misc (stream operation &optional arg1 arg2)
928 (let ((left (concatenated-stream-current stream)))
930 (let* ((current (car left)))
934 (let ((stuff (if (ansi-stream-p current)
935 (funcall (ansi-stream-misc current) current
937 (stream-misc-dispatch current :listen))))
938 (cond ((eq stuff :eof)
939 ;; Advance CURRENT, and try again.
940 (pop (concatenated-stream-current stream))
942 (car (concatenated-stream-current stream)))
944 ;; No further streams. EOF.
947 ;; Stuff's available.
950 ;; Nothing is available yet.
952 (:clear-input (clear-input current))
953 (:unread (unread-char arg1 current))
955 (set-closed-flame stream))
957 (if (ansi-stream-p current)
958 (funcall (ansi-stream-misc current) current operation arg1 arg2)
959 (stream-misc-dispatch current operation arg1 arg2))))))))
963 (defstruct (echo-stream
964 (:include two-way-stream
969 (:constructor %make-echo-stream (input-stream output-stream))
972 (def!method print-object ((x echo-stream) stream)
973 (print-unreadable-object (x stream :type t :identity t)
975 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
976 (two-way-stream-input-stream x)
977 (two-way-stream-output-stream x))))
979 (defun make-echo-stream (input-stream output-stream)
981 "Return a bidirectional stream which gets its input from INPUT-STREAM and
982 sends its output to OUTPUT-STREAM. In addition, all input is echoed to
984 (unless (or (and (synonym-stream-p output-stream)
985 (output-stream-p (symbol-value
986 (synonym-stream-symbol output-stream))))
987 (output-stream-p output-stream))
990 :expected-type '(satisfies output-stream-p)))
991 (unless (or (and (synonym-stream-p input-stream)
992 (input-stream-p (symbol-value
993 (synonym-stream-symbol input-stream))))
994 (input-stream-p input-stream))
997 :expected-type '(satisfies input-stream-p)))
998 (funcall #'%make-echo-stream input-stream output-stream))
1000 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
1001 `(defun ,name (stream ,@args)
1002 (or (pop (echo-stream-unread-stuff stream))
1003 (let* ((in (echo-stream-input-stream stream))
1004 (out (echo-stream-output-stream stream))
1005 (result (,fun in ,@args)))
1006 (if (ansi-stream-p out)
1007 (funcall (,out-slot out) out result)
1008 (,stream-method out result))
1010 (in-fun echo-in read-char ansi-stream-out stream-write-char
1011 eof-error-p eof-value)
1012 (in-fun echo-bin read-byte ansi-stream-bout stream-write-byte
1013 eof-error-p eof-value))
1015 (defun echo-misc (stream operation &optional arg1 arg2)
1016 (let* ((in (two-way-stream-input-stream stream))
1017 (out (two-way-stream-output-stream stream)))
1020 (or (not (null (echo-stream-unread-stuff stream)))
1021 (if (ansi-stream-p in)
1022 (or (/= (the fixnum (ansi-stream-in-index in))
1023 +ansi-stream-in-buffer-length+)
1024 (funcall (ansi-stream-misc in) in :listen))
1025 (stream-misc-dispatch in :listen))))
1026 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1028 (let ((in-type (stream-element-type in))
1029 (out-type (stream-element-type out)))
1030 (if (equal in-type out-type)
1031 in-type `(and ,in-type ,out-type))))
1033 (set-closed-flame stream))
1035 ;; For the special case of peeking into an echo-stream
1036 ;; arg1 is PEEK-TYPE, arg2 is (EOF-ERROR-P EOF-VALUE)
1037 ;; returns peeked-char, eof-value, or errors end-of-file
1039 ;; Note: This code could be moved into PEEK-CHAR if desired.
1040 ;; I am unsure whether this belongs with echo-streams because it is
1041 ;; echo-stream specific, or PEEK-CHAR because it is peeking code.
1042 ;; -- mrd 2002-11-18
1044 ;; UNREAD-CHAR-P indicates whether the current character was one
1045 ;; that was previously unread. In that case, we need to ensure that
1046 ;; the semantics for UNREAD-CHAR are held; the character should
1047 ;; not be echoed again.
1048 (let ((unread-char-p nil))
1050 (unless unread-char-p
1051 (if (ansi-stream-p out)
1052 (funcall (ansi-stream-out out) out c)
1054 (stream-write-char out c))))
1056 ;; Obtain input from unread buffer or input stream,
1057 ;; and set the flag appropriately.
1058 (cond ((not (null (echo-stream-unread-stuff stream)))
1059 (setf unread-char-p t)
1060 (pop (echo-stream-unread-stuff stream)))
1062 (setf unread-char-p nil)
1063 (read-char in (first arg2) (second arg2))))))
1064 (generalized-peeking-mechanism
1065 arg1 (second arg2) char
1067 (unread-char char in)
1070 (or (if (ansi-stream-p in)
1071 (funcall (ansi-stream-misc in) in operation arg1 arg2)
1072 (stream-misc-dispatch in operation arg1 arg2))
1073 (if (ansi-stream-p out)
1074 (funcall (ansi-stream-misc out) out operation arg1 arg2)
1075 (stream-misc-dispatch out operation arg1 arg2)))))))
1077 ;;;; base STRING-STREAM stuff
1079 (defstruct (string-stream
1080 (:include ansi-stream)
1083 (string nil :type string))
1085 ;;;; STRING-INPUT-STREAM stuff
1087 (defstruct (string-input-stream
1088 (:include string-stream
1090 (bin #'string-binch)
1091 (n-bin #'string-stream-read-n-bytes)
1092 (misc #'string-in-misc)
1093 (string nil :type simple-string))
1094 (:constructor internal-make-string-input-stream
1095 (string current end))
1097 (current nil :type index)
1098 (end nil :type index))
1100 (defun string-inch (stream eof-error-p eof-value)
1101 (let ((string (string-input-stream-string stream))
1102 (index (string-input-stream-current stream)))
1103 (declare (simple-string string) (fixnum index))
1104 (cond ((= index (the index (string-input-stream-end stream)))
1105 (eof-or-lose stream eof-error-p eof-value))
1107 (setf (string-input-stream-current stream) (1+ index))
1108 (aref string index)))))
1110 (defun string-binch (stream eof-error-p eof-value)
1111 (let ((string (string-input-stream-string stream))
1112 (index (string-input-stream-current stream)))
1113 (declare (simple-string string)
1115 (cond ((= index (the index (string-input-stream-end stream)))
1116 (eof-or-lose stream eof-error-p eof-value))
1118 (setf (string-input-stream-current stream) (1+ index))
1119 (char-code (aref string index))))))
1121 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1122 (declare (type string-input-stream stream)
1123 (type index start requested))
1124 (let* ((string (string-input-stream-string stream))
1125 (index (string-input-stream-current stream))
1126 (available (- (string-input-stream-end stream) index))
1127 (copy (min available requested)))
1128 (declare (simple-string string)
1129 (type index index available copy))
1131 (setf (string-input-stream-current stream)
1132 (truly-the index (+ index copy)))
1133 (sb!sys:without-gcing
1134 (system-area-copy (vector-sap string)
1135 (* index sb!vm:n-byte-bits)
1136 (if (typep buffer 'system-area-pointer)
1138 (vector-sap buffer))
1139 (* start sb!vm:n-byte-bits)
1140 (* copy sb!vm:n-byte-bits))))
1141 (if (and (> requested copy) eof-error-p)
1142 (error 'end-of-file :stream stream)
1145 (defun string-in-misc (stream operation &optional arg1 arg2)
1146 (declare (ignore arg2))
1150 (setf (string-input-stream-current stream) arg1)
1151 (string-input-stream-current stream)))
1152 (:file-length (length (string-input-stream-string stream)))
1153 (:unread (decf (string-input-stream-current stream)))
1154 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1155 (the fixnum (string-input-stream-end stream)))
1157 (:element-type 'base-char)))
1159 (defun make-string-input-stream (string &optional
1162 "Return an input stream which will supply the characters of STRING between
1163 START and END in order."
1164 (declare (type string string)
1166 (type (or index null) end))
1168 (internal-make-string-input-stream
1169 (coerce string 'simple-string)
1171 (%check-vector-sequence-bounds string start end)))
1173 ;;;; STRING-OUTPUT-STREAM stuff
1175 (defstruct (string-output-stream
1176 (:include string-stream
1178 (sout #'string-sout)
1179 (misc #'string-out-misc)
1180 ;; The string we throw stuff in.
1181 (string (make-string 40) :type simple-string))
1182 (:constructor make-string-output-stream ())
1184 ;; Index of the next location to use.
1185 (index 0 :type fixnum))
1188 (setf (fdocumentation 'make-string-output-stream 'function)
1189 "Return an output stream which will accumulate all output given it for
1190 the benefit of the function GET-OUTPUT-STREAM-STRING.")
1192 (defun string-ouch (stream character)
1193 (let ((current (string-output-stream-index stream))
1194 (workspace (string-output-stream-string stream)))
1195 (declare (simple-string workspace) (fixnum current))
1196 (if (= current (the fixnum (length workspace)))
1197 (let ((new-workspace (make-string (* current 2))))
1198 (replace new-workspace workspace)
1199 (setf (aref new-workspace current) character)
1200 (setf (string-output-stream-string stream) new-workspace))
1201 (setf (aref workspace current) character))
1202 (setf (string-output-stream-index stream) (1+ current))))
1204 (defun string-sout (stream string start end)
1205 (declare (simple-string string) (fixnum start end))
1206 (let* ((current (string-output-stream-index stream))
1207 (length (- end start))
1208 (dst-end (+ length current))
1209 (workspace (string-output-stream-string stream)))
1210 (declare (simple-string workspace)
1211 (fixnum current length dst-end))
1212 (if (> dst-end (the fixnum (length workspace)))
1213 (let ((new-workspace (make-string (+ (* current 2) length))))
1214 (replace new-workspace workspace :end2 current)
1215 (replace new-workspace string
1216 :start1 current :end1 dst-end
1217 :start2 start :end2 end)
1218 (setf (string-output-stream-string stream) new-workspace))
1219 (replace workspace string
1220 :start1 current :end1 dst-end
1221 :start2 start :end2 end))
1222 (setf (string-output-stream-index stream) dst-end)))
1224 (defun string-out-misc (stream operation &optional arg1 arg2)
1225 (declare (ignore arg2))
1229 (string-output-stream-index stream)))
1231 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1233 (count 0 (1+ count))
1234 (string (string-output-stream-string stream)))
1236 (declare (simple-string string)
1237 (fixnum index count))
1238 (if (char= (schar string index) #\newline)
1240 (:element-type 'base-char)))
1242 ;;; Return a string of all the characters sent to a stream made by
1243 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1244 (defun get-output-stream-string (stream)
1245 (declare (type string-output-stream stream))
1246 (let* ((length (string-output-stream-index stream))
1247 (result (make-string length)))
1248 (replace result (string-output-stream-string stream))
1249 (setf (string-output-stream-index stream) 0)
1252 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1253 ;;; GET-OUTPUT-STREAM-STRING would return them.
1254 (defun dump-output-stream-string (in-stream out-stream)
1255 (%write-string (string-output-stream-string in-stream)
1258 (string-output-stream-index in-stream))
1259 (setf (string-output-stream-index in-stream) 0))
1261 ;;;; fill-pointer streams
1263 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1264 ;;; the CLM, but they are required for the implementation of
1265 ;;; WITH-OUTPUT-TO-STRING.
1267 (deftype string-with-fill-pointer ()
1269 (satisfies array-has-fill-pointer-p)))
1271 (defstruct (fill-pointer-output-stream
1272 (:include string-stream
1273 (out #'fill-pointer-ouch)
1274 (sout #'fill-pointer-sout)
1275 (misc #'fill-pointer-misc)
1276 ;; a string with a fill pointer where we stuff
1277 ;; the stuff we write
1278 (string (error "missing argument")
1279 :type string-with-fill-pointer
1281 (:constructor make-fill-pointer-output-stream (string))
1284 (defun fill-pointer-ouch (stream character)
1285 (let* ((buffer (fill-pointer-output-stream-string stream))
1286 (current (fill-pointer buffer))
1287 (current+1 (1+ current)))
1288 (declare (fixnum current))
1289 (with-array-data ((workspace buffer) (start) (end))
1290 (declare (simple-string workspace))
1291 (let ((offset-current (+ start current)))
1292 (declare (fixnum offset-current))
1293 (if (= offset-current end)
1294 (let* ((new-length (1+ (* current 2)))
1295 (new-workspace (make-string new-length)))
1296 (declare (simple-string new-workspace))
1297 (%byte-blt workspace start
1298 new-workspace 0 current)
1299 (setf workspace new-workspace)
1300 (setf offset-current current)
1301 (set-array-header buffer workspace new-length
1302 current+1 0 new-length nil))
1303 (setf (fill-pointer buffer) current+1))
1304 (setf (schar workspace offset-current) character)))
1307 (defun fill-pointer-sout (stream string start end)
1308 (declare (simple-string string) (fixnum start end))
1309 (let* ((buffer (fill-pointer-output-stream-string stream))
1310 (current (fill-pointer buffer))
1311 (string-len (- end start))
1312 (dst-end (+ string-len current)))
1313 (declare (fixnum current dst-end string-len))
1314 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1315 (declare (simple-string workspace))
1316 (let ((offset-dst-end (+ dst-start dst-end))
1317 (offset-current (+ dst-start current)))
1318 (declare (fixnum offset-dst-end offset-current))
1319 (if (> offset-dst-end dst-length)
1320 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1321 (new-workspace (make-string new-length)))
1322 (declare (simple-string new-workspace))
1323 (%byte-blt workspace dst-start
1324 new-workspace 0 current)
1325 (setf workspace new-workspace)
1326 (setf offset-current current)
1327 (setf offset-dst-end dst-end)
1328 (set-array-header buffer
1335 (setf (fill-pointer buffer) dst-end))
1336 (%byte-blt string start
1337 workspace offset-current offset-dst-end)))
1340 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1341 (declare (ignore arg1 arg2))
1344 (let* ((buffer (fill-pointer-output-stream-string stream))
1345 (current (fill-pointer buffer)))
1346 (with-array-data ((string buffer) (start) (end current))
1347 (declare (simple-string string) (ignore start))
1348 (let ((found (position #\newline string :test #'char=
1349 :end end :from-end t)))
1351 (- end (the fixnum found))
1353 (:element-type 'base-char)))
1355 ;;;; indenting streams
1357 (defstruct (indenting-stream (:include ansi-stream
1358 (out #'indenting-out)
1359 (sout #'indenting-sout)
1360 (misc #'indenting-misc))
1361 (:constructor make-indenting-stream (stream))
1363 ;; the stream we're based on
1365 ;; how much we indent on each line
1369 (setf (fdocumentation 'make-indenting-stream 'function)
1370 "Return an output stream which indents its output by some amount.")
1372 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1373 ;;; output on the given STREAM based on the specified SUB-STREAM.
1374 (defmacro indenting-indent (stream sub-stream)
1375 ;; KLUDGE: bare magic number 60
1376 `(do ((i 0 (+ i 60))
1377 (indentation (indenting-stream-indentation ,stream)))
1378 ((>= i indentation))
1383 (min 60 (- indentation i)))))
1385 ;;; INDENTING-OUT writes a character to an indenting stream.
1386 (defun indenting-out (stream char)
1387 (let ((sub-stream (indenting-stream-stream stream)))
1388 (write-char char sub-stream)
1389 (if (char= char #\newline)
1390 (indenting-indent stream sub-stream))))
1392 ;;; INDENTING-SOUT writes a string to an indenting stream.
1393 (defun indenting-sout (stream string start end)
1394 (declare (simple-string string) (fixnum start end))
1396 (sub-stream (indenting-stream-stream stream)))
1398 (let ((newline (position #\newline string :start i :end end)))
1400 (%write-string string sub-stream i (1+ newline))
1401 (indenting-indent stream sub-stream)
1402 (setq i (+ newline 1)))
1404 (%write-string string sub-stream i end)
1407 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1408 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1409 ;;; the base stream minus the stream's indentation.
1410 (defun indenting-misc (stream operation &optional arg1 arg2)
1411 (let ((sub-stream (indenting-stream-stream stream)))
1412 (if (ansi-stream-p sub-stream)
1413 (let ((method (ansi-stream-misc sub-stream)))
1416 (let ((line-length (funcall method sub-stream operation)))
1418 (- line-length (indenting-stream-indentation stream)))))
1420 (let ((charpos (funcall method sub-stream operation)))
1422 (- charpos (indenting-stream-indentation stream)))))
1424 (funcall method sub-stream operation arg1 arg2))))
1425 ;; must be Gray streams FUNDAMENTAL-STREAM
1428 (let ((line-length (stream-line-length sub-stream)))
1430 (- line-length (indenting-stream-indentation stream)))))
1432 (let ((charpos (stream-line-column sub-stream)))
1434 (- charpos (indenting-stream-indentation stream)))))
1436 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1438 (declaim (maybe-inline read-char unread-char read-byte listen))
1440 ;;;; case frobbing streams, used by FORMAT ~(...~)
1442 (defstruct (case-frob-stream
1443 (:include ansi-stream
1444 (misc #'case-frob-misc))
1445 (:constructor %make-case-frob-stream (target out sout))
1447 (target (missing-arg) :type stream))
1449 (defun make-case-frob-stream (target kind)
1451 "Return a stream that sends all output to the stream TARGET, but modifies
1452 the case of letters, depending on KIND, which should be one of:
1453 :upcase - convert to upper case.
1454 :downcase - convert to lower case.
1455 :capitalize - convert the first letter of words to upper case and the
1456 rest of the word to lower case.
1457 :capitalize-first - convert the first letter of the first word to upper
1458 case and everything else to lower case."
1459 (declare (type stream target)
1460 (type (member :upcase :downcase :capitalize :capitalize-first)
1463 (if (case-frob-stream-p target)
1464 ;; If we are going to be writing to a stream that already does
1465 ;; case frobbing, why bother frobbing the case just so it can
1468 (multiple-value-bind (out sout)
1471 (values #'case-frob-upcase-out
1472 #'case-frob-upcase-sout))
1474 (values #'case-frob-downcase-out
1475 #'case-frob-downcase-sout))
1477 (values #'case-frob-capitalize-out
1478 #'case-frob-capitalize-sout))
1480 (values #'case-frob-capitalize-first-out
1481 #'case-frob-capitalize-first-sout)))
1482 (%make-case-frob-stream target out sout))))
1484 (defun case-frob-misc (stream op &optional arg1 arg2)
1485 (declare (type case-frob-stream stream))
1489 (let ((target (case-frob-stream-target stream)))
1490 (if (ansi-stream-p target)
1491 (funcall (ansi-stream-misc target) target op arg1 arg2)
1492 (stream-misc-dispatch target op arg1 arg2))))))
1494 (defun case-frob-upcase-out (stream char)
1495 (declare (type case-frob-stream stream)
1496 (type base-char char))
1497 (let ((target (case-frob-stream-target stream))
1498 (char (char-upcase char)))
1499 (if (ansi-stream-p target)
1500 (funcall (ansi-stream-out target) target char)
1501 (stream-write-char target char))))
1503 (defun case-frob-upcase-sout (stream str start end)
1504 (declare (type case-frob-stream stream)
1505 (type simple-base-string str)
1507 (type (or index null) end))
1508 (let* ((target (case-frob-stream-target stream))
1511 (string (if (and (zerop start) (= len end))
1513 (nstring-upcase (subseq str start end))))
1514 (string-len (- end start)))
1515 (if (ansi-stream-p target)
1516 (funcall (ansi-stream-sout target) target string 0 string-len)
1517 (stream-write-string target string 0 string-len))))
1519 (defun case-frob-downcase-out (stream char)
1520 (declare (type case-frob-stream stream)
1521 (type base-char char))
1522 (let ((target (case-frob-stream-target stream))
1523 (char (char-downcase char)))
1524 (if (ansi-stream-p target)
1525 (funcall (ansi-stream-out target) target char)
1526 (stream-write-char target char))))
1528 (defun case-frob-downcase-sout (stream str start end)
1529 (declare (type case-frob-stream stream)
1530 (type simple-base-string str)
1532 (type (or index null) end))
1533 (let* ((target (case-frob-stream-target stream))
1536 (string (if (and (zerop start) (= len end))
1537 (string-downcase str)
1538 (nstring-downcase (subseq str start end))))
1539 (string-len (- end start)))
1540 (if (ansi-stream-p target)
1541 (funcall (ansi-stream-sout target) target string 0 string-len)
1542 (stream-write-string target string 0 string-len))))
1544 (defun case-frob-capitalize-out (stream char)
1545 (declare (type case-frob-stream stream)
1546 (type base-char char))
1547 (let ((target (case-frob-stream-target stream)))
1548 (cond ((alphanumericp char)
1549 (let ((char (char-upcase char)))
1550 (if (ansi-stream-p target)
1551 (funcall (ansi-stream-out target) target char)
1552 (stream-write-char target char)))
1553 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1554 (setf (case-frob-stream-sout stream)
1555 #'case-frob-capitalize-aux-sout))
1557 (if (ansi-stream-p target)
1558 (funcall (ansi-stream-out target) target char)
1559 (stream-write-char target char))))))
1561 (defun case-frob-capitalize-sout (stream str start end)
1562 (declare (type case-frob-stream stream)
1563 (type simple-base-string str)
1565 (type (or index null) end))
1566 (let* ((target (case-frob-stream-target stream))
1567 (str (subseq str start end))
1571 (let ((char (schar str i)))
1572 (cond ((not (alphanumericp char))
1573 (setf inside-word nil))
1575 (setf (schar str i) (char-downcase char)))
1577 (setf inside-word t)
1578 (setf (schar str i) (char-upcase char))))))
1580 (setf (case-frob-stream-out stream)
1581 #'case-frob-capitalize-aux-out)
1582 (setf (case-frob-stream-sout stream)
1583 #'case-frob-capitalize-aux-sout))
1584 (if (ansi-stream-p target)
1585 (funcall (ansi-stream-sout target) target str 0 len)
1586 (stream-write-string target str 0 len))))
1588 (defun case-frob-capitalize-aux-out (stream char)
1589 (declare (type case-frob-stream stream)
1590 (type base-char char))
1591 (let ((target (case-frob-stream-target stream)))
1592 (cond ((alphanumericp char)
1593 (let ((char (char-downcase char)))
1594 (if (ansi-stream-p target)
1595 (funcall (ansi-stream-out target) target char)
1596 (stream-write-char target char))))
1598 (if (ansi-stream-p target)
1599 (funcall (ansi-stream-out target) target char)
1600 (stream-write-char target char))
1601 (setf (case-frob-stream-out stream)
1602 #'case-frob-capitalize-out)
1603 (setf (case-frob-stream-sout stream)
1604 #'case-frob-capitalize-sout)))))
1606 (defun case-frob-capitalize-aux-sout (stream str start end)
1607 (declare (type case-frob-stream stream)
1608 (type simple-base-string str)
1610 (type (or index null) end))
1611 (let* ((target (case-frob-stream-target stream))
1612 (str (subseq str start end))
1616 (let ((char (schar str i)))
1617 (cond ((not (alphanumericp char))
1618 (setf inside-word nil))
1620 (setf (schar str i) (char-downcase char)))
1622 (setf inside-word t)
1623 (setf (schar str i) (char-upcase char))))))
1625 (setf (case-frob-stream-out stream)
1626 #'case-frob-capitalize-out)
1627 (setf (case-frob-stream-sout stream)
1628 #'case-frob-capitalize-sout))
1629 (if (ansi-stream-p target)
1630 (funcall (ansi-stream-sout target) target str 0 len)
1631 (stream-write-string target str 0 len))))
1633 (defun case-frob-capitalize-first-out (stream char)
1634 (declare (type case-frob-stream stream)
1635 (type base-char char))
1636 (let ((target (case-frob-stream-target stream)))
1637 (cond ((alphanumericp char)
1638 (let ((char (char-upcase char)))
1639 (if (ansi-stream-p target)
1640 (funcall (ansi-stream-out target) target char)
1641 (stream-write-char target char)))
1642 (setf (case-frob-stream-out stream)
1643 #'case-frob-downcase-out)
1644 (setf (case-frob-stream-sout stream)
1645 #'case-frob-downcase-sout))
1647 (if (ansi-stream-p target)
1648 (funcall (ansi-stream-out target) target char)
1649 (stream-write-char target char))))))
1651 (defun case-frob-capitalize-first-sout (stream str start end)
1652 (declare (type case-frob-stream stream)
1653 (type simple-base-string str)
1655 (type (or index null) end))
1656 (let* ((target (case-frob-stream-target stream))
1657 (str (subseq str start end))
1660 (let ((char (schar str i)))
1661 (when (alphanumericp char)
1662 (setf (schar str i) (char-upcase char))
1663 (do ((i (1+ i) (1+ i)))
1665 (setf (schar str i) (char-downcase (schar str i))))
1666 (setf (case-frob-stream-out stream)
1667 #'case-frob-downcase-out)
1668 (setf (case-frob-stream-sout stream)
1669 #'case-frob-downcase-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 ;;;; stream commands
1677 (defstruct (stream-command (:constructor make-stream-command
1678 (name &optional args))
1680 (name nil :type symbol)
1681 (args nil :type list))
1682 (def!method print-object ((obj stream-command) str)
1683 (print-unreadable-object (obj str :type t :identity t)
1684 (prin1 (stream-command-name obj) str)))
1686 ;;; Take a stream and wait for text or a command to appear on it. If
1687 ;;; text appears before a command, return NIL, otherwise return a
1690 ;;; We can't simply call the stream's misc method because NIL is an
1691 ;;; ambiguous return value: does it mean text arrived, or does it mean
1692 ;;; the stream's misc method had no :GET-COMMAND implementation? We
1693 ;;; can't return NIL until there is text input. We don't need to loop
1694 ;;; because any stream implementing :GET-COMMAND would wait until it
1695 ;;; had some input. If the LISTEN fails, then we have some stream we
1697 (defun get-stream-command (stream)
1698 (let ((cmdp (funcall (ansi-stream-misc stream) stream :get-command)))
1703 ;; This waits for input and returns NIL when it arrives.
1704 (unread-char (read-char stream) stream)))))
1708 (defun read-sequence (seq stream &key (start 0) end)
1710 "Destructively modify SEQ by reading elements from STREAM.
1711 That part of SEQ bounded by START and END is destructively modified by
1712 copying successive elements into it from STREAM. If the end of file
1713 for STREAM is reached before copying all elements of the subsequence,
1714 then the extra elements near the end of sequence are not updated, and
1715 the index of the next element is returned."
1716 (declare (type sequence seq)
1717 (type stream stream)
1719 (type sequence-end end)
1721 (if (ansi-stream-p stream)
1722 (ansi-stream-read-sequence seq stream start end)
1723 ;; must be Gray streams FUNDAMENTAL-STREAM
1724 (stream-read-sequence stream seq start end)))
1726 (defun ansi-stream-read-sequence (seq stream start %end)
1727 (declare (type sequence seq)
1728 (type ansi-stream stream)
1730 (type sequence-end %end)
1732 (let ((end (or %end (length seq))))
1733 (declare (type index end))
1736 (let ((read-function
1737 (if (subtypep (stream-element-type stream) 'character)
1740 (do ((rem (nthcdr start seq) (rest rem))
1742 ((or (endp rem) (>= i end)) i)
1743 (declare (type list rem)
1745 (let ((el (funcall read-function stream nil :eof)))
1748 (setf (first rem) el)))))
1750 (with-array-data ((data seq) (offset-start start) (offset-end end))
1752 ((or (simple-array (unsigned-byte 8) (*))
1753 (simple-array (signed-byte 8) (*))
1755 (let* ((numbytes (- end start))
1756 (bytes-read (sb!sys:read-n-bytes stream
1761 (if (< bytes-read numbytes)
1762 (+ start bytes-read)
1765 (let ((read-function
1766 (if (subtypep (stream-element-type stream) 'character)
1769 (do ((i offset-start (1+ i)))
1770 ((>= i offset-end) end)
1771 (declare (type index i))
1772 (let ((el (funcall read-function stream nil :eof)))
1774 (return (+ start (- i offset-start))))
1775 (setf (aref data i) el)))))))))))
1779 (defun write-sequence (seq stream &key (start 0) (end nil))
1781 "Write the elements of SEQ bounded by START and END to STREAM."
1782 (declare (type sequence seq)
1783 (type stream stream)
1785 (type sequence-end end)
1787 (if (ansi-stream-p stream)
1788 (ansi-stream-write-sequence seq stream start end)
1789 ;; must be Gray-streams FUNDAMENTAL-STREAM
1790 (stream-write-sequence stream seq start end)))
1792 (defun ansi-stream-write-sequence (seq stream start %end)
1793 (declare (type sequence seq)
1794 (type ansi-stream stream)
1796 (type sequence-end %end)
1798 (let ((end (or %end (length seq))))
1799 (declare (type index end))
1802 (let ((write-function
1803 (if (subtypep (stream-element-type stream) 'character)
1806 (do ((rem (nthcdr start seq) (rest rem))
1808 ((or (endp rem) (>= i end)) seq)
1809 (declare (type list rem)
1811 (funcall write-function (first rem) stream))))
1813 (%write-string seq stream start end))
1815 (let ((write-function
1816 (if (subtypep (stream-element-type stream) 'character)
1819 (do ((i start (1+ i)))
1821 (declare (type index i))
1822 (funcall write-function (aref seq i) stream)))))))
1826 ;;; (These were inline throughout this file, but that's not appropriate
1828 (declaim (maybe-inline read-char unread-char read-byte listen))