1 ;;;; ports.scm --- R6RS port API -*- coding: utf-8 -*-
3 ;;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ;;; Author: Ludovic Courtès <ludo@gnu.org>
23 ;;; The I/O port API of the R6RS is provided by this module. In many areas
24 ;;; it complements or refines Guile's own historical port API. For instance,
25 ;;; it allows for binary I/O with bytevectors.
29 (library (rnrs io ports (6))
30 (export eof-object eof-object?
33 file-options buffer-mode buffer-mode?
34 eol-style native-eol-style error-handling-mode
35 make-transcoder transcoder-codec transcoder-eol-style
36 transcoder-error-handling-mode native-transcoder
37 latin-1-codec utf-8-codec utf-16-codec
39 ;; input & output ports
40 port? input-port? output-port?
42 port-transcoder binary-port? textual-port? transcoded-port
43 port-position set-port-position!
44 port-has-port-position? port-has-set-port-position!?
45 call-with-port close-port
48 open-bytevector-input-port
49 open-string-input-port
51 make-custom-binary-input-port
55 get-bytevector-n get-bytevector-n!
56 get-bytevector-some get-bytevector-all
59 open-bytevector-output-port
60 open-string-output-port
62 make-custom-binary-output-port
63 call-with-bytevector-output-port
64 call-with-string-output-port
65 make-custom-textual-output-port
72 get-char get-datum get-line get-string-all get-string-n get-string-n!
76 put-char put-datum put-string
79 standard-input-port standard-output-port standard-error-port
80 current-input-port current-output-port current-error-port
83 &i/o i/o-error? make-i/o-error
84 &i/o-read i/o-read-error? make-i/o-read-error
85 &i/o-write i/o-write-error? make-i/o-write-error
86 &i/o-invalid-position i/o-invalid-position-error?
87 make-i/o-invalid-position-error
88 &i/o-filename i/o-filename-error? make-i/o-filename-error
90 &i/o-file-protection i/o-file-protection-error?
91 make-i/o-file-protection-error
92 &i/o-file-is-read-only i/o-file-is-read-only-error?
93 make-i/o-file-is-read-only-error
94 &i/o-file-already-exists i/o-file-already-exists-error?
95 make-i/o-file-already-exists-error
96 &i/o-file-does-not-exist i/o-file-does-not-exist-error?
97 make-i/o-file-does-not-exist-error
98 &i/o-port i/o-port-error? make-i/o-port-error
100 &i/o-decoding-error i/o-decoding-error?
101 make-i/o-decoding-error
102 &i/o-encoding-error i/o-encoding-error?
103 make-i/o-encoding-error i/o-encoding-error-char)
104 (import (ice-9 binary-ports)
105 (only (rnrs base) assertion-violation)
107 (rnrs records syntactic)
110 (rnrs files) ;for the condition types
113 (except (guile) raise display)
114 (prefix (only (guile) display)
123 (define-enumeration file-option
124 (no-create no-fail no-truncate)
127 (define-enumeration buffer-mode
131 (define (buffer-mode? symbol)
132 (enum-set-member? symbol (enum-set-universe (buffer-modes))))
134 (define-enumeration eol-style
135 (lf cr crlf nel crnel ls none)
138 (define (native-eol-style)
141 (define-enumeration error-handling-mode
142 (ignore raise replace)
143 error-handling-modes)
145 (define-record-type (transcoder %make-transcoder transcoder?)
146 (fields codec eol-style error-handling-mode))
148 (define* (make-transcoder codec
150 (eol-style (native-eol-style))
151 (handling-mode (error-handling-mode replace)))
152 (%make-transcoder codec eol-style handling-mode))
154 (define (native-transcoder)
155 (make-transcoder (or (fluid-ref %default-port-encoding)
158 (define (latin-1-codec)
161 (define (utf-8-codec)
164 (define (utf-16-codec)
172 (define (with-i/o-filename-conditions filename thunk)
173 (with-throw-handler 'system-error
176 (let ((errno (system-error-errno args)))
177 (let ((construct-condition
178 (cond ((= errno EACCES)
179 make-i/o-file-protection-error)
181 make-i/o-file-already-exists-error)
183 make-i/o-file-does-not-exist-error)
185 make-i/o-file-is-read-only-error)
187 make-i/o-filename-error))))
188 (raise (construct-condition filename)))))))
190 (define (with-i/o-port-error port make-primary-condition thunk)
191 (with-throw-handler 'system-error
194 (let ((errno (system-error-errno args)))
195 (if (memv errno (list EIO EFBIG ENOSPC EPIPE))
196 (raise (condition (make-primary-condition)
197 (make-i/o-port-error port)))
198 (apply throw args))))))
200 (define-syntax with-textual-output-conditions
202 ((_ port body0 body ...)
203 (with-i/o-port-error port make-i/o-write-error
204 (lambda () (with-i/o-encoding-error body0 body ...))))))
206 (define-syntax with-textual-input-conditions
208 ((_ port body0 body ...)
209 (with-i/o-port-error port make-i/o-read-error
210 (lambda () (with-i/o-decoding-error body0 body ...))))))
214 ;;; Input and output ports.
217 (define (port-transcoder port)
218 "Return the transcoder object associated with @var{port}, or @code{#f}
219 if the port has no transcoder."
220 (cond ((port-encoding port)
221 => (lambda (encoding)
225 (case (port-conversion-strategy port)
227 ((substitute) 'replace)
229 (assertion-violation 'port-transcoder
230 "unsupported error handling mode"))))))
234 (define (binary-port? port)
235 "Returns @code{#t} if @var{port} does not have an associated encoding,
236 @code{#f} otherwise."
237 (not (port-encoding port)))
239 (define (textual-port? port)
240 "Always returns @code{#t}, as all ports can be used for textual I/O in
244 (define (port-eof? port)
245 (eof-object? (if (binary-port? port)
247 (lookahead-char port))))
249 (define (transcoded-port port transcoder)
250 "Return a new textual port based on @var{port}, using
251 @var{transcoder} to encode and decode data written to or
252 read from its underlying binary port @var{port}."
253 ;; Hackily get at %make-transcoded-port.
254 (let ((result ((@@ (ice-9 binary-ports) %make-transcoded-port) port)))
255 (set-port-encoding! result (transcoder-codec transcoder))
256 (case (transcoder-error-handling-mode transcoder)
258 (set-port-conversion-strategy! result 'error))
260 (set-port-conversion-strategy! result 'substitute))
262 (error "unsupported error handling mode"
263 (transcoder-error-handling-mode transcoder))))
266 (define (port-position port)
267 "Return the offset (an integer) indicating where the next octet will be
268 read from/written to in @var{port}."
270 ;; FIXME: We should raise an `&assertion' error when not supported.
271 (seek port 0 SEEK_CUR))
273 (define (set-port-position! port offset)
274 "Set the position where the next octet will be read from/written to
277 ;; FIXME: We should raise an `&assertion' error when not supported.
278 (seek port offset SEEK_SET))
280 (define (port-has-port-position? port)
281 "Return @code{#t} is @var{port} supports @code{port-position}."
282 (and (false-if-exception (port-position port)) #t))
284 (define (port-has-set-port-position!? port)
285 "Return @code{#t} is @var{port} supports @code{set-port-position!}."
286 (and (false-if-exception (set-port-position! port (port-position port)))
289 (define (call-with-port port proc)
290 "Call @var{proc}, passing it @var{port} and closing @var{port} upon exit of
291 @var{proc}. Return the return values of @var{proc}."
293 (lambda () (proc port))
296 (apply values vals))))
298 (define* (call-with-bytevector-output-port proc #:optional (transcoder #f))
299 (receive (port extract) (open-bytevector-output-port transcoder)
300 (call-with-port port proc)
303 (define (open-string-input-port str)
304 "Open an input port that will read from @var{str}."
305 (with-fluids ((%default-port-encoding "UTF-8"))
306 (open-input-string str)))
308 (define* (open-file-input-port filename
310 (file-options (file-options))
311 (buffer-mode (buffer-mode block))
313 (let ((port (with-i/o-filename-conditions filename
315 (with-fluids ((%default-port-encoding #f))
316 (open filename O_RDONLY))))))
317 (cond (maybe-transcoder
318 (set-port-encoding! port (transcoder-codec maybe-transcoder))))
321 (define (open-string-output-port)
322 "Return two values: an output port that will collect characters written to it
323 as a string, and a thunk to retrieve the characters associated with that port."
324 (let ((port (with-fluids ((%default-port-encoding "UTF-8"))
325 (open-output-string))))
327 (lambda () (get-output-string port)))))
329 (define* (open-file-output-port filename
331 (file-options (file-options))
332 (buffer-mode (buffer-mode block))
334 (let* ((flags (logior O_WRONLY
335 (if (enum-set-member? 'no-create file-options)
338 (if (enum-set-member? 'no-truncate file-options)
341 (if (enum-set-member? 'no-fail file-options)
344 (port (with-i/o-filename-conditions filename
346 (with-fluids ((%default-port-encoding #f))
347 (open filename flags))))))
348 (cond (maybe-transcoder
349 (set-port-encoding! port (transcoder-codec maybe-transcoder))))
352 (define (call-with-string-output-port proc)
353 "Call @var{proc}, passing it a string output port. When @var{proc} returns,
354 return the characters accumulated in that port."
355 (let ((port (open-output-string)))
357 (get-output-string port)))
359 (define (make-custom-textual-output-port id
364 (make-soft-port (vector (lambda (c) (write! (string c) 0 1))
365 (lambda (s) (write! s 0 (string-length s)))
371 (define (flush-output-port port)
379 (define-condition-type &i/o-encoding &i/o-port
380 make-i/o-encoding-error i/o-encoding-error?
381 (char i/o-encoding-error-char))
383 (define-syntax with-i/o-encoding-error
385 "Convert Guile throws to `encoding-error' to `&i/o-encoding-error'."
387 ;; XXX: This is heavyweight for small functions like `put-char'.
388 (with-throw-handler 'encoding-error
391 (lambda (key subr message errno port chr)
392 (raise (make-i/o-encoding-error port chr)))))))
394 (define (put-char port char)
395 (with-textual-output-conditions port (write-char char port)))
397 (define (put-datum port datum)
398 (with-textual-output-conditions port (write datum port)))
400 (define* (put-string port s #:optional start count)
401 (with-textual-output-conditions port
402 (cond ((not (string? s))
403 (assertion-violation 'put-string "expected string" s))
405 (display (substring/shared s start (+ start count)) port))
407 (display (substring/shared s start (string-length s)) port))
411 ;; Defined here to be able to make use of `with-i/o-encoding-error', but
412 ;; not exported from here, but from `(rnrs io simple)'.
413 (define* (display object #:optional (port (current-output-port)))
414 (with-textual-output-conditions port (guile:display object port)))
421 (define-condition-type &i/o-decoding &i/o-port
422 make-i/o-decoding-error i/o-decoding-error?)
424 (define-syntax with-i/o-decoding-error
426 "Convert Guile throws to `decoding-error' to `&i/o-decoding-error'."
428 ;; XXX: This is heavyweight for small functions like `get-char' and
430 (with-throw-handler 'decoding-error
433 (lambda (key subr message errno port)
434 (raise (make-i/o-decoding-error port)))))))
436 (define (get-char port)
437 (with-textual-input-conditions port (read-char port)))
439 (define (get-datum port)
440 (with-textual-input-conditions port (read port)))
442 (define (get-line port)
443 (with-textual-input-conditions port (read-line port 'trim)))
445 (define (get-string-all port)
446 (with-textual-input-conditions port (read-delimited "" port 'concat)))
448 (define (get-string-n port count)
449 "Read up to @var{count} characters from @var{port}.
450 If no characters could be read before encountering the end of file,
451 return the end-of-file object, otherwise return a string containing
452 the characters read."
453 (let* ((s (make-string count))
454 (rv (get-string-n! port s 0 count)))
455 (cond ((eof-object? rv) rv)
457 (else (substring/shared s 0 rv)))))
459 (define (lookahead-char port)
460 (with-textual-input-conditions port (peek-char port)))
467 (define (standard-input-port)
468 (with-fluids ((%default-port-encoding #f))
471 (define (standard-output-port)
472 (with-fluids ((%default-port-encoding #f))
475 (define (standard-error-port)
476 (with-fluids ((%default-port-encoding #f))
481 ;;; ports.scm ends here