more-port.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Additional port types
  3. ;----------------
  4. ; Ports which keep track of the current row and column.
  5. ;
  6. ; Note that we're only counting character access---single-byte access
  7. ; or block access are ignored.
  8. ;
  9. ; sub-port: port being tracked
  10. ; row, column: position of the next character
  11. (define-record-type port-location :port-location
  12. (really-make-port-location sub-port row column)
  13. port-location?
  14. (sub-port port-location-sub-port)
  15. (row port-location-row set-port-location-row!)
  16. (column port-location-column set-port-location-column!))
  17. (define (make-port-location sub-port)
  18. (really-make-port-location sub-port 0 0))
  19. (define (row-column-accessor accessor)
  20. (lambda (port)
  21. (let ((data (port-data port)))
  22. (if (port-location? data)
  23. (accessor data)
  24. #f))))
  25. (define current-row (row-column-accessor port-location-row))
  26. (define current-column (row-column-accessor port-location-column))
  27. (define (port-location-update! location ch)
  28. (if (char=? ch #\newline)
  29. (begin
  30. (set-port-location-row! location
  31. (+ 1 (port-location-row location)))
  32. (set-port-location-column! location 0))
  33. (set-port-location-column! location
  34. (+ 1 (port-location-column location)))))
  35. ; A codec that doesn't trigger the VM taking over
  36. (define-text-codec utf-8/diy "UTF-8/DIY"
  37. (lambda (char buffer start count)
  38. (encode-char (enum text-encoding-option utf-8) char buffer start count))
  39. (lambda (buffer start count)
  40. (decode-char (enum text-encoding-option utf-8) buffer start count)))
  41. ;----------------
  42. ; Input ports that keep track of the current row and column.
  43. (define (make-tracking-input-port port)
  44. (if (input-port? port)
  45. (let ((tracking
  46. (make-buffered-input-port tracking-input-port-handler
  47. (make-port-location port)
  48. (make-byte-vector (default-buffer-size) 0)
  49. 0
  50. 0)))
  51. ;; We need this because otherwise the VM won't give control
  52. ;; back to our handler for every character.
  53. (set-port-text-codec-spec! tracking utf-8/diy)
  54. tracking)
  55. (call-error "not an input port" make-tracking-input-port port)))
  56. (define (make-tracking-one-char-input plain-one-char-input)
  57. (lambda (port mode)
  58. (let ((thing (plain-one-char-input port mode)))
  59. (cond
  60. (mode thing) ; PEEK or READY?
  61. ((eof-object? thing) thing)
  62. (else
  63. (port-location-update! (port-data port) thing)
  64. thing)))))
  65. (define tracking-input-port-handler
  66. (let ((plain-handler
  67. (make-buffered-input-port-handler
  68. (lambda (location)
  69. (list 'tracking-port (port-location-sub-port location)))
  70. (lambda (port) ; close
  71. (maybe-commit))
  72. (lambda (port wait?)
  73. (if (maybe-commit)
  74. (let ((got (read-block (port-buffer port)
  75. 0
  76. (byte-vector-length (port-buffer port))
  77. (port-location-sub-port (port-data port))
  78. wait?)))
  79. ;;(note-buffer-reuse! port)
  80. (if (eof-object? got)
  81. (set-port-pending-eof?! port #t)
  82. (begin
  83. (set-port-index! port 0)
  84. (set-port-limit! port got)))
  85. #t)
  86. #f))
  87. (lambda (port)
  88. (let ((ready? (byte-ready? (port-location-sub-port (port-data port)))))
  89. (if (maybe-commit)
  90. (values #t ready?)
  91. (values #f #f)))))))
  92. (make-port-handler
  93. (port-handler-discloser plain-handler)
  94. (port-handler-close plain-handler)
  95. (port-handler-byte plain-handler)
  96. (make-tracking-one-char-input (port-handler-char plain-handler))
  97. (port-handler-block plain-handler)
  98. (port-handler-ready? plain-handler)
  99. (port-handler-force plain-handler))))
  100. ;----------------
  101. ; Output ports that keep track of the current row and column.
  102. (define (make-tracking-one-char-output plain-one-char-output)
  103. (lambda (port ch)
  104. (plain-one-char-output port ch)
  105. (port-location-update! (port-data port) ch)))
  106. (define tracking-output-port-handler
  107. (let ((plain-handler
  108. (make-buffered-output-port-handler
  109. (lambda (location)
  110. (list 'tracking-port (port-location-sub-port location)))
  111. (lambda (port) ; close
  112. (maybe-commit))
  113. (lambda (port necessary?) ; we ignore necessary? and always write
  114. (if (maybe-commit) ; out whatever we have
  115. (begin
  116. (write-block (port-buffer port)
  117. 0
  118. (port-index port)
  119. (port-location-sub-port (port-data port)))
  120. ;;(note-buffer-reuse! port)
  121. (set-port-index! port 0)
  122. #t)
  123. #f))
  124. (lambda (port)
  125. (let ((ready? (output-port-ready?
  126. (port-location-sub-port (port-data port)))))
  127. (if (maybe-commit)
  128. (values #t ready?)
  129. (values #f #f)))))))
  130. (make-port-handler
  131. (port-handler-discloser plain-handler)
  132. (port-handler-close plain-handler)
  133. (port-handler-byte plain-handler)
  134. (make-tracking-one-char-output (port-handler-char plain-handler))
  135. (port-handler-block plain-handler)
  136. (port-handler-ready? plain-handler)
  137. (port-handler-force plain-handler))))
  138. (define (make-tracking-output-port port)
  139. (if (output-port? port)
  140. (let ((tracking
  141. (make-buffered-output-port tracking-output-port-handler
  142. (make-port-location port)
  143. (make-byte-vector (default-buffer-size) 0)
  144. 0
  145. (default-buffer-size))))
  146. ;; We need this because otherwise the VM won't give control
  147. ;; back to our handler for every character.
  148. (set-port-text-codec-spec! tracking utf-8/diy)
  149. tracking)
  150. (call-error "not an output port" make-tracking-output-port port)))
  151. (define (fresh-line port)
  152. (let ((column (current-column port)))
  153. (if (and column (< 0 column))
  154. (newline port))))
  155. ;----------------
  156. ; String input ports
  157. ; All the work is done by the buffered-port code.
  158. (define string-input-port-handler
  159. (make-buffered-input-port-handler
  160. (lambda (ignore)
  161. (list 'string-input-port))
  162. (lambda (ignore)
  163. (maybe-commit))
  164. (lambda (port wait?)
  165. (set-port-pending-eof?! port #t)
  166. (maybe-commit))
  167. (lambda (port)
  168. (if (maybe-commit)
  169. (values #t #f)
  170. (values #f #f)))))
  171. ; We copy the input because it's mutable.
  172. (define (make-byte-vector-input-port bytes)
  173. (let* ((size (byte-vector-length bytes))
  174. (buffer (make-byte-vector size 0)))
  175. (copy-bytes! bytes 0 buffer 0 size)
  176. (make-byte-vector-input-port-internal buffer)))
  177. (define (make-byte-vector-input-port-internal buffer)
  178. (make-buffered-input-port string-input-port-handler
  179. #f ; no additional state needed
  180. buffer
  181. 0
  182. (byte-vector-length buffer)))
  183. (define (make-string-input-port string)
  184. (let* ((string-size (string-length string))
  185. (encoding-size (string-encoding-length/utf-8 string 0 string-size))
  186. (buffer (make-byte-vector encoding-size 0)))
  187. (encode-string/utf-8 string 0 string-size buffer 0 encoding-size)
  188. (let ((port (make-byte-vector-input-port-internal buffer)))
  189. (set-port-text-codec! port utf-8-codec)
  190. port)))
  191. ;----------------
  192. ; String output ports
  193. ; The cdr of the data field of the port is a list of buffers (the car is not
  194. ; used). When the output is wanted the buffers are concatenated together to
  195. ; get the final string.
  196. ;
  197. ; These are thread-safe for no particular reason.
  198. (define (make-byte-vector-output-port)
  199. (make-buffered-output-port string-output-port-handler
  200. (list #f)
  201. (make-byte-vector (default-buffer-size) 0)
  202. 0
  203. (default-buffer-size)))
  204. (define make-string-output-port make-byte-vector-output-port)
  205. ; Concatenates all of the buffers into single string.
  206. ; Could use a proposal...
  207. (define (byte-vector-output-port-output port)
  208. (ensure-atomicity
  209. (check-buffer-timestamp! port) ; makes the proposal check this
  210. (let* ((full (provisional-cdr (port-data port)))
  211. (last (port-buffer port))
  212. (index (provisional-port-index port))
  213. (out (make-byte-vector (apply +
  214. index
  215. (map byte-vector-length full))
  216. 0)))
  217. (let loop ((full (reverse full)) (i 0))
  218. (if (null? full)
  219. (copy-bytes! last 0 out i index)
  220. (let* ((buffer (car full))
  221. (count (byte-vector-length buffer)))
  222. (copy-bytes! buffer 0 out i count)
  223. (loop (cdr full) (+ i count)))))
  224. out)))
  225. (define (string-output-port-output port)
  226. (utf-8->string (byte-vector-output-port-output port) #\?))
  227. (define string-output-port-handler
  228. (make-buffered-output-port-handler
  229. (lambda (port)
  230. '(string-output-port))
  231. (lambda (port) ; closer
  232. (maybe-commit))
  233. (lambda (port necessary?) ; we ignore necessary? and always write
  234. (provisional-set-cdr! ; out whatever we have
  235. (port-data port)
  236. (cons (let* ((size (provisional-port-index port))
  237. (new (make-byte-vector size 0)))
  238. (copy-bytes! (port-buffer port)
  239. 0
  240. new
  241. 0
  242. size)
  243. new)
  244. (provisional-cdr (port-data port))))
  245. (provisional-set-port-index! port 0)
  246. ;(note-buffer-reuse! port)
  247. (maybe-commit))
  248. (lambda (port)
  249. (if (maybe-commit)
  250. (values #t #f)
  251. (values #f #f)))))
  252. (define (call-with-string-output-port proc)
  253. (let ((port (make-string-output-port)))
  254. (proc port)
  255. (string-output-port-output port)))
  256. ;----------------
  257. ; Output ports from single byte consumers
  258. (define (byte-sink->output-port proc)
  259. (make-unbuffered-output-port byte-sink-output-port-handler
  260. proc))
  261. (define byte-sink-output-port-handler
  262. (make-unbuffered-output-port-handler
  263. (lambda (proc)
  264. (list 'byte-sink-output-port))
  265. make-output-port-closed!
  266. (lambda (port buffer start count)
  267. (let ((proc (port-data port)))
  268. (do ((i 0 (+ i 1)))
  269. ((= i count))
  270. (proc (byte-vector-ref buffer (+ start i)))))
  271. count)
  272. (lambda (port) ; ready?
  273. #t)))
  274. ; Output ports from single char consumers
  275. (define (char-sink->output-port proc)
  276. (make-unbuffered-output-port char-sink-output-port-handler
  277. proc))
  278. (define char-sink-output-port-handler
  279. (make-port-handler
  280. (lambda (proc)
  281. (list 'char-sink-output-port))
  282. make-output-port-closed!
  283. (lambda (port byte)
  284. (call-error "char port does not accept bytes"))
  285. (lambda (port ch)
  286. ((port-data port) ch))
  287. (lambda (port buffer start count)
  288. (call-error "char port does not accept bytes"))
  289. (lambda (port) ; ready?
  290. #t)
  291. (lambda (port error-if-closed?) ; output forcer
  292. (unspecific))))
  293. ; Call PROC on a port that will transfer COUNT bytes to PORT and
  294. ; then quit.
  295. (define (limit-output port count proc)
  296. (call-with-current-continuation
  297. (lambda (quit)
  298. (proc (byte-sink->output-port
  299. (lambda (byte)
  300. (write-byte byte port)
  301. (set! count (- count 1))
  302. (if (<= count 0)
  303. (quit #f))))))))
  304. ; Old name.
  305. (define write-one-line limit-output)
  306. ;----------------
  307. ; Input ports from single value producers
  308. ;
  309. ; ((make-source->input-port handler)
  310. ; <next-thunk>
  311. ; [<ready?-thunk>
  312. ; [<close-thunk>]])
  313. (define (make-source->input-port handler)
  314. (lambda (source . more)
  315. (make-buffered-input-port handler
  316. (make-source-data source
  317. (if (null? more)
  318. (lambda () #t)
  319. (car more))
  320. (if (or (null? more)
  321. (null? (cdr more)))
  322. values
  323. (cadr more)))
  324. (make-byte-vector 128 0)
  325. 0
  326. 0)))
  327. (define-record-type source-data :source-data
  328. (make-source-data source ready? close)
  329. source-data?
  330. (source source-data-source)
  331. (ready? source-data-ready?)
  332. (close source-data-close))
  333. (define (make-source-input-port-handler discloser-name encode-data)
  334. (make-buffered-input-port-handler
  335. (lambda (proc)
  336. (list discloser-name))
  337. (lambda (port)
  338. (make-input-port-closed! port)
  339. ((source-data-close (port-data port))))
  340. (lambda (port wait?)
  341. (let ((buffer (port-buffer port))
  342. (data (port-data port))
  343. (limit (provisional-port-limit port)))
  344. (let ((got
  345. (source-read-block encode-data
  346. port data
  347. buffer
  348. limit
  349. (- (byte-vector-length buffer) limit))))
  350. (provisional-set-port-limit! port (+ limit got))
  351. (maybe-commit))))
  352. (lambda (port)
  353. (if (port-pending-eof? port)
  354. #t
  355. ((source-data-ready? (port-data port)))))))
  356. (define (source-read-block encode-data port data buffer start count)
  357. (let loop ((i 0))
  358. (if (= i count)
  359. count
  360. (let ((next ((source-data-source data))))
  361. (if (eof-object? next)
  362. (begin
  363. (provisional-set-port-pending-eof?! port #t)
  364. i)
  365. (let ((got (encode-data next buffer (+ start i)))) ; we know the end is the limit
  366. (loop (+ i got))))))))
  367. (define (encode-byte thing buffer start)
  368. (byte-vector-set! buffer start thing)
  369. 1)
  370. (define byte-source-input-port-handler
  371. (make-source-input-port-handler 'byte-source-input-port
  372. encode-byte))
  373. (define byte-source->input-port
  374. (make-source->input-port byte-source-input-port-handler))
  375. (define char-source-input-port-handler
  376. (make-source-input-port-handler 'char-source-input-port
  377. encode-char/utf-8))
  378. (define char-source->input-port
  379. (let ((make (make-source->input-port char-source-input-port-handler)))
  380. (lambda (source . more)
  381. (let ((port (apply make source more)))
  382. (set-port-text-codec! port utf-8-codec)
  383. port))))