diff --git a/README.md b/README.md
index 2eb7597..2cfa047 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
-# fd-stream
+# unistd-stream
-fd-stream streams are `(unsigned-byte 8)` cl-stream streams
+unistd streams are `(unsigned-byte 8)` cl-stream streams
using `unistd:read`, `unistd:write` and `unistd:close` to
operate on Unix file descriptors.
@@ -9,26 +9,26 @@ Depends on :
- https://github.com/cffi-posix/cffi-unistd
- https://github.com/cl-stream/cl-stream
-## Class: fd-stream
+## Class: unistd-stream
Base class for file descriptor streams.
-### Generic: stream-fd *fd-stream* => *fd*
-Returns the file descriptor of FD-STREAM.
+### Generic: stream-fd *unistd-stream* => *fd*
+Returns the file descriptor of UNISTD-STREAM.
-## Class: fd-input-stream
+## Class: unistd-input-stream
A buffered input stream using UNISTD:READ.
-### Function: fd-input-stream *fd* => *stream*
+### Function: unistd-input-stream *fd* => *stream*
Creates a buffered input stream for file descriptor *fd*.
-## Class: fd-output-stream
+## Class: unistd-output-stream
A buffered output stream using UNISTD:WRITE.
-### Function: fd-output-stream *fd* => *stream*
+### Function: unistd-output-stream *fd* => *stream*
Creates a buffered output stream for file descriptor *fd*.
-## Class: fd-io-stream
+## Class: unistd-io-stream
A buffered input/output stream using UNISTD:READ and UNISTD:WRITE.
-### Function: fd-io-stream *fd* => *stream*
+### Function: unistd-io-stream *fd* => *stream*
Creates a buffered input/output stream for file descriptor *fd*.
diff --git a/fd-stream.asd b/fd-stream.asd
deleted file mode 100644
index f85f3d5..0000000
--- a/fd-stream.asd
+++ /dev/null
@@ -1,36 +0,0 @@
-;;
-;; fd-stream - Unix file descriptor layer for cl-stream
-;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
-;;
-;; Permission to use, copy, modify, and distribute this software for any
-;; purpose with or without fee is hereby granted, provided that the above
-;; copyright notice and this permission notice appear in all copies.
-;;
-;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-;;
-
-(in-package :common-lisp-user)
-
-(defpackage :fd-stream.system
- (:use :common-lisp :asdf))
-
-(in-package :fd-stream.system)
-
-(defsystem :fd-stream
- :name "fd-stream"
- :author "Thomas de Grivel <thoxdg@gmail.com>"
- :version "0.1"
- :description "Unix file descriptor stream streams for Common Lisp"
- :depends-on ("cffi-fcntl"
- "cffi-unistd"
- "cl-stream")
- :components
- ((:file "package")
- (:file "fd-stream" :depends-on ("package"))))
diff --git a/fd-stream.lisp b/fd-stream.lisp
deleted file mode 100644
index 9cc73e9..0000000
--- a/fd-stream.lisp
+++ /dev/null
@@ -1,191 +0,0 @@
-;;
-;; fd-stream - Unix file descriptor layer for cl-stream
-;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
-;;
-;; Permission to use, copy, modify, and distribute this software for any
-;; purpose with or without fee is hereby granted, provided that the above
-;; copyright notice and this permission notice appear in all copies.
-;;
-;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-;;
-
-(in-package :fd-stream)
-
-(deftype fixnum+ (&optional (start 0))
- `(integer ,start ,most-positive-fixnum))
-
-(defclass fd-stream (stream)
- ((fd :initarg :fd
- :reader stream-fd
- :type file-descriptor)
- (blocking-p :initarg :blocking-p
- :type boolean))
- (:documentation "Base class for file descriptor streams."))
-
-(defmethod stream-blocking-p ((stream fd-stream))
- (or (when (slot-boundp stream 'blocking-p)
- (slot-value stream 'blocking-p))
- (setf (slot-value stream 'blocking-p)
- (let ((flags (fcntl:getfl (stream-fd stream))))
- (not (= 0 (logand fcntl:+o-nonblock+ flags)))))))
-
-(defmethod (setf stream-blocking-p) (value (stream fd-stream))
- (let* ((fd (stream-fd stream))
- (flags (fcntl:getfl fd))
- (o-nonblock (not (= 0 (logand fcntl:+o-nonblock+ flags)))))
- (cond
- ((and value o-nonblock)
- t)
- ((and (not value) (not o-nonblock))
- nil)
- (t
- (fcntl:setfl fd (if value
- (logand (lognot fcntl:+o-nonblock+) flags)
- (logior fcntl:+o-nonblock+ flags)))
- (setf (slot-value stream 'blocking-p) value)))))
-
-(defmethod stream-element-type ((stream fd-stream))
- '(unsigned-byte 8))
-
-(defmethod close ((stream fd-stream))
- (unistd:close (stream-fd stream))
- (call-next-method))
-
-(defclass fd-input-stream (fd-stream buffered-input-stream)
- ()
- (:documentation "A buffered input stream using UNISTD:READ."))
-
-(defmethod make-stream-input-buffer ((stream fd-input-stream))
- (cffi:foreign-alloc :unsigned-char
- :count (stream-input-buffer-size stream)))
-
-(defmethod discard-stream-input-buffer ((stream fd-input-stream))
- (cffi:foreign-free (stream-input-buffer stream))
- (setf (stream-input-buffer stream) nil))
-
-(defmethod stream-fill-input-buffer ((stream fd-input-stream))
- (let* ((buffer (stream-input-buffer stream))
- (length (stream-input-length stream))
- (fd (stream-fd stream))
- (buf (cffi:mem-aptr buffer :unsigned-char length))
- (buflen (- (stream-input-buffer-size stream) length))
- (r (if (stream-blocking-p stream)
- (unistd:read fd buf buflen)
- (unistd:read-non-blocking fd buf buflen))))
- (cond ((eq :non-blocking r)
- :non-blocking)
- ((= r 0)
- :eof)
- ((< r 0)
- (error 'stream-input-error :stream stream))
- (t
- (incf (stream-input-length stream) r)
- nil))))
-
-(defmethod stream-read-element-from-buffer ((stream fd-input-stream))
- (let ((element (cffi:mem-aref (stream-input-buffer stream) :unsigned-char
- (stream-input-index stream))))
- (incf (stream-input-index stream))
- (values element nil)))
-
-(defun fd-input-stream (fd)
- "Creates a buffered input stream for file descriptor FD."
- (make-instance 'fd-input-stream :fd fd))
-
-(defclass fd-output-stream (fd-stream buffered-output-stream)
- ()
- (:documentation "A buffered output stream using UNISTD:WRITE."))
-
-(defmethod make-stream-output-buffer ((stream fd-output-stream))
- (cffi:foreign-alloc :unsigned-char
- :count (stream-output-buffer-size stream)))
-
-(defmethod discard-stream-output-buffer ((stream fd-output-stream))
- (cffi:foreign-free (stream-output-buffer stream))
- (setf (stream-output-buffer stream) nil))
-
-(defmethod stream-flush-output-buffer ((stream fd-output-stream))
- (let ((buffer (stream-output-buffer stream)))
- (let* ((fd (stream-fd stream))
- (index (stream-output-index stream))
- (buf (cffi:mem-aptr buffer :unsigned-char index))
- (buflen (- (stream-output-length stream) index))
- (r (if (stream-blocking-p stream)
- (unistd:write fd buf buflen)
- (unistd:write-non-blocking fd buf buflen))))
- (cond ((eq :non-blocking r)
- :non-blocking)
- ((= 0 r)
- :eof)
- ((< r 0)
- (error 'stream-output-error :stream stream))
- (t
- (incf (stream-output-index stream) r)
- (when (= (stream-output-index stream)
- (stream-output-length stream))
- (setf (stream-output-index stream) 0
- (stream-output-length stream) 0))
- nil)))))
-
-(defmethod stream-write-element-to-buffer ((stream fd-output-stream)
- element)
- (setf (cffi:mem-aref (stream-output-buffer stream) :unsigned-char
- (stream-output-length stream))
- element)
- (incf (stream-output-length stream))
- nil)
-
-(defun fd-output-stream (fd)
- "Creates a buffered output stream for file descriptor FD."
- (make-instance 'fd-output-stream :fd fd))
-
-(defclass fd-io-stream (fd-input-stream fd-output-stream)
- ()
- (:documentation "A buffered input/output stream using
-UNISTD:READ and UNISTD:WRITE."))
-
-(defmethod close ((stream fd-io-stream))
- (call-next-method)
- (cffi:foreign-free (stream-input-buffer stream))
- (cffi:foreign-free (stream-output-buffer stream)))
-
-(defun fd-io-stream (fd)
- "Creates a buffered input/output stream for file descriptor FD."
- (make-instance 'fd-io-stream :fd fd))
-
-(defun open-file (pathname &key
- (append nil)
- (blocking t)
- (create t)
- (direction :io)
- (input-buffer-size *default-buffer-size*)
- (mode #o777)
- (output-buffer-size *default-buffer-size*))
- (let* ((flags (logior (if append
- fcntl:+o-append+
- 0)
- (if blocking
- 0
- fcntl:+o-nonblock+)
- (if create
- fcntl:+o-creat+
- 0)
- (ecase direction
- ((:input) fcntl:+o-rdonly+)
- ((:output) fcntl:+o-wronly+)
- ((:io) fcntl:+o-rdwr+))))
- (fd (fcntl:open pathname flags mode)))
- (make-instance (case direction
- ((:input) 'fd-input-stream)
- ((:output) 'fd-output-stream)
- ((:io) 'fd-io-stream))
- :fd fd
- :input-buffer-size input-buffer-size
- :output-buffer-size output-buffer-size)))
diff --git a/package.lisp b/package.lisp
index 4d08469..8549521 100644
--- a/package.lisp
+++ b/package.lisp
@@ -1,7 +1,7 @@
;;
-;; fd-stream - Unix file descriptor layer for cl-stream
+;; unistd-stream - Unix file descriptor layer for cl-stream
;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
@@ -18,14 +18,14 @@
(in-package :common-lisp-user)
-(defpackage :fd-stream
+(defpackage :unistd-stream
(:use
:common-lisp
:cl-stream)
#.(cl-stream:shadowing-import-from)
(:export
- #:fd-input-stream
- #:fd-io-stream
- #:fd-output-stream
- #:fd-stream
- #:open-file))
+ #:unistd-input-stream
+ #:unistd-io-stream
+ #:unistd-output-stream
+ #:unistd-stream
+ #:unistd-stream-open))
diff --git a/unistd-stream.asd b/unistd-stream.asd
new file mode 100644
index 0000000..be1414f
--- /dev/null
+++ b/unistd-stream.asd
@@ -0,0 +1,36 @@
+;;
+;; unistd-stream - Unix file descriptor layer for cl-stream
+;;
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
+;;
+;; Permission to use, copy, modify, and distribute this software for any
+;; purpose with or without fee is hereby granted, provided that the above
+;; copyright notice and this permission notice appear in all copies.
+;;
+;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :common-lisp-user)
+
+(defpackage :unistd-stream.system
+ (:use :common-lisp :asdf))
+
+(in-package :unistd-stream.system)
+
+(defsystem :unistd-stream
+ :name "unistd-stream"
+ :author "Thomas de Grivel <thoxdg@gmail.com>"
+ :version "0.1"
+ :description "Unix file descriptor layer for cl-stream"
+ :depends-on ("cffi-fcntl"
+ "cffi-unistd"
+ "cl-stream")
+ :components
+ ((:file "package")
+ (:file "unistd-stream" :depends-on ("package"))))
diff --git a/unistd-stream.lisp b/unistd-stream.lisp
new file mode 100644
index 0000000..a9a876e
--- /dev/null
+++ b/unistd-stream.lisp
@@ -0,0 +1,191 @@
+;;
+;; unistd-stream - Unix file descriptor layer for cl-stream
+;;
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
+;;
+;; Permission to use, copy, modify, and distribute this software for any
+;; purpose with or without fee is hereby granted, provided that the above
+;; copyright notice and this permission notice appear in all copies.
+;;
+;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :unistd-stream)
+
+(deftype fixnum+ (&optional (start 0))
+ `(integer ,start ,most-positive-fixnum))
+
+(defclass unistd-stream (stream)
+ ((fd :initarg :fd
+ :reader stream-fd
+ :type file-descriptor)
+ (blocking-p :initarg :blocking-p
+ :type boolean))
+ (:documentation "Base class for file descriptor streams."))
+
+(defmethod stream-blocking-p ((stream unistd-stream))
+ (or (when (slot-boundp stream 'blocking-p)
+ (slot-value stream 'blocking-p))
+ (setf (slot-value stream 'blocking-p)
+ (let ((flags (fcntl:getfl (stream-fd stream))))
+ (not (= 0 (logand fcntl:+o-nonblock+ flags)))))))
+
+(defmethod (setf stream-blocking-p) (value (stream unistd-stream))
+ (let* ((fd (stream-fd stream))
+ (flags (fcntl:getfl fd))
+ (o-nonblock (not (= 0 (logand fcntl:+o-nonblock+ flags)))))
+ (cond
+ ((and value o-nonblock)
+ t)
+ ((and (not value) (not o-nonblock))
+ nil)
+ (t
+ (fcntl:setfl fd (if value
+ (logand (lognot fcntl:+o-nonblock+) flags)
+ (logior fcntl:+o-nonblock+ flags)))
+ (setf (slot-value stream 'blocking-p) value)))))
+
+(defmethod stream-element-type ((stream unistd-stream))
+ '(unsigned-byte 8))
+
+(defmethod close ((stream unistd-stream))
+ (unistd:close (stream-fd stream))
+ (call-next-method))
+
+(defclass unistd-input-stream (unistd-stream buffered-input-stream)
+ ()
+ (:documentation "A buffered input stream using UNISTD:READ."))
+
+(defmethod make-stream-input-buffer ((stream unistd-input-stream))
+ (cffi:foreign-alloc :unsigned-char
+ :count (stream-input-buffer-size stream)))
+
+(defmethod discard-stream-input-buffer ((stream unistd-input-stream))
+ (cffi:foreign-free (stream-input-buffer stream))
+ (setf (stream-input-buffer stream) nil))
+
+(defmethod stream-fill-input-buffer ((stream unistd-input-stream))
+ (let* ((buffer (stream-input-buffer stream))
+ (length (stream-input-length stream))
+ (fd (stream-fd stream))
+ (buf (cffi:mem-aptr buffer :unsigned-char length))
+ (buflen (- (stream-input-buffer-size stream) length))
+ (r (if (stream-blocking-p stream)
+ (unistd:read fd buf buflen)
+ (unistd:read-non-blocking fd buf buflen))))
+ (cond ((eq :non-blocking r)
+ :non-blocking)
+ ((= r 0)
+ :eof)
+ ((< r 0)
+ (error 'stream-input-error :stream stream))
+ (t
+ (incf (stream-input-length stream) r)
+ nil))))
+
+(defmethod stream-read-element-from-buffer ((stream unistd-input-stream))
+ (let ((element (cffi:mem-aref (stream-input-buffer stream) :unsigned-char
+ (stream-input-index stream))))
+ (incf (stream-input-index stream))
+ (values element nil)))
+
+(defun unistd-input-stream (fd)
+ "Creates a buffered input stream for file descriptor FD."
+ (make-instance 'unistd-input-stream :fd fd))
+
+(defclass unistd-output-stream (unistd-stream buffered-output-stream)
+ ()
+ (:documentation "A buffered output stream using UNISTD:WRITE."))
+
+(defmethod make-stream-output-buffer ((stream unistd-output-stream))
+ (cffi:foreign-alloc :unsigned-char
+ :count (stream-output-buffer-size stream)))
+
+(defmethod discard-stream-output-buffer ((stream unistd-output-stream))
+ (cffi:foreign-free (stream-output-buffer stream))
+ (setf (stream-output-buffer stream) nil))
+
+(defmethod stream-flush-output-buffer ((stream unistd-output-stream))
+ (let ((buffer (stream-output-buffer stream)))
+ (let* ((fd (stream-fd stream))
+ (index (stream-output-index stream))
+ (buf (cffi:mem-aptr buffer :unsigned-char index))
+ (buflen (- (stream-output-length stream) index))
+ (r (if (stream-blocking-p stream)
+ (unistd:write fd buf buflen)
+ (unistd:write-non-blocking fd buf buflen))))
+ (cond ((eq :non-blocking r)
+ :non-blocking)
+ ((= 0 r)
+ :eof)
+ ((< r 0)
+ (error 'stream-output-error :stream stream))
+ (t
+ (incf (stream-output-index stream) r)
+ (when (= (stream-output-index stream)
+ (stream-output-length stream))
+ (setf (stream-output-index stream) 0
+ (stream-output-length stream) 0))
+ nil)))))
+
+(defmethod stream-write-element-to-buffer ((stream unistd-output-stream)
+ element)
+ (setf (cffi:mem-aref (stream-output-buffer stream) :unsigned-char
+ (stream-output-length stream))
+ element)
+ (incf (stream-output-length stream))
+ nil)
+
+(defun unistd-output-stream (fd)
+ "Creates a buffered output stream for file descriptor FD."
+ (make-instance 'unistd-output-stream :fd fd))
+
+(defclass unistd-io-stream (unistd-input-stream unistd-output-stream)
+ ()
+ (:documentation "A buffered input/output stream using
+UNISTD:READ and UNISTD:WRITE."))
+
+(defmethod close ((stream unistd-io-stream))
+ (call-next-method)
+ (cffi:foreign-free (stream-input-buffer stream))
+ (cffi:foreign-free (stream-output-buffer stream)))
+
+(defun unistd-io-stream (fd)
+ "Creates a buffered input/output stream for file descriptor FD."
+ (make-instance 'unistd-io-stream :fd fd))
+
+(defun unistd-stream-open (pathname &key
+ (append nil)
+ (blocking t)
+ (create t)
+ (direction :io)
+ (input-buffer-size *stream-default-buffer-size*)
+ (mode #o777)
+ (output-buffer-size *stream-default-buffer-size*))
+ (let* ((flags (logior (if append
+ fcntl:+o-append+
+ 0)
+ (if blocking
+ 0
+ fcntl:+o-nonblock+)
+ (if create
+ fcntl:+o-creat+
+ 0)
+ (ecase direction
+ ((:input) fcntl:+o-rdonly+)
+ ((:output) fcntl:+o-wronly+)
+ ((:io) fcntl:+o-rdwr+))))
+ (fd (fcntl:open pathname flags mode)))
+ (make-instance (case direction
+ ((:input) 'unistd-input-stream)
+ ((:output) 'unistd-output-stream)
+ ((:io) 'unistd-io-stream))
+ :fd fd
+ :input-buffer-size input-buffer-size
+ :output-buffer-size output-buffer-size)))