ports.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. ;;;; ports.scm --- R6RS port API -*- coding: utf-8 -*-
  2. ;;;; Copyright (C) 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Ludovic Courtès <ludo@gnu.org>
  18. ;;; Commentary:
  19. ;;;
  20. ;;; The I/O port API of the R6RS is provided by this module. In many areas
  21. ;;; it complements or refines Guile's own historical port API. For instance,
  22. ;;; it allows for binary I/O with bytevectors.
  23. ;;;
  24. ;;; Code:
  25. (library (rnrs io ports (6))
  26. (export eof-object eof-object?
  27. ;; auxiliary types
  28. file-options buffer-mode buffer-mode?
  29. eol-style native-eol-style error-handling-mode
  30. make-transcoder transcoder-codec transcoder-eol-style
  31. transcoder-error-handling-mode native-transcoder
  32. latin-1-codec utf-8-codec utf-16-codec
  33. ;; input & output ports
  34. port? input-port? output-port?
  35. port-eof?
  36. port-transcoder binary-port? textual-port? transcoded-port
  37. port-position set-port-position!
  38. port-has-port-position? port-has-set-port-position!?
  39. call-with-port close-port
  40. ;; input ports
  41. open-bytevector-input-port
  42. open-string-input-port
  43. open-file-input-port
  44. make-custom-binary-input-port
  45. ;; binary input
  46. get-u8 lookahead-u8
  47. get-bytevector-n get-bytevector-n!
  48. get-bytevector-some get-bytevector-all
  49. ;; output ports
  50. open-bytevector-output-port
  51. open-string-output-port
  52. open-file-output-port
  53. make-custom-binary-output-port
  54. call-with-bytevector-output-port
  55. call-with-string-output-port
  56. make-custom-textual-output-port
  57. flush-output-port
  58. ;; input/output ports
  59. open-file-input/output-port
  60. ;; binary output
  61. put-u8 put-bytevector
  62. ;; textual input
  63. get-char get-datum get-line get-string-all get-string-n get-string-n!
  64. lookahead-char
  65. ;; textual output
  66. put-char put-datum put-string
  67. ;; standard ports
  68. standard-input-port standard-output-port standard-error-port
  69. current-input-port current-output-port current-error-port
  70. ;; condition types
  71. &i/o i/o-error? make-i/o-error
  72. &i/o-read i/o-read-error? make-i/o-read-error
  73. &i/o-write i/o-write-error? make-i/o-write-error
  74. &i/o-invalid-position i/o-invalid-position-error?
  75. make-i/o-invalid-position-error
  76. &i/o-filename i/o-filename-error? make-i/o-filename-error
  77. i/o-error-filename
  78. &i/o-file-protection i/o-file-protection-error?
  79. make-i/o-file-protection-error
  80. &i/o-file-is-read-only i/o-file-is-read-only-error?
  81. make-i/o-file-is-read-only-error
  82. &i/o-file-already-exists i/o-file-already-exists-error?
  83. make-i/o-file-already-exists-error
  84. &i/o-file-does-not-exist i/o-file-does-not-exist-error?
  85. make-i/o-file-does-not-exist-error
  86. &i/o-port i/o-port-error? make-i/o-port-error
  87. i/o-error-port
  88. &i/o-decoding-error i/o-decoding-error?
  89. make-i/o-decoding-error
  90. &i/o-encoding-error i/o-encoding-error?
  91. make-i/o-encoding-error i/o-encoding-error-char)
  92. (import (ice-9 binary-ports)
  93. (only (rnrs base) assertion-violation)
  94. (rnrs enums)
  95. (rnrs records syntactic)
  96. (rnrs exceptions)
  97. (rnrs conditions)
  98. (rnrs files) ;for the condition types
  99. (srfi srfi-8)
  100. (ice-9 rdelim)
  101. (except (guile) raise display)
  102. (prefix (only (guile) display)
  103. guile:))
  104. ;;;
  105. ;;; Auxiliary types
  106. ;;;
  107. (define-enumeration file-option
  108. (no-create no-fail no-truncate)
  109. file-options)
  110. (define-enumeration buffer-mode
  111. (none line block)
  112. buffer-modes)
  113. (define (buffer-mode? symbol)
  114. (enum-set-member? symbol (enum-set-universe (buffer-modes))))
  115. (define-enumeration eol-style
  116. (lf cr crlf nel crnel ls none)
  117. eol-styles)
  118. (define (native-eol-style)
  119. (eol-style none))
  120. (define-enumeration error-handling-mode
  121. (ignore raise replace)
  122. error-handling-modes)
  123. (define-record-type (transcoder %make-transcoder transcoder?)
  124. (fields codec eol-style error-handling-mode))
  125. (define* (make-transcoder codec
  126. #:optional
  127. (eol-style (native-eol-style))
  128. (handling-mode (error-handling-mode replace)))
  129. (%make-transcoder codec eol-style handling-mode))
  130. (define (native-transcoder)
  131. (make-transcoder (or (fluid-ref %default-port-encoding)
  132. (latin-1-codec))))
  133. (define (latin-1-codec)
  134. "ISO-8859-1")
  135. (define (utf-8-codec)
  136. "UTF-8")
  137. (define (utf-16-codec)
  138. "UTF-16")
  139. ;;;
  140. ;;; Internal helpers
  141. ;;;
  142. (define (with-i/o-filename-conditions filename thunk)
  143. (with-throw-handler 'system-error
  144. thunk
  145. (lambda args
  146. (let ((errno (system-error-errno args)))
  147. (let ((construct-condition
  148. (cond ((= errno EACCES)
  149. make-i/o-file-protection-error)
  150. ((= errno EEXIST)
  151. make-i/o-file-already-exists-error)
  152. ((= errno ENOENT)
  153. make-i/o-file-does-not-exist-error)
  154. ((= errno EROFS)
  155. make-i/o-file-is-read-only-error)
  156. (else
  157. make-i/o-filename-error))))
  158. (raise (construct-condition filename)))))))
  159. (define (with-i/o-port-error port make-primary-condition thunk)
  160. (with-throw-handler 'system-error
  161. thunk
  162. (lambda args
  163. (let ((errno (system-error-errno args)))
  164. (if (memv errno (list EIO EFBIG ENOSPC EPIPE))
  165. (raise (condition (make-primary-condition)
  166. (make-i/o-port-error port)))
  167. (apply throw args))))))
  168. (define-syntax with-textual-output-conditions
  169. (syntax-rules ()
  170. ((_ port body0 body ...)
  171. (with-i/o-port-error port make-i/o-write-error
  172. (lambda () (with-i/o-encoding-error body0 body ...))))))
  173. (define-syntax with-textual-input-conditions
  174. (syntax-rules ()
  175. ((_ port body0 body ...)
  176. (with-i/o-port-error port make-i/o-read-error
  177. (lambda () (with-i/o-decoding-error body0 body ...))))))
  178. ;;;
  179. ;;; Input and output ports.
  180. ;;;
  181. (define (port-transcoder port)
  182. "Return the transcoder object associated with @var{port}, or @code{#f}
  183. if the port has no transcoder."
  184. (and (textual-port? port)
  185. ;; All textual ports have transcoders.
  186. (make-transcoder
  187. (port-encoding port)
  188. (native-eol-style)
  189. (case (port-conversion-strategy port)
  190. ((error) 'raise)
  191. ((substitute) 'replace)
  192. (else
  193. (assertion-violation 'port-transcoder
  194. "unsupported error handling mode"))))))
  195. (define (binary-port? port)
  196. "Always returns @code{#t}, as all ports can be used for binary I/O in
  197. Guile."
  198. (equal? (port-encoding port) "ISO-8859-1"))
  199. (define (textual-port? port)
  200. "Always returns @code{#t}, as all ports can be used for textual I/O in
  201. Guile."
  202. #t)
  203. (define (port-eof? port)
  204. (eof-object? (if (binary-port? port)
  205. (lookahead-u8 port)
  206. (lookahead-char port))))
  207. (define (transcoded-port port transcoder)
  208. "Return a new textual port based on @var{port}, using
  209. @var{transcoder} to encode and decode data written to or
  210. read from its underlying binary port @var{port}."
  211. ;; Hackily get at %make-transcoded-port.
  212. (let ((result ((@@ (ice-9 binary-ports) %make-transcoded-port) port)))
  213. (set-port-encoding! result (transcoder-codec transcoder))
  214. (case (transcoder-error-handling-mode transcoder)
  215. ((raise)
  216. (set-port-conversion-strategy! result 'error))
  217. ((replace)
  218. (set-port-conversion-strategy! result 'substitute))
  219. (else
  220. (error "unsupported error handling mode"
  221. (transcoder-error-handling-mode transcoder))))
  222. result))
  223. (define (port-position port)
  224. "Return the offset (an integer) indicating where the next octet will be
  225. read from/written to in @var{port}."
  226. ;; FIXME: We should raise an `&assertion' error when not supported.
  227. (seek port 0 SEEK_CUR))
  228. (define (set-port-position! port offset)
  229. "Set the position where the next octet will be read from/written to
  230. @var{port}."
  231. ;; FIXME: We should raise an `&assertion' error when not supported.
  232. (seek port offset SEEK_SET))
  233. (define (port-has-port-position? port)
  234. "Return @code{#t} is @var{port} supports @code{port-position}."
  235. (and (false-if-exception (port-position port)) #t))
  236. (define (port-has-set-port-position!? port)
  237. "Return @code{#t} is @var{port} supports @code{set-port-position!}."
  238. (and (false-if-exception (set-port-position! port (port-position port)))
  239. #t))
  240. (define (call-with-port port proc)
  241. "Call @var{proc}, passing it @var{port} and closing @var{port} upon exit of
  242. @var{proc}. Return the return values of @var{proc}."
  243. (call-with-values
  244. (lambda () (proc port))
  245. (lambda vals
  246. (close-port port)
  247. (apply values vals))))
  248. (define* (call-with-bytevector-output-port proc #:optional (transcoder #f))
  249. (receive (port extract) (open-bytevector-output-port transcoder)
  250. (call-with-port port proc)
  251. (extract)))
  252. (define (open-string-input-port str)
  253. "Open an input port that will read from @var{str}."
  254. (open-input-string str))
  255. (define (r6rs-open filename mode buffer-mode transcoder)
  256. (let ((port (with-i/o-filename-conditions filename
  257. (lambda ()
  258. (with-fluids ((%default-port-encoding #f))
  259. (open filename mode))))))
  260. (cond (transcoder
  261. (set-port-encoding! port (transcoder-codec transcoder))))
  262. port))
  263. (define (file-options->mode file-options base-mode)
  264. (logior base-mode
  265. (if (enum-set-member? 'no-create file-options)
  266. 0
  267. O_CREAT)
  268. (if (enum-set-member? 'no-truncate file-options)
  269. 0
  270. O_TRUNC)
  271. (if (enum-set-member? 'no-fail file-options)
  272. 0
  273. O_EXCL)))
  274. (define* (open-file-input-port filename
  275. #:optional
  276. (file-options (file-options))
  277. (buffer-mode (buffer-mode block))
  278. transcoder)
  279. "Return an input port for reading from @var{filename}."
  280. (r6rs-open filename O_RDONLY buffer-mode transcoder))
  281. (define* (open-file-input/output-port filename
  282. #:optional
  283. (file-options (file-options))
  284. (buffer-mode (buffer-mode block))
  285. transcoder)
  286. "Return a port for reading from and writing to @var{filename}."
  287. (r6rs-open filename
  288. (file-options->mode file-options O_RDWR)
  289. buffer-mode
  290. transcoder))
  291. (define (open-string-output-port)
  292. "Return two values: an output port that will collect characters written to it
  293. as a string, and a thunk to retrieve the characters associated with that port."
  294. (let ((port (open-output-string)))
  295. (values port
  296. (lambda () (get-output-string port)))))
  297. (define* (open-file-output-port filename
  298. #:optional
  299. (file-options (file-options))
  300. (buffer-mode (buffer-mode block))
  301. maybe-transcoder)
  302. "Return an output port for writing to @var{filename}."
  303. (r6rs-open filename
  304. (file-options->mode file-options O_WRONLY)
  305. buffer-mode
  306. maybe-transcoder))
  307. (define (call-with-string-output-port proc)
  308. "Call @var{proc}, passing it a string output port. When @var{proc} returns,
  309. return the characters accumulated in that port."
  310. (let ((port (open-output-string)))
  311. (proc port)
  312. (get-output-string port)))
  313. (define (make-custom-textual-output-port id
  314. write!
  315. get-position
  316. set-position!
  317. close)
  318. (make-soft-port (vector (lambda (c) (write! (string c) 0 1))
  319. (lambda (s) (write! s 0 (string-length s)))
  320. #f ;flush
  321. #f ;read character
  322. close)
  323. "w"))
  324. (define (flush-output-port port)
  325. (force-output port))
  326. ;;;
  327. ;;; Textual output.
  328. ;;;
  329. (define-condition-type &i/o-encoding &i/o-port
  330. make-i/o-encoding-error i/o-encoding-error?
  331. (char i/o-encoding-error-char))
  332. (define-syntax with-i/o-encoding-error
  333. (syntax-rules ()
  334. "Convert Guile throws to `encoding-error' to `&i/o-encoding-error'."
  335. ((_ body ...)
  336. ;; XXX: This is heavyweight for small functions like `put-char'.
  337. (with-throw-handler 'encoding-error
  338. (lambda ()
  339. (begin body ...))
  340. (lambda (key subr message errno port chr)
  341. (raise (make-i/o-encoding-error port chr)))))))
  342. (define (put-char port char)
  343. (with-textual-output-conditions port (write-char char port)))
  344. (define (put-datum port datum)
  345. (with-textual-output-conditions port (write datum port)))
  346. (define* (put-string port s #:optional start count)
  347. (with-textual-output-conditions port
  348. (cond ((not (string? s))
  349. (assertion-violation 'put-string "expected string" s))
  350. ((and start count)
  351. (display (substring/shared s start (+ start count)) port))
  352. (start
  353. (display (substring/shared s start (string-length s)) port))
  354. (else
  355. (display s port)))))
  356. ;; Defined here to be able to make use of `with-i/o-encoding-error', but
  357. ;; not exported from here, but from `(rnrs io simple)'.
  358. (define* (display object #:optional (port (current-output-port)))
  359. (with-textual-output-conditions port (guile:display object port)))
  360. ;;;
  361. ;;; Textual input.
  362. ;;;
  363. (define-condition-type &i/o-decoding &i/o-port
  364. make-i/o-decoding-error i/o-decoding-error?)
  365. (define-syntax with-i/o-decoding-error
  366. (syntax-rules ()
  367. "Convert Guile throws to `decoding-error' to `&i/o-decoding-error'."
  368. ((_ body ...)
  369. ;; XXX: This is heavyweight for small functions like `get-char' and
  370. ;; `lookahead-char'.
  371. (with-throw-handler 'decoding-error
  372. (lambda ()
  373. (begin body ...))
  374. (lambda (key subr message errno port)
  375. (raise (make-i/o-decoding-error port)))))))
  376. (define (get-char port)
  377. (with-textual-input-conditions port (read-char port)))
  378. (define (get-datum port)
  379. (with-textual-input-conditions port (read port)))
  380. (define (get-line port)
  381. (with-textual-input-conditions port (read-line port 'trim)))
  382. (define (get-string-all port)
  383. (with-textual-input-conditions port (read-string port)))
  384. (define (get-string-n port count)
  385. "Read up to @var{count} characters from @var{port}.
  386. If no characters could be read before encountering the end of file,
  387. return the end-of-file object, otherwise return a string containing
  388. the characters read."
  389. (let* ((s (make-string count))
  390. (rv (get-string-n! port s 0 count)))
  391. (cond ((eof-object? rv) rv)
  392. ((= rv count) s)
  393. (else (substring/shared s 0 rv)))))
  394. (define (lookahead-char port)
  395. (with-textual-input-conditions port (peek-char port)))
  396. ;;;
  397. ;;; Standard ports.
  398. ;;;
  399. (define (standard-input-port)
  400. (with-fluids ((%default-port-encoding #f))
  401. (dup->inport 0)))
  402. (define (standard-output-port)
  403. (with-fluids ((%default-port-encoding #f))
  404. (dup->outport 1)))
  405. (define (standard-error-port)
  406. (with-fluids ((%default-port-encoding #f))
  407. (dup->outport 2)))
  408. )
  409. ;;; ports.scm ends here