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")
14 (deftype string-stream ()
15 '(or string-input-stream string-output-stream
16 fill-pointer-output-stream))
20 ;;; The initialization of these streams is performed by
21 ;;; STREAM-COLD-INIT-OR-RESET.
22 (defvar *terminal-io* () #!+sb-doc "Terminal I/O stream.")
23 (defvar *standard-input* () #!+sb-doc "Default input stream.")
24 (defvar *standard-output* () #!+sb-doc "Default output stream.")
25 (defvar *error-output* () #!+sb-doc "Error output stream.")
26 (defvar *query-io* () #!+sb-doc "Query I/O stream.")
27 (defvar *trace-output* () #!+sb-doc "Trace output stream.")
28 (defvar *debug-io* () #!+sb-doc "Interactive debugging stream.")
30 (defun ill-in (stream &rest ignore)
31 (declare (ignore ignore))
32 (error 'simple-type-error
34 :expected-type '(satisfies input-stream-p)
35 :format-control "~S is not a character input stream."
36 :format-arguments (list stream)))
37 (defun ill-out (stream &rest ignore)
38 (declare (ignore ignore))
39 (error 'simple-type-error
41 :expected-type '(satisfies output-stream-p)
42 :format-control "~S is not a character output stream."
43 :format-arguments (list stream)))
44 (defun ill-bin (stream &rest ignore)
45 (declare (ignore ignore))
46 (error 'simple-type-error
48 :expected-type '(satisfies input-stream-p)
49 :format-control "~S is not a binary input stream."
50 :format-arguments (list stream)))
51 (defun ill-bout (stream &rest ignore)
52 (declare (ignore ignore))
53 (error 'simple-type-error
55 :expected-type '(satisfies output-stream-p)
56 :format-control "~S is not a binary output stream."
57 :format-arguments (list stream)))
58 (defun closed-flame (stream &rest ignore)
59 (declare (ignore ignore))
60 (error "~S is closed." stream))
61 (defun do-nothing (&rest ignore)
62 (declare (ignore ignore)))
64 ;;; HOW THE STREAM STRUCTURE IS USED:
66 ;;; Many of the slots of the stream structure contain functions
67 ;;; which are called to perform some operation on the stream. Closed
68 ;;; streams have #'CLOSED-FLAME in all of their function slots. If
69 ;;; one side of an I/O or echo stream is closed, the whole stream is
70 ;;; considered closed. The functions in the operation slots take
71 ;;; arguments as follows:
73 ;;; In: Stream, Eof-Errorp, Eof-Value
74 ;;; Bin: Stream, Eof-Errorp, Eof-Value
75 ;;; N-Bin: Stream, Buffer, Start, Numbytes, Eof-Errorp
76 ;;; Out: Stream, Character
77 ;;; Bout: Stream, Integer
78 ;;; Sout: Stream, String, Start, End
79 ;;; Misc: Stream, Operation, &Optional Arg1, Arg2
81 ;;; In order to save space, some of the less common stream operations
82 ;;; are handled by just one function, the MISC method. This function
83 ;;; is passed a keyword which indicates the operation to perform.
84 ;;; The following keywords are used:
85 ;;; :listen - Return the following values:
86 ;;; t if any input waiting.
88 ;;; nil if no input is available and not at eof.
89 ;;; :unread - Unread the character Arg.
90 ;;; :close - Do any stream specific stuff to close the stream.
91 ;;; The methods are set to closed-flame by the close
92 ;;; function, so that need not be done by this
94 ;;; :clear-input - Clear any unread input
96 ;;; :force-output - Cause output to happen
97 ;;; :clear-output - Clear any undone output
98 ;;; :element-type - Return the type of element the stream deals with.
99 ;;; :line-length - Return the length of a line of output.
100 ;;; :charpos - Return current output position on the line.
101 ;;; :file-length - Return the file length of a file stream.
102 ;;; :file-position - Return or change the current position of a
104 ;;; :file-name - Return the name of an associated file.
105 ;;; :interactive-p - Is this an interactive device?
107 ;;; In order to do almost anything useful, it is necessary to
108 ;;; define a new type of structure that includes stream, so that the
109 ;;; stream can have some state information.
111 ;;; THE STREAM IN-BUFFER:
113 ;;; The IN-BUFFER in the stream holds characters or bytes that
114 ;;; are ready to be read by some input function. If there is any
115 ;;; stuff in the IN-BUFFER, then the reading function can use it
116 ;;; without calling any stream method. Any stream may put stuff in
117 ;;; the IN-BUFFER, and may also assume that any input in the IN-BUFFER
118 ;;; has been consumed before any in-method is called. If a text
119 ;;; stream has in IN-BUFFER, then the first character should not be
120 ;;; used to buffer normal input so that it is free for unreading into.
122 ;;; The IN-BUFFER slot is a vector +IN-BUFFER-LENGTH+ long. The
123 ;;; IN-INDEX is the index in the IN-BUFFER of the first available
124 ;;; object. The available objects are thus between IN-INDEX and the
125 ;;; length of the IN-BUFFER.
127 ;;; When this buffer is only accessed by the normal stream
128 ;;; functions, the number of function calls is halved, thus
129 ;;; potentially doubling the speed of simple operations. If the
130 ;;; FAST-READ-CHAR and FAST-READ-BYTE macros are used, nearly all
131 ;;; function call overhead is removed, vastly speeding up these
132 ;;; important operations.
134 ;;; If a stream does not have an IN-BUFFER, then the IN-BUFFER slot
135 ;;; must be nil, and the IN-INDEX must be +IN-BUFFER-LENGTH+. These are
136 ;;; the default values for the slots.
138 ;;; stream manipulation functions
140 (defun input-stream-p (stream)
141 (declare (type stream stream))
144 (when (synonym-stream-p stream)
146 (symbol-value (synonym-stream-symbol stream))))
148 (and (lisp-stream-p stream)
149 (not (eq (lisp-stream-in stream) #'closed-flame))
150 ;;; KLUDGE: It's probably not good to have EQ tests on function
151 ;;; values like this. What if someone's redefined the function?
152 ;;; Is there a better way? (Perhaps just VALID-FOR-INPUT and
153 ;;; VALID-FOR-OUTPUT flags? -- WHN 19990902
154 (or (not (eq (lisp-stream-in stream) #'ill-in))
155 (not (eq (lisp-stream-bin stream) #'ill-bin)))))
157 (defun output-stream-p (stream)
158 (declare (type stream stream))
161 (when (synonym-stream-p stream)
162 (setf stream (symbol-value
163 (synonym-stream-symbol stream))))
165 (and (lisp-stream-p stream)
166 (not (eq (lisp-stream-in stream) #'closed-flame))
167 (or (not (eq (lisp-stream-out stream) #'ill-out))
168 (not (eq (lisp-stream-bout stream) #'ill-bout)))))
170 (defun open-stream-p (stream)
171 (declare (type stream stream))
172 (not (eq (lisp-stream-in stream) #'closed-flame)))
174 (defun stream-element-type (stream)
175 (declare (type stream stream))
176 (funcall (lisp-stream-misc stream) stream :element-type))
178 (defun interactive-stream-p (stream)
179 (declare (type stream stream))
180 (funcall (lisp-stream-misc stream) stream :interactive-p))
182 (defun open-stream-p (stream)
183 (declare (type stream stream))
184 (not (eq (lisp-stream-in stream) #'closed-flame)))
186 (defun close (stream &key abort)
187 (declare (type stream stream))
188 (when (open-stream-p stream)
189 (funcall (lisp-stream-misc stream) stream :close abort))
192 (defun set-closed-flame (stream)
193 (setf (lisp-stream-in stream) #'closed-flame)
194 (setf (lisp-stream-bin stream) #'closed-flame)
195 (setf (lisp-stream-n-bin stream) #'closed-flame)
196 (setf (lisp-stream-in stream) #'closed-flame)
197 (setf (lisp-stream-out stream) #'closed-flame)
198 (setf (lisp-stream-bout stream) #'closed-flame)
199 (setf (lisp-stream-sout stream) #'closed-flame)
200 (setf (lisp-stream-misc stream) #'closed-flame))
202 ;;;; file position and file length
204 ;;; Call the misc method with the :file-position operation.
205 (defun file-position (stream &optional position)
206 (declare (type stream stream))
207 (declare (type (or index (member nil :start :end)) position))
210 (setf (lisp-stream-in-index stream) +in-buffer-length+)
211 (funcall (lisp-stream-misc stream) stream :file-position position))
213 (let ((res (funcall (lisp-stream-misc stream) stream :file-position nil)))
215 (- res (- +in-buffer-length+ (lisp-stream-in-index stream))))))))
217 ;;; declaration test functions
220 (defun stream-associated-with-file (stream)
222 "Tests if the stream is associated with a file"
223 (or (typep stream 'file-stream)
224 (and (synonym-stream-p stream)
225 (typep (symbol-value (synonym-stream-symbol stream))
228 ;;; Like File-Position, only use :file-length.
229 (defun file-length (stream)
230 (declare (type (or file-stream synonym-stream) stream))
233 (check-type-var stream '(satisfies stream-associated-with-file)
234 "a stream associated with a file")
236 (funcall (lisp-stream-misc stream) stream :file-length))
240 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
242 (declare (ignore recursive-p))
243 (let ((stream (in-synonym-of stream)))
244 (if (lisp-stream-p stream)
245 (prepare-for-fast-read-char stream
246 (let ((res (make-string 80))
250 (let ((ch (fast-read-char nil nil)))
252 (when (char= ch #\newline)
253 (done-with-fast-read-char)
254 (return (values (shrink-vector res index) nil)))
257 (let ((new (make-string len)))
260 (setf (schar res index) ch)
263 (done-with-fast-read-char)
264 (return (values (eof-or-lose stream
268 ;; Since FAST-READ-CHAR already hit the eof char, we
269 ;; shouldn't do another READ-CHAR.
271 (done-with-fast-read-char)
272 (return (values (shrink-vector res index) t))))))))
273 ;; must be Gray streams FUNDAMENTAL-STREAM
274 (multiple-value-bind (string eof) (stream-read-line stream)
275 (if (and eof (zerop (length string)))
276 (values (eof-or-lose stream eof-error-p eof-value) t)
277 (values string eof))))))
279 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
280 ;;; so, except in this file, they are not inline by default, but they can be.
281 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
283 (defun read-char (&optional (stream *standard-input*)
287 (declare (ignore recursive-p))
288 (let ((stream (in-synonym-of stream)))
289 (if (lisp-stream-p stream)
290 (prepare-for-fast-read-char stream
292 (fast-read-char eof-error-p eof-value)
293 (done-with-fast-read-char)))
294 ;; must be Gray streams FUNDAMENTAL-STREAM
295 (let ((char (stream-read-char stream)))
297 (eof-or-lose stream eof-error-p eof-value)
300 (defun unread-char (character &optional (stream *standard-input*))
301 (let ((stream (in-synonym-of stream)))
302 (if (lisp-stream-p stream)
303 (let ((index (1- (lisp-stream-in-index stream)))
304 (buffer (lisp-stream-in-buffer stream)))
305 (declare (fixnum index))
306 (when (minusp index) (error "Nothing to unread."))
308 (setf (aref buffer index) (char-code character))
309 (setf (lisp-stream-in-index stream) index))
311 (funcall (lisp-stream-misc stream) stream
312 :unread character))))
313 ;; must be Gray streams FUNDAMENTAL-STREAM
314 (stream-unread-char stream character)))
317 (defun peek-char (&optional (peek-type nil)
318 (stream *standard-input*)
320 eof-value recursive-p)
322 (let ((stream (in-synonym-of stream)))
323 (if (lisp-stream-p stream)
324 (let ((char (read-char stream eof-error-p eof-value)))
325 (cond ((eq char eof-value) char)
326 ((characterp peek-type)
327 (do ((char char (read-char stream eof-error-p eof-value)))
328 ((or (eq char eof-value) (char= char peek-type))
329 (unless (eq char eof-value)
330 (unread-char char stream))
333 (do ((char char (read-char stream eof-error-p eof-value)))
334 ((or (eq char eof-value) (not (whitespace-char-p char)))
335 (unless (eq char eof-value)
336 (unread-char char stream))
339 (unread-char char stream)
341 ;; must be Gray streams FUNDAMENTAL-STREAM
342 (cond ((characterp peek-type)
343 (do ((char (stream-read-char stream) (stream-read-char stream)))
344 ((or (eq char :eof) (char= char peek-type))
345 (cond ((eq char :eof)
346 (eof-or-lose stream eof-error-p eof-value))
348 (stream-unread-char stream char)
351 (do ((char (stream-read-char stream) (stream-read-char stream)))
352 ((or (eq char :eof) (not (whitespace-char-p char)))
353 (cond ((eq char :eof)
354 (eof-or-lose stream eof-error-p eof-value))
356 (stream-unread-char stream char)
359 (let ((char (stream-peek-char stream)))
361 (eof-or-lose stream eof-error-p eof-value)
364 (defun listen (&optional (stream *standard-input*))
365 (let ((stream (in-synonym-of stream)))
366 (if (lisp-stream-p stream)
367 (or (/= (the fixnum (lisp-stream-in-index stream)) +in-buffer-length+)
368 ;; Test for T explicitly since misc methods return :EOF sometimes.
369 (eq (funcall (lisp-stream-misc stream) stream :listen) t))
370 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
371 (stream-listen stream))))
373 (defun read-char-no-hang (&optional (stream *standard-input*)
377 (declare (ignore recursive-p))
378 (let ((stream (in-synonym-of stream)))
379 (if (lisp-stream-p stream)
380 (if (funcall (lisp-stream-misc stream) stream :listen)
381 ;; On T or :EOF get READ-CHAR to do the work.
382 (read-char stream eof-error-p eof-value)
384 ;; must be Gray streams FUNDAMENTAL-STREAM
385 (let ((char (stream-read-char-no-hang stream)))
387 (eof-or-lose stream eof-error-p eof-value)
390 (defun clear-input (&optional (stream *standard-input*))
391 (let ((stream (in-synonym-of stream)))
392 (cond ((lisp-stream-p stream)
393 (setf (lisp-stream-in-index stream) +in-buffer-length+)
394 (funcall (lisp-stream-misc stream) stream :clear-input))
396 (stream-clear-input stream))))
399 (declaim (maybe-inline read-byte))
400 (defun read-byte (stream &optional (eof-error-p t) eof-value)
401 (let ((stream (in-synonym-of stream)))
402 (if (lisp-stream-p stream)
403 (prepare-for-fast-read-byte stream
405 (fast-read-byte eof-error-p eof-value t)
406 (done-with-fast-read-byte)))
407 ;; must be Gray streams FUNDAMENTAL-STREAM
408 (let ((char (stream-read-byte stream)))
410 (eof-or-lose stream eof-error-p eof-value)
413 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
414 ;;; number of bytes read.
416 ;;; Note: CMU CL's version of this had a special interpretation of EOF-ERROR-P
417 ;;; which SBCL does not have. (In the EOF-ERROR-P=NIL case, CMU CL's version
418 ;;; would return as soon as any data became available.) This could be useful
419 ;;; behavior for things like pipes in some cases, but it wasn't being used in
420 ;;; SBCL, so it was dropped. If we ever need it, it could be added later as a
421 ;;; new variant N-BIN method (perhaps N-BIN-ASAP?) or something.
422 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
423 (declare (type lisp-stream stream)
424 (type index numbytes start)
425 (type (or (simple-array * (*)) system-area-pointer) buffer))
426 (let* ((stream (in-synonym-of stream lisp-stream))
427 (in-buffer (lisp-stream-in-buffer stream))
428 (index (lisp-stream-in-index stream))
429 (num-buffered (- +in-buffer-length+ index)))
430 (declare (fixnum index num-buffered))
433 (funcall (lisp-stream-n-bin stream)
439 ((<= numbytes num-buffered)
440 (%primitive sb!c:byte-blt
446 (setf (lisp-stream-in-index stream) (+ index numbytes))
449 (let ((end (+ start num-buffered)))
450 (%primitive sb!c:byte-blt in-buffer index buffer start end)
451 (setf (lisp-stream-in-index stream) +in-buffer-length+)
452 (+ (funcall (lisp-stream-n-bin stream)
456 (- numbytes num-buffered)
460 ;;; the amount of space we leave at the start of the in-buffer for
463 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
464 (defconstant +in-buffer-extra+ 4) ; FIXME: should be symbolic constant
466 ;;; This function is called by the FAST-READ-CHAR expansion to refill
467 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
468 ;;; and hence must be an N-BIN method.
469 (defun fast-read-char-refill (stream eof-error-p eof-value)
470 (let* ((ibuf (lisp-stream-in-buffer stream))
471 (count (funcall (lisp-stream-n-bin stream)
475 (- +in-buffer-length+ +in-buffer-extra+)
477 (start (- +in-buffer-length+ count)))
478 (declare (type index start count))
480 (setf (lisp-stream-in-index stream) +in-buffer-length+)
481 (funcall (lisp-stream-in stream) stream eof-error-p eof-value))
483 (when (/= start +in-buffer-extra+)
484 (bit-bash-copy ibuf (+ (* +in-buffer-extra+ sb!vm:byte-bits)
485 (* sb!vm:vector-data-offset
487 ibuf (+ (the index (* start sb!vm:byte-bits))
488 (* sb!vm:vector-data-offset
490 (* count sb!vm:byte-bits)))
491 (setf (lisp-stream-in-index stream) (1+ start))
492 (code-char (aref ibuf start))))))
494 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
495 ;;; leave room for unreading.
496 (defun fast-read-byte-refill (stream eof-error-p eof-value)
497 (let* ((ibuf (lisp-stream-in-buffer stream))
498 (count (funcall (lisp-stream-n-bin stream) stream
499 ibuf 0 +in-buffer-length+
501 (start (- +in-buffer-length+ count)))
502 (declare (type index start count))
504 (setf (lisp-stream-in-index stream) +in-buffer-length+)
505 (funcall (lisp-stream-bin stream) stream eof-error-p eof-value))
507 (unless (zerop start)
508 (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:word-bits)
509 ibuf (+ (the index (* start sb!vm:byte-bits))
510 (* sb!vm:vector-data-offset
512 (* count sb!vm:byte-bits)))
513 (setf (lisp-stream-in-index stream) (1+ start))
514 (aref ibuf start)))))
518 (defun write-char (character &optional (stream *standard-output*))
519 (with-out-stream stream (lisp-stream-out character)
520 (stream-write-char character))
523 (defun terpri (&optional (stream *standard-output*))
524 (with-out-stream stream (lisp-stream-out #\newline) (stream-terpri))
527 (defun fresh-line (&optional (stream *standard-output*))
528 (let ((stream (out-synonym-of stream)))
529 (if (lisp-stream-p stream)
530 (when (/= (or (charpos stream) 1) 0)
531 (funcall (lisp-stream-out stream) stream #\newline)
533 ;; must be Gray streams FUNDAMENTAL-STREAM
534 (stream-fresh-line stream))))
536 (defun write-string (string &optional (stream *standard-output*)
537 &key (start 0) (end (length (the vector string))))
539 ;; FIXME: These SETFs don't look right to me. Looking at the
540 ;; definition of "bounding indices" in the glossary of the ANSI
541 ;; spec, and extrapolating from the behavior of other operations
542 ;; when their operands are the wrong type, it seems that it would be
543 ;; more correct to essentially
544 ;; (ASSERT (<= 0 START END (LENGTH STRING)))
545 ;; instead of modifying the incorrect values.
547 (setf end (min end (length (the vector string))))
549 (setf start (max start 0))
551 ;; FIXME: And I'd just signal a non-continuable error..
554 (cerror "Continue with switched start and end ~S <-> ~S"
555 "Write-string: start (~S) and end (~S) exchanged."
559 (write-string* string stream start end))
561 (defun write-string* (string &optional (stream *standard-output*)
562 (start 0) (end (length (the vector string))))
563 (declare (fixnum start end))
564 (let ((stream (out-synonym-of stream)))
565 (cond ((lisp-stream-p stream)
566 (if (array-header-p string)
567 (with-array-data ((data string) (offset-start start)
569 (funcall (lisp-stream-sout stream)
570 stream data offset-start offset-end))
571 (funcall (lisp-stream-sout stream) stream string start end))
573 (t ; must be Gray streams FUNDAMENTAL-STREAM
574 (stream-write-string stream string start end)))))
576 (defun write-line (string &optional (stream *standard-output*)
577 &key (start 0) (end (length string)))
578 (write-line* string stream start end))
580 (defun write-line* (string &optional (stream *standard-output*)
581 (start 0) (end (length string)))
582 (declare (fixnum start end))
583 (let ((stream (out-synonym-of stream)))
584 (cond ((lisp-stream-p stream)
585 (if (array-header-p string)
586 (with-array-data ((data string) (offset-start start)
588 (with-out-stream stream (lisp-stream-sout data offset-start
590 (with-out-stream stream (lisp-stream-sout string start end)))
591 (funcall (lisp-stream-out stream) stream #\newline))
592 (t ; must be Gray streams FUNDAMENTAL-STREAM
593 (stream-write-string stream string start end)
594 (stream-write-char stream #\Newline)))
597 (defun charpos (&optional (stream *standard-output*))
598 (with-out-stream stream (lisp-stream-misc :charpos) (stream-line-column)))
600 (defun line-length (&optional (stream *standard-output*))
601 (with-out-stream stream (lisp-stream-misc :line-length)
602 (stream-line-length)))
604 (defun finish-output (&optional (stream *standard-output*))
605 (with-out-stream stream (lisp-stream-misc :finish-output)
606 (stream-finish-output))
609 (defun force-output (&optional (stream *standard-output*))
610 (with-out-stream stream (lisp-stream-misc :force-output)
611 (stream-force-output))
614 (defun clear-output (&optional (stream *standard-output*))
615 (with-out-stream stream (lisp-stream-misc :clear-output)
616 (stream-force-output))
619 (defun write-byte (integer stream)
620 (with-out-stream stream (lisp-stream-bout integer)
621 (stream-write-byte integer))
624 ;;; This is called from LISP-STREAM routines that encapsulate CLOS
625 ;;; streams to handle the misc routines and dispatch to the
626 ;;; appropriate Gray stream functions.
627 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
628 (declare (type fundamental-stream stream)
632 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
633 (let ((char (stream-read-char-no-hang stream)))
634 (when (characterp char)
635 (stream-unread-char stream char))
638 (stream-unread-char stream arg1))
642 (stream-clear-input stream))
644 (stream-force-output stream))
646 (stream-finish-output stream))
648 (stream-element-type stream))
650 (interactive-stream-p stream))
652 (stream-line-length stream))
654 (stream-line-column stream))
656 (file-length stream))
658 (file-position stream arg1))))
660 ;;;; broadcast streams
662 (defstruct (broadcast-stream (:include lisp-stream
663 (out #'broadcast-out)
664 (bout #'broadcast-bout)
665 (sout #'broadcast-sout)
666 (misc #'broadcast-misc))
667 (:constructor #!-high-security-support
668 make-broadcast-stream
669 #!+high-security-support
670 %make-broadcast-stream (&rest
673 ;; a list of all the streams we broadcast to
674 (streams () :type list :read-only t))
676 #!+high-security-support
677 (defun make-broadcast-stream (&rest streams)
678 (dolist (stream streams)
679 (unless (or (and (synonym-stream-p stream)
680 (output-stream-p (symbol-value
681 (synonym-stream-symbol stream))))
682 (output-stream-p stream))
685 :expected-type '(satisfies output-stream-p))))
686 (apply #'%make-broadcast-stream streams))
688 (macrolet ((out-fun (fun method stream-method &rest args)
689 `(defun ,fun (stream ,@args)
690 (dolist (stream (broadcast-stream-streams stream))
691 (if (lisp-stream-p stream)
692 (funcall (,method stream) stream ,@args)
693 (,stream-method stream ,@args))))))
694 (out-fun broadcast-out lisp-stream-out stream-write-char char)
695 (out-fun broadcast-bout lisp-stream-bout stream-write-byte byte)
696 (out-fun broadcast-sout lisp-stream-sout stream-write-string
699 (defun broadcast-misc (stream operation &optional arg1 arg2)
700 (let ((streams (broadcast-stream-streams stream)))
703 (dolist (stream streams)
704 (let ((charpos (charpos stream)))
705 (if charpos (return charpos)))))
708 (dolist (stream streams min)
709 (let ((res (line-length stream)))
710 (when res (setq min (if min (min res min) res)))))))
713 (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
714 (pushnew (stream-element-type stream) res :test #'equal))))
718 (dolist (stream streams res)
720 (if (lisp-stream-p stream)
721 (funcall (lisp-stream-misc stream) stream operation
723 (stream-misc-dispatch stream operation arg1 arg2)))))))))
727 (defstruct (synonym-stream (:include lisp-stream
730 (n-bin #'synonym-n-bin)
732 (bout #'synonym-bout)
733 (sout #'synonym-sout)
734 (misc #'synonym-misc))
735 (:constructor make-synonym-stream (symbol))
737 ;; This is the symbol, the value of which is the stream we are synonym to.
738 (symbol nil :type symbol :read-only t))
739 (def!method print-object ((x synonym-stream) stream)
740 (print-unreadable-object (x stream :type t :identity t)
741 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
743 ;;; The output simple output methods just call the corresponding method
744 ;;; in the synonymed stream.
745 (macrolet ((out-fun (name slot stream-method &rest args)
746 `(defun ,name (stream ,@args)
747 (declare (optimize (safety 1)))
748 (let ((syn (symbol-value (synonym-stream-symbol stream))))
749 (if (lisp-stream-p syn)
750 (funcall (,slot syn) syn ,@args)
751 (,stream-method syn ,@args))))))
752 (out-fun synonym-out lisp-stream-out stream-write-char ch)
753 (out-fun synonym-bout lisp-stream-bout stream-write-byte n)
754 (out-fun synonym-sout lisp-stream-sout stream-write-string string start end))
756 ;;; For the input methods, we just call the corresponding function on the
757 ;;; synonymed stream. These functions deal with getting input out of
758 ;;; the In-Buffer if there is any.
759 (macrolet ((in-fun (name fun &rest args)
760 `(defun ,name (stream ,@args)
761 (declare (optimize (safety 1)))
762 (,fun (symbol-value (synonym-stream-symbol stream))
764 (in-fun synonym-in read-char eof-error-p eof-value)
765 (in-fun synonym-bin read-byte eof-error-p eof-value)
766 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
768 ;;; We have to special-case the operations which could look at stuff in
770 (defun synonym-misc (stream operation &optional arg1 arg2)
771 (declare (optimize (safety 1)))
772 (let ((syn (symbol-value (synonym-stream-symbol stream))))
773 (if (lisp-stream-p syn)
775 (:listen (or (/= (the fixnum (lisp-stream-in-index syn))
777 (funcall (lisp-stream-misc syn) syn :listen)))
779 (funcall (lisp-stream-misc syn) syn operation arg1 arg2)))
780 (stream-misc-dispatch syn operation arg1 arg2))))
784 (defstruct (two-way-stream
785 (:include lisp-stream
788 (n-bin #'two-way-n-bin)
790 (bout #'two-way-bout)
791 (sout #'two-way-sout)
792 (misc #'two-way-misc))
793 (:constructor #!-high-security-support
795 #!+high-security-support
796 %make-two-way-stream (input-stream output-stream))
798 (input-stream (required-argument) :type stream :read-only t)
799 (output-stream (required-argument) :type stream :read-only t))
800 (def!method print-object ((x two-way-stream) stream)
801 (print-unreadable-object (x stream :type t :identity t)
803 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
804 (two-way-stream-input-stream x)
805 (two-way-stream-output-stream x))))
807 #!-high-security-support
808 (setf (fdocumentation 'make-two-way-stream 'function)
809 "Returns a bidirectional stream which gets its input from Input-Stream and
810 sends its output to Output-Stream.")
811 #!+high-security-support
812 (defun make-two-way-stream (input-stream output-stream)
814 "Returns a bidirectional stream which gets its input from Input-Stream and
815 sends its output to Output-Stream."
816 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
817 ;; should be encapsulated in a function, and used here and most of
818 ;; the other places that SYNONYM-STREAM-P appears.
819 (unless (or (and (synonym-stream-p output-stream)
820 (output-stream-p (symbol-value
821 (synonym-stream-symbol output-stream))))
822 (output-stream-p output-stream))
825 :expected-type '(satisfies output-stream-p)))
826 (unless (or (and (synonym-stream-p input-stream)
827 (input-stream-p (symbol-value
828 (synonym-stream-symbol input-stream))))
829 (input-stream-p input-stream))
832 :expected-type '(satisfies input-stream-p)))
833 (funcall #'%make-two-way-stream input-stream output-stream))
835 (macrolet ((out-fun (name slot stream-method &rest args)
836 `(defun ,name (stream ,@args)
837 (let ((syn (two-way-stream-output-stream stream)))
838 (if (lisp-stream-p syn)
839 (funcall (,slot syn) syn ,@args)
840 (,stream-method syn ,@args))))))
841 (out-fun two-way-out lisp-stream-out stream-write-char ch)
842 (out-fun two-way-bout lisp-stream-bout stream-write-byte n)
843 (out-fun two-way-sout lisp-stream-sout stream-write-string string start end))
845 (macrolet ((in-fun (name fun &rest args)
846 `(defun ,name (stream ,@args)
847 (force-output (two-way-stream-output-stream stream))
848 (,fun (two-way-stream-input-stream stream) ,@args))))
849 (in-fun two-way-in read-char eof-error-p eof-value)
850 (in-fun two-way-bin read-byte eof-error-p eof-value)
851 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
853 (defun two-way-misc (stream operation &optional arg1 arg2)
854 (let* ((in (two-way-stream-input-stream stream))
855 (out (two-way-stream-output-stream stream))
856 (in-lisp-stream-p (lisp-stream-p in))
857 (out-lisp-stream-p (lisp-stream-p out)))
861 (or (/= (the fixnum (lisp-stream-in-index in)) +in-buffer-length+)
862 (funcall (lisp-stream-misc in) in :listen))
864 ((:finish-output :force-output :clear-output)
865 (if out-lisp-stream-p
866 (funcall (lisp-stream-misc out) out operation arg1 arg2)
867 (stream-misc-dispatch out operation arg1 arg2)))
868 ((:clear-input :unread)
870 (funcall (lisp-stream-misc in) in operation arg1 arg2)
871 (stream-misc-dispatch in operation arg1 arg2)))
873 (let ((in-type (stream-element-type in))
874 (out-type (stream-element-type out)))
875 (if (equal in-type out-type)
876 in-type `(and ,in-type ,out-type))))
878 (set-closed-flame stream))
880 (or (if in-lisp-stream-p
881 (funcall (lisp-stream-misc in) in operation arg1 arg2)
882 (stream-misc-dispatch in operation arg1 arg2))
883 (if out-lisp-stream-p
884 (funcall (lisp-stream-misc out) out operation arg1 arg2)
885 (stream-misc-dispatch out operation arg1 arg2)))))))
887 ;;;; concatenated streams
889 (defstruct (concatenated-stream
890 (:include lisp-stream
891 (in #'concatenated-in)
892 (bin #'concatenated-bin)
893 (misc #'concatenated-misc))
895 #!-high-security-support make-concatenated-stream
896 #!+high-security-support %make-concatenated-stream
897 (&rest streams &aux (current streams)))
899 ;; The car of this is the stream we are reading from now.
901 ;; This is a list of all the streams. We need to remember them so that
902 ;; we can close them.
904 ;; FIXME: ANSI says this is supposed to be the list of streams that
905 ;; we still have to read from. So either this needs to become a
906 ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
907 ;; around it which discards closed files from the head of the list)
908 ;; or we need to update it as we run out of files.
909 (streams nil :type list :read-only t))
910 (def!method print-object ((x concatenated-stream) stream)
911 (print-unreadable-object (x stream :type t :identity t)
914 (concatenated-stream-streams x))))
916 #!-high-security-support
917 (setf (fdocumentation 'make-concatenated-stream 'function)
918 "Returns a stream which takes its input from each of the Streams in turn,
919 going on to the next at EOF.")
921 #!+high-security-support
922 (defun make-concatenated-stream (&rest streams)
924 "Returns a stream which takes its input from each of the Streams in turn,
925 going on to the next at EOF."
926 (dolist (stream streams)
927 (unless (or (and (synonym-stream-p stream)
928 (input-stream-p (symbol-value
929 (synonym-stream-symbol stream))))
930 (input-stream-p stream))
933 :expected-type '(satisfies input-stream-p))))
934 (apply #'%make-concatenated-stream streams))
936 (macrolet ((in-fun (name fun)
937 `(defun ,name (stream eof-error-p eof-value)
938 (do ((current (concatenated-stream-current stream) (cdr current)))
940 (eof-or-lose stream eof-error-p eof-value))
941 (let* ((stream (car current))
942 (result (,fun stream nil nil)))
943 (when result (return result)))
944 (setf (concatenated-stream-current stream) current)))))
945 (in-fun concatenated-in read-char)
946 (in-fun concatenated-bin read-byte))
948 (defun concatenated-misc (stream operation &optional arg1 arg2)
949 (let ((left (concatenated-stream-current stream)))
951 (let* ((current (car left)))
955 (let ((stuff (if (lisp-stream-p current)
956 (funcall (lisp-stream-misc current) current
958 (stream-misc-dispatch current :listen))))
959 (cond ((eq stuff :eof)
960 ;; Advance CURRENT, and try again.
961 (pop (concatenated-stream-current stream))
963 (car (concatenated-stream-current stream)))
965 ;; No further streams. EOF.
968 ;; Stuff's available.
971 ;; Nothing is available yet.
974 (set-closed-flame stream))
976 (if (lisp-stream-p current)
977 (funcall (lisp-stream-misc current) current operation arg1 arg2)
978 (stream-misc-dispatch current operation arg1 arg2))))))))
982 (defstruct (echo-stream
983 (:include two-way-stream
988 (:constructor make-echo-stream (input-stream output-stream))
991 (def!method print-object ((x echo-stream) stream)
992 (print-unreadable-object (x stream :type t :identity t)
994 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
995 (two-way-stream-input-stream x)
996 (two-way-stream-output-stream x))))
998 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
999 `(defun ,name (stream ,@args)
1000 (or (pop (echo-stream-unread-stuff stream))
1001 (let* ((in (echo-stream-input-stream stream))
1002 (out (echo-stream-output-stream stream))
1003 (result (,fun in ,@args)))
1004 (if (lisp-stream-p out)
1005 (funcall (,out-slot out) out result)
1006 (,stream-method out result))
1008 (in-fun echo-in read-char lisp-stream-out stream-write-char
1009 eof-error-p eof-value)
1010 (in-fun echo-bin read-byte lisp-stream-bout stream-write-byte
1011 eof-error-p eof-value))
1013 (defun echo-misc (stream operation &optional arg1 arg2)
1014 (let* ((in (two-way-stream-input-stream stream))
1015 (out (two-way-stream-output-stream stream)))
1018 (or (not (null (echo-stream-unread-stuff stream)))
1019 (if (lisp-stream-p in)
1020 (or (/= (the fixnum (lisp-stream-in-index in))
1022 (funcall (lisp-stream-misc in) in :listen))
1023 (stream-misc-dispatch in :listen))))
1024 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1026 (let ((in-type (stream-element-type in))
1027 (out-type (stream-element-type out)))
1028 (if (equal in-type out-type)
1029 in-type `(and ,in-type ,out-type))))
1031 (set-closed-flame stream))
1033 (or (if (lisp-stream-p in)
1034 (funcall (lisp-stream-misc in) in operation arg1 arg2)
1035 (stream-misc-dispatch in operation arg1 arg2))
1036 (if (lisp-stream-p out)
1037 (funcall (lisp-stream-misc out) out operation arg1 arg2)
1038 (stream-misc-dispatch out operation arg1 arg2)))))))
1041 (setf (fdocumentation 'make-echo-stream 'function)
1042 "Returns a bidirectional stream which gets its input from Input-Stream and
1043 sends its output to Output-Stream. In addition, all input is echoed to
1046 ;;;; string input streams
1048 (defstruct (string-input-stream
1049 (:include lisp-stream
1051 (bin #'string-binch)
1052 (n-bin #'string-stream-read-n-bytes)
1053 (misc #'string-in-misc))
1054 (:constructor internal-make-string-input-stream
1055 (string current end))
1057 (string nil :type simple-string)
1058 (current nil :type index)
1059 (end nil :type index))
1061 (defun string-inch (stream eof-error-p eof-value)
1062 (let ((string (string-input-stream-string stream))
1063 (index (string-input-stream-current stream)))
1064 (declare (simple-string string) (fixnum index))
1065 (cond ((= index (the index (string-input-stream-end stream)))
1066 (eof-or-lose stream eof-error-p eof-value))
1068 (setf (string-input-stream-current stream) (1+ index))
1069 (aref string index)))))
1071 (defun string-binch (stream eof-error-p eof-value)
1072 (let ((string (string-input-stream-string stream))
1073 (index (string-input-stream-current stream)))
1074 (declare (simple-string string)
1076 (cond ((= index (the index (string-input-stream-end stream)))
1077 (eof-or-lose stream eof-error-p eof-value))
1079 (setf (string-input-stream-current stream) (1+ index))
1080 (char-code (aref string index))))))
1082 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1083 (declare (type string-input-stream stream)
1084 (type index start requested))
1085 (let* ((string (string-input-stream-string stream))
1086 (index (string-input-stream-current stream))
1087 (available (- (string-input-stream-end stream) index))
1088 (copy (min available requested)))
1089 (declare (simple-string string)
1090 (type index index available copy))
1092 (setf (string-input-stream-current stream)
1093 (truly-the index (+ index copy)))
1094 (sb!sys:without-gcing
1095 (system-area-copy (vector-sap string)
1096 (* index sb!vm:byte-bits)
1097 (if (typep buffer 'system-area-pointer)
1099 (vector-sap buffer))
1100 (* start sb!vm:byte-bits)
1101 (* copy sb!vm:byte-bits))))
1102 (if (and (> requested copy) eof-error-p)
1103 (error 'end-of-file :stream stream)
1106 (defun string-in-misc (stream operation &optional arg1 arg2)
1107 (declare (ignore arg2))
1111 (setf (string-input-stream-current stream) arg1)
1112 (string-input-stream-current stream)))
1113 (:file-length (length (string-input-stream-string stream)))
1114 (:unread (decf (string-input-stream-current stream)))
1115 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1116 (the fixnum (string-input-stream-end stream)))
1118 (:element-type 'base-char)))
1120 (defun make-string-input-stream (string &optional
1121 (start 0) (end (length string)))
1123 "Returns an input stream which will supply the characters of String between
1124 Start and End in order."
1125 (declare (type string string)
1127 (type (or index null) end))
1130 (when (> end (length string))
1131 (cerror "Continue with end changed from ~S to ~S"
1132 "Write-string: end (~S) is larger then the length of the string (~S)"
1133 end (1- (length string))))
1135 (internal-make-string-input-stream (coerce string 'simple-string)
1138 ;;;; string output streams
1140 (defstruct (string-output-stream
1141 (:include lisp-stream
1143 (sout #'string-sout)
1144 (misc #'string-out-misc))
1145 (:constructor make-string-output-stream ())
1147 ;; The string we throw stuff in.
1148 (string (make-string 40) :type simple-string)
1149 ;; Index of the next location to use.
1150 (index 0 :type fixnum))
1153 (setf (fdocumentation 'make-string-output-stream 'function)
1154 "Returns an Output stream which will accumulate all output given it for
1155 the benefit of the function Get-Output-Stream-String.")
1157 (defun string-ouch (stream character)
1158 (let ((current (string-output-stream-index stream))
1159 (workspace (string-output-stream-string stream)))
1160 (declare (simple-string workspace) (fixnum current))
1161 (if (= current (the fixnum (length workspace)))
1162 (let ((new-workspace (make-string (* current 2))))
1163 (replace new-workspace workspace)
1164 (setf (aref new-workspace current) character)
1165 (setf (string-output-stream-string stream) new-workspace))
1166 (setf (aref workspace current) character))
1167 (setf (string-output-stream-index stream) (1+ current))))
1169 (defun string-sout (stream string start end)
1170 (declare (simple-string string) (fixnum start end))
1171 (let* ((current (string-output-stream-index stream))
1172 (length (- end start))
1173 (dst-end (+ length current))
1174 (workspace (string-output-stream-string stream)))
1175 (declare (simple-string workspace)
1176 (fixnum current length dst-end))
1177 (if (> dst-end (the fixnum (length workspace)))
1178 (let ((new-workspace (make-string (+ (* current 2) length))))
1179 (replace new-workspace workspace :end2 current)
1180 (replace new-workspace string
1181 :start1 current :end1 dst-end
1182 :start2 start :end2 end)
1183 (setf (string-output-stream-string stream) new-workspace))
1184 (replace workspace string
1185 :start1 current :end1 dst-end
1186 :start2 start :end2 end))
1187 (setf (string-output-stream-index stream) dst-end)))
1189 (defun string-out-misc (stream operation &optional arg1 arg2)
1190 (declare (ignore arg2))
1194 (string-output-stream-index stream)))
1196 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1198 (count 0 (1+ count))
1199 (string (string-output-stream-string stream)))
1201 (declare (simple-string string)
1202 (fixnum index count))
1203 (if (char= (schar string index) #\newline)
1205 (:element-type 'base-char)))
1207 ;;; Return a string of all the characters sent to a stream made by
1208 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1209 (defun get-output-stream-string (stream)
1210 (declare (type string-output-stream stream))
1211 (let* ((length (string-output-stream-index stream))
1212 (result (make-string length)))
1213 (replace result (string-output-stream-string stream))
1214 (setf (string-output-stream-index stream) 0)
1217 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1218 ;;; GET-OUTPUT-STREAM-STRING would return them.
1219 (defun dump-output-stream-string (in-stream out-stream)
1220 (write-string* (string-output-stream-string in-stream) out-stream
1221 0 (string-output-stream-index in-stream))
1222 (setf (string-output-stream-index in-stream) 0))
1224 ;;;; fill-pointer streams
1226 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1227 ;;; the CLM, but they are required for the implementation of
1228 ;;; WITH-OUTPUT-TO-STRING.
1230 (defstruct (fill-pointer-output-stream
1231 (:include lisp-stream
1232 (out #'fill-pointer-ouch)
1233 (sout #'fill-pointer-sout)
1234 (misc #'fill-pointer-misc))
1235 (:constructor make-fill-pointer-output-stream (string))
1237 ;; the string we throw stuff in
1240 (defun fill-pointer-ouch (stream character)
1241 (let* ((buffer (fill-pointer-output-stream-string stream))
1242 (current (fill-pointer buffer))
1243 (current+1 (1+ current)))
1244 (declare (fixnum current))
1245 (with-array-data ((workspace buffer) (start) (end))
1246 (declare (simple-string workspace))
1247 (let ((offset-current (+ start current)))
1248 (declare (fixnum offset-current))
1249 (if (= offset-current end)
1250 (let* ((new-length (* current 2))
1251 (new-workspace (make-string new-length)))
1252 (declare (simple-string new-workspace))
1253 (%primitive sb!c:byte-blt
1259 (setf workspace new-workspace)
1260 (setf offset-current current)
1261 (set-array-header buffer workspace new-length
1262 current+1 0 new-length nil))
1263 (setf (fill-pointer buffer) current+1))
1264 (setf (schar workspace offset-current) character)))
1267 (defun fill-pointer-sout (stream string start end)
1268 (declare (simple-string string) (fixnum start end))
1269 (let* ((buffer (fill-pointer-output-stream-string stream))
1270 (current (fill-pointer buffer))
1271 (string-len (- end start))
1272 (dst-end (+ string-len current)))
1273 (declare (fixnum current dst-end string-len))
1274 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1275 (declare (simple-string workspace))
1276 (let ((offset-dst-end (+ dst-start dst-end))
1277 (offset-current (+ dst-start current)))
1278 (declare (fixnum offset-dst-end offset-current))
1279 (if (> offset-dst-end dst-length)
1280 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1281 (new-workspace (make-string new-length)))
1282 (declare (simple-string new-workspace))
1283 (%primitive sb!c:byte-blt
1289 (setf workspace new-workspace)
1290 (setf offset-current current)
1291 (setf offset-dst-end dst-end)
1292 (set-array-header buffer
1299 (setf (fill-pointer buffer) dst-end))
1300 (%primitive sb!c:byte-blt
1308 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1309 (declare (ignore arg1 arg2))
1312 (let* ((buffer (fill-pointer-output-stream-string stream))
1313 (current (fill-pointer buffer)))
1314 (with-array-data ((string buffer) (start) (end current))
1315 (declare (simple-string string) (ignore start))
1316 (let ((found (position #\newline string :test #'char=
1317 :end end :from-end t)))
1319 (- end (the fixnum found))
1321 (:element-type 'base-char)))
1323 ;;;; indenting streams
1325 (defstruct (indenting-stream (:include lisp-stream
1326 (out #'indenting-out)
1327 (sout #'indenting-sout)
1328 (misc #'indenting-misc))
1329 (:constructor make-indenting-stream (stream))
1331 ;; the stream we're based on
1333 ;; how much we indent on each line
1337 (setf (fdocumentation 'make-indenting-stream 'function)
1338 "Returns an output stream which indents its output by some amount.")
1340 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1341 ;;; output on the given STREAM based on the specified SUB-STREAM.
1342 (defmacro indenting-indent (stream sub-stream)
1343 ;; KLUDGE: bare magic number 60
1344 `(do ((i 0 (+ i 60))
1345 (indentation (indenting-stream-indentation ,stream)))
1346 ((>= i indentation))
1349 ,sub-stream 0 (min 60 (- indentation i)))))
1351 ;;; INDENTING-OUT writes a character to an indenting stream.
1352 (defun indenting-out (stream char)
1353 (let ((sub-stream (indenting-stream-stream stream)))
1354 (write-char char sub-stream)
1355 (if (char= char #\newline)
1356 (indenting-indent stream sub-stream))))
1358 ;;; INDENTING-SOUT writes a string to an indenting stream.
1359 (defun indenting-sout (stream string start end)
1360 (declare (simple-string string) (fixnum start end))
1362 (sub-stream (indenting-stream-stream stream)))
1364 (let ((newline (position #\newline string :start i :end end)))
1366 (write-string* string sub-stream i (1+ newline))
1367 (indenting-indent stream sub-stream)
1368 (setq i (+ newline 1)))
1370 (write-string* string sub-stream i end)
1373 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1374 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1375 ;;; the base stream minus the stream's indentation.
1376 (defun indenting-misc (stream operation &optional arg1 arg2)
1377 (let ((sub-stream (indenting-stream-stream stream)))
1378 (if (lisp-stream-p sub-stream)
1379 (let ((method (lisp-stream-misc sub-stream)))
1382 (let ((line-length (funcall method sub-stream operation)))
1384 (- line-length (indenting-stream-indentation stream)))))
1386 (let ((charpos (funcall method sub-stream operation)))
1388 (- charpos (indenting-stream-indentation stream)))))
1390 (funcall method sub-stream operation arg1 arg2))))
1391 ;; must be Gray streams FUNDAMENTAL-STREAM
1394 (let ((line-length (stream-line-length sub-stream)))
1396 (- line-length (indenting-stream-indentation stream)))))
1398 (let ((charpos (stream-line-column sub-stream)))
1400 (- charpos (indenting-stream-indentation stream)))))
1402 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1404 (declaim (maybe-inline read-char unread-char read-byte listen))
1406 ;;;; case frobbing streams, used by FORMAT ~(...~)
1408 (defstruct (case-frob-stream
1409 (:include lisp-stream
1410 (:misc #'case-frob-misc))
1411 (:constructor %make-case-frob-stream (target out sout))
1413 (target (required-argument) :type stream))
1415 (defun make-case-frob-stream (target kind)
1417 "Returns a stream that sends all output to the stream TARGET, but modifies
1418 the case of letters, depending on KIND, which should be one of:
1419 :upcase - convert to upper case.
1420 :downcase - convert to lower case.
1421 :capitalize - convert the first letter of words to upper case and the
1422 rest of the word to lower case.
1423 :capitalize-first - convert the first letter of the first word to upper
1424 case and everything else to lower case."
1425 (declare (type stream target)
1426 (type (member :upcase :downcase :capitalize :capitalize-first)
1429 (if (case-frob-stream-p target)
1430 ;; If we are going to be writing to a stream that already does
1431 ;; case frobbing, why bother frobbing the case just so it can
1434 (multiple-value-bind (out sout)
1437 (values #'case-frob-upcase-out
1438 #'case-frob-upcase-sout))
1440 (values #'case-frob-downcase-out
1441 #'case-frob-downcase-sout))
1443 (values #'case-frob-capitalize-out
1444 #'case-frob-capitalize-sout))
1446 (values #'case-frob-capitalize-first-out
1447 #'case-frob-capitalize-first-sout)))
1448 (%make-case-frob-stream target out sout))))
1450 (defun case-frob-misc (stream op &optional arg1 arg2)
1451 (declare (type case-frob-stream stream))
1455 (let ((target (case-frob-stream-target stream)))
1456 (if (lisp-stream-p target)
1457 (funcall (lisp-stream-misc target) target op arg1 arg2)
1458 (stream-misc-dispatch target op arg1 arg2))))))
1460 (defun case-frob-upcase-out (stream char)
1461 (declare (type case-frob-stream stream)
1462 (type base-char char))
1463 (let ((target (case-frob-stream-target stream))
1464 (char (char-upcase char)))
1465 (if (lisp-stream-p target)
1466 (funcall (lisp-stream-out target) target char)
1467 (stream-write-char target char))))
1469 (defun case-frob-upcase-sout (stream str start end)
1470 (declare (type case-frob-stream stream)
1471 (type simple-base-string str)
1473 (type (or index null) end))
1474 (let* ((target (case-frob-stream-target stream))
1477 (string (if (and (zerop start) (= len end))
1479 (nstring-upcase (subseq str start end))))
1480 (string-len (- end start)))
1481 (if (lisp-stream-p target)
1482 (funcall (lisp-stream-sout target) target string 0 string-len)
1483 (stream-write-string target string 0 string-len))))
1485 (defun case-frob-downcase-out (stream char)
1486 (declare (type case-frob-stream stream)
1487 (type base-char char))
1488 (let ((target (case-frob-stream-target stream))
1489 (char (char-downcase char)))
1490 (if (lisp-stream-p target)
1491 (funcall (lisp-stream-out target) target char)
1492 (stream-write-char target char))))
1494 (defun case-frob-downcase-sout (stream str start end)
1495 (declare (type case-frob-stream stream)
1496 (type simple-base-string str)
1498 (type (or index null) end))
1499 (let* ((target (case-frob-stream-target stream))
1502 (string (if (and (zerop start) (= len end))
1503 (string-downcase str)
1504 (nstring-downcase (subseq str start end))))
1505 (string-len (- end start)))
1506 (if (lisp-stream-p target)
1507 (funcall (lisp-stream-sout target) target string 0 string-len)
1508 (stream-write-string target string 0 string-len))))
1510 (defun case-frob-capitalize-out (stream char)
1511 (declare (type case-frob-stream stream)
1512 (type base-char char))
1513 (let ((target (case-frob-stream-target stream)))
1514 (cond ((alphanumericp char)
1515 (let ((char (char-upcase char)))
1516 (if (lisp-stream-p target)
1517 (funcall (lisp-stream-out target) target char)
1518 (stream-write-char target char)))
1519 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1520 (setf (case-frob-stream-sout stream)
1521 #'case-frob-capitalize-aux-sout))
1523 (if (lisp-stream-p target)
1524 (funcall (lisp-stream-out target) target char)
1525 (stream-write-char target char))))))
1527 (defun case-frob-capitalize-sout (stream str start end)
1528 (declare (type case-frob-stream stream)
1529 (type simple-base-string str)
1531 (type (or index null) end))
1532 (let* ((target (case-frob-stream-target stream))
1533 (str (subseq str start end))
1537 (let ((char (schar str i)))
1538 (cond ((not (alphanumericp char))
1539 (setf inside-word nil))
1541 (setf (schar str i) (char-downcase char)))
1543 (setf inside-word t)
1544 (setf (schar str i) (char-upcase char))))))
1546 (setf (case-frob-stream-out stream)
1547 #'case-frob-capitalize-aux-out)
1548 (setf (case-frob-stream-sout stream)
1549 #'case-frob-capitalize-aux-sout))
1550 (if (lisp-stream-p target)
1551 (funcall (lisp-stream-sout target) target str 0 len)
1552 (stream-write-string target str 0 len))))
1554 (defun case-frob-capitalize-aux-out (stream char)
1555 (declare (type case-frob-stream stream)
1556 (type base-char char))
1557 (let ((target (case-frob-stream-target stream)))
1558 (cond ((alphanumericp char)
1559 (let ((char (char-downcase char)))
1560 (if (lisp-stream-p target)
1561 (funcall (lisp-stream-out target) target char)
1562 (stream-write-char target char))))
1564 (if (lisp-stream-p target)
1565 (funcall (lisp-stream-out target) target char)
1566 (stream-write-char target char))
1567 (setf (case-frob-stream-out stream)
1568 #'case-frob-capitalize-out)
1569 (setf (case-frob-stream-sout stream)
1570 #'case-frob-capitalize-sout)))))
1572 (defun case-frob-capitalize-aux-sout (stream str start end)
1573 (declare (type case-frob-stream stream)
1574 (type simple-base-string str)
1576 (type (or index null) end))
1577 (let* ((target (case-frob-stream-target stream))
1578 (str (subseq str start end))
1582 (let ((char (schar str i)))
1583 (cond ((not (alphanumericp char))
1584 (setf inside-word nil))
1586 (setf (schar str i) (char-downcase char)))
1588 (setf inside-word t)
1589 (setf (schar str i) (char-upcase char))))))
1591 (setf (case-frob-stream-out stream)
1592 #'case-frob-capitalize-out)
1593 (setf (case-frob-stream-sout stream)
1594 #'case-frob-capitalize-sout))
1595 (if (lisp-stream-p target)
1596 (funcall (lisp-stream-sout target) target str 0 len)
1597 (stream-write-string target str 0 len))))
1599 (defun case-frob-capitalize-first-out (stream char)
1600 (declare (type case-frob-stream stream)
1601 (type base-char char))
1602 (let ((target (case-frob-stream-target stream)))
1603 (cond ((alphanumericp char)
1604 (let ((char (char-upcase char)))
1605 (if (lisp-stream-p target)
1606 (funcall (lisp-stream-out target) target char)
1607 (stream-write-char target char)))
1608 (setf (case-frob-stream-out stream)
1609 #'case-frob-downcase-out)
1610 (setf (case-frob-stream-sout stream)
1611 #'case-frob-downcase-sout))
1613 (if (lisp-stream-p target)
1614 (funcall (lisp-stream-out target) target char)
1615 (stream-write-char target char))))))
1617 (defun case-frob-capitalize-first-sout (stream str start end)
1618 (declare (type case-frob-stream stream)
1619 (type simple-base-string str)
1621 (type (or index null) end))
1622 (let* ((target (case-frob-stream-target stream))
1623 (str (subseq str start end))
1626 (let ((char (schar str i)))
1627 (when (alphanumericp char)
1628 (setf (schar str i) (char-upcase char))
1629 (do ((i (1+ i) (1+ i)))
1631 (setf (schar str i) (char-downcase (schar str i))))
1632 (setf (case-frob-stream-out stream)
1633 #'case-frob-downcase-out)
1634 (setf (case-frob-stream-sout stream)
1635 #'case-frob-downcase-sout)
1637 (if (lisp-stream-p target)
1638 (funcall (lisp-stream-sout target) target str 0 len)
1639 (stream-write-string target str 0 len))))
1641 ;;;; stream commands
1643 (defstruct (stream-command (:constructor make-stream-command
1644 (name &optional args))
1646 (name nil :type symbol)
1647 (args nil :type list))
1648 (def!method print-object ((obj stream-command) str)
1649 (print-unreadable-object (obj str :type t :identity t)
1650 (prin1 (stream-command-name obj) str)))
1652 ;;; Take a stream and wait for text or a command to appear on it. If
1653 ;;; text appears before a command, return NIL, otherwise return a
1656 ;;; We can't simply call the stream's misc method because NIL is an
1657 ;;; ambiguous return value: does it mean text arrived, or does it mean
1658 ;;; the stream's misc method had no :GET-COMMAND implementation? We
1659 ;;; can't return NIL until there is text input. We don't need to loop
1660 ;;; because any stream implementing :GET-COMMAND would wait until it
1661 ;;; had some input. If the LISTEN fails, then we have some stream we
1663 (defun get-stream-command (stream)
1664 (let ((cmdp (funcall (lisp-stream-misc stream) stream :get-command)))
1669 ;; This waits for input and returns NIL when it arrives.
1670 (unread-char (read-char stream) stream)))))
1672 (defun read-sequence (seq stream &key (start 0) (end nil))
1674 "Destructively modify SEQ by reading elements from STREAM.
1675 That part of SEQ bounded by START and END is destructively modified by
1676 copying successive elements into it from STREAM. If the end of file
1677 for STREAM is reached before copying all elements of the subsequence,
1678 then the extra elements near the end of sequence are not updated, and
1679 the index of the next element is returned."
1680 (declare (type sequence seq)
1681 (type stream stream)
1683 (type sequence-end end)
1685 (let ((end (or end (length seq))))
1686 (declare (type index end))
1689 (let ((read-function
1690 (if (subtypep (stream-element-type stream) 'character)
1693 (do ((rem (nthcdr start seq) (rest rem))
1695 ((or (endp rem) (>= i end)) i)
1696 (declare (type list rem)
1698 (let ((el (funcall read-function stream nil :eof)))
1701 (setf (first rem) el)))))
1703 (with-array-data ((data seq) (offset-start start) (offset-end end))
1705 ((or (simple-array (unsigned-byte 8) (*))
1706 (simple-array (signed-byte 8) (*))
1708 (let* ((numbytes (- end start))
1709 (bytes-read (sb!sys:read-n-bytes stream
1714 (if (< bytes-read numbytes)
1715 (+ start bytes-read)
1718 (let ((read-function
1719 (if (subtypep (stream-element-type stream) 'character)
1722 (do ((i offset-start (1+ i)))
1723 ((>= i offset-end) end)
1724 (declare (type index i))
1725 (let ((el (funcall read-function stream nil :eof)))
1727 (return (+ start (- i offset-start))))
1728 (setf (aref data i) el)))))))))))
1730 (defun write-sequence (seq stream &key (start 0) (end nil))
1732 "Write the elements of SEQ bounded by START and END to STREAM."
1733 (declare (type sequence seq)
1734 (type stream stream)
1736 (type sequence-end end)
1738 (let ((end (or end (length seq))))
1739 (declare (type index start end))
1742 (let ((write-function
1743 (if (subtypep (stream-element-type stream) 'character)
1746 (do ((rem (nthcdr start seq) (rest rem))
1748 ((or (endp rem) (>= i end)) seq)
1749 (declare (type list rem)
1751 (funcall write-function (first rem) stream))))
1753 (write-string* seq stream start end))
1755 (let ((write-function
1756 (if (subtypep (stream-element-type stream) 'character)
1759 (do ((i start (1+ i)))
1761 (declare (type index i))
1762 (funcall write-function (aref seq i) stream)))))))
1764 ;;; (These were inline throughout this file, but that's not appropriate
1766 (declaim (maybe-inline read-char unread-char read-byte listen))