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 ;;; This is a literal translation of the ANSI glossary entry "stream
218 ;;; associated with a file".
220 ;;; KLUDGE: Note that since Unix famously thinks "everything is a
221 ;;; file", and in particular stdin, stdout, and stderr are files, we
222 ;;; end up with this test being satisfied for weird things like
223 ;;; *STANDARD-OUTPUT* (to a tty). That seems unlikely to be what the
224 ;;; ANSI spec really had in mind, especially since this is used as a
225 ;;; qualification for operations like FILE-LENGTH (so that ANSI was
226 ;;; probably thinking of something like what Unix calls block devices)
227 ;;; but I can't see any better way to do it. -- WHN 2001-04-14
228 (defun stream-associated-with-file-p (x)
229 "Test for the ANSI concept \"stream associated with a file\"."
230 (or (typep x 'file-stream)
231 (and (synonym-stream-p x)
232 (stream-associated-with-file-p (symbol-value
233 (synonym-stream-symbol x))))))
235 (defun stream-must-be-associated-with-file (stream)
236 (declare (type stream stream))
237 (unless (stream-associated-with-file-p stream)
238 (error 'simple-type-error
239 ;; KLUDGE: The ANSI spec for FILE-LENGTH specifically says
240 ;; this should be TYPE-ERROR. But what then can we use for
241 ;; EXPECTED-TYPE? This SATISFIES type (with a nonstandard
242 ;; private predicate function..) is ugly and confusing, but
243 ;; I can't see any other way. -- WHN 2001-04-14
244 :expected-type '(satisfies stream-associated-with-file-p)
246 "~@<The stream ~2I~_~S ~I~_isn't associated with a file.~:>"
247 :format-arguments (list stream))))
249 ;;; like FILE-POSITION, only using :FILE-LENGTH
250 (defun file-length (stream)
251 (declare (type (or file-stream synonym-stream) stream))
252 (stream-must-be-associated-with-file stream)
253 (funcall (lisp-stream-misc stream) stream :file-length))
257 (defun read-line (&optional (stream *standard-input*) (eof-error-p t) eof-value
259 (declare (ignore recursive-p))
260 (let ((stream (in-synonym-of stream)))
261 (if (lisp-stream-p stream)
262 (prepare-for-fast-read-char stream
263 (let ((res (make-string 80))
267 (let ((ch (fast-read-char nil nil)))
269 (when (char= ch #\newline)
270 (done-with-fast-read-char)
271 (return (values (shrink-vector res index) nil)))
274 (let ((new (make-string len)))
277 (setf (schar res index) ch)
280 (done-with-fast-read-char)
281 (return (values (eof-or-lose stream
285 ;; Since FAST-READ-CHAR already hit the eof char, we
286 ;; shouldn't do another READ-CHAR.
288 (done-with-fast-read-char)
289 (return (values (shrink-vector res index) t))))))))
290 ;; must be Gray streams FUNDAMENTAL-STREAM
291 (multiple-value-bind (string eof) (stream-read-line stream)
292 (if (and eof (zerop (length string)))
293 (values (eof-or-lose stream eof-error-p eof-value) t)
294 (values string eof))))))
296 ;;; We proclaim them INLINE here, then proclaim them MAYBE-INLINE at EOF,
297 ;;; so, except in this file, they are not inline by default, but they can be.
298 #!-sb-fluid (declaim (inline read-char unread-char read-byte listen))
300 (defun read-char (&optional (stream *standard-input*)
304 (declare (ignore recursive-p))
305 (let ((stream (in-synonym-of stream)))
306 (if (lisp-stream-p stream)
307 (prepare-for-fast-read-char stream
309 (fast-read-char eof-error-p eof-value)
310 (done-with-fast-read-char)))
311 ;; must be Gray streams FUNDAMENTAL-STREAM
312 (let ((char (stream-read-char stream)))
314 (eof-or-lose stream eof-error-p eof-value)
317 (defun unread-char (character &optional (stream *standard-input*))
318 (let ((stream (in-synonym-of stream)))
319 (if (lisp-stream-p stream)
320 (let ((index (1- (lisp-stream-in-index stream)))
321 (buffer (lisp-stream-in-buffer stream)))
322 (declare (fixnum index))
323 (when (minusp index) (error "nothing to unread"))
325 (setf (aref buffer index) (char-code character))
326 (setf (lisp-stream-in-index stream) index))
328 (funcall (lisp-stream-misc stream) stream
329 :unread character))))
330 ;; must be Gray streams FUNDAMENTAL-STREAM
331 (stream-unread-char stream character)))
334 (defun peek-char (&optional (peek-type nil)
335 (stream *standard-input*)
339 (declare (ignore recursive-p))
340 ;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
341 ;; the compiler doesn't seem to be smart enough to go from there to
342 ;; imposing a type check. Figure out why (because PEEK-TYPE is an
343 ;; &OPTIONAL argument?) and fix it, and then this explicit type
344 ;; check can go away.
345 (unless (typep peek-type '(or character boolean))
346 (error 'simple-type-error
348 :expected-type '(or character boolean)
349 :format-control "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>"
350 :format-arguments (list peek-type '(or character boolean))))
351 (let ((stream (in-synonym-of stream)))
352 (if (lisp-stream-p stream)
353 (let ((char (read-char stream eof-error-p eof-value)))
354 (cond ((eq char eof-value) char)
355 ((characterp peek-type)
356 (do ((char char (read-char stream eof-error-p eof-value)))
357 ((or (eq char eof-value) (char= char peek-type))
358 (unless (eq char eof-value)
359 (unread-char char stream))
362 (do ((char char (read-char stream eof-error-p eof-value)))
363 ((or (eq char eof-value) (not (whitespace-char-p char)))
364 (unless (eq char eof-value)
365 (unread-char char stream))
368 (unread-char char stream)
371 (error "internal error: impossible case"))))
372 ;; by elimination, must be Gray streams FUNDAMENTAL-STREAM
373 (cond ((characterp peek-type)
374 (do ((char (stream-read-char stream)
375 (stream-read-char stream)))
376 ((or (eq char :eof) (char= char peek-type))
377 (cond ((eq char :eof)
378 (eof-or-lose stream eof-error-p eof-value))
380 (stream-unread-char stream char)
383 (do ((char (stream-read-char stream)
384 (stream-read-char stream)))
385 ((or (eq char :eof) (not (whitespace-char-p char)))
386 (cond ((eq char :eof)
387 (eof-or-lose stream eof-error-p eof-value))
389 (stream-unread-char stream char)
392 (let ((char (stream-peek-char stream)))
394 (eof-or-lose stream eof-error-p eof-value)
397 (error "internal error: impossible case"))))))
399 (defun listen (&optional (stream *standard-input*))
400 (let ((stream (in-synonym-of stream)))
401 (if (lisp-stream-p stream)
402 (or (/= (the fixnum (lisp-stream-in-index stream)) +in-buffer-length+)
403 ;; Test for T explicitly since misc methods return :EOF sometimes.
404 (eq (funcall (lisp-stream-misc stream) stream :listen) t))
405 ;; Fall through to Gray streams FUNDAMENTAL-STREAM case.
406 (stream-listen stream))))
408 (defun read-char-no-hang (&optional (stream *standard-input*)
412 (declare (ignore recursive-p))
413 (let ((stream (in-synonym-of stream)))
414 (if (lisp-stream-p stream)
415 (if (funcall (lisp-stream-misc stream) stream :listen)
416 ;; On T or :EOF get READ-CHAR to do the work.
417 (read-char stream eof-error-p eof-value)
419 ;; must be Gray streams FUNDAMENTAL-STREAM
420 (let ((char (stream-read-char-no-hang stream)))
422 (eof-or-lose stream eof-error-p eof-value)
425 (defun clear-input (&optional (stream *standard-input*))
426 (let ((stream (in-synonym-of stream)))
427 (cond ((lisp-stream-p stream)
428 (setf (lisp-stream-in-index stream) +in-buffer-length+)
429 (funcall (lisp-stream-misc stream) stream :clear-input))
431 (stream-clear-input stream))))
434 (declaim (maybe-inline read-byte))
435 (defun read-byte (stream &optional (eof-error-p t) eof-value)
436 (let ((stream (in-synonym-of stream)))
437 (if (lisp-stream-p stream)
438 (prepare-for-fast-read-byte stream
440 (fast-read-byte eof-error-p eof-value t)
441 (done-with-fast-read-byte)))
442 ;; must be Gray streams FUNDAMENTAL-STREAM
443 (let ((char (stream-read-byte stream)))
445 (eof-or-lose stream eof-error-p eof-value)
448 ;;; Read NUMBYTES bytes into BUFFER beginning at START, and return the
449 ;;; number of bytes read.
451 ;;; Note: CMU CL's version of this had a special interpretation of
452 ;;; EOF-ERROR-P which SBCL does not have. (In the EOF-ERROR-P=NIL
453 ;;; case, CMU CL's version would return as soon as any data became
454 ;;; available.) This could be useful behavior for things like pipes in
455 ;;; some cases, but it wasn't being used in SBCL, so it was dropped.
456 ;;; If we ever need it, it could be added later as a new variant N-BIN
457 ;;; method (perhaps N-BIN-ASAP?) or something.
458 (defun read-n-bytes (stream buffer start numbytes &optional (eof-error-p t))
459 (declare (type lisp-stream stream)
460 (type index numbytes start)
461 (type (or (simple-array * (*)) system-area-pointer) buffer))
462 (let* ((stream (in-synonym-of stream lisp-stream))
463 (in-buffer (lisp-stream-in-buffer stream))
464 (index (lisp-stream-in-index stream))
465 (num-buffered (- +in-buffer-length+ index)))
466 (declare (fixnum index num-buffered))
469 (funcall (lisp-stream-n-bin stream)
475 ((<= numbytes num-buffered)
476 (%byte-blt in-buffer index
477 buffer start (+ start numbytes))
478 (setf (lisp-stream-in-index stream) (+ index numbytes))
481 (let ((end (+ start num-buffered)))
482 (%byte-blt in-buffer index buffer start end)
483 (setf (lisp-stream-in-index stream) +in-buffer-length+)
484 (+ (funcall (lisp-stream-n-bin stream)
488 (- numbytes num-buffered)
492 ;;; the amount of space we leave at the start of the in-buffer for
495 ;;; (It's 4 instead of 1 to allow word-aligned copies.)
496 (defconstant +in-buffer-extra+ 4) ; FIXME: should be symbolic constant
498 ;;; This function is called by the FAST-READ-CHAR expansion to refill
499 ;;; the IN-BUFFER for text streams. There is definitely an IN-BUFFER,
500 ;;; and hence must be an N-BIN method.
501 (defun fast-read-char-refill (stream eof-error-p eof-value)
502 (let* ((ibuf (lisp-stream-in-buffer stream))
503 (count (funcall (lisp-stream-n-bin stream)
507 (- +in-buffer-length+ +in-buffer-extra+)
509 (start (- +in-buffer-length+ count)))
510 (declare (type index start count))
512 (setf (lisp-stream-in-index stream) +in-buffer-length+)
513 (funcall (lisp-stream-in stream) stream eof-error-p eof-value))
515 (when (/= start +in-buffer-extra+)
516 (bit-bash-copy ibuf (+ (* +in-buffer-extra+ sb!vm:byte-bits)
517 (* sb!vm:vector-data-offset
519 ibuf (+ (the index (* start sb!vm:byte-bits))
520 (* sb!vm:vector-data-offset
522 (* count sb!vm:byte-bits)))
523 (setf (lisp-stream-in-index stream) (1+ start))
524 (code-char (aref ibuf start))))))
526 ;;; This is similar to FAST-READ-CHAR-REFILL, but we don't have to
527 ;;; leave room for unreading.
528 (defun fast-read-byte-refill (stream eof-error-p eof-value)
529 (let* ((ibuf (lisp-stream-in-buffer stream))
530 (count (funcall (lisp-stream-n-bin stream) stream
531 ibuf 0 +in-buffer-length+
533 (start (- +in-buffer-length+ count)))
534 (declare (type index start count))
536 (setf (lisp-stream-in-index stream) +in-buffer-length+)
537 (funcall (lisp-stream-bin stream) stream eof-error-p eof-value))
539 (unless (zerop start)
540 (bit-bash-copy ibuf (* sb!vm:vector-data-offset sb!vm:word-bits)
541 ibuf (+ (the index (* start sb!vm:byte-bits))
542 (* sb!vm:vector-data-offset
544 (* count sb!vm:byte-bits)))
545 (setf (lisp-stream-in-index stream) (1+ start))
546 (aref ibuf start)))))
550 (defun write-char (character &optional (stream *standard-output*))
551 (with-out-stream stream (lisp-stream-out character)
552 (stream-write-char character))
555 (defun terpri (&optional (stream *standard-output*))
556 (with-out-stream stream (lisp-stream-out #\newline) (stream-terpri))
559 (defun fresh-line (&optional (stream *standard-output*))
560 (let ((stream (out-synonym-of stream)))
561 (if (lisp-stream-p stream)
562 (when (/= (or (charpos stream) 1) 0)
563 (funcall (lisp-stream-out stream) stream #\newline)
565 ;; must be Gray streams FUNDAMENTAL-STREAM
566 (stream-fresh-line stream))))
568 (defun write-string (string &optional (stream *standard-output*)
569 &key (start 0) (end (length (the vector string))))
571 ;; FIXME: These SETFs don't look right to me. Looking at the
572 ;; definition of "bounding indices" in the glossary of the ANSI
573 ;; spec, and extrapolating from the behavior of other operations
574 ;; when their operands are the wrong type, it seems that it would be
575 ;; more correct to essentially
576 ;; (AVER (<= 0 START END (LENGTH STRING)))
577 ;; instead of modifying the incorrect values.
579 (setf end (min end (length (the vector string))))
581 (setf start (max start 0))
583 ;; FIXME: And I'd just signal a non-continuable error..
586 (cerror "Continue with switched start and end ~S <-> ~S"
587 "Write-string: start (~S) and end (~S) exchanged."
591 (write-string* string stream start end))
593 (defun write-string* (string &optional (stream *standard-output*)
594 (start 0) (end (length (the vector string))))
595 (declare (fixnum start end))
596 (let ((stream (out-synonym-of stream)))
597 (cond ((lisp-stream-p stream)
598 (if (array-header-p string)
599 (with-array-data ((data string) (offset-start start)
601 (funcall (lisp-stream-sout stream)
602 stream data offset-start offset-end))
603 (funcall (lisp-stream-sout stream) stream string start end))
605 (t ; must be Gray streams FUNDAMENTAL-STREAM
606 (stream-write-string stream string start end)))))
608 (defun write-line (string &optional (stream *standard-output*)
609 &key (start 0) (end (length string)))
610 (write-line* string stream start end))
612 (defun write-line* (string &optional (stream *standard-output*)
613 (start 0) (end (length string)))
614 (declare (fixnum start end))
615 (let ((stream (out-synonym-of stream)))
616 (cond ((lisp-stream-p stream)
617 (if (array-header-p string)
618 (with-array-data ((data string) (offset-start start)
620 (with-out-stream stream (lisp-stream-sout data offset-start
622 (with-out-stream stream (lisp-stream-sout string start end)))
623 (funcall (lisp-stream-out stream) stream #\newline))
624 (t ; must be Gray streams FUNDAMENTAL-STREAM
625 (stream-write-string stream string start end)
626 (stream-write-char stream #\Newline)))
629 (defun charpos (&optional (stream *standard-output*))
630 (with-out-stream stream (lisp-stream-misc :charpos) (stream-line-column)))
632 (defun line-length (&optional (stream *standard-output*))
633 (with-out-stream stream (lisp-stream-misc :line-length)
634 (stream-line-length)))
636 (defun finish-output (&optional (stream *standard-output*))
637 (with-out-stream stream (lisp-stream-misc :finish-output)
638 (stream-finish-output))
641 (defun force-output (&optional (stream *standard-output*))
642 (with-out-stream stream (lisp-stream-misc :force-output)
643 (stream-force-output))
646 (defun clear-output (&optional (stream *standard-output*))
647 (with-out-stream stream (lisp-stream-misc :clear-output)
648 (stream-force-output))
651 (defun write-byte (integer stream)
652 (with-out-stream stream (lisp-stream-bout integer)
653 (stream-write-byte integer))
656 ;;; This is called from LISP-STREAM routines that encapsulate CLOS
657 ;;; streams to handle the misc routines and dispatch to the
658 ;;; appropriate Gray stream functions.
659 (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
660 (declare (type fundamental-stream stream)
664 ;; Return T if input available, :EOF for end-of-file, otherwise NIL.
665 (let ((char (stream-read-char-no-hang stream)))
666 (when (characterp char)
667 (stream-unread-char stream char))
670 (stream-unread-char stream arg1))
674 (stream-clear-input stream))
676 (stream-force-output stream))
678 (stream-finish-output stream))
680 (stream-element-type stream))
682 (interactive-stream-p stream))
684 (stream-line-length stream))
686 (stream-line-column stream))
688 (file-length stream))
690 (file-position stream arg1))))
692 ;;;; broadcast streams
694 (defstruct (broadcast-stream (:include lisp-stream
695 (out #'broadcast-out)
696 (bout #'broadcast-bout)
697 (sout #'broadcast-sout)
698 (misc #'broadcast-misc))
699 (:constructor #!-high-security-support
700 make-broadcast-stream
701 #!+high-security-support
702 %make-broadcast-stream (&rest
705 ;; a list of all the streams we broadcast to
706 (streams () :type list :read-only t))
708 #!+high-security-support
709 (defun make-broadcast-stream (&rest streams)
710 (dolist (stream streams)
711 (unless (or (and (synonym-stream-p stream)
712 (output-stream-p (symbol-value
713 (synonym-stream-symbol stream))))
714 (output-stream-p stream))
717 :expected-type '(satisfies output-stream-p))))
718 (apply #'%make-broadcast-stream streams))
720 (macrolet ((out-fun (fun method stream-method &rest args)
721 `(defun ,fun (stream ,@args)
722 (dolist (stream (broadcast-stream-streams stream))
723 (if (lisp-stream-p stream)
724 (funcall (,method stream) stream ,@args)
725 (,stream-method stream ,@args))))))
726 (out-fun broadcast-out lisp-stream-out stream-write-char char)
727 (out-fun broadcast-bout lisp-stream-bout stream-write-byte byte)
728 (out-fun broadcast-sout lisp-stream-sout stream-write-string
731 (defun broadcast-misc (stream operation &optional arg1 arg2)
732 (let ((streams (broadcast-stream-streams stream)))
735 (dolist (stream streams)
736 (let ((charpos (charpos stream)))
737 (if charpos (return charpos)))))
740 (dolist (stream streams min)
741 (let ((res (line-length stream)))
742 (when res (setq min (if min (min res min) res)))))))
745 (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
746 (pushnew (stream-element-type stream) res :test #'equal))))
750 (dolist (stream streams res)
752 (if (lisp-stream-p stream)
753 (funcall (lisp-stream-misc stream) stream operation
755 (stream-misc-dispatch stream operation arg1 arg2)))))))))
759 (defstruct (synonym-stream (:include lisp-stream
762 (n-bin #'synonym-n-bin)
764 (bout #'synonym-bout)
765 (sout #'synonym-sout)
766 (misc #'synonym-misc))
767 (:constructor make-synonym-stream (symbol))
769 ;; This is the symbol, the value of which is the stream we are synonym to.
770 (symbol nil :type symbol :read-only t))
771 (def!method print-object ((x synonym-stream) stream)
772 (print-unreadable-object (x stream :type t :identity t)
773 (format stream ":SYMBOL ~S" (synonym-stream-symbol x))))
775 ;;; The output simple output methods just call the corresponding method
776 ;;; in the synonymed stream.
777 (macrolet ((out-fun (name slot stream-method &rest args)
778 `(defun ,name (stream ,@args)
779 (declare (optimize (safety 1)))
780 (let ((syn (symbol-value (synonym-stream-symbol stream))))
781 (if (lisp-stream-p syn)
782 (funcall (,slot syn) syn ,@args)
783 (,stream-method syn ,@args))))))
784 (out-fun synonym-out lisp-stream-out stream-write-char ch)
785 (out-fun synonym-bout lisp-stream-bout stream-write-byte n)
786 (out-fun synonym-sout lisp-stream-sout stream-write-string string start end))
788 ;;; For the input methods, we just call the corresponding function on the
789 ;;; synonymed stream. These functions deal with getting input out of
790 ;;; the In-Buffer if there is any.
791 (macrolet ((in-fun (name fun &rest args)
792 `(defun ,name (stream ,@args)
793 (declare (optimize (safety 1)))
794 (,fun (symbol-value (synonym-stream-symbol stream))
796 (in-fun synonym-in read-char eof-error-p eof-value)
797 (in-fun synonym-bin read-byte eof-error-p eof-value)
798 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-error-p))
800 (defun synonym-misc (stream operation &optional arg1 arg2)
801 (declare (optimize (safety 1)))
802 (let ((syn (symbol-value (synonym-stream-symbol stream))))
803 (if (lisp-stream-p syn)
804 ;; We have to special-case some operations which interact with
805 ;; the in-buffer of the wrapped stream, since just calling
806 ;; LISP-STREAM-MISC on them
808 (:listen (or (/= (the fixnum (lisp-stream-in-index syn))
810 (funcall (lisp-stream-misc syn) syn :listen)))
811 (:clear-input (clear-input syn))
812 (:unread (unread-char arg1 syn))
814 (funcall (lisp-stream-misc syn) syn operation arg1 arg2)))
815 (stream-misc-dispatch syn operation arg1 arg2))))
819 (defstruct (two-way-stream
820 (:include lisp-stream
823 (n-bin #'two-way-n-bin)
825 (bout #'two-way-bout)
826 (sout #'two-way-sout)
827 (misc #'two-way-misc))
828 (:constructor #!-high-security-support
830 #!+high-security-support
831 %make-two-way-stream (input-stream output-stream))
833 (input-stream (required-argument) :type stream :read-only t)
834 (output-stream (required-argument) :type stream :read-only t))
835 (defprinter (two-way-stream) input-stream output-stream)
837 #!-high-security-support
838 (setf (fdocumentation 'make-two-way-stream 'function)
839 "Return a bidirectional stream which gets its input from Input-Stream and
840 sends its output to Output-Stream.")
841 #!+high-security-support
842 (defun make-two-way-stream (input-stream output-stream)
844 "Return a bidirectional stream which gets its input from Input-Stream and
845 sends its output to Output-Stream."
846 ;; FIXME: This idiom of the-real-stream-of-a-possibly-synonym-stream
847 ;; should be encapsulated in a function, and used here and most of
848 ;; the other places that SYNONYM-STREAM-P appears.
849 (unless (or (and (synonym-stream-p output-stream)
850 (output-stream-p (symbol-value
851 (synonym-stream-symbol output-stream))))
852 (output-stream-p output-stream))
855 :expected-type '(satisfies output-stream-p)))
856 (unless (or (and (synonym-stream-p input-stream)
857 (input-stream-p (symbol-value
858 (synonym-stream-symbol input-stream))))
859 (input-stream-p input-stream))
862 :expected-type '(satisfies input-stream-p)))
863 (funcall #'%make-two-way-stream input-stream output-stream))
865 (macrolet ((out-fun (name slot stream-method &rest args)
866 `(defun ,name (stream ,@args)
867 (let ((syn (two-way-stream-output-stream stream)))
868 (if (lisp-stream-p syn)
869 (funcall (,slot syn) syn ,@args)
870 (,stream-method syn ,@args))))))
871 (out-fun two-way-out lisp-stream-out stream-write-char ch)
872 (out-fun two-way-bout lisp-stream-bout stream-write-byte n)
873 (out-fun two-way-sout lisp-stream-sout stream-write-string string start end))
875 (macrolet ((in-fun (name fun &rest args)
876 `(defun ,name (stream ,@args)
877 (force-output (two-way-stream-output-stream stream))
878 (,fun (two-way-stream-input-stream stream) ,@args))))
879 (in-fun two-way-in read-char eof-error-p eof-value)
880 (in-fun two-way-bin read-byte eof-error-p eof-value)
881 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-error-p))
883 (defun two-way-misc (stream operation &optional arg1 arg2)
884 (let* ((in (two-way-stream-input-stream stream))
885 (out (two-way-stream-output-stream stream))
886 (in-lisp-stream-p (lisp-stream-p in))
887 (out-lisp-stream-p (lisp-stream-p out)))
891 (or (/= (the fixnum (lisp-stream-in-index in)) +in-buffer-length+)
892 (funcall (lisp-stream-misc in) in :listen))
894 ((:finish-output :force-output :clear-output)
895 (if out-lisp-stream-p
896 (funcall (lisp-stream-misc out) out operation arg1 arg2)
897 (stream-misc-dispatch out operation arg1 arg2)))
898 (:clear-input (clear-input in))
899 (:unread (unread-char arg1 in))
901 (let ((in-type (stream-element-type in))
902 (out-type (stream-element-type out)))
903 (if (equal in-type out-type)
904 in-type `(and ,in-type ,out-type))))
906 (set-closed-flame stream))
908 (or (if in-lisp-stream-p
909 (funcall (lisp-stream-misc in) in operation arg1 arg2)
910 (stream-misc-dispatch in operation arg1 arg2))
911 (if out-lisp-stream-p
912 (funcall (lisp-stream-misc out) out operation arg1 arg2)
913 (stream-misc-dispatch out operation arg1 arg2)))))))
915 ;;;; concatenated streams
917 (defstruct (concatenated-stream
918 (:include lisp-stream
919 (in #'concatenated-in)
920 (bin #'concatenated-bin)
921 (n-bin #'concatenated-n-bin)
922 (misc #'concatenated-misc))
924 #!-high-security-support make-concatenated-stream
925 #!+high-security-support %make-concatenated-stream
926 (&rest streams &aux (current streams)))
928 ;; The car of this is the substream we are reading from now.
930 ;; This is a list of all the substreams there ever were. We need to
931 ;; remember them so that we can close them.
933 ;; FIXME: ANSI says this is supposed to be the list of streams that
934 ;; we still have to read from. So either this needs to become a
935 ;; private member %STREAM (with CONCATENATED-STREAM-STREAMS a wrapper
936 ;; around it which discards closed files from the head of the list)
937 ;; or we need to update it as we run out of files.
938 (streams nil :type list :read-only t))
939 (def!method print-object ((x concatenated-stream) stream)
940 (print-unreadable-object (x stream :type t :identity t)
943 (concatenated-stream-streams x))))
945 #!-high-security-support
946 (setf (fdocumentation 'make-concatenated-stream 'function)
947 "Returns a stream which takes its input from each of the Streams in turn,
948 going on to the next at EOF.")
950 #!+high-security-support
951 (defun make-concatenated-stream (&rest streams)
953 "Returns a stream which takes its input from each of the Streams in turn,
954 going on to the next at EOF."
955 (dolist (stream streams)
956 (unless (or (and (synonym-stream-p stream)
957 (input-stream-p (symbol-value
958 (synonym-stream-symbol stream))))
959 (input-stream-p stream))
962 :expected-type '(satisfies input-stream-p))))
963 (apply #'%make-concatenated-stream streams))
965 (macrolet ((in-fun (name fun)
966 `(defun ,name (stream eof-error-p eof-value)
967 (do ((current (concatenated-stream-current stream)
970 (eof-or-lose stream eof-error-p eof-value))
971 (let* ((stream (car current))
972 (result (,fun stream nil nil)))
973 (when result (return result)))
974 (setf (concatenated-stream-current stream) current)))))
975 (in-fun concatenated-in read-char)
976 (in-fun concatenated-bin read-byte))
978 (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
979 (do ((current (concatenated-stream-current stream) (cdr current))
980 (current-start start)
981 (remaining-bytes numbytes))
984 (error 'end-of-file :stream stream)
985 (- numbytes remaining-bytes)))
986 (let* ((stream (car current))
987 (bytes-read (read-n-bytes stream buffer current-start
988 remaining-bytes nil)))
989 (incf current-start bytes-read)
990 (decf remaining-bytes bytes-read)
991 (when (zerop remaining-bytes) (return numbytes)))
992 (setf (concatenated-stream-current stream) (cdr current))))
994 (defun concatenated-misc (stream operation &optional arg1 arg2)
995 (let ((left (concatenated-stream-current stream)))
997 (let* ((current (car left)))
1001 (let ((stuff (if (lisp-stream-p current)
1002 (funcall (lisp-stream-misc current) current
1004 (stream-misc-dispatch current :listen))))
1005 (cond ((eq stuff :eof)
1006 ;; Advance CURRENT, and try again.
1007 (pop (concatenated-stream-current stream))
1009 (car (concatenated-stream-current stream)))
1011 ;; No further streams. EOF.
1014 ;; Stuff's available.
1017 ;; Nothing is available yet.
1019 (:clear-input (clear-input current))
1020 (:unread (unread-char arg1 current))
1022 (set-closed-flame stream))
1024 (if (lisp-stream-p current)
1025 (funcall (lisp-stream-misc current) current operation arg1 arg2)
1026 (stream-misc-dispatch current operation arg1 arg2))))))))
1030 (defstruct (echo-stream
1031 (:include two-way-stream
1036 (:constructor make-echo-stream (input-stream output-stream))
1039 (def!method print-object ((x echo-stream) stream)
1040 (print-unreadable-object (x stream :type t :identity t)
1042 ":INPUT-STREAM ~S :OUTPUT-STREAM ~S"
1043 (two-way-stream-input-stream x)
1044 (two-way-stream-output-stream x))))
1046 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
1047 `(defun ,name (stream ,@args)
1048 (or (pop (echo-stream-unread-stuff stream))
1049 (let* ((in (echo-stream-input-stream stream))
1050 (out (echo-stream-output-stream stream))
1051 (result (,fun in ,@args)))
1052 (if (lisp-stream-p out)
1053 (funcall (,out-slot out) out result)
1054 (,stream-method out result))
1056 (in-fun echo-in read-char lisp-stream-out stream-write-char
1057 eof-error-p eof-value)
1058 (in-fun echo-bin read-byte lisp-stream-bout stream-write-byte
1059 eof-error-p eof-value))
1061 (defun echo-misc (stream operation &optional arg1 arg2)
1062 (let* ((in (two-way-stream-input-stream stream))
1063 (out (two-way-stream-output-stream stream)))
1066 (or (not (null (echo-stream-unread-stuff stream)))
1067 (if (lisp-stream-p in)
1068 (or (/= (the fixnum (lisp-stream-in-index in))
1070 (funcall (lisp-stream-misc in) in :listen))
1071 (stream-misc-dispatch in :listen))))
1072 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1074 (let ((in-type (stream-element-type in))
1075 (out-type (stream-element-type out)))
1076 (if (equal in-type out-type)
1077 in-type `(and ,in-type ,out-type))))
1079 (set-closed-flame stream))
1081 (or (if (lisp-stream-p in)
1082 (funcall (lisp-stream-misc in) in operation arg1 arg2)
1083 (stream-misc-dispatch in operation arg1 arg2))
1084 (if (lisp-stream-p out)
1085 (funcall (lisp-stream-misc out) out operation arg1 arg2)
1086 (stream-misc-dispatch out operation arg1 arg2)))))))
1089 (setf (fdocumentation 'make-echo-stream 'function)
1090 "Returns a bidirectional stream which gets its input from Input-Stream and
1091 sends its output to Output-Stream. In addition, all input is echoed to
1094 ;;;; string input streams
1096 (defstruct (string-input-stream
1097 (:include lisp-stream
1099 (bin #'string-binch)
1100 (n-bin #'string-stream-read-n-bytes)
1101 (misc #'string-in-misc))
1102 (:constructor internal-make-string-input-stream
1103 (string current end))
1105 (string nil :type simple-string)
1106 (current nil :type index)
1107 (end nil :type index))
1109 (defun string-inch (stream eof-error-p eof-value)
1110 (let ((string (string-input-stream-string stream))
1111 (index (string-input-stream-current stream)))
1112 (declare (simple-string string) (fixnum index))
1113 (cond ((= index (the index (string-input-stream-end stream)))
1114 (eof-or-lose stream eof-error-p eof-value))
1116 (setf (string-input-stream-current stream) (1+ index))
1117 (aref string index)))))
1119 (defun string-binch (stream eof-error-p eof-value)
1120 (let ((string (string-input-stream-string stream))
1121 (index (string-input-stream-current stream)))
1122 (declare (simple-string string)
1124 (cond ((= index (the index (string-input-stream-end stream)))
1125 (eof-or-lose stream eof-error-p eof-value))
1127 (setf (string-input-stream-current stream) (1+ index))
1128 (char-code (aref string index))))))
1130 (defun string-stream-read-n-bytes (stream buffer start requested eof-error-p)
1131 (declare (type string-input-stream stream)
1132 (type index start requested))
1133 (let* ((string (string-input-stream-string stream))
1134 (index (string-input-stream-current stream))
1135 (available (- (string-input-stream-end stream) index))
1136 (copy (min available requested)))
1137 (declare (simple-string string)
1138 (type index index available copy))
1140 (setf (string-input-stream-current stream)
1141 (truly-the index (+ index copy)))
1142 (sb!sys:without-gcing
1143 (system-area-copy (vector-sap string)
1144 (* index sb!vm:byte-bits)
1145 (if (typep buffer 'system-area-pointer)
1147 (vector-sap buffer))
1148 (* start sb!vm:byte-bits)
1149 (* copy sb!vm:byte-bits))))
1150 (if (and (> requested copy) eof-error-p)
1151 (error 'end-of-file :stream stream)
1154 (defun string-in-misc (stream operation &optional arg1 arg2)
1155 (declare (ignore arg2))
1159 (setf (string-input-stream-current stream) arg1)
1160 (string-input-stream-current stream)))
1161 (:file-length (length (string-input-stream-string stream)))
1162 (:unread (decf (string-input-stream-current stream)))
1163 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1164 (the fixnum (string-input-stream-end stream)))
1166 (:element-type 'base-char)))
1168 (defun make-string-input-stream (string &optional
1169 (start 0) (end (length string)))
1171 "Returns an input stream which will supply the characters of String between
1172 Start and End in order."
1173 (declare (type string string)
1175 (type (or index null) end))
1178 (when (> end (length string))
1179 (cerror "Continue with end changed from ~S to ~S"
1180 "Write-string: end (~S) is larger then the length of the string (~S)"
1181 end (1- (length string))))
1183 (internal-make-string-input-stream (coerce string 'simple-string)
1186 ;;;; string output streams
1188 (defstruct (string-output-stream
1189 (:include lisp-stream
1191 (sout #'string-sout)
1192 (misc #'string-out-misc))
1193 (:constructor make-string-output-stream ())
1195 ;; The string we throw stuff in.
1196 (string (make-string 40) :type simple-string)
1197 ;; Index of the next location to use.
1198 (index 0 :type fixnum))
1201 (setf (fdocumentation 'make-string-output-stream 'function)
1202 "Returns an Output stream which will accumulate all output given it for
1203 the benefit of the function Get-Output-Stream-String.")
1205 (defun string-ouch (stream character)
1206 (let ((current (string-output-stream-index stream))
1207 (workspace (string-output-stream-string stream)))
1208 (declare (simple-string workspace) (fixnum current))
1209 (if (= current (the fixnum (length workspace)))
1210 (let ((new-workspace (make-string (* current 2))))
1211 (replace new-workspace workspace)
1212 (setf (aref new-workspace current) character)
1213 (setf (string-output-stream-string stream) new-workspace))
1214 (setf (aref workspace current) character))
1215 (setf (string-output-stream-index stream) (1+ current))))
1217 (defun string-sout (stream string start end)
1218 (declare (simple-string string) (fixnum start end))
1219 (let* ((current (string-output-stream-index stream))
1220 (length (- end start))
1221 (dst-end (+ length current))
1222 (workspace (string-output-stream-string stream)))
1223 (declare (simple-string workspace)
1224 (fixnum current length dst-end))
1225 (if (> dst-end (the fixnum (length workspace)))
1226 (let ((new-workspace (make-string (+ (* current 2) length))))
1227 (replace new-workspace workspace :end2 current)
1228 (replace new-workspace string
1229 :start1 current :end1 dst-end
1230 :start2 start :end2 end)
1231 (setf (string-output-stream-string stream) new-workspace))
1232 (replace workspace string
1233 :start1 current :end1 dst-end
1234 :start2 start :end2 end))
1235 (setf (string-output-stream-index stream) dst-end)))
1237 (defun string-out-misc (stream operation &optional arg1 arg2)
1238 (declare (ignore arg2))
1242 (string-output-stream-index stream)))
1244 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1246 (count 0 (1+ count))
1247 (string (string-output-stream-string stream)))
1249 (declare (simple-string string)
1250 (fixnum index count))
1251 (if (char= (schar string index) #\newline)
1253 (:element-type 'base-char)))
1255 ;;; Return a string of all the characters sent to a stream made by
1256 ;;; MAKE-STRING-OUTPUT-STREAM since the last call to this function.
1257 (defun get-output-stream-string (stream)
1258 (declare (type string-output-stream stream))
1259 (let* ((length (string-output-stream-index stream))
1260 (result (make-string length)))
1261 (replace result (string-output-stream-string stream))
1262 (setf (string-output-stream-index stream) 0)
1265 ;;; Dump the characters buffer up in IN-STREAM to OUT-STREAM as
1266 ;;; GET-OUTPUT-STREAM-STRING would return them.
1267 (defun dump-output-stream-string (in-stream out-stream)
1268 (write-string* (string-output-stream-string in-stream) out-stream
1269 0 (string-output-stream-index in-stream))
1270 (setf (string-output-stream-index in-stream) 0))
1272 ;;;; fill-pointer streams
1274 ;;; Fill pointer STRING-OUTPUT-STREAMs are not explicitly mentioned in
1275 ;;; the CLM, but they are required for the implementation of
1276 ;;; WITH-OUTPUT-TO-STRING.
1278 (defstruct (fill-pointer-output-stream
1279 (:include lisp-stream
1280 (out #'fill-pointer-ouch)
1281 (sout #'fill-pointer-sout)
1282 (misc #'fill-pointer-misc))
1283 (:constructor make-fill-pointer-output-stream (string))
1285 ;; the string we throw stuff in
1288 (defun fill-pointer-ouch (stream character)
1289 (let* ((buffer (fill-pointer-output-stream-string stream))
1290 (current (fill-pointer buffer))
1291 (current+1 (1+ current)))
1292 (declare (fixnum current))
1293 (with-array-data ((workspace buffer) (start) (end))
1294 (declare (simple-string workspace))
1295 (let ((offset-current (+ start current)))
1296 (declare (fixnum offset-current))
1297 (if (= offset-current end)
1298 (let* ((new-length (1+ (* current 2)))
1299 (new-workspace (make-string new-length)))
1300 (declare (simple-string new-workspace))
1301 (%byte-blt workspace start
1302 new-workspace 0 current)
1303 (setf workspace new-workspace)
1304 (setf offset-current current)
1305 (set-array-header buffer workspace new-length
1306 current+1 0 new-length nil))
1307 (setf (fill-pointer buffer) current+1))
1308 (setf (schar workspace offset-current) character)))
1311 (defun fill-pointer-sout (stream string start end)
1312 (declare (simple-string string) (fixnum start end))
1313 (let* ((buffer (fill-pointer-output-stream-string stream))
1314 (current (fill-pointer buffer))
1315 (string-len (- end start))
1316 (dst-end (+ string-len current)))
1317 (declare (fixnum current dst-end string-len))
1318 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1319 (declare (simple-string workspace))
1320 (let ((offset-dst-end (+ dst-start dst-end))
1321 (offset-current (+ dst-start current)))
1322 (declare (fixnum offset-dst-end offset-current))
1323 (if (> offset-dst-end dst-length)
1324 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1325 (new-workspace (make-string new-length)))
1326 (declare (simple-string new-workspace))
1327 (%byte-blt workspace dst-start
1328 new-workspace 0 current)
1329 (setf workspace new-workspace)
1330 (setf offset-current current)
1331 (setf offset-dst-end dst-end)
1332 (set-array-header buffer
1339 (setf (fill-pointer buffer) dst-end))
1340 (%byte-blt string start
1341 workspace offset-current offset-dst-end)))
1344 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1345 (declare (ignore arg1 arg2))
1348 (let* ((buffer (fill-pointer-output-stream-string stream))
1349 (current (fill-pointer buffer)))
1350 (with-array-data ((string buffer) (start) (end current))
1351 (declare (simple-string string) (ignore start))
1352 (let ((found (position #\newline string :test #'char=
1353 :end end :from-end t)))
1355 (- end (the fixnum found))
1357 (:element-type 'base-char)))
1359 ;;;; indenting streams
1361 (defstruct (indenting-stream (:include lisp-stream
1362 (out #'indenting-out)
1363 (sout #'indenting-sout)
1364 (misc #'indenting-misc))
1365 (:constructor make-indenting-stream (stream))
1367 ;; the stream we're based on
1369 ;; how much we indent on each line
1373 (setf (fdocumentation 'make-indenting-stream 'function)
1374 "Returns an output stream which indents its output by some amount.")
1376 ;;; INDENTING-INDENT writes the correct number of spaces needed to indent
1377 ;;; output on the given STREAM based on the specified SUB-STREAM.
1378 (defmacro indenting-indent (stream sub-stream)
1379 ;; KLUDGE: bare magic number 60
1380 `(do ((i 0 (+ i 60))
1381 (indentation (indenting-stream-indentation ,stream)))
1382 ((>= i indentation))
1385 ,sub-stream 0 (min 60 (- indentation i)))))
1387 ;;; INDENTING-OUT writes a character to an indenting stream.
1388 (defun indenting-out (stream char)
1389 (let ((sub-stream (indenting-stream-stream stream)))
1390 (write-char char sub-stream)
1391 (if (char= char #\newline)
1392 (indenting-indent stream sub-stream))))
1394 ;;; INDENTING-SOUT writes a string to an indenting stream.
1395 (defun indenting-sout (stream string start end)
1396 (declare (simple-string string) (fixnum start end))
1398 (sub-stream (indenting-stream-stream stream)))
1400 (let ((newline (position #\newline string :start i :end end)))
1402 (write-string* string sub-stream i (1+ newline))
1403 (indenting-indent stream sub-stream)
1404 (setq i (+ newline 1)))
1406 (write-string* string sub-stream i end)
1409 ;;; INDENTING-MISC just treats just the :LINE-LENGTH message
1410 ;;; differently. INDENTING-CHARPOS says the charpos is the charpos of
1411 ;;; the base stream minus the stream's indentation.
1412 (defun indenting-misc (stream operation &optional arg1 arg2)
1413 (let ((sub-stream (indenting-stream-stream stream)))
1414 (if (lisp-stream-p sub-stream)
1415 (let ((method (lisp-stream-misc sub-stream)))
1418 (let ((line-length (funcall method sub-stream operation)))
1420 (- line-length (indenting-stream-indentation stream)))))
1422 (let ((charpos (funcall method sub-stream operation)))
1424 (- charpos (indenting-stream-indentation stream)))))
1426 (funcall method sub-stream operation arg1 arg2))))
1427 ;; must be Gray streams FUNDAMENTAL-STREAM
1430 (let ((line-length (stream-line-length sub-stream)))
1432 (- line-length (indenting-stream-indentation stream)))))
1434 (let ((charpos (stream-line-column sub-stream)))
1436 (- charpos (indenting-stream-indentation stream)))))
1438 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1440 (declaim (maybe-inline read-char unread-char read-byte listen))
1442 ;;;; case frobbing streams, used by FORMAT ~(...~)
1444 (defstruct (case-frob-stream
1445 (:include lisp-stream
1446 (:misc #'case-frob-misc))
1447 (:constructor %make-case-frob-stream (target out sout))
1449 (target (required-argument) :type stream))
1451 (defun make-case-frob-stream (target kind)
1453 "Returns a stream that sends all output to the stream TARGET, but modifies
1454 the case of letters, depending on KIND, which should be one of:
1455 :upcase - convert to upper case.
1456 :downcase - convert to lower case.
1457 :capitalize - convert the first letter of words to upper case and the
1458 rest of the word to lower case.
1459 :capitalize-first - convert the first letter of the first word to upper
1460 case and everything else to lower case."
1461 (declare (type stream target)
1462 (type (member :upcase :downcase :capitalize :capitalize-first)
1465 (if (case-frob-stream-p target)
1466 ;; If we are going to be writing to a stream that already does
1467 ;; case frobbing, why bother frobbing the case just so it can
1470 (multiple-value-bind (out sout)
1473 (values #'case-frob-upcase-out
1474 #'case-frob-upcase-sout))
1476 (values #'case-frob-downcase-out
1477 #'case-frob-downcase-sout))
1479 (values #'case-frob-capitalize-out
1480 #'case-frob-capitalize-sout))
1482 (values #'case-frob-capitalize-first-out
1483 #'case-frob-capitalize-first-sout)))
1484 (%make-case-frob-stream target out sout))))
1486 (defun case-frob-misc (stream op &optional arg1 arg2)
1487 (declare (type case-frob-stream stream))
1491 (let ((target (case-frob-stream-target stream)))
1492 (if (lisp-stream-p target)
1493 (funcall (lisp-stream-misc target) target op arg1 arg2)
1494 (stream-misc-dispatch target op arg1 arg2))))))
1496 (defun case-frob-upcase-out (stream char)
1497 (declare (type case-frob-stream stream)
1498 (type base-char char))
1499 (let ((target (case-frob-stream-target stream))
1500 (char (char-upcase char)))
1501 (if (lisp-stream-p target)
1502 (funcall (lisp-stream-out target) target char)
1503 (stream-write-char target char))))
1505 (defun case-frob-upcase-sout (stream str start end)
1506 (declare (type case-frob-stream stream)
1507 (type simple-base-string str)
1509 (type (or index null) end))
1510 (let* ((target (case-frob-stream-target stream))
1513 (string (if (and (zerop start) (= len end))
1515 (nstring-upcase (subseq str start end))))
1516 (string-len (- end start)))
1517 (if (lisp-stream-p target)
1518 (funcall (lisp-stream-sout target) target string 0 string-len)
1519 (stream-write-string target string 0 string-len))))
1521 (defun case-frob-downcase-out (stream char)
1522 (declare (type case-frob-stream stream)
1523 (type base-char char))
1524 (let ((target (case-frob-stream-target stream))
1525 (char (char-downcase char)))
1526 (if (lisp-stream-p target)
1527 (funcall (lisp-stream-out target) target char)
1528 (stream-write-char target char))))
1530 (defun case-frob-downcase-sout (stream str start end)
1531 (declare (type case-frob-stream stream)
1532 (type simple-base-string str)
1534 (type (or index null) end))
1535 (let* ((target (case-frob-stream-target stream))
1538 (string (if (and (zerop start) (= len end))
1539 (string-downcase str)
1540 (nstring-downcase (subseq str start end))))
1541 (string-len (- end start)))
1542 (if (lisp-stream-p target)
1543 (funcall (lisp-stream-sout target) target string 0 string-len)
1544 (stream-write-string target string 0 string-len))))
1546 (defun case-frob-capitalize-out (stream char)
1547 (declare (type case-frob-stream stream)
1548 (type base-char char))
1549 (let ((target (case-frob-stream-target stream)))
1550 (cond ((alphanumericp char)
1551 (let ((char (char-upcase char)))
1552 (if (lisp-stream-p target)
1553 (funcall (lisp-stream-out target) target char)
1554 (stream-write-char target char)))
1555 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1556 (setf (case-frob-stream-sout stream)
1557 #'case-frob-capitalize-aux-sout))
1559 (if (lisp-stream-p target)
1560 (funcall (lisp-stream-out target) target char)
1561 (stream-write-char target char))))))
1563 (defun case-frob-capitalize-sout (stream str start end)
1564 (declare (type case-frob-stream stream)
1565 (type simple-base-string str)
1567 (type (or index null) end))
1568 (let* ((target (case-frob-stream-target stream))
1569 (str (subseq str start end))
1573 (let ((char (schar str i)))
1574 (cond ((not (alphanumericp char))
1575 (setf inside-word nil))
1577 (setf (schar str i) (char-downcase char)))
1579 (setf inside-word t)
1580 (setf (schar str i) (char-upcase char))))))
1582 (setf (case-frob-stream-out stream)
1583 #'case-frob-capitalize-aux-out)
1584 (setf (case-frob-stream-sout stream)
1585 #'case-frob-capitalize-aux-sout))
1586 (if (lisp-stream-p target)
1587 (funcall (lisp-stream-sout target) target str 0 len)
1588 (stream-write-string target str 0 len))))
1590 (defun case-frob-capitalize-aux-out (stream char)
1591 (declare (type case-frob-stream stream)
1592 (type base-char char))
1593 (let ((target (case-frob-stream-target stream)))
1594 (cond ((alphanumericp char)
1595 (let ((char (char-downcase char)))
1596 (if (lisp-stream-p target)
1597 (funcall (lisp-stream-out target) target char)
1598 (stream-write-char target char))))
1600 (if (lisp-stream-p target)
1601 (funcall (lisp-stream-out target) target char)
1602 (stream-write-char target char))
1603 (setf (case-frob-stream-out stream)
1604 #'case-frob-capitalize-out)
1605 (setf (case-frob-stream-sout stream)
1606 #'case-frob-capitalize-sout)))))
1608 (defun case-frob-capitalize-aux-sout (stream str start end)
1609 (declare (type case-frob-stream stream)
1610 (type simple-base-string str)
1612 (type (or index null) end))
1613 (let* ((target (case-frob-stream-target stream))
1614 (str (subseq str start end))
1618 (let ((char (schar str i)))
1619 (cond ((not (alphanumericp char))
1620 (setf inside-word nil))
1622 (setf (schar str i) (char-downcase char)))
1624 (setf inside-word t)
1625 (setf (schar str i) (char-upcase char))))))
1627 (setf (case-frob-stream-out stream)
1628 #'case-frob-capitalize-out)
1629 (setf (case-frob-stream-sout stream)
1630 #'case-frob-capitalize-sout))
1631 (if (lisp-stream-p target)
1632 (funcall (lisp-stream-sout target) target str 0 len)
1633 (stream-write-string target str 0 len))))
1635 (defun case-frob-capitalize-first-out (stream char)
1636 (declare (type case-frob-stream stream)
1637 (type base-char char))
1638 (let ((target (case-frob-stream-target stream)))
1639 (cond ((alphanumericp char)
1640 (let ((char (char-upcase char)))
1641 (if (lisp-stream-p target)
1642 (funcall (lisp-stream-out target) target char)
1643 (stream-write-char target char)))
1644 (setf (case-frob-stream-out stream)
1645 #'case-frob-downcase-out)
1646 (setf (case-frob-stream-sout stream)
1647 #'case-frob-downcase-sout))
1649 (if (lisp-stream-p target)
1650 (funcall (lisp-stream-out target) target char)
1651 (stream-write-char target char))))))
1653 (defun case-frob-capitalize-first-sout (stream str start end)
1654 (declare (type case-frob-stream stream)
1655 (type simple-base-string str)
1657 (type (or index null) end))
1658 (let* ((target (case-frob-stream-target stream))
1659 (str (subseq str start end))
1662 (let ((char (schar str i)))
1663 (when (alphanumericp char)
1664 (setf (schar str i) (char-upcase char))
1665 (do ((i (1+ i) (1+ i)))
1667 (setf (schar str i) (char-downcase (schar str i))))
1668 (setf (case-frob-stream-out stream)
1669 #'case-frob-downcase-out)
1670 (setf (case-frob-stream-sout stream)
1671 #'case-frob-downcase-sout)
1673 (if (lisp-stream-p target)
1674 (funcall (lisp-stream-sout target) target str 0 len)
1675 (stream-write-string target str 0 len))))
1677 ;;;; stream commands
1679 (defstruct (stream-command (:constructor make-stream-command
1680 (name &optional args))
1682 (name nil :type symbol)
1683 (args nil :type list))
1684 (def!method print-object ((obj stream-command) str)
1685 (print-unreadable-object (obj str :type t :identity t)
1686 (prin1 (stream-command-name obj) str)))
1688 ;;; Take a stream and wait for text or a command to appear on it. If
1689 ;;; text appears before a command, return NIL, otherwise return a
1692 ;;; We can't simply call the stream's misc method because NIL is an
1693 ;;; ambiguous return value: does it mean text arrived, or does it mean
1694 ;;; the stream's misc method had no :GET-COMMAND implementation? We
1695 ;;; can't return NIL until there is text input. We don't need to loop
1696 ;;; because any stream implementing :GET-COMMAND would wait until it
1697 ;;; had some input. If the LISTEN fails, then we have some stream we
1699 (defun get-stream-command (stream)
1700 (let ((cmdp (funcall (lisp-stream-misc stream) stream :get-command)))
1705 ;; This waits for input and returns NIL when it arrives.
1706 (unread-char (read-char stream) stream)))))
1708 (defun read-sequence (seq stream &key (start 0) (end nil))
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 (let ((end (or end (length seq))))
1722 (declare (type index end))
1725 (let ((read-function
1726 (if (subtypep (stream-element-type stream) 'character)
1729 (do ((rem (nthcdr start seq) (rest rem))
1731 ((or (endp rem) (>= i end)) i)
1732 (declare (type list rem)
1734 (let ((el (funcall read-function stream nil :eof)))
1737 (setf (first rem) el)))))
1739 (with-array-data ((data seq) (offset-start start) (offset-end end))
1741 ((or (simple-array (unsigned-byte 8) (*))
1742 (simple-array (signed-byte 8) (*))
1744 (let* ((numbytes (- end start))
1745 (bytes-read (sb!sys:read-n-bytes stream
1750 (if (< bytes-read numbytes)
1751 (+ start bytes-read)
1754 (let ((read-function
1755 (if (subtypep (stream-element-type stream) 'character)
1758 (do ((i offset-start (1+ i)))
1759 ((>= i offset-end) end)
1760 (declare (type index i))
1761 (let ((el (funcall read-function stream nil :eof)))
1763 (return (+ start (- i offset-start))))
1764 (setf (aref data i) el)))))))))))
1766 (defun write-sequence (seq stream &key (start 0) (end nil))
1768 "Write the elements of SEQ bounded by START and END to STREAM."
1769 (declare (type sequence seq)
1770 (type stream stream)
1772 (type sequence-end end)
1774 (let ((end (or end (length seq))))
1775 (declare (type index start end))
1778 (let ((write-function
1779 (if (subtypep (stream-element-type stream) 'character)
1782 (do ((rem (nthcdr start seq) (rest rem))
1784 ((or (endp rem) (>= i end)) seq)
1785 (declare (type list rem)
1787 (funcall write-function (first rem) stream))))
1789 (write-string* seq stream start end))
1791 (let ((write-function
1792 (if (subtypep (stream-element-type stream) 'character)
1795 (do ((i start (1+ i)))
1797 (declare (type index i))
1798 (funcall write-function (aref seq i) stream)))))))
1800 ;;; (These were inline throughout this file, but that's not appropriate
1802 (declaim (maybe-inline read-char unread-char read-byte listen))