port.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Ports and port handlers
  3. ; (discloser <port>) -> (<symbol> <value> ...)
  4. ; (close <port>) -> whatever
  5. ;
  6. ; Input ports
  7. ; (byte <port> <read?>) -> <byte>
  8. ; (char <port> <mode>) -> <char>
  9. ; <mode> says whether we're doing ...
  10. ; - #t: a PEEK
  11. ; - #f: a READ
  12. ; - (): CHAR-READY?
  13. ; (block <port> <buffer> <start> <count>) -> <byte count>
  14. ; (ready? <port>) -> <boolean>
  15. ;
  16. ; Output ports
  17. ; (byte <port> <byte>) -> whatever
  18. ; (char <port> <char>) -> whatever
  19. ; (block <port> <buffer> <start> <count>) -> whatever
  20. ; (ready? <port>) -> <boolean>
  21. ; (force-output <port>) -> whatever
  22. (define-record-type port-handler :port-handler
  23. (make-port-handler discloser close byte char block ready? force)
  24. port-handler?
  25. (discloser port-handler-discloser)
  26. (close port-handler-close)
  27. (byte port-handler-byte)
  28. (char port-handler-char)
  29. (block port-handler-block)
  30. (ready? port-handler-ready?)
  31. (force port-handler-force)) ; only used for output
  32. ;----------------
  33. ; Disclosing ports by calling the disclose handler.
  34. (define (disclose-port port)
  35. ((port-handler-discloser (port-handler port))
  36. port))
  37. (define-method &disclose ((port :input-port))
  38. (disclose-port port))
  39. (define-method &disclose ((port :output-port))
  40. (disclose-port port))
  41. ;----------------
  42. ; Set up VM exception handlers for the three unnecessary I/O primitives,
  43. ; READ-BYTE, PEEK-BYTE, and WRITE-BYTE. These do the right thing in
  44. ; the case of unbuffered ports or buffer overflow or underflow.
  45. ;
  46. ; This is abstracted to avoid a circular module dependency.
  47. (define (initialize-i/o-handlers! define-vm-exception-handler signal-exception)
  48. (define-vm-exception-handler (enum op read-byte)
  49. (one-arg-proc->handler (lambda (port)
  50. ((port-handler-byte (port-handler port))
  51. port
  52. #t))
  53. signal-vm-exception))
  54. (define-vm-exception-handler (enum op peek-byte)
  55. (one-arg-proc->handler (lambda (port)
  56. ((port-handler-byte (port-handler port))
  57. port
  58. #f))
  59. signal-vm-exception))
  60. (define-vm-exception-handler (enum op read-char)
  61. (one-arg-proc->handler (lambda (port)
  62. ((port-handler-char (port-handler port))
  63. port
  64. #f))
  65. signal-vm-exception))
  66. (define-vm-exception-handler (enum op peek-char)
  67. (one-arg-proc->handler (lambda (port)
  68. ((port-handler-char (port-handler port))
  69. port
  70. #t))
  71. signal-vm-exception))
  72. (define-vm-exception-handler (enum op write-byte)
  73. (two-arg-proc->handler (lambda (byte port)
  74. ((port-handler-byte (port-handler port))
  75. port
  76. byte))
  77. signal-vm-exception))
  78. (define-vm-exception-handler (enum op write-char)
  79. (two-arg-proc->handler (lambda (ch port)
  80. ((port-handler-char (port-handler port))
  81. port
  82. ch))
  83. signal-vm-exception)))
  84. ; Check the VM exception and then lock the port.
  85. (define (one-arg-proc->handler proc signal-vm-exception)
  86. (lambda (opcode reason port)
  87. (if (= reason (enum exception buffer-full/empty))
  88. (proc port)
  89. (signal-vm-exception opcode reason port))))
  90. ; This could combined with on-arg-... if the port were the first argument.
  91. (define (two-arg-proc->handler proc signal-vm-exception)
  92. (lambda (opcode reason arg port)
  93. (if (= reason (enum exception buffer-full/empty))
  94. (proc arg port)
  95. (signal-vm-exception opcode reason arg port))))
  96. ;----------------
  97. ; Wrappers for the various port operations. These check types and arguments
  98. ; and then call the appropriate handler procedure.
  99. (define (real-char-ready? port)
  100. (cond
  101. ((open-input-port? port)
  102. ((port-handler-char (port-handler port)) port '()))
  103. (else
  104. (call-error "invalid argument" char-ready? port))))
  105. ; See if there is a character available. BYTE-READY? itself is defined
  106. ; in current-ports.scm as it needs CURRENT-INPUT-PORT when called with
  107. ; no arguments.
  108. (define (real-byte-ready? port)
  109. (if (open-input-port? port)
  110. ((port-handler-ready? (port-handler port))
  111. port)
  112. (call-error "invalid argument" real-byte-ready? port)))
  113. ; Reading in a block of characters at once.
  114. (define (read-block buffer start count port . maybe-wait?)
  115. (if (and (port? port)
  116. (open-input-port? port)
  117. (okay-limits? buffer
  118. start
  119. count))
  120. (if (= count 0)
  121. 0
  122. ((port-handler-block (port-handler port))
  123. port
  124. buffer
  125. start
  126. count
  127. (or (null? maybe-wait?)
  128. (car maybe-wait?))))
  129. (call-error "invalid argument" read-block buffer start count port)))
  130. ; Write the COUNT bytes beginning at START from BUFFER to PORT.
  131. (define (write-block buffer start count port)
  132. (if (and (port? port)
  133. (open-output-port? port)
  134. (okay-limits? buffer start count))
  135. (if (< 0 count)
  136. ((port-handler-block (port-handler port))
  137. port
  138. buffer
  139. start
  140. count))
  141. (call-error "invalid argument" write-block buffer start count port)))
  142. (define (write-string string port)
  143. (do ((size (string-length string))
  144. (i 0 (+ 1 i)))
  145. ((>= i size) (unspecific))
  146. (write-char (string-ref string i) port)))
  147. ; BYTE-READY? for output ports.
  148. (define (output-port-ready? port)
  149. (if (open-output-port? port)
  150. ((port-handler-ready? (port-handler port))
  151. port)
  152. (call-error "invalid argument" output-port-ready? port)))
  153. ; Forcing output.
  154. (define (force-output port)
  155. (if (open-output-port? port)
  156. ((port-handler-force (port-handler port))
  157. port
  158. #t) ; raise error if PORT is not open
  159. (call-error "invalid argument" force-output port)))
  160. (define (force-output-if-open port)
  161. (if (open-output-port? port)
  162. ((port-handler-force (port-handler port))
  163. port
  164. #f))) ; do not raise error if PORT is not open
  165. ; Closing input and output ports.
  166. ; RnRS says that CLOSE-{IN|OUT}PUT-PORT is idempotent.
  167. (define (close-input-port port)
  168. (if (input-port? port)
  169. (begin
  170. (if (open-input-port? port)
  171. ((port-handler-close (port-handler port))
  172. port))
  173. (unspecific))
  174. (call-error "invalid argument" close-input-port port)))
  175. (define (close-output-port port)
  176. (if (output-port? port)
  177. (begin
  178. (if (open-output-port? port)
  179. ((port-handler-close (port-handler port))
  180. port))
  181. (unspecific))
  182. (call-error "invalid argument" close-output-port port)))
  183. ;----------------
  184. (define (port-text-codec p)
  185. (spec->text-codec (port-text-codec-spec p)))
  186. (define (set-port-text-codec! p codec)
  187. (set-port-text-codec-spec! p (text-codec->spec codec)))
  188. ;----------------
  189. ; Check that BUFFER contains COUNT characters starting from START.
  190. (define (okay-limits? buffer start count)
  191. (and (integer? start)
  192. (exact? start)
  193. (<= 0 start)
  194. (integer? count)
  195. (exact? count)
  196. (<= 0 count)
  197. (<= (+ start count)
  198. (cond ((byte-vector? buffer)
  199. (byte-vector-length buffer))
  200. (else
  201. -1)))))
  202. ;----------------
  203. ; Is PORT open?
  204. (define (open-port? port)
  205. (not (= 0 (bitwise-and open-port-mask (provisional-port-status port)))))
  206. (define open-port-mask
  207. (bitwise-ior (arithmetic-shift 1 (enum port-status-options open-for-input))
  208. (arithmetic-shift 1 (enum port-status-options open-for-output))))
  209. ;----------------
  210. ; Input ports
  211. (define input-port-mask
  212. (arithmetic-shift 1
  213. (enum port-status-options input)))
  214. (define open-input-port-mask
  215. (arithmetic-shift 1
  216. (enum port-status-options open-for-input)))
  217. (define open-input-port-status
  218. (bitwise-ior input-port-mask
  219. open-input-port-mask))
  220. (define (open-input-port? port)
  221. (not (= 0 (bitwise-and open-input-port-mask
  222. (provisional-port-status port)))))
  223. (define (make-input-port-closed! port)
  224. (provisional-set-port-status! port
  225. (bitwise-and (provisional-port-status port)
  226. (bitwise-not open-input-port-mask))))
  227. ;----------------
  228. ; Output ports
  229. (define output-port-mask
  230. (arithmetic-shift 1
  231. (enum port-status-options output)))
  232. (define open-output-port-mask
  233. (arithmetic-shift 1
  234. (enum port-status-options open-for-output)))
  235. (define open-output-port-status
  236. (bitwise-ior output-port-mask
  237. open-output-port-mask))
  238. (define (open-output-port? port)
  239. (not (= 0 (bitwise-and open-output-port-mask
  240. (provisional-port-status port)))))
  241. (define (make-output-port-closed! port)
  242. (provisional-set-port-status! port
  243. (bitwise-and (provisional-port-status port)
  244. (bitwise-not open-output-port-mask))))
  245. (define (make-unbuffered-output-port handler data)
  246. (if (port-handler? handler)
  247. (make-port handler
  248. (enum text-encoding-option latin-1)
  249. #f
  250. open-output-port-status
  251. #f ; lock (not used in unbuffered ports)
  252. data
  253. (make-byte-vector 128 0) ; buffer
  254. #f ; index
  255. #f ; limit
  256. #f ; pending-cr?
  257. #f) ; pending-eof?
  258. (call-error "invalid argument"
  259. make-unbuffered-output-port handler data)))
  260. (define (make-one-byte-handler write-block)
  261. (lambda (port byte)
  262. (let ((buffer (port-buffer port)))
  263. (byte-vector-set! buffer 0 byte)
  264. (let loop ()
  265. (if (= 0 (write-block port buffer 0 1))
  266. (loop))))))
  267. (define (make-one-char-handler write-block)
  268. (lambda (port ch)
  269. (let ((buffer (port-buffer port))
  270. (encode-char
  271. (text-codec-encode-char-proc (port-text-codec port))))
  272. (let ((encode-count
  273. (if (and (port-crlf? port)
  274. (char=? ch #\newline))
  275. (atomically
  276. (call-with-values
  277. (lambda ()
  278. (encode-char cr
  279. buffer 0 (byte-vector-length buffer)))
  280. (lambda (ok? encode-count-cr)
  281. ;; OK? must be true
  282. (call-with-values
  283. (lambda ()
  284. (encode-char #\newline
  285. buffer
  286. encode-count-cr
  287. (- (byte-vector-length buffer) encode-count-cr)))
  288. (lambda (ok? encode-count-lf)
  289. ;; OK? must be true
  290. (+ encode-count-cr encode-count-lf))))))
  291. (atomically
  292. (call-with-values
  293. (lambda ()
  294. (encode-char ch
  295. buffer 0 (byte-vector-length buffer)))
  296. (lambda (ok? encode-count)
  297. (if ok?
  298. encode-count
  299. ;; hrmpfl ...
  300. (call-with-values
  301. (lambda ()
  302. (encode-char #\?
  303. buffer 0 (byte-vector-length buffer)))
  304. (lambda (ok? encode-count)
  305. encode-count)))))))))
  306. (let loop ((index 0))
  307. (let* ((to-write (- encode-count index))
  308. (written
  309. (write-block port buffer index to-write)))
  310. (if (< written to-write)
  311. (loop (+ index written)))))))))
  312. (define cr (ascii->char 13))
  313. (define (make-write-block-handler write-block)
  314. (lambda (port buffer start count)
  315. (let loop ((sent 0))
  316. (let ((sent (+ sent
  317. (write-block port
  318. buffer
  319. (+ start sent)
  320. (- count sent)))))
  321. (if (< sent count)
  322. (loop sent))))))
  323. (define (make-unbuffered-output-port-handler discloser closer! write-block ready?)
  324. (make-port-handler discloser
  325. closer!
  326. (make-one-byte-handler write-block)
  327. (make-one-char-handler write-block)
  328. (make-write-block-handler write-block)
  329. ready?
  330. (lambda (port error-if-closed?) ; output forcer
  331. (unspecific))))
  332. ;----------------
  333. ; Output ports that just discard any output.
  334. (define null-output-port-handler
  335. (make-port-handler
  336. (lambda (ignore) ; disclose
  337. (list 'null-output-port))
  338. make-output-port-closed! ; close
  339. (lambda (port byte) ; one-byte (we just empty the buffer)
  340. (set-port-index! port 0))
  341. (lambda (port char) ; one-char (we just empty the buffer)
  342. (set-port-index! port 0))
  343. (lambda (port buffer start count) ; write-block
  344. count)
  345. (lambda (port) ; ready?
  346. #t)
  347. (lambda (port error-if-closed?) ; force-output
  348. (unspecific))))
  349. ; They can all share a buffer. The buffer is needed because the WRITE-BYTE
  350. ; byte code actually wants to put characters somewhere.
  351. (define null-output-buffer
  352. (make-byte-vector 1024 0))
  353. (define (make-null-output-port)
  354. (make-port null-output-port-handler
  355. null-text-codec
  356. #f
  357. open-output-port-status
  358. #f ; timestamp
  359. #f ; data
  360. null-output-buffer
  361. 0 ; index
  362. (byte-vector-length null-output-buffer) ; limit
  363. #f ; pending-cr?
  364. #f)) ; pending-eof?