1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
3 ;;; gray-stream-methods.lisp --- Implementation using gray streams.
5 ;;; Copyright (C) 2006-2008, Stelian Ionescu <sionescu@common-lisp.net>
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :io.streams
)
26 ;;;; Instance Initialization
28 ;;; TODO: use the buffer pool
29 ;;; TODO: handle instance reinitialization
30 (defmethod shared-initialize :after
((stream dual-channel-gray-stream
) slot-names
31 &key
(input-buffer-size +bytes-per-iobuf
+)
32 (output-buffer-size +bytes-per-iobuf
+)
33 (external-format :default
))
34 (declare (ignore slot-names
))
35 (check-type input-buffer-size buffer-index
)
36 (check-type output-buffer-size buffer-index
)
37 (with-accessors ((ib input-buffer-of
)
39 (ef external-format-of
))
41 (setf ib
(allocate-iobuf input-buffer-size
)
42 ob
(allocate-iobuf output-buffer-size
)
47 (defmethod stream-element-type ((stream dual-channel-gray-stream
))
50 ;; TODO: use the buffer pool
51 (defmethod close :around
((stream dual-channel-gray-stream
) &key abort
)
52 (with-accessors ((ib input-buffer-of
)
53 (ob output-buffer-of
))
55 (unless (or abort
(null ib
)) (finish-output stream
))
56 (when ib
(free-iobuf ib
))
57 (when ob
(free-iobuf ob
))
62 (defmethod close ((stream dual-channel-gray-stream
) &key abort
)
63 (declare (ignore stream abort
)))
65 (defmethod (setf external-format-of
)
66 (external-format (stream dual-channel-gray-stream
))
67 (setf (slot-value stream
'external-format
)
68 (babel:ensure-external-format external-format
)))
72 (defun %to-octets
(buff start end ef
)
73 (babel:string-to-octets buff
:start start
:end end
74 :encoding
(babel:external-format-encoding ef
)))
76 (defmethod stream-clear-input ((stream dual-channel-gray-stream
))
77 (with-accessors ((ib input-buffer-of
))
82 (defun %fill-ibuf
(read-fn fd buf
&optional timeout
)
84 (let ((readablep (iomux:wait-until-fd-ready fd
:read timeout
)))
86 (return-from %fill-ibuf
:timeout
))))
87 (let ((num (nix:repeat-upon-eintr
88 (funcall read-fn fd
(iobuf-end-pointer buf
)
89 (iobuf-end-space-length buf
)))))
92 (incf (iobuf-end buf
) num
))))
94 (defun %read-into-simple-array-ub8
(stream array start end
)
95 (declare (type dual-channel-gray-stream stream
))
96 (with-accessors ((ib input-buffer-of
)
100 (let ((octets-needed (- end start
)))
101 (loop :with array-offset
:= start
102 :for octets-in-buffer
:= (iobuf-length ib
)
103 :for nbytes
:= (min octets-needed octets-in-buffer
)
104 :when
(plusp nbytes
) :do
105 (iobuf-copy-into-lisp-array ib
(iobuf-start ib
)
106 array array-offset nbytes
)
107 (incf array-offset nbytes
)
108 (decf octets-needed nbytes
)
109 (incf (iobuf-start ib
) nbytes
)
110 :if
(zerop octets-needed
) :do
(loop-finish)
111 :else
:do
(iobuf-reset ib
)
112 :when
(eq :eof
(%fill-ibuf read-fn fd ib
)) :do
(loop-finish)
113 :finally
(return array-offset
)))))
115 (defun %read-into-string
(stream string start end
)
116 (declare (type dual-channel-gray-stream stream
))
117 (loop :for offset
:from start
:below end
118 :for char
:= (stream-read-char stream
)
119 :if
(eq char
:eof
) :do
(loop-finish)
120 :else
:do
(setf (char string offset
) char
)
121 :finally
(return offset
)))
123 (defun %read-into-vector
(stream vector start end
)
124 (declare (type dual-channel-gray-stream stream
))
125 (loop :for offset
:from start
:below end
126 :for octet
:= (stream-read-byte stream
)
127 :if
(eq octet
:eof
) :do
(loop-finish)
128 :else
:do
(setf (aref vector offset
) octet
)
129 :finally
(return offset
)))
131 (defmacro check-bounds
(sequence start end
)
132 (with-gensyms (length)
133 `(let ((,length
(length ,sequence
)))
136 (unless (<= ,start
,end
,length
)
137 (error "Wrong sequence bounds. start: ~S end: ~S" ,start
,end
)))))
139 (declaim (inline %read-sequence
))
140 (defun %read-sequence
(stream seq start end
)
141 (check-bounds seq start end
)
144 (ub8-sarray (%read-into-simple-array-ub8 stream seq start end
))
145 (string (%read-into-string stream seq start end
))
146 (ub8-vector (%read-into-vector stream seq start end
)))))
148 (declaim (inline read-sequence
*))
149 (defun read-sequence* (stream sequence
&key
(start 0) end
)
150 (%read-sequence stream sequence start end
))
152 (defmethod stream-read-sequence
153 ((stream dual-channel-gray-stream
) sequence start end
&key
)
154 (%read-sequence stream sequence start end
))
158 (defun %write-n-bytes
(write-fn fd buf nbytes
&optional timeout
)
159 (declare (type stream-buffer buf
))
160 (let ((bytes-written 0))
161 (labels ((write-once ()
162 (let ((num (handler-case
163 (nix:repeat-upon-condition-decreasing-timeout
164 ((nix:eintr
) timeout-var timeout
)
166 (funcall write-fn fd
(inc-pointer buf bytes-written
)
168 (when (and timeout-var
(zerop timeout-var
))
169 (return-from %write-n-bytes
170 (values nil
:timeout
)))))
172 (return-from %write-n-bytes
(values nil
:eof
))))))
173 (unless (zerop num
) (incf bytes-written num
))))
177 ;; FIXME signal something better -- maybe analyze the status
178 (return-from %write-n-bytes
(values nil
:fail
)))))
179 (buffer-emptyp () (= bytes-written nbytes
))
180 (errorp () (handler-case (iomux:wait-until-fd-ready fd
:write
)
181 (iomux:poll-error
() t
)
182 (:no-error
(r w
) (declare (ignore r w
)) nil
))))
183 (loop :until
(buffer-emptyp) :do
(write-or-return)
184 :finally
(return (values t bytes-written
))))))
186 (defun %flush-obuf
(write-fn fd buf
&optional timeout
)
187 (declare (type iobuf buf
))
188 (let ((bytes-written 0))
189 (labels ((write-once ()
190 (let ((num (handler-case
191 (nix:repeat-upon-condition-decreasing-timeout
192 ((nix:eintr
) timeout-var timeout
)
194 (funcall write-fn fd
(iobuf-start-pointer buf
)
196 (when (and timeout-var
(zerop timeout-var
))
197 (return-from %flush-obuf
198 (values nil
:timeout
)))))
200 (return-from %flush-obuf
(values nil
:eof
))))))
202 (incf (iobuf-start buf
) num
)
203 (incf bytes-written num
))))
207 ;; FIXME signal something better -- maybe analyze the status
208 (return-from %flush-obuf
(values nil
:fail
)))))
210 (when (iobuf-empty-p buf
)
211 (iobuf-reset buf
) t
))
212 (errorp () (handler-case (iomux:wait-until-fd-ready fd
:write
)
213 (iomux:poll-error
() t
)
214 (:no-error
(r w
) (declare (ignore r w
)) nil
))))
215 (loop :until
(buffer-emptyp) :do
(write-or-return)
216 :finally
(return (values t bytes-written
))))))
218 ;;; TODO: add timeout support
219 (defun %flush-obuf-if-needed
(stream)
220 (declare (type dual-channel-gray-stream stream
))
221 (with-accessors ((fd output-fd-of
)
222 (write-fn write-fn-of
)
223 (ob output-buffer-of
)
226 (when (or dirtyp
(iobuf-full-p ob
))
227 (%flush-obuf write-fn fd ob
)
230 (defmethod stream-clear-output ((stream dual-channel-gray-stream
))
231 (with-accessors ((ob output-buffer-of
)
238 (defmethod stream-finish-output ((stream dual-channel-gray-stream
))
239 (with-accessors ((fd output-fd-of
)
240 (write-fn write-fn-of
)
241 (ob output-buffer-of
)
244 (%flush-obuf write-fn fd ob
)
248 (defmethod stream-force-output ((stream dual-channel-gray-stream
))
249 (setf (dirtyp stream
) t
))
251 (defun %write-simple-array-ub8
(stream array start end
)
252 (declare (type dual-channel-gray-stream stream
))
253 (with-accessors ((fd output-fd-of
)
254 (write-fn write-fn-of
)
255 (ob output-buffer-of
))
257 (let ((octets-needed (- end start
)))
258 (cond ((<= octets-needed
(iobuf-end-space-length ob
))
259 (iobuf-copy-from-lisp-array array start ob
260 (iobuf-end ob
) octets-needed
)
261 (incf (iobuf-end ob
) octets-needed
)
262 (%flush-obuf-if-needed stream
))
264 (with-pointer-to-vector-data (ptr array
)
265 (%flush-obuf write-fn fd ob
)
266 (let ((ret (%write-n-bytes write-fn fd
(inc-pointer ptr start
)
269 (incf (iobuf-end ob
) octets-needed
))))))
272 (defun %write-vector-ub8
(stream vector start end
)
273 (declare (type dual-channel-gray-stream stream
))
274 (%write-simple-array-ub8 stream
(coerce vector
'ub8-sarray
) start end
))
276 (defun %write-vector
(stream vector start end
)
277 (declare (type dual-channel-gray-stream stream
))
278 (loop :for offset
:from start
:below end
279 :for octet
:= (aref vector offset
)
280 :do
(stream-write-byte stream octet
)
281 :finally
(return vector
)))
283 (declaim (inline %write-sequence
))
284 (defun %write-sequence
(stream seq start end
)
285 (check-bounds seq start end
)
288 (ub8-sarray (%write-simple-array-ub8 stream seq start end
))
289 (string (stream-write-string stream seq start end
))
290 (ub8-vector (%write-vector-ub8 stream seq start end
))
291 (vector (%write-vector stream seq start end
)))))
293 (declaim (inline write-sequence
*))
294 (defun write-sequence* (stream sequence
&key
(start 0) end
)
295 (%write-sequence stream sequence start end
))
297 (defmethod stream-write-sequence ((stream dual-channel-gray-stream
)
298 sequence start end
&key
)
299 (%write-sequence stream sequence start end
))
303 (defun maybe-find-line-ending (read-fn fd ib ef
)
304 (let* ((start-off (iobuf-start ib
))
305 (char-code (bref ib start-off
)))
307 (ecase (babel:external-format-eol-style ef
)
308 (:lf
(when (= char-code
(char-code #\Linefeed
))
309 (incf (iobuf-start ib
))
311 (:cr
(when (= char-code
(char-code #\Return
))
312 (incf (iobuf-start ib
))
314 (:crlf
(when (= char-code
(char-code #\Return
))
315 (when (and (= (iobuf-length ib
) 1)
316 (eq :eof
(%fill-ibuf read-fn fd ib
)))
317 (incf (iobuf-start ib
))
319 (when (= (bref ib
(1+ start-off
))
320 (char-code #\Linefeed
))
321 (incf (iobuf-start ib
) 2)
322 (return #\Newline
))))))))
324 (defconstant +max-octets-per-char
+ 6)
326 ;;; FIXME: currently we return :EOF when read(2) returns 0
327 ;;; we should distinguish hard end-of-files (EOF and buffer empty)
328 ;;; from soft end-of-files (EOF and *some* bytes still in the buffer
329 ;;; but not enough to make a full character)
330 (defmethod stream-read-char ((stream dual-channel-gray-stream
))
331 (with-accessors ((fd input-fd-of
)
334 (unread-index ibuf-unread-index-of
)
335 (ef external-format-of
))
337 (setf unread-index
(iobuf-start ib
))
340 (flet ((fill-buf-or-eof ()
341 (setf ret
(%fill-ibuf read-fn fd ib
))
343 (return-from stream-read-char
:eof
))))
344 (cond ((zerop (iobuf-length ib
))
347 ;; Some encodings such as CESU or Java's modified UTF-8 take
348 ;; as much as 6 bytes per character. Make sure we have enough
349 ;; space to collect read-ahead bytes if required.
350 ((< (iobuf-length ib
) +max-octets-per-char
+)
351 (iobuf-copy-data-to-start ib
)
352 (setf unread-index
0)))
354 (when-let ((it (maybe-find-line-ending read-fn fd ib ef
)))
355 (return-from stream-read-char it
))
358 (setf (values str ret
)
359 (foreign-string-to-lisp
361 :offset
(iobuf-start ib
)
362 :count
(iobuf-length ib
)
363 :encoding
(babel:external-format-encoding ef
)
365 (babel:end-of-input-in-character
()
368 (incf (iobuf-start ib
) ret
))
371 (defun maybe-find-line-ending-no-hang (fd ib ef
)
372 (declare (ignore fd
))
373 (let* ((start-off (iobuf-start ib
))
374 (char-code (bref ib start-off
)))
376 (ecase (babel:external-format-eol-style ef
)
377 (:lf
(when (= char-code
(char-code #\Linefeed
))
378 (incf (iobuf-start ib
))
380 (:cr
(when (= char-code
(char-code #\Return
))
381 (incf (iobuf-start ib
))
383 (:crlf
(when (= char-code
(char-code #\Return
))
384 (when (= (iobuf-length ib
) 1)
385 (incf (iobuf-start ib
))
386 (return :starvation
))
387 (when (= (bref ib
(1+ start-off
))
388 (char-code #\Linefeed
))
389 (incf (iobuf-start ib
) 2)
390 (return #\Newline
))))))))
392 (defmethod stream-read-char-no-hang ((stream dual-channel-gray-stream
))
393 (with-accessors ((fd input-fd-of
)
396 (ef external-format-of
))
402 ;; BUG: this comparision is probably buggy, FIXME. A similar
403 ;; bug was fixed in STREAM-READ-CHAR. Must write a test for
405 (when (< 0 (iobuf-end-space-length ib
) 4)
406 (iobuf-copy-data-to-start ib
))
407 (when (and (iomux:fd-ready-p fd
:read
)
408 (eq :eof
(%fill-ibuf read-fn fd ib
)))
410 (when (zerop (iobuf-length ib
))
411 (return (if eof
:eof nil
)))
413 (let ((line-end (maybe-find-line-ending-no-hang fd ib ef
)))
414 (cond ((eq line-end
:starvation
)
415 (return (if eof
#\Return nil
)))
416 ((characterp line-end
)
420 (setf (values str ret
)
421 (foreign-string-to-lisp
423 :offset
(iobuf-start ib
)
424 :count
(iobuf-length ib
)
425 :encoding
(babel:external-format-encoding ef
)
427 (babel:end-of-input-in-character
()
429 (incf (iobuf-start ib
) ret
)
432 (defun %stream-unread-char
(stream)
433 (declare (type dual-channel-gray-stream stream
))
434 (with-accessors ((ib input-buffer-of
)
435 (unread-index ibuf-unread-index-of
))
437 (symbol-macrolet ((start (iobuf-start ib
)))
439 ((> start unread-index
) (setf start unread-index
))
440 (t (error "No uncommitted character to unread")))))
443 (defmethod stream-unread-char ((stream dual-channel-gray-stream
) character
)
444 (declare (ignore character
))
445 (%stream-unread-char stream
))
447 (defmethod stream-peek-char ((stream dual-channel-gray-stream
))
448 (let ((char (stream-read-char stream
)))
449 (cond ((eq char
:eof
) :eof
)
450 (t (%stream-unread-char stream
)
453 ;; (defmethod stream-read-line ((stream dual-channel-gray-stream))
456 (defmethod stream-listen ((stream dual-channel-gray-stream
))
457 (let ((char (stream-read-char-no-hang stream
)))
458 (cond ((characterp char
) (stream-unread-char stream char
) t
)
462 ;;;; Character Output
464 (defmethod stream-write-char ((stream dual-channel-gray-stream
)
465 (character character
))
466 (%flush-obuf-if-needed stream
)
467 (if (char= character
#\Newline
)
468 (%write-line-terminator
469 stream
(babel:external-format-eol-style
(external-format-of stream
)))
470 ;; FIXME: avoid consing a string here. At worst, declare it dynamic-extent
471 (stream-write-string stream
(make-string 1 :initial-element character
))))
473 (defmethod stream-line-column ((stream dual-channel-gray-stream
))
476 (defmethod stream-start-line-p ((stream dual-channel-gray-stream
))
479 (defmethod stream-terpri ((stream dual-channel-gray-stream
))
480 (write-char #\Newline stream
) nil
)
482 (defmethod stream-fresh-line ((stream dual-channel-gray-stream
))
483 (write-char #\Newline stream
) t
)
485 (define-constant +unix-line-terminator
+
486 (make-array 1 :element-type
'ub8
:initial-contents
'(10))
489 (define-constant +dos-line-terminator
+
490 (make-array 2 :element-type
'ub8
:initial-contents
'(13 10))
493 (define-constant +mac-line-terminator
+
494 (make-array 1 :element-type
'ub8
:initial-contents
'(13))
497 (defun %write-line-terminator
(stream line-terminator
)
498 (case line-terminator
499 (:lf
(%write-simple-array-ub8 stream
+unix-line-terminator
+ 0 1))
500 (:cr
(%write-simple-array-ub8 stream
+mac-line-terminator
+ 0 1))
501 (:crlf
(%write-simple-array-ub8 stream
+dos-line-terminator
+ 0 2))))
503 (defmethod stream-write-string ((stream dual-channel-gray-stream
)
504 (string string
) &optional
(start 0) end
)
505 (check-bounds string start end
)
508 (ef (external-format-of stream
))
509 (line-terminator (babel:external-format-eol-style ef
)))
510 (loop :for off1
:= start
:then
(1+ off2
)
511 :for nl-off
:= (position #\Newline string
:start off1
)
512 :for off2
:= (or nl-off end
)
513 :when nl-off
:do
(%write-line-terminator stream line-terminator
)
514 :when
(> off2 off1
) :do
515 ;; FIXME: should probably convert directly to a foreign buffer?
516 (setf octets
(%to-octets string off1 off2 ef
))
517 (%write-simple-array-ub8 stream octets
0 (length octets
))
518 :while
(< off2 end
))))
523 (defmethod stream-read-byte ((stream dual-channel-gray-stream
))
524 (with-accessors ((fd input-fd-of
)
526 (ib input-buffer-of
))
528 (flet ((fill-buf-or-eof ()
530 (when (eq :eof
(%fill-ibuf read-fn fd ib
))
531 (return-from stream-read-byte
:eof
))))
532 (when (zerop (iobuf-length ib
))
534 (iobuf-pop-octet ib
))))
538 (defmethod stream-write-byte ((stream dual-channel-gray-stream
) integer
)
539 (check-type integer ub8
"an unsigned 8-bit value")
540 (with-accessors ((ob output-buffer-of
))
542 (%flush-obuf-if-needed stream
)
543 (iobuf-push-octet ob integer
)))