3 ;;; **********************************************************************
4 ;;; This code was written by Paul Foley and has been placed in the public
8 ;;; Sbcl port by Rudi Schlatte.
10 (in-package "SB-SIMPLE-STREAMS")
13 ;;; **********************************************************************
15 ;;; Implementations of standard Common Lisp functions for simple-streams
17 (defun %uninitialized (stream)
18 (error "~S has not been initialized." stream))
20 (defun %check (stream kind)
21 (declare (type simple-stream stream)
22 (optimize (speed 3) (space 1) (debug 0) (safety 0)))
23 (with-stream-class (simple-stream stream)
24 (cond ((not (any-stream-instance-flags stream :simple))
25 (%uninitialized stream))
27 (not (any-stream-instance-flags stream :input :output)))
28 (sb-kernel:closed-flame stream))
29 ((and (or (eq kind :input) (eq kind :io))
30 (not (any-stream-instance-flags stream :input)))
31 (sb-kernel:ill-in stream))
32 ((and (or (eq kind :output) (eq kind :io))
33 (not (any-stream-instance-flags stream :output)))
34 (sb-kernel:ill-out stream)))))
36 (defmethod input-stream-p ((stream simple-stream))
37 (any-stream-instance-flags stream :input))
39 (defmethod output-stream-p ((stream simple-stream))
40 (any-stream-instance-flags stream :output))
42 (defmethod open-stream-p ((stream simple-stream))
43 (any-stream-instance-flags stream :input :output))
45 ;;; From the simple-streams documentation: "A generic function implies
46 ;;; a specialization capability that does not exist for
47 ;;; simple-streams; simple-stream specializations should be on
48 ;;; device-close." So don't do it.
49 (defmethod close ((stream simple-stream) &key abort)
50 (device-close stream abort))
52 (defun %file-position (stream position)
53 (declare (type simple-stream stream)
54 (type (or (integer 0 *) (member nil :start :end)) position))
55 (with-stream-class (simple-stream stream)
58 ;; Adjust current position
59 (let ((position (case position (:start 0) (:end -1)
60 (otherwise position))))
62 (single-channel-simple-stream
63 (when (sc-dirty-p stream)
64 (flush-buffer stream t)))
65 (dual-channel-simple-stream
66 (with-stream-class (dual-channel-simple-stream stream)
67 (when (> (sm outpos stream) 0)
68 (device-write stream :flush 0 nil t))))
72 (setf (sm last-char-read-size stream) 0)
73 (setf (sm buffpos stream) 0 ; set pointer to 0 to force a read
74 (sm buffer-ptr stream) 0)
75 (setf (sm charpos stream) nil)
76 (remove-stream-instance-flags stream :eof)
77 (setf (device-file-position stream) position))
78 ;; Just report current position
79 (let ((posn (device-file-position stream)))
81 (when (sm handler stream)
82 (dolist (queued (sm pending stream))
83 (incf posn (- (the sb-int:index (third queued))
84 (the sb-int:index (second queued))))))
86 (single-channel-simple-stream
87 (case (sm mode stream)
88 ((0 3) ; read, read-modify
89 ;; Note that posn can increase here if we wrote
90 ;; past the end of previously-read data
91 (decf posn (- (sm buffer-ptr stream) (sm buffpos stream))))
93 (incf posn (sm buffpos stream)))))
94 (dual-channel-simple-stream
95 (with-stream-class (dual-channel-simple-stream stream)
96 (incf posn (sm outpos stream))
97 (when (>= (sm buffer-ptr stream) 0)
98 (decf posn (- (sm buffer-ptr stream) (sm buffpos stream))))))
103 (defun %file-length (stream)
104 (declare (type simple-stream stream))
105 (%check stream :open)
106 (device-file-length stream))
109 (defun %file-name (stream)
110 (declare (type simple-stream stream))
114 (with-stream-class (file-simple-stream stream)
115 (sm pathname stream)))
117 (with-stream-class (probe-simple-stream stream)
118 (sm pathname stream)))
123 (defun %file-rename (stream new-name)
124 (declare (type simple-stream stream))
126 (if (typep stream 'file-simple-stream)
127 (with-stream-class (file-simple-stream stream)
128 (setf (sm pathname stream) new-name)
129 (setf (sm filename stream) (sb-int:unix-namestring new-name nil))
134 (defun %file-string-length (stream object)
135 (declare (type simple-stream stream))
136 (with-stream-class (simple-stream stream)
137 (%check stream :output)
138 ;; FIXME: need to account for compositions on the stream...
141 (declare (ignore octet))
146 (char-to-octets (sm external-format stream) object x #'fn)))
149 (ef (sm external-format stream)))
150 (dotimes (i (length object))
151 (declare (type sb-int:index i))
152 (char-to-octets ef (char object i) x #'fn))))))
156 (defun %read-line (stream eof-error-p eof-value recursive-p)
157 (declare (optimize (speed 3) (space 1) (safety 0) (debug 0))
158 (type simple-stream stream)
159 (ignore recursive-p))
160 (with-stream-class (simple-stream stream)
161 (%check stream :input)
162 (when (any-stream-instance-flags stream :eof)
163 (return-from %read-line
164 (sb-impl::eof-or-lose stream eof-error-p eof-value)))
165 ;; for interactive streams, finish output first to force prompt
166 (when (and (any-stream-instance-flags stream :output)
167 (any-stream-instance-flags stream :interactive))
168 (%finish-output stream))
169 (let* ((encap (sm melded-stream stream)) ; encapsulating stream
170 (cbuf (make-string 80)) ; current buffer
171 (bufs (list cbuf)) ; list of buffers
172 (tail bufs) ; last cons of bufs list
173 (index 0) ; current index in current buffer
174 (total 0)) ; total characters
175 (declare (type simple-stream encap)
176 (type simple-string cbuf)
177 (type cons bufs tail)
178 (type sb-int:index index total))
180 (multiple-value-bind (chars done)
181 (funcall-stm-handler j-read-chars encap cbuf
182 #\Newline index (length cbuf) t)
183 (declare (type sb-int:index chars))
186 (when (and (eq done :eof) (zerop total))
188 (error 'end-of-file :stream stream)
189 (return (values eof-value t))))
191 ;; If there's only one buffer in use, return it directly
192 (when (null (cdr bufs))
193 (return (values (sb-kernel:shrink-vector cbuf total)
195 ;; If total fits in final buffer, use it
196 (when (<= total (length cbuf))
197 (replace cbuf cbuf :start1 (- total index) :end2 index)
199 (declare (type sb-int:index idx))
200 (do ((list bufs (cdr list)))
202 (let ((buf (car list)))
203 (declare (type simple-string buf))
204 (replace cbuf buf :start1 idx)
205 (incf idx (length buf)))))
206 (return (values (sb-kernel:shrink-vector cbuf total)
208 ;; Allocate new string of appropriate length
209 (let ((string (make-string total))
211 (declare (type sb-int:index index))
213 (declare (type simple-string buf))
214 (replace string buf :start1 index)
215 (incf index (length buf)))
216 (return (values string (eq done :eof)))))
217 (when (>= index (length cbuf))
218 (setf cbuf (make-string (the sb-int:index (* 2 index))))
220 (setf (cdr tail) (cons cbuf nil))
221 (setf tail (cdr tail))))))))
223 (defun %read-char (stream eof-error-p eof-value recursive-p blocking-p)
224 (declare (type simple-stream stream)
225 (ignore recursive-p))
226 (with-stream-class (simple-stream stream)
227 (%check stream :input)
228 (when (any-stream-instance-flags stream :eof)
229 (return-from %read-char
230 (sb-impl::eof-or-lose stream eof-error-p eof-value)))
231 ;; for interactive streams, finish output first to force prompt
232 (when (and (any-stream-instance-flags stream :output)
233 (any-stream-instance-flags stream :interactive))
234 (%finish-output stream))
235 (funcall-stm-handler j-read-char (sm melded-stream stream)
236 eof-error-p eof-value blocking-p)))
239 (defun %unread-char (stream character)
240 (declare (type simple-stream stream) (ignore character))
241 (with-stream-class (simple-stream stream)
242 (%check stream :input)
243 (if (zerop (sm last-char-read-size stream))
244 (error "Nothing to unread.")
246 (funcall-stm-handler j-unread-char (sm melded-stream stream) nil)
247 (remove-stream-instance-flags stream :eof)
248 (setf (sm last-char-read-size stream) 0)))))
251 (defun %peek-char (stream peek-type eof-error-p eof-value recursive-p)
252 (declare (type simple-stream stream)
253 (ignore recursive-p))
254 (with-stream-class (simple-stream stream)
255 (%check stream :input)
256 (when (any-stream-instance-flags stream :eof)
257 (return-from %peek-char
258 (sb-impl::eof-or-lose stream eof-error-p eof-value)))
259 (let* ((encap (sm melded-stream stream))
260 (char (funcall-stm-handler j-read-char encap
261 eof-error-p stream t)))
262 (cond ((eq char stream) eof-value)
263 ((characterp peek-type)
264 (do ((char char (funcall-stm-handler j-read-char encap
267 ((or (eq char stream) (char= char peek-type))
268 (unless (eq char stream)
269 (funcall-stm-handler j-unread-char encap t))
270 (if (eq char stream) eof-value char))))
272 (do ((char char (funcall-stm-handler j-read-char encap
275 ((or (eq char stream)
276 (not (sb-impl::whitespace[2]p char)))
277 (unless (eq char stream)
278 (funcall-stm-handler j-unread-char encap t))
279 (if (eq char stream) eof-value char))))
281 (funcall-stm-handler j-unread-char encap t)
284 (defun %listen (stream width)
285 (declare (type simple-stream stream))
286 ;; WIDTH is number of octets which must be available; any value
287 ;; other than 1 is treated as 'character.
288 (with-stream-class (simple-stream stream)
289 (%check stream :input)
290 (when (any-stream-instance-flags stream :eof)
291 (return-from %listen nil))
292 (if (not (or (eql width 1) (null width)))
293 (funcall-stm-handler j-listen (sm melded-stream stream))
294 (or (< (sm buffpos stream) (sm buffer-ptr stream))
295 ;; Attempt buffer refill
296 (let ((lcrs (sm last-char-read-size stream)))
297 (when (and (not (any-stream-instance-flags stream :dual :string))
298 (>= (sm mode stream) 0))
299 ;; single-channel stream dirty -> write data before reading
300 (flush-buffer stream nil))
301 (>= (refill-buffer stream nil) width))))))
303 (defun %clear-input (stream buffer-only)
304 (declare (type simple-stream stream))
305 (with-stream-class (simple-stream stream)
306 (%check stream :input)
307 (setf (sm buffpos stream) 0
308 (sm buffer-ptr stream) 0
309 (sm last-char-read-size stream) 0
310 #|(sm unread-past-soft-eof stream) nil|#)
311 #| (setf (sm reread-count stream) 0) on dual-channel streams? |#
313 (device-clear-input stream buffer-only))
316 (defun %read-byte (stream eof-error-p eof-value)
317 (declare (type simple-stream stream))
318 (with-stream-class (simple-stream stream)
319 (%check stream :input)
320 (if (any-stream-instance-flags stream :eof)
321 (sb-impl::eof-or-lose stream eof-error-p eof-value)
323 (single-channel-simple-stream
324 (read-byte-internal stream eof-error-p eof-value t))
325 (dual-channel-simple-stream
326 (read-byte-internal stream eof-error-p eof-value t))
327 (string-simple-stream
328 (with-stream-class (string-simple-stream stream)
329 (let ((encap (sm input-handle stream)))
331 (error 'simple-type-error
333 :expected-type 'stream
334 :format-control "Can't read-byte on string streams"
335 :format-arguments '()))
337 (read-byte encap eof-error-p eof-value)
338 (setf (sm last-char-read-size stream) 0
339 (sm encapsulated-char-read-size stream) 0)))))))))
342 (defun %write-char (stream character)
343 (declare (type simple-stream stream))
344 (with-stream-class (simple-stream stream)
345 (%check stream :output)
346 (funcall-stm-handler-2 j-write-char character (sm melded-stream stream))))
349 (defun %fresh-line (stream)
350 (declare (type simple-stream stream))
351 (with-stream-class (simple-stream stream)
352 (%check stream :output)
353 (when (/= (or (sm charpos stream) 1) 0)
354 (funcall-stm-handler-2 j-write-char #\Newline (sm melded-stream stream))
358 (defun %write-string (stream string start end)
359 (declare (type simple-stream stream))
360 (with-stream-class (simple-stream stream)
361 (%check stream :output)
362 (funcall-stm-handler-2 j-write-chars string (sm melded-stream stream)
366 (defun %line-length (stream)
367 (declare (type simple-stream stream))
368 (%check stream :output)
373 (defun %finish-output (stream)
374 (declare (type simple-stream stream))
375 (with-stream-class (simple-stream stream)
376 (%check stream :output)
377 (when (sm handler stream)
379 ((null (sm pending stream)))
380 (sb-sys:serve-all-events)))
382 (single-channel-simple-stream
383 ;(when (and (> (sm mode stream) 0) (> (sm buffer-ptr stream) 0))
384 ; (setf (device-file-position stream)
385 ; (- (device-file-position stream) (sm buffer-ptr stream))))
386 ;(device-write stream :flush 0 nil t)
387 (flush-buffer stream t)
388 (setf (sm buffpos stream) 0))
389 (dual-channel-simple-stream
390 (with-stream-class (dual-channel-simple-stream stream)
391 (device-write stream :flush 0 nil t)
392 (setf (sm outpos stream) 0)))
393 (string-simple-stream
394 (device-write stream :flush 0 nil t))))
398 (defun %force-output (stream)
399 (declare (type simple-stream stream))
400 (with-stream-class (simple-stream stream)
401 (%check stream :output)
403 (single-channel-simple-stream
404 ;(when (> (sm buffer-ptr stream) 0)
405 ; (setf (device-file-position stream)
406 ; (- (device-file-position stream) (sm buffer-ptr stream))))
407 ;(device-write stream :flush 0 nil nil)
408 (flush-buffer stream nil)
409 (setf (sm buffpos stream) 0))
410 (dual-channel-simple-stream
411 (with-stream-class (dual-channel-simple-stream stream)
412 (device-write stream :flush 0 nil nil)
413 (setf (sm outpos stream) 0)))
414 (string-simple-stream
415 (device-write stream :flush 0 nil nil))))
419 (defun %clear-output (stream)
420 (declare (type simple-stream stream))
421 (with-stream-class (simple-stream stream)
422 (%check stream :output)
423 (when (sm handler stream)
424 (sb-sys:remove-fd-handler (sm handler stream))
425 (setf (sm handler stream) nil
426 (sm pending stream) nil))
428 (single-channel-simple-stream
429 (with-stream-class (single-channel-simple-stream stream)
430 (case (sm mode stream)
431 (1 (setf (sm buffpos stream) 0))
432 (3 (setf (sm mode stream) 0)))))
433 (dual-channel-simple-stream
434 (setf (sm outpos stream) 0))
435 (string-simple-stream
437 (device-clear-output stream)))
440 (defun %write-byte (stream integer)
441 (declare (type simple-stream stream))
442 (with-stream-class (simple-stream stream)
443 (%check stream :output)
445 (single-channel-simple-stream
446 (with-stream-class (single-channel-simple-stream stream)
447 (let ((ptr (sm buffpos stream)))
448 (when (>= ptr (sm buf-len stream))
449 (setf ptr (flush-buffer stream t)))
450 (setf (sm buffpos stream) (1+ ptr))
451 (setf (sm charpos stream) nil)
452 (setf (bref (sm buffer stream) ptr) integer)
453 (sc-set-dirty stream))))
454 (dual-channel-simple-stream
455 (with-stream-class (dual-channel-simple-stream stream)
456 (let ((ptr (sm outpos stream)))
457 (when (>= ptr (sm max-out-pos stream))
458 (setf ptr (flush-out-buffer stream t)))
459 (setf (sm outpos stream) (1+ ptr))
460 (setf (sm charpos stream) nil)
461 (setf (bref (sm out-buffer stream) ptr) integer))))
462 (string-simple-stream
463 (with-stream-class (string-simple-stream stream)
464 (let ((encap (sm output-handle stream)))
466 (error 'simple-type-error
468 :expected-type 'stream
469 :format-control "Can't write-byte on string streams."
470 :format-arguments '()))
471 (write-byte integer encap)))))))
474 (defun %read-sequence (stream seq start end partial-fill)
475 (declare (type simple-stream stream)
477 (type sb-int:index start end)
478 (type boolean partial-fill))
479 (with-stream-class (simple-stream stream)
480 (%check stream :input)
481 (when (any-stream-instance-flags stream :eof)
482 (return-from %read-sequence 0))
483 (when (and (not (any-stream-instance-flags stream :dual :string))
485 (flush-buffer stream t))
488 (funcall-stm-handler j-read-chars (sm melded-stream stream) seq nil
490 (if partial-fill :bnb t)))
491 ((or (simple-array (unsigned-byte 8) (*))
492 (simple-array (signed-byte 8) (*)))
493 (when (any-stream-instance-flags stream :string)
494 (error "Can't read into byte sequence from a string stream."))
495 ;; "read-vector" equivalent, but blocking if partial-fill is NIL
496 ;; FIXME: this could be implemented faster via buffer-copy
497 (loop with encap = (sm melded-stream stream)
498 for index from start below end
499 for byte = (read-byte-internal encap nil nil t)
500 then (read-byte-internal encap nil nil partial-fill)
502 do (setf (bref seq index) byte)
503 finally (return index)))
504 ;; extend to work on other sequences: repeated read-byte
507 (defun %write-sequence (stream seq start end)
508 (declare (type simple-stream stream)
510 (type sb-int:index start end))
511 (with-stream-class (simple-stream stream)
512 (%check stream :output)
515 (funcall-stm-handler-2 j-write-chars seq (sm melded-stream stream)
517 ((or (simple-array (unsigned-byte 8) (*))
518 (simple-array (signed-byte 8) (*)))
519 ;; "write-vector" equivalent
520 (setf (sm charpos stream) nil)
522 (single-channel-simple-stream
523 (with-stream-class (single-channel-simple-stream stream)
524 (loop with max-ptr fixnum = (sm buf-len stream)
525 for src-pos fixnum = start then (+ src-pos count)
526 for src-rest fixnum = (- end src-pos)
527 while (> src-rest 0) ; FIXME: this is non-ANSI
528 for ptr fixnum = (let ((ptr (sm buffpos stream)))
530 (flush-buffer stream t)
532 for buf-rest fixnum = (- max-ptr ptr)
533 for count fixnum = (min buf-rest src-rest)
534 do (progn (setf (sm mode stream) 1)
535 (setf (sm buffpos stream) (+ ptr count))
536 (buffer-copy seq src-pos (sm buffer stream) ptr count)))))
537 (dual-channel-simple-stream
538 (with-stream-class (dual-channel-simple-stream stream)
539 (loop with max-ptr fixnum = (sm max-out-pos stream)
540 for src-pos fixnum = start then (+ src-pos count)
541 for src-rest fixnum = (- end src-pos)
542 while (> src-rest 0) ; FIXME: this is non-ANSI
543 for ptr fixnum = (let ((ptr (sm outpos stream)))
545 (flush-out-buffer stream t)
547 for buf-rest fixnum = (- max-ptr ptr)
548 for count fixnum = (min buf-rest src-rest)
549 do (progn (setf (sm outpos stream) (+ ptr count))
550 (buffer-copy seq src-pos (sm out-buffer stream) ptr count)))))
551 (string-simple-stream
552 (error 'simple-type-error
554 :expected-type 'stream
555 :format-control "Can't write a byte sequence to a string stream."
556 :format-arguments '())))
558 ;; extend to work on other sequences: repeated write-byte
563 (defun read-vector (vector stream &key (start 0) end (endian-swap :byte-8))
564 (declare (type (sb-kernel:simple-unboxed-array (*)) vector)
565 (type stream stream))
566 ;; START and END are octet offsets, not vector indices! [Except for strings]
567 ;; Return value is index of next octet to be read into (i.e., start+count)
570 (with-stream-class (simple-stream stream)
571 (cond ((stringp vector)
572 (let* ((start (or start 0))
573 (end (or end (length vector)))
574 (encap (sm melded-stream stream))
575 (char (funcall-stm-handler j-read-char encap nil nil t)))
577 (setf (schar vector start) char)
579 (+ start (funcall-stm-handler j-read-chars encap vector nil
581 ((any-stream-instance-flags stream :string)
582 (error "Can't READ-BYTE on string streams."))
584 (do* ((encap (sm melded-stream stream))
585 (index (or start 0) (1+ index))
586 (end (or end (* (length vector) (vector-elt-width vector))))
587 (endian-swap (endian-swap-value vector endian-swap))
588 (byte (read-byte-internal encap nil nil t)
589 (read-byte-internal encap nil nil nil)))
590 ((or (null byte) (>= index end)) index)
591 (setf (bref vector (logxor index endian-swap)) byte))))))
592 ((or ansi-stream fundamental-stream)
593 (unless (typep vector '(or string
594 (simple-array (signed-byte 8) (*))
595 (simple-array (unsigned-byte 8) (*))))
596 (error "Wrong vector type for read-vector on stream not of type simple-stream."))
597 (read-sequence vector stream :start (or start 0) :end end))))
601 ;;; USER-LEVEL FUNCTIONS
604 (defmethod open-stream-p ((stream simple-stream))
605 (any-stream-instance-flags stream :input :output))
607 (defmethod input-stream-p ((stream simple-stream))
608 (any-stream-instance-flags stream :input))
610 (defmethod output-stream-p ((stream simple-stream))
611 (any-stream-instance-flags stream :output))
613 (defmethod stream-element-type ((stream simple-stream))
616 (defun interactive-stream-p (stream)
617 "Return true if Stream does I/O on a terminal or other interactive device."
620 (%check stream :open)
621 (any-stream-instance-flags stream :interactive))
623 (funcall (sb-kernel:ansi-stream-misc stream) stream :interactive-p))
627 (defun (setf interactive-stream-p) (flag stream)
630 (%check stream :open)
632 (add-stream-instance-flags stream :interactive)
633 (remove-stream-instance-flags stream :interactive)))
635 (error 'simple-type-error
637 :expected-type 'simple-stream
638 :format-control "Can't set interactive flag on ~S."
639 :format-arguments (list stream)))))
641 (defun file-string-length (stream object)
642 (declare (type (or string character) object) (type stream stream))
643 "Return the delta in STREAM's FILE-POSITION that would be caused by writing
644 OBJECT to STREAM. Non-trivial only in implementations that support
645 international character sets."
647 (simple-stream (%file-string-length stream object))
651 (string (length object))))))
653 (defun stream-external-format (stream)
654 "Returns Stream's external-format."
657 (with-stream-class (simple-stream)
658 (%check stream :open)
659 (sm external-format stream)))
665 (defun open (filename &rest options
666 &key (direction :input)
667 (element-type 'character element-type-given)
668 if-exists if-does-not-exist
669 (external-format :default)
670 class mapped input-handle output-handle
672 "Return a stream which reads from or writes to Filename.
674 :direction - one of :input, :output, :io, or :probe
675 :element-type - type of object to read or write, default BASE-CHAR
676 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
677 :overwrite, :append, :supersede or NIL
678 :if-does-not-exist - one of :error, :create or NIL
679 :external-format - :default
680 See the manual for details.
682 The following are simple-streams-specific additions:
683 :class - class of stream object to be created
684 :mapped - T to open a memory-mapped file
685 :input-handle - a stream or Unix file descriptor to read from
686 :output-handle - a stream or Unix file descriptor to write to"
687 (declare (ignore element-type external-format input-handle output-handle
688 if-exists if-does-not-exist))
689 (let ((class (or class 'sb-sys:fd-stream))
690 (options (copy-list options))
691 (filespec (merge-pathnames filename)))
692 (cond ((eq class 'sb-sys:fd-stream)
693 (remf options :class)
694 (remf options :mapped)
695 (remf options :input-handle)
696 (remf options :output-handle)
697 (apply #'open-fd-stream filespec options))
698 ((subtypep class 'simple-stream)
699 (when element-type-given
700 (cerror "Do it anyway."
701 "Can't create simple-streams with an element-type."))
702 (when (and (eq class 'file-simple-stream) mapped)
703 (setq class 'mapped-file-simple-stream)
704 (setf (getf options :class) 'mapped-file-simple-stream))
705 (when (subtypep class 'file-simple-stream)
706 (when (eq direction :probe)
707 (setq class 'probe-simple-stream)))
708 (apply #'make-instance class :filename filespec options))
709 ((subtypep class 'sb-gray:fundamental-stream)
710 (remf options :class)
711 (remf options :mapped)
712 (remf options :input-handle)
713 (remf options :output-handle)
714 (make-instance class :lisp-stream
715 (apply #'open-fd-stream filespec options))))))
718 (declaim (inline read-byte read-char read-char-no-hang unread-char))
720 (defun read-byte (stream &optional (eof-error-p t) eof-value)
721 "Returns the next byte of the Stream."
722 (let ((stream (sb-impl::in-synonym-of stream)))
725 (%read-byte stream eof-error-p eof-value))
727 (sb-impl::ansi-stream-read-byte stream eof-error-p eof-value nil))
729 (let ((char (sb-gray:stream-read-byte stream)))
731 (sb-impl::eof-or-lose stream eof-error-p eof-value)
734 (defun read-char (&optional (stream *standard-input*) (eof-error-p t)
735 eof-value recursive-p)
736 "Inputs a character from Stream and returns it."
737 (let ((stream (sb-impl::in-synonym-of stream)))
740 (%read-char stream eof-error-p eof-value recursive-p t))
742 (sb-impl::ansi-stream-read-char stream eof-error-p eof-value
745 (let ((char (sb-gray:stream-read-char stream)))
747 (sb-impl::eof-or-lose stream eof-error-p eof-value)
750 (defun read-char-no-hang (&optional (stream *standard-input*) (eof-error-p t)
751 eof-value recursive-p)
752 "Returns the next character from the Stream if one is availible, or nil."
753 (declare (ignore recursive-p))
754 (let ((stream (sb-impl::in-synonym-of stream)))
757 (%check stream :input)
758 (with-stream-class (simple-stream)
759 (funcall-stm-handler j-read-char stream eof-error-p eof-value nil)))
761 (sb-impl::ansi-stream-read-char-no-hang stream eof-error-p eof-value
764 (let ((char (sb-gray:stream-read-char-no-hang stream)))
766 (sb-impl::eof-or-lose stream eof-error-p eof-value)
769 (defun unread-char (character &optional (stream *standard-input*))
770 "Puts the Character back on the front of the input Stream."
771 (let ((stream (sb-impl::in-synonym-of stream)))
774 (%unread-char stream character))
776 (sb-impl::ansi-stream-unread-char character stream))
778 (sb-gray:stream-unread-char stream character))))
781 (declaim (notinline read-byte read-char read-char-no-hang unread-char))
783 (defun peek-char (&optional (peek-type nil) (stream *standard-input*)
784 (eof-error-p t) eof-value recursive-p)
785 "Peeks at the next character in the input Stream. See manual for details."
786 (let ((stream (sb-impl::in-synonym-of stream)))
789 (%peek-char stream peek-type eof-error-p eof-value recursive-p))
790 ;; FIXME: Broken on ECHO-STREAM (cf internal implementation?) --
793 (sb-impl::ansi-stream-peek-char peek-type stream eof-error-p eof-value
796 (cond ((characterp peek-type)
797 (do ((char (sb-gray:stream-read-char stream)
798 (sb-gray:stream-read-char stream)))
799 ((or (eq char :eof) (char= char peek-type))
800 (cond ((eq char :eof)
801 (sb-impl::eof-or-lose stream eof-error-p eof-value))
803 (sb-gray:stream-unread-char stream char)
806 (do ((char (sb-gray:stream-read-char stream)
807 (sb-gray:stream-read-char stream)))
808 ((or (eq char :eof) (not (sb-impl::whitespace[2]p char)))
809 (cond ((eq char :eof)
810 (sb-impl::eof-or-lose stream eof-error-p eof-value))
812 (sb-gray:stream-unread-char stream char)
815 (let ((char (sb-gray:stream-peek-char stream)))
817 (sb-impl::eof-or-lose stream eof-error-p eof-value)
820 (defun listen (&optional (stream *standard-input*) (width 1))
821 "Returns T if WIDTH octets are available on STREAM. If WIDTH is
822 given as 'CHARACTER, check for a character. Note: the WIDTH argument
823 is supported only on simple-streams."
824 ;; WIDTH is number of octets which must be available; any value
825 ;; other than 1 is treated as 'character.
826 (let ((stream (sb-impl::in-synonym-of stream)))
829 (%listen stream width))
831 (sb-impl::ansi-stream-listen stream))
833 (sb-gray:stream-listen stream)))))
836 (defun read-line (&optional (stream *standard-input*) (eof-error-p t)
837 eof-value recursive-p)
838 "Returns a line of text read from the Stream as a string, discarding the
840 (let ((stream (sb-impl::in-synonym-of stream)))
843 (%read-line stream eof-error-p eof-value recursive-p))
845 (sb-impl::ansi-stream-read-line stream eof-error-p eof-value
848 (multiple-value-bind (string eof) (sb-gray:stream-read-line stream)
849 (if (and eof (zerop (length string)))
850 (values (sb-impl::eof-or-lose stream eof-error-p eof-value) t)
851 (values string eof)))))))
853 (defun read-sequence (seq stream &key (start 0) (end nil) partial-fill)
854 "Destructively modify SEQ by reading elements from STREAM.
855 SEQ is bounded by START and END. SEQ is destructively modified by
856 copying successive elements into it from STREAM. If the end of file
857 for STREAM is reached before copying all elements of the subsequence,
858 then the extra elements near the end of sequence are not updated, and
859 the index of the next element is returned."
860 (let ((stream (sb-impl::in-synonym-of stream))
861 (end (or end (length seq))))
864 (with-stream-class (simple-stream stream)
865 (%read-sequence stream seq start end partial-fill)))
867 (sb-impl::ansi-stream-read-sequence seq stream start end))
869 (sb-gray:stream-read-sequence stream seq start end)))))
871 (defun clear-input (&optional (stream *standard-input*) buffer-only)
872 "Clears any buffered input associated with the Stream."
873 (let ((stream (sb-impl::in-synonym-of stream)))
876 (%clear-input stream buffer-only))
878 (sb-impl::ansi-stream-clear-input stream))
880 (sb-gray:stream-clear-input stream))))
883 (defun write-byte (integer stream)
884 "Outputs an octet to the Stream."
885 (let ((stream (sb-impl::out-synonym-of stream)))
888 (%write-byte stream integer))
890 (funcall (sb-kernel:ansi-stream-bout stream) stream integer))
892 (sb-gray:stream-write-byte stream integer))))
895 (defun write-char (character &optional (stream *standard-output*))
896 "Outputs the Character to the Stream."
897 (let ((stream (sb-impl::out-synonym-of stream)))
900 (%write-char stream character))
902 (funcall (sb-kernel:ansi-stream-out stream) stream character))
904 (sb-gray:stream-write-char stream character))))
907 (defun write-string (string &optional (stream *standard-output*)
908 &key (start 0) (end nil))
909 "Outputs the String to the given Stream."
910 (let ((stream (sb-impl::out-synonym-of stream))
911 (end (sb-impl::%check-vector-sequence-bounds string start end)))
914 (%write-string stream string start end)
917 (sb-impl::ansi-stream-write-string string stream start end))
919 (sb-gray:stream-write-string stream string start end)))))
921 (defun write-line (string &optional (stream *standard-output*)
923 (declare (type string string))
924 (let ((stream (sb-impl::out-synonym-of stream))
925 (end (sb-impl::%check-vector-sequence-bounds string start end)))
928 (%check stream :output)
929 (with-stream-class (simple-stream stream)
930 (funcall-stm-handler-2 j-write-chars string stream start end)
931 (funcall-stm-handler-2 j-write-char #\Newline stream)))
933 (sb-impl::ansi-stream-write-string string stream start end)
934 (funcall (sb-kernel:ansi-stream-out stream) stream #\Newline))
936 (sb-gray:stream-write-string stream string start end)
937 (sb-gray:stream-terpri stream))))
940 (defun write-sequence (seq stream &key (start 0) (end nil))
941 "Write the elements of SEQ bounded by START and END to STREAM."
942 (let ((stream (sb-impl::out-synonym-of stream))
943 (end (or end (length seq))))
946 (%write-sequence stream seq start end))
948 (sb-impl::ansi-stream-write-sequence seq stream start end))
950 (sb-gray:stream-write-sequence stream seq start end)))))
952 (defun terpri (&optional (stream *standard-output*))
953 "Outputs a new line to the Stream."
954 (let ((stream (sb-impl::out-synonym-of stream)))
957 (%check stream :output)
958 (with-stream-class (simple-stream stream)
959 (funcall-stm-handler-2 j-write-char #\Newline stream)))
961 (funcall (sb-kernel:ansi-stream-out stream) stream #\Newline))
963 (sb-gray:stream-terpri stream))))
966 (defun fresh-line (&optional (stream *standard-output*))
967 "Outputs a new line to the Stream if it is not positioned at the beginning of
968 a line. Returns T if it output a new line, nil otherwise."
969 (let ((stream (sb-impl::out-synonym-of stream)))
972 (%fresh-line stream))
974 (sb-impl::ansi-stream-fresh-line stream))
976 (sb-gray:stream-fresh-line stream)))))
978 (defun finish-output (&optional (stream *standard-output*))
979 "Attempts to ensure that all output sent to the Stream has reached its
980 destination, and only then returns."
981 (let ((stream (sb-impl::out-synonym-of stream)))
984 (%finish-output stream))
986 (funcall (sb-kernel:ansi-stream-misc stream) stream :finish-output))
988 (sb-gray:stream-finish-output stream))))
991 (defun force-output (&optional (stream *standard-output*))
992 "Attempts to force any buffered output to be sent."
993 (let ((stream (sb-impl::out-synonym-of stream)))
996 (%force-output stream))
998 (funcall (sb-kernel:ansi-stream-misc stream) stream :force-output))
1000 (sb-gray:stream-force-output stream))))
1003 (defun clear-output (&optional (stream *standard-output*))
1004 "Clears the given output Stream."
1005 (let ((stream (sb-impl::out-synonym-of stream)))
1008 (%clear-output stream))
1010 (funcall (sb-kernel:ansi-stream-misc stream) stream :clear-output))
1012 (sb-gray:stream-clear-output stream))))
1016 (defun file-position (stream &optional position)
1017 "With one argument returns the current position within the file
1018 File-Stream is open to. If the second argument is supplied, then
1019 this becomes the new file position. The second argument may also
1020 be :start or :end for the start and end of the file, respectively."
1021 (declare (type (or sb-int:index (member nil :start :end)) position))
1024 (%file-position stream position))
1026 (sb-impl::ansi-stream-file-position stream position))))
1028 (defun file-length (stream)
1029 "This function returns the length of the file that File-Stream is open to."
1032 (%file-length stream))
1034 (sb-impl::stream-must-be-associated-with-file stream)
1035 (funcall (sb-kernel:ansi-stream-misc stream) stream :file-length))))
1037 (defun charpos (&optional (stream *standard-output*))
1038 "Returns the number of characters on the current line of output of the given
1039 Stream, or Nil if that information is not availible."
1040 (let ((stream (sb-impl::out-synonym-of stream)))
1043 (with-stream-class (simple-stream stream)
1044 (%check stream :open)
1045 (sm charpos stream)))
1047 (funcall (sb-kernel:ansi-stream-misc stream) stream :charpos))
1049 (sb-gray:stream-line-column stream)))))
1051 (defun line-length (&optional (stream *standard-output*))
1052 "Returns the number of characters in a line of output of the given
1053 Stream, or Nil if that information is not availible."
1054 (let ((stream (sb-impl::out-synonym-of stream)))
1057 (%check stream :output)
1058 ;; TODO (sat 2003-04-02): a way to specify a line length would
1059 ;; be good, I suppose. Returning nil here means
1060 ;; sb-pretty::default-line-length is used.
1063 (funcall (sb-kernel:ansi-stream-misc stream) stream :line-length))
1065 (sb-gray:stream-line-length stream)))))
1067 (defun wait-for-input-available (stream &optional timeout)
1068 "Waits for input to become available on the Stream and returns T. If
1069 Timeout expires, Nil is returned."
1070 (let ((stream (sb-impl::in-synonym-of stream)))
1073 (sb-sys:wait-until-fd-usable stream :input timeout))
1075 (%check stream :input)
1076 (with-stream-class (simple-stream stream)
1077 (or (< (sm buffpos stream) (sm buffer-ptr stream))
1078 (wait-for-input-available (sm input-handle stream) timeout))))
1080 (wait-for-input-available (two-way-stream-input-stream stream) timeout))
1082 (wait-for-input-available (symbol-value (synonym-stream-symbol stream))
1085 (or (< (sb-impl::fd-stream-in-index stream)
1086 (length (sb-impl::fd-stream-in-buffer stream)))
1087 (wait-for-input-available (sb-sys:fd-stream-fd stream) timeout))))))
1089 ;; Make PATHNAME and NAMESTRING work
1090 (defun sb-int:file-name (stream &optional new-name)
1093 (with-stream-class (file-simple-stream stream)
1095 (%file-rename stream new-name))
1097 (%file-name stream)))))
1100 (setf (sb-impl::fd-stream-pathname stream) new-name)
1101 (setf (sb-impl::fd-stream-file stream)
1102 (sb-int:unix-namestring new-name nil))
1105 (sb-impl::fd-stream-pathname stream))))))