Skip to content
Snippets Groups Projects
stream.lisp 108 KiB
Newer Older
ram's avatar
ram committed
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
(ext:file-comment
  "$Header: src/code/stream.lisp $")
ram's avatar
ram committed
;;; **********************************************************************
;;;
;;; Stream functions for Spice Lisp.
;;; Written by Skef Wholey and Rob MacLachlan.
;;; Gray streams support by Douglas Crosher, 1998.
;;; Simple-streams support by Paul Foley, 2003.
ram's avatar
ram committed
;;;
;;; This file contains the OS-independent stream functions.
ram's avatar
ram committed
;;;
ram's avatar
ram committed

(export '(broadcast-stream make-broadcast-stream broadcast-stream-streams 
	  synonym-stream make-synonym-stream synonym-stream-symbol
	  concatenated-stream make-concatenated-stream
	  concatenated-stream-streams
	  two-way-stream make-two-way-stream two-way-stream-input-stream
	  two-way-stream-output-stream
	  echo-stream make-echo-stream echo-stream-input-stream
	  echo-stream-output-stream
	  make-string-input-stream make-string-output-stream
ram's avatar
ram committed
	  get-output-stream-string stream-element-type input-stream-p
	  output-stream-p open-stream-p interactive-stream-p
	  open-stream-p close read-line read-char
ram's avatar
ram committed
	  unread-char peek-char listen read-char-no-hang clear-input read-byte
	  write-char write-string write-line terpri fresh-line
	  finish-output force-output clear-output write-byte
          stream streamp string-stream
	  *standard-input* *standard-output*
ram's avatar
ram committed
          *error-output* *query-io* *debug-io* *terminal-io* *trace-output*))

(in-package "SYSTEM")
ram's avatar
ram committed
(export '(make-indenting-stream read-n-bytes))

(in-package "EXT")

(export '(get-stream-command
	  stream-command stream-command-p stream-command-name 
	  stream-command-args make-stream-command make-case-frob-stream))

(in-package "LISP")
ram's avatar
ram committed

;;;; Standard streams:
;;;
;;; The initialization of these streams is performed by Stream-Init,
;;; which lives in the file of machine-specific stream functions.
;;;
(defvar *terminal-io* () "Terminal I/O stream.")
(defvar *standard-input* () "Default input stream.")
(defvar *standard-output* () "Default output stream.")
(defvar *error-output* () "Error output stream.")
(defvar *query-io* () "Query I/O stream.")
(defvar *trace-output* () "Trace output stream.")
(defvar *debug-io* () "Interactive debugging stream.")
ram's avatar
ram committed

(defun ill-in-any (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies input-stream-p)
	 :format-control (intl:gettext "~S is not an input stream.")
	 :format-arguments (list stream)))
(defun ill-out-any (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies output-stream-p)
	 :format-control (intl:gettext "~S is not an output stream.")
	 :format-arguments (list stream)))
ram's avatar
ram committed
(defun ill-in (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies input-stream-p)
	 :format-control (intl:gettext "~S is not a character input stream.")
	 :format-arguments (list stream)))
ram's avatar
ram committed
(defun ill-out (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies output-stream-p)
	 :format-control (intl:gettext "~S is not a character output stream.")
	 :format-arguments (list stream)))
ram's avatar
ram committed
(defun ill-bin (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies input-stream-p)
	 :format-control (intl:gettext "~S is not a binary input stream.")
	 :format-arguments (list stream)))
(defun ill-n-bin (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies input-stream-p)
	 :format-control (intl:gettext "~S is not a binary input stream ~
                          or does not support multi-byte read operations.")
	 :format-arguments (list stream)))
ram's avatar
ram committed
(defun ill-bout (stream &rest ignore)
  (declare (ignore ignore))
  (error 'simple-type-error
	 :datum stream
	 :expected-type '(satisfies output-stream-p)
	 :format-control (intl:gettext "~S is not a binary output stream.")
	 :format-arguments (list stream)))
ram's avatar
ram committed
(defun closed-flame (stream &rest ignore)
  (declare (ignore ignore))
  (error (intl:gettext "~S is closed.") stream))
ram's avatar
ram committed
(defun do-nothing (&rest ignore)
  (declare (ignore ignore)))
(defun no-gray-streams (stream)
  (error 'simple-type-error
	 :datum stream
	 :expected-type 'stream
	 :format-control (intl:gettext "~S is an unsupported Gray stream.")
	 :format-arguments (list stream)))
ram's avatar
ram committed

(defun %print-stream (structure stream d)
  (declare (ignore d structure))
  (write-string "#<Bare Stream>" stream))

