suspendable-ports.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. ;;; Ports, implemented in Scheme
  2. ;;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software: you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU Lesser General Public License as
  6. ;;; published by the Free Software Foundation, either version 3 of the
  7. ;;; License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful, but
  10. ;;; 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 program. If not, see
  16. ;;; <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;;
  19. ;;; We would like to be able to implement green threads using delimited
  20. ;;; continuations. When a green thread would block on I/O, it should
  21. ;;; suspend and arrange to be resumed when it can make progress.
  22. ;;;
  23. ;;; The problem is that the ports code is written in C. A delimited
  24. ;;; continuation that captures a C activation can't be resumed, because
  25. ;;; Guile doesn't know about the internal structure of the C activation
  26. ;;; (stack frame) and so can't compose it with the current continuation.
  27. ;;; For that reason, to implement this desired future, we have to
  28. ;;; implement ports in Scheme.
  29. ;;;
  30. ;;; If Scheme were fast enough, we would just implement ports in Scheme
  31. ;;; early in Guile's boot, and that would be that. However currently
  32. ;;; that's not the case: character-by-character I/O is about three or
  33. ;;; four times slower in Scheme than in C. This is mostly bytecode
  34. ;;; overhead, though there are some ways that compiler improvements
  35. ;;; could help us too.
  36. ;;;
  37. ;;; Note that the difference between Scheme and C is much less for
  38. ;;; batched operations, like read-bytes or read-line.
  39. ;;;
  40. ;;; So the upshot is that we need to keep the C I/O routines around for
  41. ;;; performance reasons. We can still have our Scheme routines
  42. ;;; available as a module, though, for use by people working with green
  43. ;;; threads. That's this module. People that want green threads can
  44. ;;; even replace the core bindings, which enables green threading over
  45. ;;; other generic routines like the HTTP server.
  46. ;;;
  47. ;;; Code:
  48. (define-module (ice-9 suspendable-ports)
  49. #:use-module (rnrs bytevectors)
  50. #:use-module (ice-9 ports internal)
  51. #:use-module (ice-9 match)
  52. #:export (current-read-waiter
  53. current-write-waiter
  54. install-suspendable-ports!
  55. uninstall-suspendable-ports!))
  56. (define (default-read-waiter port) (port-poll port "r"))
  57. (define (default-write-waiter port) (port-poll port "w"))
  58. (define current-read-waiter (make-parameter default-read-waiter))
  59. (define current-write-waiter (make-parameter default-write-waiter))
  60. (define (wait-for-readable port) ((current-read-waiter) port))
  61. (define (wait-for-writable port) ((current-write-waiter) port))
  62. (define (read-bytes port dst start count)
  63. (cond
  64. (((port-read port) port dst start count)
  65. => (lambda (read)
  66. (unless (<= 0 read count)
  67. (error "bad return from port read function" read))
  68. read))
  69. (else
  70. (wait-for-readable port)
  71. (read-bytes port dst start count))))
  72. (define (write-bytes port src start count)
  73. (cond
  74. (((port-write port) port src start count)
  75. => (lambda (written)
  76. (unless (<= 0 written count)
  77. (error "bad return from port write function" written))
  78. (when (< written count)
  79. (write-bytes port src (+ start written) (- count written)))))
  80. (else
  81. (wait-for-writable port)
  82. (write-bytes port src start count))))
  83. (define (flush-input port)
  84. (let* ((buf (port-read-buffer port))
  85. (cur (port-buffer-cur buf))
  86. (end (port-buffer-end buf)))
  87. (when (< cur end)
  88. (set-port-buffer-cur! buf 0)
  89. (set-port-buffer-end! buf 0)
  90. (seek port (- cur end) SEEK_CUR))))
  91. (define (flush-output port)
  92. (let* ((buf (port-write-buffer port))
  93. (cur (port-buffer-cur buf))
  94. (end (port-buffer-end buf)))
  95. (when (< cur end)
  96. ;; Update cursors before attempting to write, assuming that I/O
  97. ;; errors are sticky. That way if the write throws an error,
  98. ;; causing the computation to abort, and possibly causing the port
  99. ;; to be collected by GC when it's open, any subsequent close-port
  100. ;; or force-output won't signal *another* error.
  101. (set-port-buffer-cur! buf 0)
  102. (set-port-buffer-end! buf 0)
  103. (write-bytes port (port-buffer-bytevector buf) cur (- end cur)))))
  104. (define utf8-bom #vu8(#xEF #xBB #xBF))
  105. (define utf16be-bom #vu8(#xFE #xFF))
  106. (define utf16le-bom #vu8(#xFF #xFE))
  107. (define utf32be-bom #vu8(#x00 #x00 #xFE #xFF))
  108. (define utf32le-bom #vu8(#xFF #xFE #x00 #x00))
  109. (define (clear-stream-start-for-bom-read port io-mode)
  110. (define (maybe-consume-bom bom)
  111. (and (eq? (peek-byte port) (bytevector-u8-ref bom 0))
  112. (call-with-values (lambda ()
  113. (fill-input port (bytevector-length bom)))
  114. (lambda (buf cur buffered)
  115. (and (<= (bytevector-length bom) buffered)
  116. (let ((bv (port-buffer-bytevector buf)))
  117. (let lp ((i 1))
  118. (if (= i (bytevector-length bom))
  119. (begin
  120. (set-port-buffer-cur! buf (+ cur i))
  121. #t)
  122. (and (eq? (bytevector-u8-ref bv (+ cur i))
  123. (bytevector-u8-ref bom i))
  124. (lp (1+ i)))))))))))
  125. (when (and (port-clear-stream-start-for-bom-read port)
  126. (eq? io-mode 'text))
  127. (case (%port-encoding port)
  128. ((UTF-8)
  129. (maybe-consume-bom utf8-bom))
  130. ((UTF-16)
  131. (cond
  132. ((maybe-consume-bom utf16le-bom)
  133. (specialize-port-encoding! port 'UTF-16LE))
  134. (else
  135. (maybe-consume-bom utf16be-bom)
  136. (specialize-port-encoding! port 'UTF-16BE))))
  137. ((UTF-32)
  138. (cond
  139. ((maybe-consume-bom utf32le-bom)
  140. (specialize-port-encoding! port 'UTF-32LE))
  141. (else
  142. (maybe-consume-bom utf32be-bom)
  143. (specialize-port-encoding! port 'UTF-32BE)))))))
  144. (define* (fill-input port #:optional (minimum-buffering 1) (io-mode 'text))
  145. (clear-stream-start-for-bom-read port io-mode)
  146. (let* ((buf (port-read-buffer port))
  147. (cur (port-buffer-cur buf))
  148. (buffered (max (- (port-buffer-end buf) cur) 0)))
  149. (cond
  150. ((or (<= minimum-buffering buffered) (port-buffer-has-eof? buf))
  151. (values buf cur buffered))
  152. (else
  153. (unless (input-port? port)
  154. (error "not an input port" port))
  155. (when (port-random-access? port)
  156. (flush-output port))
  157. (let ((bv (port-buffer-bytevector buf)))
  158. (cond
  159. ((< (bytevector-length bv) minimum-buffering)
  160. (expand-port-read-buffer! port minimum-buffering)
  161. (fill-input port minimum-buffering))
  162. (else
  163. (when (< 0 cur)
  164. (bytevector-copy! bv cur bv 0 buffered)
  165. (set-port-buffer-cur! buf 0)
  166. (set-port-buffer-end! buf buffered))
  167. (let ((buffering (max (port-read-buffering port) minimum-buffering)))
  168. (let lp ((buffered buffered))
  169. (let* ((count (- buffering buffered))
  170. (read (read-bytes port bv buffered count)))
  171. (cond
  172. ((zero? read)
  173. (set-port-buffer-has-eof?! buf #t)
  174. (values buf 0 buffered))
  175. (else
  176. (let ((buffered (+ buffered read)))
  177. (set-port-buffer-end! buf buffered)
  178. (if (< buffered minimum-buffering)
  179. (lp buffered)
  180. (values buf 0 buffered)))))))))))))))
  181. (define* (force-output #:optional (port (current-output-port)))
  182. (unless (and (output-port? port) (not (port-closed? port)))
  183. (error "not an open output port" port))
  184. (flush-output port))
  185. (define close-port
  186. (let ((%close-port (@ (guile) close-port)))
  187. (lambda (port)
  188. (cond
  189. ((port-closed? port) #f)
  190. (else
  191. (when (output-port? port) (flush-output port))
  192. (%close-port port))))))
  193. (define-inlinable (peek-bytes port count kfast kslow)
  194. (let* ((buf (port-read-buffer port))
  195. (cur (port-buffer-cur buf))
  196. (buffered (- (port-buffer-end buf) cur)))
  197. (if (<= count buffered)
  198. (kfast buf (port-buffer-bytevector buf) cur buffered)
  199. (call-with-values (lambda () (fill-input port count))
  200. (lambda (buf cur buffered)
  201. (kslow buf (port-buffer-bytevector buf) cur buffered))))))
  202. (define (peek-byte port)
  203. (peek-bytes port 1
  204. (lambda (buf bv cur buffered)
  205. (bytevector-u8-ref bv cur))
  206. (lambda (buf bv cur buffered)
  207. (and (> buffered 0)
  208. (bytevector-u8-ref bv cur)))))
  209. (define* (lookahead-u8 port)
  210. (define (fast-path buf bv cur buffered)
  211. (bytevector-u8-ref bv cur))
  212. (define (slow-path buf bv cur buffered)
  213. (if (zero? buffered)
  214. the-eof-object
  215. (fast-path buf bv cur buffered)))
  216. (peek-bytes port 1 fast-path slow-path))
  217. (define* (get-u8 port)
  218. (define (fast-path buf bv cur buffered)
  219. (set-port-buffer-cur! buf (1+ cur))
  220. (bytevector-u8-ref bv cur))
  221. (define (slow-path buf bv cur buffered)
  222. (if (zero? buffered)
  223. (begin
  224. (set-port-buffer-has-eof?! buf #f)
  225. the-eof-object)
  226. (fast-path buf bv cur buffered)))
  227. (peek-bytes port 1 fast-path slow-path))
  228. (define* (get-bytevector-n port count)
  229. (let ((ret (make-bytevector count)))
  230. (define (port-buffer-take! pos buf cur to-copy)
  231. (bytevector-copy! (port-buffer-bytevector buf) cur
  232. ret pos to-copy)
  233. (set-port-buffer-cur! buf (+ cur to-copy))
  234. (+ pos to-copy))
  235. (define (take-already-buffered)
  236. (let* ((buf (port-read-buffer port))
  237. (cur (port-buffer-cur buf))
  238. (buffered (max (- (port-buffer-end buf) cur) 0)))
  239. (port-buffer-take! 0 buf cur (min count buffered))))
  240. (define (trim-and-return len)
  241. (if (zero? len)
  242. the-eof-object
  243. (let ((partial (make-bytevector len)))
  244. (bytevector-copy! ret 0 partial 0 len)
  245. partial)))
  246. (define (buffer-and-fill pos)
  247. (call-with-values (lambda () (fill-input port 1 'binary))
  248. (lambda (buf cur buffered)
  249. (if (zero? buffered)
  250. (begin
  251. (set-port-buffer-has-eof?! buf #f)
  252. (trim-and-return pos))
  253. (let ((pos (port-buffer-take! pos buf cur
  254. (min (- count pos) buffered))))
  255. (if (= pos count)
  256. ret
  257. (buffer-and-fill pos)))))))
  258. (define (fill-directly pos)
  259. (when (port-random-access? port)
  260. (flush-output port))
  261. (port-clear-stream-start-for-bom-read port)
  262. (let lp ((pos pos))
  263. (let ((read (read-bytes port ret pos (- count pos))))
  264. (cond
  265. ((= read (- count pos)) ret)
  266. ((zero? read) (trim-and-return pos))
  267. (else (lp (+ pos read)))))))
  268. (let ((pos (take-already-buffered)))
  269. (cond
  270. ((= pos count) (if (zero? pos) the-eof-object ret))
  271. ((< (- count pos) (port-read-buffering port)) (buffer-and-fill pos))
  272. (else (fill-directly pos))))))
  273. (define (put-u8 port byte)
  274. (let* ((buf (port-write-buffer port))
  275. (bv (port-buffer-bytevector buf))
  276. (end (port-buffer-end buf)))
  277. (unless (<= 0 end (bytevector-length bv))
  278. (error "not an output port" port))
  279. (when (and (eq? (port-buffer-cur buf) end) (port-random-access? port))
  280. (flush-input port))
  281. (cond
  282. ((= end (bytevector-length bv))
  283. ;; Multiple threads racing; race to flush, then retry.
  284. (flush-output port)
  285. (put-u8 port byte))
  286. (else
  287. (bytevector-u8-set! bv end byte)
  288. (set-port-buffer-end! buf (1+ end))
  289. (when (= (1+ end) (bytevector-length bv)) (flush-output port))))))
  290. (define* (put-bytevector port src #:optional (start 0)
  291. (count (- (bytevector-length src) start)))
  292. (unless (<= 0 start (+ start count) (bytevector-length src))
  293. (error "invalid start/count" start count))
  294. (let* ((buf (port-write-buffer port))
  295. (bv (port-buffer-bytevector buf))
  296. (size (bytevector-length bv))
  297. (cur (port-buffer-cur buf))
  298. (end (port-buffer-end buf))
  299. (buffered (max (- end cur) 0)))
  300. (when (and (eq? cur end) (port-random-access? port))
  301. (flush-input port))
  302. (cond
  303. ((<= size count)
  304. ;; The write won't fit in the buffer at all; write directly.
  305. ;; Write directly. Flush write buffer first if needed.
  306. (when (< cur end) (flush-output port))
  307. (write-bytes port src start count))
  308. ((< (- size buffered) count)
  309. ;; The write won't fit into the buffer along with what's already
  310. ;; buffered. Flush and fill.
  311. (flush-output port)
  312. (set-port-buffer-end! buf count)
  313. (bytevector-copy! src start bv 0 count))
  314. (else
  315. ;; The write will fit in the buffer, but we need to shuffle the
  316. ;; already-buffered bytes (if any) down.
  317. (set-port-buffer-cur! buf 0)
  318. (set-port-buffer-end! buf (+ buffered count))
  319. (bytevector-copy! bv cur bv 0 buffered)
  320. (bytevector-copy! src start bv buffered count)
  321. ;; If the buffer completely fills, we flush.
  322. (when (= (+ buffered count) size)
  323. (flush-output port))))))
  324. (define (decoding-error subr port)
  325. ;; GNU definition; fixme?
  326. (define EILSEQ 84)
  327. (throw 'decoding-error subr "input decoding error" EILSEQ port))
  328. (define-inlinable (decode-utf8 bv start avail u8_0 kt kf)
  329. (cond
  330. ((< u8_0 #x80)
  331. (kt (integer->char u8_0) 1))
  332. ((and (<= #xc2 u8_0 #xdf) (<= 2 avail))
  333. (let ((u8_1 (bytevector-u8-ref bv (1+ start))))
  334. (if (= (logand u8_1 #xc0) #x80)
  335. (kt (integer->char
  336. (logior (ash (logand u8_0 #x1f) 6)
  337. (logand u8_1 #x3f)))
  338. 2)
  339. (kf))))
  340. ((and (= (logand u8_0 #xf0) #xe0) (<= 3 avail))
  341. (let ((u8_1 (bytevector-u8-ref bv (+ start 1)))
  342. (u8_2 (bytevector-u8-ref bv (+ start 2))))
  343. (if (and (= (logand u8_1 #xc0) #x80)
  344. (= (logand u8_2 #xc0) #x80)
  345. (case u8_0
  346. ((#xe0) (>= u8_1 #xa0))
  347. ((#xed) (>= u8_1 #x9f))
  348. (else #t)))
  349. (kt (integer->char
  350. (logior (ash (logand u8_0 #x0f) 12)
  351. (ash (logand u8_1 #x3f) 6)
  352. (logand u8_2 #x3f)))
  353. 3)
  354. (kf))))
  355. ((and (<= #xf0 u8_0 #xf4) (<= 4 avail))
  356. (let ((u8_1 (bytevector-u8-ref bv (+ start 1)))
  357. (u8_2 (bytevector-u8-ref bv (+ start 2)))
  358. (u8_3 (bytevector-u8-ref bv (+ start 3))))
  359. (if (and (= (logand u8_1 #xc0) #x80)
  360. (= (logand u8_2 #xc0) #x80)
  361. (= (logand u8_3 #xc0) #x80)
  362. (case u8_0
  363. ((#xf0) (>= u8_1 #x90))
  364. ((#xf4) (>= u8_1 #x8f))
  365. (else #t)))
  366. (kt (integer->char
  367. (logior (ash (logand u8_0 #x07) 18)
  368. (ash (logand u8_1 #x3f) 12)
  369. (ash (logand u8_2 #x3f) 6)
  370. (logand u8_3 #x3f)))
  371. 4)
  372. (kf))))
  373. (else (kf))))
  374. (define (bad-utf8-len bv cur buffering first-byte)
  375. (define (ref n)
  376. (bytevector-u8-ref bv (+ cur n)))
  377. (cond
  378. ((< first-byte #x80) 0)
  379. ((<= #xc2 first-byte #xdf)
  380. (cond
  381. ((< buffering 2) 1)
  382. ((not (= (logand (ref 1) #xc0) #x80)) 1)
  383. (else 0)))
  384. ((= (logand first-byte #xf0) #xe0)
  385. (cond
  386. ((< buffering 2) 1)
  387. ((not (= (logand (ref 1) #xc0) #x80)) 1)
  388. ((and (eq? first-byte #xe0) (< (ref 1) #xa0)) 1)
  389. ((and (eq? first-byte #xed) (< (ref 1) #x9f)) 1)
  390. ((< buffering 3) 2)
  391. ((not (= (logand (ref 2) #xc0) #x80)) 2)
  392. (else 0)))
  393. ((<= #xf0 first-byte #xf4)
  394. (cond
  395. ((< buffering 2) 1)
  396. ((not (= (logand (ref 1) #xc0) #x80)) 1)
  397. ((and (eq? first-byte #xf0) (< (ref 1) #x90)) 1)
  398. ((and (eq? first-byte #xf4) (< (ref 1) #x8f)) 1)
  399. ((< buffering 3) 2)
  400. ((not (= (logand (ref 2) #xc0) #x80)) 2)
  401. ((< buffering 4) 3)
  402. ((not (= (logand (ref 3) #xc0) #x80)) 3)
  403. (else 0)))
  404. (else 1)))
  405. (define (peek-char-and-next-cur/utf8 port buf cur first-byte)
  406. (if (< first-byte #x80)
  407. (values (integer->char first-byte) buf (+ cur 1))
  408. (call-with-values (lambda ()
  409. (fill-input port
  410. (cond
  411. ((<= #xc2 first-byte #xdf) 2)
  412. ((= (logand first-byte #xf0) #xe0) 3)
  413. (else 4))))
  414. (lambda (buf cur buffering)
  415. (let ((bv (port-buffer-bytevector buf)))
  416. (define (bad-utf8)
  417. (let ((len (bad-utf8-len bv cur buffering first-byte)))
  418. (when (zero? len) (error "internal error"))
  419. (if (eq? (port-conversion-strategy port) 'substitute)
  420. (values #\xFFFD buf (+ cur len))
  421. (decoding-error "peek-char" port))))
  422. (decode-utf8 bv cur buffering first-byte
  423. (lambda (char len)
  424. (values char buf (+ cur len)))
  425. bad-utf8))))))
  426. (define (peek-char-and-next-cur/iso-8859-1 port buf cur first-byte)
  427. (values (integer->char first-byte) buf (+ cur 1)))
  428. (define (peek-char-and-next-cur/iconv port)
  429. (let lp ((prev-input-size 0))
  430. (let ((input-size (1+ prev-input-size)))
  431. (call-with-values (lambda () (fill-input port input-size))
  432. (lambda (buf cur buffered)
  433. (cond
  434. ((< buffered input-size)
  435. ;; Buffer failed to fill; EOF, possibly premature.
  436. (cond
  437. ((zero? prev-input-size)
  438. (values the-eof-object buf cur))
  439. ((eq? (port-conversion-strategy port) 'substitute)
  440. (values #\xFFFD buf (+ cur prev-input-size)))
  441. (else
  442. (decoding-error "peek-char" port))))
  443. ((port-decode-char port (port-buffer-bytevector buf)
  444. cur input-size)
  445. => (lambda (char)
  446. (values char buf (+ cur input-size))))
  447. (else
  448. (lp input-size))))))))
  449. (define (peek-char-and-next-cur port)
  450. (define (have-byte buf bv cur buffered)
  451. (let ((first-byte (bytevector-u8-ref bv cur)))
  452. (case (%port-encoding port)
  453. ((UTF-8)
  454. (peek-char-and-next-cur/utf8 port buf cur first-byte))
  455. ((ISO-8859-1)
  456. (peek-char-and-next-cur/iso-8859-1 port buf cur first-byte))
  457. (else
  458. (peek-char-and-next-cur/iconv port)))))
  459. (peek-bytes port 1 have-byte
  460. (lambda (buf bv cur buffered)
  461. (if (< 0 buffered)
  462. (have-byte buf bv cur buffered)
  463. (values the-eof-object buf cur)))))
  464. (define* (peek-char #:optional (port (current-input-port)))
  465. (define (slow-path)
  466. (call-with-values (lambda () (peek-char-and-next-cur port))
  467. (lambda (char buf cur)
  468. char)))
  469. (define (fast-path buf bv cur buffered)
  470. (let ((u8 (bytevector-u8-ref bv cur))
  471. (enc (%port-encoding port)))
  472. (case enc
  473. ((UTF-8) (decode-utf8 bv cur buffered u8 (lambda (char len) char)
  474. slow-path))
  475. ((ISO-8859-1) (integer->char u8))
  476. (else (slow-path)))))
  477. (peek-bytes port 1 fast-path
  478. (lambda (buf bv cur buffered) (slow-path))))
  479. (define-inlinable (advance-port-position! pos char)
  480. ;; FIXME: this cond is a speed hack; really we should just compile
  481. ;; `case' better.
  482. (cond
  483. ;; FIXME: char>? et al should compile well.
  484. ((<= (char->integer #\space) (char->integer char))
  485. (set-port-position-column! pos (1+ (port-position-column pos))))
  486. (else
  487. (case char
  488. ((#\alarm) #t) ; No change.
  489. ((#\backspace)
  490. (let ((col (port-position-column pos)))
  491. (when (> col 0)
  492. (set-port-position-column! pos (1- col)))))
  493. ((#\newline)
  494. (set-port-position-line! pos (1+ (port-position-line pos)))
  495. (set-port-position-column! pos 0))
  496. ((#\return)
  497. (set-port-position-column! pos 0))
  498. ((#\tab)
  499. (let ((col (port-position-column pos)))
  500. (set-port-position-column! pos (- (+ col 8) (remainder col 8)))))
  501. (else
  502. (set-port-position-column! pos (1+ (port-position-column pos))))))))
  503. (define* (read-char #:optional (port (current-input-port)))
  504. (define (finish buf char)
  505. (advance-port-position! (port-buffer-position buf) char)
  506. char)
  507. (define (slow-path)
  508. (call-with-values (lambda () (peek-char-and-next-cur port))
  509. (lambda (char buf cur)
  510. (set-port-buffer-cur! buf cur)
  511. (if (eq? char the-eof-object)
  512. (begin
  513. (set-port-buffer-has-eof?! buf #f)
  514. char)
  515. (finish buf char)))))
  516. (define (fast-path buf bv cur buffered)
  517. (let ((u8 (bytevector-u8-ref bv cur))
  518. (enc (%port-encoding port)))
  519. (case enc
  520. ((UTF-8)
  521. (decode-utf8 bv cur buffered u8
  522. (lambda (char len)
  523. (set-port-buffer-cur! buf (+ cur len))
  524. (finish buf char))
  525. slow-path))
  526. ((ISO-8859-1)
  527. (set-port-buffer-cur! buf (+ cur 1))
  528. (finish buf (integer->char u8)))
  529. (else (slow-path)))))
  530. (peek-bytes port 1 fast-path
  531. (lambda (buf bv cur buffered) (slow-path))))
  532. (define-inlinable (port-fold-chars/iso-8859-1 port proc seed)
  533. (let* ((buf (port-read-buffer port))
  534. (cur (port-buffer-cur buf)))
  535. (let fold-buffer ((buf buf) (cur cur) (seed seed))
  536. (let ((bv (port-buffer-bytevector buf))
  537. (end (port-buffer-end buf)))
  538. (let fold-chars ((cur cur) (seed seed))
  539. (cond
  540. ((= end cur)
  541. (call-with-values (lambda () (fill-input port))
  542. (lambda (buf cur buffered)
  543. (if (zero? buffered)
  544. (call-with-values (lambda () (proc the-eof-object seed))
  545. (lambda (seed done?)
  546. (if done? seed (fold-buffer buf cur seed))))
  547. (fold-buffer buf cur seed)))))
  548. (else
  549. (let ((ch (integer->char (bytevector-u8-ref bv cur)))
  550. (cur (1+ cur)))
  551. (set-port-buffer-cur! buf cur)
  552. (advance-port-position! (port-buffer-position buf) ch)
  553. (call-with-values (lambda () (proc ch seed))
  554. (lambda (seed done?)
  555. (if done? seed (fold-chars cur seed))))))))))))
  556. (define-inlinable (port-fold-chars port proc seed)
  557. (case (%port-encoding port)
  558. ((ISO-8859-1) (port-fold-chars/iso-8859-1 port proc seed))
  559. (else
  560. (let lp ((seed seed))
  561. (let ((ch (read-char port)))
  562. (call-with-values (lambda () (proc ch seed))
  563. (lambda (seed done?)
  564. (if done? seed (lp seed)))))))))
  565. (define* (read-delimited delims #:optional (port (current-input-port))
  566. (handle-delim 'trim))
  567. ;; Currently this function conses characters into a list, then uses
  568. ;; reverse-list->string. It wastes 2 words per character but it still
  569. ;; seems to be the fastest thing at the moment.
  570. (define (finish delim chars)
  571. (define (->string chars)
  572. (if (and (null? chars) (not (char? delim)))
  573. the-eof-object
  574. (reverse-list->string chars)))
  575. (case handle-delim
  576. ((trim) (->string chars))
  577. ((split) (cons (->string chars) delim))
  578. ((concat)
  579. (->string (if (char? delim) (cons delim chars) chars)))
  580. ((peek)
  581. (when (char? delim) (unread-char delim port))
  582. (->string chars))
  583. (else
  584. (error "unexpected handle-delim value: " handle-delim))))
  585. (define-syntax-rule (make-folder delimiter?)
  586. (lambda (char chars)
  587. (if (or (not (char? char)) (delimiter? char))
  588. (values (finish char chars) #t)
  589. (values (cons char chars) #f))))
  590. (define-syntax-rule (specialized-fold delimiter?)
  591. (port-fold-chars port (make-folder delimiter?) '()))
  592. (case (string-length delims)
  593. ((0) (specialized-fold (lambda (char) #f)))
  594. ((1) (let ((delim (string-ref delims 0)))
  595. (specialized-fold (lambda (char) (eqv? char delim)))))
  596. (else => (lambda (ndelims)
  597. (specialized-fold
  598. (lambda (char)
  599. (let lp ((i 0))
  600. (and (< i ndelims)
  601. (or (eqv? char (string-ref delims i))
  602. (lp (1+ i)))))))))))
  603. (define* (read-line #:optional (port (current-input-port))
  604. (handle-delim 'trim))
  605. (read-delimited "\n" port handle-delim))
  606. (define* (%read-line port)
  607. (read-line port 'split))
  608. (define* (put-string port str #:optional (start 0)
  609. (count (- (string-length str) start)))
  610. (let* ((aux (port-auxiliary-write-buffer port))
  611. (pos (port-buffer-position aux))
  612. (line (port-position-line pos)))
  613. (set-port-buffer-cur! aux 0)
  614. (port-clear-stream-start-for-bom-write port aux)
  615. (let lp ((encoded 0))
  616. (when (< encoded count)
  617. (let ((encoded (+ encoded
  618. (port-encode-chars port aux str
  619. (+ start encoded)
  620. (- count encoded)))))
  621. (let ((end (port-buffer-end aux)))
  622. (set-port-buffer-end! aux 0)
  623. (put-bytevector port (port-buffer-bytevector aux) 0 end)
  624. (lp encoded)))))
  625. (when (and (not (eqv? line (port-position-line pos)))
  626. (port-line-buffered? port))
  627. (flush-output port))))
  628. (define* (put-char port char)
  629. (let ((aux (port-auxiliary-write-buffer port)))
  630. (set-port-buffer-cur! aux 0)
  631. (port-clear-stream-start-for-bom-write port aux)
  632. (port-encode-char port aux char)
  633. (let ((end (port-buffer-end aux)))
  634. (set-port-buffer-end! aux 0)
  635. (put-bytevector port (port-buffer-bytevector aux) 0 end))
  636. (when (and (eqv? char #\newline) (port-line-buffered? port))
  637. (flush-output port))))
  638. (define accept
  639. (let ((%accept (@ (guile) accept)))
  640. (lambda* (port #:optional (flags 0))
  641. (let lp ()
  642. (or (%accept port flags)
  643. (begin
  644. (wait-for-readable port)
  645. (lp)))))))
  646. (define connect
  647. (let ((%connect (@ (guile) connect)))
  648. (lambda (port sockaddr . args)
  649. (unless (apply %connect port sockaddr args)
  650. ;; Clownshoes semantics; see connect(2).
  651. (wait-for-writable port)
  652. (let ((err (getsockopt port SOL_SOCKET SO_ERROR)))
  653. (unless (zero? err)
  654. (scm-error 'system-error "connect" "~A"
  655. (list (strerror err)) #f)))))))
  656. (define saved-port-bindings #f)
  657. (define port-bindings
  658. '(((guile)
  659. read-char peek-char force-output close-port
  660. accept connect)
  661. ((ice-9 binary-ports)
  662. get-u8 lookahead-u8 get-bytevector-n
  663. put-u8 put-bytevector)
  664. ((ice-9 textual-ports)
  665. put-char put-string)
  666. ((ice-9 rdelim) %read-line read-line read-delimited)))
  667. (define (install-suspendable-ports!)
  668. (unless saved-port-bindings
  669. (set! saved-port-bindings (make-hash-table))
  670. (let ((suspendable-ports (resolve-module '(ice-9 suspendable-ports))))
  671. (for-each
  672. (match-lambda
  673. ((mod . syms)
  674. (let ((mod (resolve-module mod)))
  675. (for-each (lambda (sym)
  676. (hashq-set! saved-port-bindings sym
  677. (module-ref mod sym))
  678. (module-set! mod sym
  679. (module-ref suspendable-ports sym)))
  680. syms))))
  681. port-bindings))))
  682. (define (uninstall-suspendable-ports!)
  683. (when saved-port-bindings
  684. (for-each
  685. (match-lambda
  686. ((mod . syms)
  687. (let ((mod (resolve-module mod)))
  688. (for-each (lambda (sym)
  689. (let ((saved (hashq-ref saved-port-bindings sym)))
  690. (module-set! mod sym saved)))
  691. syms))))
  692. port-bindings)
  693. (set! saved-port-bindings #f)))