;;; HOW THE STREAM STRUCTURE IS USED:
;;;
;;;    Many of the slots of the stream structure contain functions
;;; which are called to perform some operation on the stream.  Closed
;;; streams have #'Closed-Flame in all of their function slots.  If
;;; one side of an I/O or echo stream is closed, the whole stream is
;;; considered closed.  The functions in the operation slots take
;;; arguments as follows:
;;;
;;; In:			Stream, Eof-Errorp, Eof-Value
;;; Bin:		Stream, Eof-Errorp, Eof-Value
;;; N-Bin:		Stream, Buffer, Start, Numbytes, Eof-Errorp
;;; Out:		Stream, Character
;;; Bout:		Stream, Integer
;;; Sout:		Stream, String, Start, End
;;; Misc:		Stream, Operation, &Optional Arg1, Arg2
;;;
;;;    In order to save space, some of the less common stream operations
;;; are handled by just one function, the Misc method.  This function
;;; is passed a keyword which indicates the operation to perform.
;;; The following keywords are used:
;;;  :listen 		- Return the following values:
;;; 			     t if any input waiting.
;;; 			     :eof if at eof.
;;; 			     nil if no input is available and not at eof.
ram's avatar
ram committed
;;;  :unread		- Unread the character Arg.
;;;  :close		- Do any stream specific stuff to close the stream.
;;;			  The methods are set to closed-flame by the close
;;;			  function, so that need not be done by this
;;;			  function.
;;;  :clear-input 	- Clear any unread input
;;;  :finish-output,
;;;  :force-output	- Cause output to happen
;;;  :clear-output	- Clear any undone output
;;;  :element-type 	- Return the type of element the stream deals with.
ram's avatar
ram committed
;;;  :line-length	- Return the length of a line of output.
;;;  :charpos		- Return current output position on the line.
;;;  :file-length	- Return the file length of a file stream.
;;;  :file-position	- Return or change the current position of a file stream.
;;;  :file-name		- Return the name of an associated file.
;;;  :interactive-p     - Is this an interactive device?
ram's avatar
ram committed
;;;
;;;    In order to do almost anything useful, it is necessary to
;;; define a new type of structure that includes stream, so that the
;;; stream can have some state information.
;;;
;;; THE STREAM IN-BUFFER:
;;;
;;;    The In-Buffer in the stream holds characters or bytes that
;;; are ready to be read by some input function.  If there is any
;;; stuff in the In-Buffer, then the reading function can use it
;;; without calling any stream method.  Any stream may put stuff in
;;; the In-Buffer, and may also assume that any input in the In-Buffer
;;; has been consumed before any in-method is called.  If a text
;;; stream has in In-Buffer, then the first character should not be
;;; used to buffer normal input so that it is free for unreading into.
;;;
;;;    The In-Buffer slot is a vector In-Buffer-Length long.  The
;;; In-Index is the index in the In-Buffer of the first available
;;; object.  The available objects are thus between In-Index and the
;;; length of the In-Buffer.
;;;
;;;    When this buffer is only accessed by the normal stream
;;; functions, the number of function calls is halved, thus
;;; potentially doubling the speed of simple operations.  If the
;;; Fast-Read-Char and Fast-Read-Byte macros are used, nearly all
;;; function call overhead is removed, vastly speeding up these
;;; important operations.
;;;
;;;    If a stream does not have an In-Buffer, then the In-Buffer slot
;;; must be nil, and the In-Index must be In-Buffer-Length.  These are
;;; the default values for the slots. 
ram's avatar
ram committed

;;; Stream manipulation functions.

(defun input-stream-p (stream)
  "Returns non-nil if the given Stream can perform input operations."
  (declare (type stream stream))
  ;; Note: Gray streams redefines this function; any changes made here need
  ;; to be duplicated in .../pcl/gray-streams.lisp
  (stream-dispatch stream
    ;; simple-stream
    (stream::%input-stream-p stream)
    ;; lisp-stream
rtoy's avatar
rtoy committed
    (let ((stream (if (synonym-stream-p stream)
		      (symbol-value (synonym-stream-symbol stream))
		      stream)))
      (and (not (eq (lisp-stream-in stream) #'closed-flame))
	   (or (not (eq (lisp-stream-in stream) #'ill-in))
	       (not (eq (lisp-stream-bin stream) #'ill-bin))
	       (not (eq (lisp-stream-n-bin stream) #'ill-n-bin)))))))
ram's avatar
ram committed

(defun output-stream-p (stream)
  "Returns non-nil if the given Stream can perform output operations."
  (declare (type stream stream))
  ;; Note: Gray streams redefines this function; any changes made here need
  ;; to be duplicated in .../pcl/gray-streams.lisp
  (stream-dispatch stream
    ;; simple-stream
    (stream::%output-stream-p stream)
    ;; lisp-stream
rtoy's avatar
rtoy committed
    (let ((stream (if (synonym-stream-p stream)
		      (symbol-value (synonym-stream-symbol stream))
		      stream)))
      (and (not (eq (lisp-stream-in stream) #'closed-flame))
	   (or (not (eq (lisp-stream-out stream) #'ill-out))
	       (not (eq (lisp-stream-bout stream) #'ill-bout)))))))
ram's avatar
ram committed

(defun open-stream-p (stream)
  "Return true if Stream is not closed."
  (declare (type stream stream))
  ;; Note: Gray streams redefines this function; any changes made here need
  ;; to be duplicated in .../pcl/gray-streams.lisp
  (stream-dispatch stream
    ;; simple-stream
    (stream::%open-stream-p stream)
    ;; lisp-stream
    (not (eq (lisp-stream-in stream) #'closed-flame))))
ram's avatar
ram committed
(defun stream-element-type (stream)
  "Returns a type specifier for the kind of object returned by the Stream."
  (declare (type stream stream))
  ;; Note: Gray streams redefines this function; any changes made here need
  ;; to be duplicated in .../pcl/gray-streams.lisp
  (stream-dispatch stream
    ;; simple-stream
    '(unsigned-byte 8)
    ;; lisp-stream
    (funcall (lisp-stream-misc stream) stream :element-type)))
ram's avatar
ram committed

(defun interactive-stream-p (stream)
  "Return true if Stream does I/O on a terminal or other interactive device."
  (declare (type stream stream))
  (stream-dispatch stream
    ;; simple-stream
    (stream::%interactive-stream-p stream)
    ;; lisp-stream
    (funcall (lisp-stream-misc stream) stream :interactive-p)))
(defun (setf interactive-stream-p) (flag stream)
  (declare (type stream stream))
  (stream-dispatch stream
    ;; simple-stream
    (if flag
	(stream::%interactive-stream-y stream)
	(stream::%interactive-stream-n stream))
    ;; lisp-stream
    (error 'simple-type-error
	   :datum stream
	   :expected-type 'stream:simple-stream
	   :format-control (intl:gettext "Can't set interactive flag on ~S.")
	   :format-arguments (list stream))))

(defun stream-external-format (stream)
  "Returns the external format used by the given Stream."
  (declare (type stream stream))
  (stream-dispatch stream
    ;; simple-stream
    (stream::%stream-external-format stream)
    ;; lisp-stream.  For unsupported streams, signal a type error.
    (etypecase stream
rtoy's avatar
rtoy committed
      #+unicode
      (fd-stream (fd-stream-external-format stream))
      (broadcast-stream
       ;; See http://www.lispworks.com/documentation/HyperSpec/Body/t_broadc.htm
       (let ((components (broadcast-stream-streams stream)))
	 (if (null components)
	     :default
	     (stream-external-format (car (last components))))))
      (synonym-stream
       ;; Not defined by CLHS.  What should happen if
       ;; (synonym-stream-symbol stream) is unbound?
       (stream-external-format
	(symbol-value (synonym-stream-symbol stream)))))
    ;; fundamental-stream
    :default))
;; This is only used while building; it's reimplemented in
;; fd-stream-extfmt.lisp
#+unicode
(defun %set-fd-stream-external-format (stream extfmt &optional (updatep t))
rtoy's avatar
rtoy committed

;; This is only used while building; it's reimplemented in
;; fd-stream-extfmt.lisp
(defun (setf stream-external-format) (extfmt stream)
rtoy's avatar
rtoy committed
  (declare (ignore stream))
  extfmt)
ram's avatar
ram committed
(defun close (stream &key abort)
  "Closes the given Stream.  No more I/O may be performed, but inquiries
ram's avatar
ram committed
  may still be made.  If :Abort is non-nil, an attempt is made to clean
  up the side effects of having created the stream."
  (declare (type stream stream))
  ;; Note: Gray streams redefines this function; any changes made here need
  ;; to be duplicated in .../pcl/gray-streams.lisp
  (stream-dispatch stream
    ;; simple-stream
    (stream:device-close stream abort)
    ;; lisp-stream
    (when (open-stream-p stream)
      (funcall (lisp-stream-misc stream) stream :close abort)))
ram's avatar
ram committed
  t)

(defun set-closed-flame (stream)
  (declare (type lisp-stream stream))
dtc's avatar
dtc committed
  (setf (lisp-stream-in stream) #'closed-flame)
  (setf (lisp-stream-bin stream) #'closed-flame)
  (setf (lisp-stream-n-bin stream) #'closed-flame)
  (setf (lisp-stream-in stream) #'closed-flame)
  (setf (lisp-stream-out stream) #'closed-flame)
  (setf (lisp-stream-bout stream) #'closed-flame)
  (setf (lisp-stream-sout stream) #'closed-flame)
  (setf (lisp-stream-misc stream) #'closed-flame))


;;;; File position and file length.

;;; File-Position  --  Public
;;;
;;;    Call the misc method with the :file-position operation.
;;;
(defun file-position (stream &optional position)
  "With one argument returns the current position within the file
   File-Stream is open to.  If the second argument is supplied, then
   this becomes the new file position.  The second argument may also
   be :start or :end for the start and end of the file, respectively."
	   (type (or (integer 0 *) (member nil :start :end)) position))
  (stream-dispatch stream
    ;; simple-stream
    (stream::%file-position stream position)
    ;; lisp-stream
    (cond
      (position
       (setf (lisp-stream-in-index stream) in-buffer-length)
       (funcall (lisp-stream-misc stream) stream :file-position position))
      (t
       (let ((res (funcall (lisp-stream-misc stream) stream
			   :file-position nil)))
	 ;; For Unicode, the LISP-STREAM-MISC function handles
	 ;; everything, so we can just return the result.
	 #-unicode
	   (- res (- in-buffer-length (lisp-stream-in-index stream))))
	 #+unicode
	 res)))
    ;; fundamental stream
    (cond
      (position
       (setf (stream-file-position stream) position))
      (t
       (stream-file-position stream)))))


;;; File-Length  --  Public
;;;
;;;    Like File-Position, only use :file-length.
;;;
(defun file-length (stream)
  "This function returns the length of the file that File-Stream is open to."
  (stream-dispatch stream
    ;; simple-stream
    (stream::%file-length stream)
    ;; lisp-stream
    (funcall (lisp-stream-misc stream) stream :file-length)))
ram's avatar
ram committed

;;; Input functions:

(defun read-line (&optional (stream *standard-input*) (eof-errorp t) eof-value
			    recursive-p)
  "Returns a line of text read from the Stream as a string, discarding the
ram's avatar
ram committed
  newline character."
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%read-line stream eof-errorp eof-value recursive-p)
      ;; lisp-stream
      (prepare-for-fast-read-char stream
        (let ((res (make-string 80))
	      (len 80)
	      (index 0))
	  (loop
	    (let ((ch (fast-read-char nil nil)))
	      (cond (ch
		     (when (char= ch #\newline)
		       (done-with-fast-read-char)
		       (return (values (shrink-vector res index) nil)))
		     (when (= index len)
		       (setq len (* len 2))
		       (let ((new (make-string len)))
			 (replace new res)
			 (setq res new)))
		     (setf (schar res index) ch)
		     (incf index))
		    ((zerop index)
		     (done-with-fast-read-char)
		     (return (values (eof-or-lose stream eof-errorp eof-value)
				     t)))
		    ;; since fast-read-char hit already the eof char, we
		    ;; shouldn't do another read-char
		    (t
		     (done-with-fast-read-char)
		     (return (values (shrink-vector res index) t))))))))
      ;; fundamental-stream
      (multiple-value-bind (string eof)
	  (stream-read-line stream)
	(if (and eof (zerop (length string)))
	    (values (eof-or-lose stream eof-errorp eof-value) t)
	    (values string eof))))))
ram's avatar
ram committed
;;; We proclaim them inline here, then proclaim them notinline at EOF,
;;; so, except in this file, they are not inline by default, but they can be.
;;;
pw's avatar
pw committed
(declaim (inline read-char unread-char read-byte listen))
ram's avatar
ram committed
(defun read-char (&optional (stream *standard-input*) (eof-errorp t) eof-value
			    recursive-p)
  "Inputs a character from Stream and returns it."
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%read-char stream eof-errorp eof-value recursive-p t)
      ;; lisp-stream
      (prepare-for-fast-read-char stream
	(prog1
	    (fast-read-char eof-errorp eof-value)
	  (done-with-fast-read-char)))
      ;; fundamental-stream
      (let ((char (stream-read-char stream)))
	(if (eq char :eof)
	    (eof-or-lose stream eof-errorp eof-value)
	    char)))))
ram's avatar
ram committed

(defun unread-char (character &optional (stream *standard-input*))
  "Puts the Character back on the front of the input Stream."
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%unread-char stream character)
      ;; lisp-stream
      (let ((index (1- (lisp-stream-in-index stream)))
	    (buffer (lisp-stream-in-buffer stream)))
	(declare (fixnum index))
	(when (minusp index) (error (intl:gettext "Nothing to unread.")))
	(cond (buffer
	       (setf (aref buffer index) (char-code character))
	       (setf (lisp-stream-in-index stream) index))
	      (t
	       (funcall (lisp-stream-misc stream) stream 
			:unread character))))
      #+unicode
      (let ((sbuf (lisp-stream-string-buffer stream))
	    (ibuf (lisp-stream-in-buffer stream)))
	(cond (sbuf
	       (let ((index (1- (lisp-stream-string-index stream))))
		 (when (minusp index)
		   (error (intl:gettext "Nothing to unread.")))
		 (setf (aref sbuf index) character)
		 (setf (lisp-stream-string-index stream) index)))
	      (ibuf
	       (let ((index (1- (lisp-stream-in-index stream))))
		 (when (minusp index)
		   (error (intl:gettext "Nothing to unread.")))
		 ;; This only works for iso8859-1!
		 (setf (aref ibuf index) (char-code character))
		 (setf (lisp-stream-in-index stream) index)))
	      (t
	       (funcall (lisp-stream-misc stream) stream 
			:unread character))))
      ;; fundamental-stream
      (stream-unread-char stream character)))
ram's avatar
ram committed
  nil)

;;; In the interest of ``once and only once'' this macro contains the
;;; framework necessary to implement a peek-char function, which has
;;; two special-cases (one for gray streams and one for echo streams)
;;; in addition to the normal case.
;;;
;;; All arguments are forms which will be used for a specific purpose
;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
;;; EOF-VALUE - the eof-value argument to peek-char
;;; CHAR-VAR - the variable which will be used to store the current character
;;; READ-FORM - the form which will be used to read a character
;;; READ-EOF - the result returned from READ-FORM when eof is reached.
;;; UNREAD-FORM - ditto for unread-char
;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
;;;                     (this will default to CHAR-VAR)
(defmacro generalized-peeking-mechanism (peek-type eof-value char-var
					 read-form read-eof unread-form
					 &key (skipped-char-form nil)
					      (eof-detected-form nil))
  `(let ((,char-var ,read-form))
    (cond ((eql ,char-var ,read-eof) 
	   ,(if eof-detected-form
		eof-detected-form
	  ((characterp ,peek-type)
	   (do ((,char-var ,char-var ,read-form))
	       ((or (eql ,char-var ,read-eof) 
		    (char= ,char-var ,peek-type))
		(cond ((eql ,char-var ,read-eof)
		       ,(if eof-detected-form
			    eof-detected-form
		      (t ,unread-form
			 ,char-var)))
	     ,skipped-char-form))
	  ((eql ,peek-type t)
	   (do ((,char-var ,char-var ,read-form))
	       ((or (eql ,char-var ,read-eof)
		    (not (eql (get-cat-entry ,char-var *readtable*) whitespace)))
		(cond ((eql ,char-var ,read-eof)
		       ,(if eof-detected-form
			    eof-detected-form
		      (t ,unread-form
			 ,char-var)))
	     ,skipped-char-form))
	  ((null ,peek-type)
	   ,unread-form
	   ,(if eof-detected-form
		(when (eql char-var read-eof)
		  eof-detected-form))
	   (error (intl:gettext "Impossible case reached in PEEK-CHAR"))))))
ram's avatar
ram committed
(defun peek-char (&optional (peek-type nil) (stream *standard-input*)
			    (eof-errorp t) eof-value recursive-p)
  "Peeks at the next character in the input Stream.  See manual for details."
  ;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
  ;; the compiler doesn't seem to be smart enough to go from there to
  ;; imposing a type check. Figure out why (because PEEK-TYPE is an
  ;; &OPTIONAL argument?) and fix it, and then this explicit type
  ;; check can go away.
  (unless (typep peek-type '(or character boolean))
    (error 'simple-type-error
	   :datum peek-type
	   :expected-type '(or character boolean)
	   :format-control (intl:gettext "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>")
	   :format-arguments (list peek-type '(or character boolean))))
  (let ((stream (in-synonym-of stream)))
    (if (typep stream 'echo-stream)
	(echo-misc stream :peek-char peek-type (list eof-errorp eof-value))
	(stream-dispatch stream
	  ;; simple-stream
	  (stream::%peek-char stream peek-type eof-errorp eof-value
			      recursive-p)
	  ;; lisp-stream
	  (generalized-peeking-mechanism
	   peek-type eof-value char
	   (read-char stream eof-errorp :eof)
	   :eof
	   (unread-char char stream)
	   :skipped-char-form nil
	   :eof-detected-form (eof-or-lose stream (or eof-errorp recursive-p) eof-value))
	  ;; fundamental-stream
	  (generalized-peeking-mechanism
	   peek-type :eof char
	   (if (null peek-type)
	       (stream-peek-char stream)
	       (stream-read-char stream))
	   (if (null peek-type)
	       ()
	       (stream-unread-char stream char))
	   :skipped-char-form ()
	   :eof-detected-form (eof-or-lose stream eof-errorp eof-value))))))

(defun listen (&optional (stream *standard-input*) (width 1))
  "Returns T if a character is available on the given Stream."
  (declare (type streamlike stream))
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%listen stream width)
      ;; lisp-stream
      (or (/= (the fixnum (lisp-stream-in-index stream)) in-buffer-length)
	  ;; Test for t explicitly since misc methods return :eof sometimes.
	  (eq (funcall (lisp-stream-misc stream) stream :listen) t))
      ;; fundamental-stream
      (stream-listen stream))))
ram's avatar
ram committed

(defun read-char-no-hang (&optional (stream *standard-input*)
				    (eof-errorp t) eof-value recursive-p)
  "Returns the next character from the Stream if one is available, or nil."
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%read-char stream eof-errorp eof-value recursive-p nil)
      ;; lisp-stream
      (if (funcall (lisp-stream-misc stream) stream :listen)
	  ;; On t or :eof get READ-CHAR to do the work.
	  (read-char stream eof-errorp eof-value)
	  nil)
      ;; fundamental-stream
      (let ((char (stream-read-char-no-hang stream)))
	(if (eq char :eof)
	    (eof-or-lose stream eof-errorp eof-value)
	    char)))))


(defun clear-input (&optional (stream *standard-input*) buffer-only)
  "Clears any buffered input associated with the Stream."
  (declare (type streamlike stream))
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%clear-input stream buffer-only)
      ;; lisp-stream
      (progn
	(setf (lisp-stream-in-index stream) in-buffer-length)
	(funcall (lisp-stream-misc stream) stream :clear-input))
      ;; fundamental-stream
      (stream-clear-input stream)))
dtc's avatar
dtc committed
  nil)
ram's avatar
ram committed

(defun read-byte (stream &optional (eof-errorp t) eof-value)
  "Returns the next byte of the Stream."
  (declare (type stream stream))
  (let ((stream (in-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%read-byte stream eof-errorp eof-value)
      ;; lisp-stream
      (prepare-for-fast-read-byte stream
	(prog1
	    (fast-read-byte eof-errorp eof-value t)
	  (done-with-fast-read-byte)))
      ;; fundamental-stream
      (let ((char (stream-read-byte stream)))
	(if (eq char :eof)
	    (eof-or-lose stream eof-errorp eof-value)
	    char)))))
ram's avatar
ram committed

(defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
  "Reads Numbytes bytes into the Buffer starting at Start, returning the number
   of bytes read.
   -- If EOF-ERROR-P is true, an END-OF-FILE condition is signalled if
      end-of-file is encountered before Count bytes have been read.
   -- If EOF-ERROR-P is false, READ-N-BYTES reads as much data as is currently
      available (up to count bytes).  On pipes or similar devices, this
      function returns as soon as any data is available, even if the amount
      read is less than Count and eof has not been hit."
dtc's avatar
dtc committed
  (declare (type lisp-stream stream)
	   (type index numbytes start)
	   (type (or (simple-array * (*)) system-area-pointer) buffer))
  (let* ((stream (in-synonym-of stream lisp-stream))
dtc's avatar
dtc committed
	 (in-buffer (lisp-stream-in-buffer stream))
	 (index (lisp-stream-in-index stream))
ram's avatar
ram committed
	 (num-buffered (- in-buffer-length index)))
    (declare (fixnum index num-buffered))
    (cond
      (funcall (lisp-stream-n-bin stream) stream
	       buffer start numbytes eof-errorp))
ram's avatar
ram committed
     ((<= numbytes num-buffered)
      (%primitive byte-blt in-buffer index buffer start (+ start numbytes))
dtc's avatar
dtc committed
      (setf (lisp-stream-in-index stream) (+ index numbytes))
ram's avatar
ram committed
      numbytes)
     (t
      (let ((end (+ start num-buffered)))
	(%primitive byte-blt in-buffer index buffer start end)
dtc's avatar
dtc committed
	(setf (lisp-stream-in-index stream) in-buffer-length)
	(+ (funcall (lisp-stream-n-bin stream) stream buffer end
		    (- numbytes num-buffered)
		    eof-errorp)
ram's avatar
ram committed
	   num-buffered))))))


;;; Amount of space we leave at the start of the in-buffer for unreading.  4
;;; instead of 1 to allow word-aligned copies.
;;;
(defconstant in-buffer-extra 4)

;;; FAST-READ-CHAR-REFILL  --  Interface
;;;
;;;    This function is called by the fast-read-char expansion to refill the
;;; in-buffer for text streams.  There is definitely an in-buffer, and hence
;;; must be an n-bin method.
;;;
(defun fast-read-char-refill (stream eof-errorp eof-value)
dtc's avatar
dtc committed
  (let* ((ibuf (lisp-stream-in-buffer stream))
	 (count (funcall (lisp-stream-n-bin stream) stream
			 ibuf in-buffer-extra
			 (- in-buffer-length in-buffer-extra)
			 nil))
	 (start (- in-buffer-length count)))
    (declare (type index start count))
    (cond ((zerop count)
dtc's avatar
dtc committed
	   (setf (lisp-stream-in-index stream) in-buffer-length)
	   (funcall (lisp-stream-in stream) stream eof-errorp eof-value))
	  (t
	   (when (/= start in-buffer-extra)
	     (bit-bash-copy ibuf (+ (* in-buffer-extra vm:byte-bits)
				    (* vm:vector-data-offset vm:word-bits))
			    ibuf (+ (the index (* start vm:byte-bits))
				    (* vm:vector-data-offset vm:word-bits))
			    (* count vm:byte-bits)))
dtc's avatar
dtc committed
	   (setf (lisp-stream-in-index stream) (1+ start))
	   (code-char (aref ibuf start))))))

;;; FAST-READ-CHAR-STRING-REFILL  --  Interface
;;;
;;;    This function is called by the fast-read-char expansion to refill the
;;; string-buffer for text streams.  There is definitely a
;;; string-buffer and an in-buffer, which implies there must be an
;;; n-bin method.
;;;
#+unicode
(defun fast-read-char-string-refill (stream eof-errorp eof-value)
  ;; Like fast-read-char-refill, but we don't need or want the
  ;; in-buffer-extra.
  (let* ((ibuf (lisp-stream-in-buffer stream))
	 (index (lisp-stream-in-index stream))
	 (in-length (fd-stream-in-length stream)))
    (declare (type (integer 0 #.in-buffer-length) index in-length))

    #+(or debug-frc-sr)
    (progn
      (format t "index = ~A~%" index)
      (format t "in-length = ~A~%" in-length)
      (format t "ibuf before = ~A~%" ibuf)
      (format t "sbuf before = ~S~%" (subseq (lisp-stream-string-buffer stream) 0
					     (lisp-stream-string-buffer-len stream))))

    ;; For debugging, clear out the stuff we've already read so we can
    ;; see what's happening.
    #+(or debug-frc-sr)
    (fill ibuf 0 :start 0 :end index)

    ;; Copy the stuff we haven't read from in-buffer to the beginning
    ;; of the buffer.
    (if (< index in-length)
	(replace ibuf ibuf
		 :start1 0
		 :start2 index :end2 in-length)
	(setf index in-length))

    ;; For debugging, clear out the stuff we've already read so we can
    ;; see what's happening.
    #+(or debug-frc-sr)
    (when (< index (1- in-buffer-length))
      (fill ibuf 0 :start (1+ index) :end in-buffer-length))
    
    (setf index (- in-length index))
    
    #+(or debug-frc-sr)
    (format t "ibuf after  = ~A~%" ibuf)
rtoy's avatar
rtoy committed
    (flet
	((get-octets (start)
	   (funcall (lisp-stream-n-bin stream) stream
		    ibuf start
		    (- in-buffer-length start)
		    nil))
	 (handle-eof ()
	   ;; Nothing left in the stream, so update our pointers to
	   ;; indicate we've read everything and call the stream-in
	   ;; function so that we do the right thing for eof.
	   (setf (lisp-stream-in-index stream) in-buffer-length)
	   (setf (lisp-stream-string-index stream)
		 (lisp-stream-string-buffer-len stream))
	   (funcall (lisp-stream-in stream) stream eof-errorp eof-value)))
      (let ((count (get-octets index)))
	(declare (type (integer 0 #.in-buffer-length) count))

	#+(or debug-frc-sr)
	(progn
	  (format t "count = ~D~%" count)
	  (format t "new ibuf = ~A~%" ibuf))
rtoy's avatar
rtoy committed
	(cond ((zerop count)
	       (handle-eof))
	      (t
	       (let ((sbuf (lisp-stream-string-buffer stream))
		     (slen (lisp-stream-string-buffer-len stream)))
		 (declare (simple-string sbuf)
			  (type (integer 0 #.(1+ in-buffer-length)) slen)
			  (optimize (speed 3)))

		 ;; Update in-length.  This is needed if we change the
		 ;; external-format of the stream because we need to
		 ;; know how many octets are valid (in case
		 ;; end-of-file was reached)
rtoy's avatar
rtoy committed
		 (setf (fd-stream-in-length stream) (+ count index))
		 #+(or debug-frc-sr)
		 (format t "in-length = ~D~%" (fd-stream-in-length stream))

		 #+(or debug-frc-sr)
		 (format t "slen = ~A~%" slen)

		 ;; Copy the last read character to the beginning of the
		 ;; buffer to support unreading.
		 (when (plusp slen)
		   (setf (schar sbuf 0) (schar sbuf (1- slen))))

		 #+(or debug-frc-sr)
		 (progn
		   (format t "sbuf[0] = ~S~%" (schar sbuf 0))
		   (format t "index = ~S~%" index))


		 ;; Convert all the octets, including the ones that we
		 ;; haven't processed yet and the ones we just read in.
		 (flet
		     ((convert-buffer ()
			(let ((old-state (fd-stream-oc-state stream)))
			  #+(or debug-frc-sr)
			  (format t "old-state = ~S~%" old-state)
			  (multiple-value-bind (s char-count octet-count new-state)
			      (stream::octets-to-string-counted
			       ibuf
			       (fd-stream-octet-count stream)
			       :start 0
			       :end (fd-stream-in-length stream)
			       :state (fd-stream-oc-state stream)
			       :string sbuf
			       :s-start 1
			       :external-format (fd-stream-external-format stream)
			       :error (fd-stream-octets-to-char-error stream))
			    (declare (ignore s)
				     (type (integer 0 #.in-buffer-length) char-count octet-count))
rtoy's avatar
rtoy committed
			    #+(or debug-frc-sr)
			    (progn
			      (format t "char-count = ~A~%" char-count)
			      (format t "octet-count = ~A~%" octet-count)
			      (format t "in-index = ~A~%" (lisp-stream-in-index stream))
			      (format t "new state = ~S~%" new-state))
			    ;; FIXME: We need to know if a BOM character was read so that
			    ;; we can adjust the octet count correctly because
			    ;; OCTETS-TO-CHAR does not include the BOM in the number of
			    ;; octets processed.  To do that, we look into the state, and
			    ;; thus is very fragile.  OCTETS-TO-CHAR and thus
			    ;; OCTETS-TO-STRING-COUNTED should indicate that instead of
			    ;; doing it here.
			    ;; So far, only utf-16 and utf-32 needs to handle BOM
			    ;; specially.  In both of these cases, (cadr state) contains
			    ;; information about whether a BOM character was read or not.
			    ;; If a BOM was read, then we need to increment the
			    ;; octet-count by 2 for the BOM because OCTETS-TO-STRING
			    ;; doesn't include that in its count.
			    ;;
			    ;; But we could have a composing external format too, like
			    ;; :crlf, so what we really want to look at is the last
			    ;; element of the state.
			    (when (and (consp (cdr new-state))
				       (not (eq (car (last old-state))
						(car (last new-state)))))
			      #+debug-frc-sr
			      (format t "state changed from ~S to ~S~%" old-state new-state)
			      ;; See utf-16.lisp and utf-32.lisp.  The part of the state
			      ;; we're interested in encodes the endianness and the size
			      ;; of the BOM in octets.
			      (incf octet-count (abs (the (integer -4 4)
						       (car (last new-state))))))
			    (when (> char-count 0)
			      (setf (fd-stream-oc-state stream) new-state)
			      (setf (lisp-stream-string-buffer-len stream) (1+ char-count))
			      (setf (lisp-stream-string-index stream) 2)
			      (setf (lisp-stream-in-index stream) octet-count)
			      #+(or debug-frc-sr)
			      (progn
				(format t "new in-index = ~A~%" (lisp-stream-in-index stream))
				(format t "new sbuf = ~S~%" 
					(subseq sbuf 0 (1+ char-count))))
			      (schar sbuf 1))))))
rtoy's avatar
rtoy committed
		   (let ((out (convert-buffer)))
		     (or out
			 ;; There weren't enough octets to convert at
			 ;; least one character.  Try to read some more
			 ;; octets and try again.  (If we still fail,
			 ;; what should we do then?  Currently, just
			 ;; just return NIL and let other parts of Lisp
			 ;; catch that.)
			 ;;
			 ;; The in buffer holds unread octets up to
			 ;; index in-length.  So start reading octets there.
			 (let* ((index (fd-stream-in-length stream))
				(count (get-octets index)))
			   (declare (type (integer 0 #.in-buffer-length) count index))
			   (cond ((zerop count)
				  (handle-eof))
				 (t
				  ;; Adjust in-length to the total
				  ;; number of octets that are now in
				  ;; the buffer.
				  (setf (fd-stream-in-length stream) (+ count index))
				  (convert-buffer))))))))))))))

;;; FAST-READ-BYTE-REFILL  --  Interface
;;;
;;;    Similar to FAST-READ-CHAR-REFILL, but we don't have to leave room for
;;; unreading.
;;;
(defun fast-read-byte-refill (stream eof-errorp eof-value)
dtc's avatar
dtc committed
  (let* ((ibuf (lisp-stream-in-buffer stream))
	 (count (funcall (lisp-stream-n-bin stream) stream
			 ibuf 0 in-buffer-length
			 nil))
	 (start (- in-buffer-length count)))
    (declare (type index start count))
    (cond ((zerop count)
dtc's avatar
dtc committed
	   (setf (lisp-stream-in-index stream) in-buffer-length)
	   (funcall (lisp-stream-bin stream) stream eof-errorp eof-value))
	  (t
	   (unless (zerop start)
	     (bit-bash-copy ibuf (* vm:vector-data-offset vm:word-bits)
			    ibuf (+ (the index (* start vm:byte-bits))
				    (* vm:vector-data-offset vm:word-bits))
			    (* count vm:byte-bits)))
dtc's avatar
dtc committed
	   (setf (lisp-stream-in-index stream) (1+ start))
	   (aref ibuf start)))))
  
ram's avatar
ram committed

;;; Output functions:

(defun write-char (character &optional (stream *standard-output*))
  "Outputs the Character to the Stream."
  (declare (type streamlike stream))
  (let ((stream (out-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%write-char stream character)
      ;; lisp-stream
      (funcall (lisp-stream-out stream) stream character)
      ;; fundamental-stream
      (stream-write-char stream character)))
ram's avatar
ram committed
  character)

(defun terpri (&optional (stream *standard-output*))
  "Outputs a new line to the Stream."
  (declare (type streamlike stream))
  (let ((stream (out-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%write-char stream #\Newline)
      ;; lisp-stream
      (funcall (lisp-stream-out stream) stream #\Newline)
      ;; fundamental-stream
      (stream-terpri stream)))
ram's avatar
ram committed
  nil)

(defun fresh-line (&optional (stream *standard-output*))
  "Outputs a new line to the Stream if it is not positioned at the beginning of
ram's avatar
ram committed
   a line.  Returns T if it output a new line, nil otherwise."
  (declare (type streamlike stream))
  (let ((stream (out-synonym-of stream)))
    (stream-dispatch stream
      ;; simple-stream
      (stream::%fresh-line stream)
      ;; lisp-stream
      (when (/= (or (charpos stream) 1) 0)
	(funcall (lisp-stream-out stream) stream #\newline)
	t)
      ;; fundamental-stream
      (stream-fresh-line stream))))
ram's avatar
ram committed

(defun write-string (string &optional (stream *standard-output*)
toy's avatar
toy committed
			    &key (start 0) end)
  "Outputs the String to the given Stream."
toy's avatar
toy committed
  (write-string* string stream start (or end (length (the vector string)))))