2007-12-10 08:08:50 -05:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-12-10 08:08:50 -05:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-12-05 05:33:31 -05:00
|
|
|
|
2008-06-09 04:36:27 -04:00
|
|
|
(library (ikarus.io)
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2007-12-06 05:05:26 -05:00
|
|
|
(export
|
2007-12-09 17:13:09 -05:00
|
|
|
port? input-port? output-port? textual-port? binary-port?
|
|
|
|
open-file-input-port open-input-file
|
|
|
|
call-with-input-file with-input-from-file
|
|
|
|
standard-input-port current-input-port
|
2007-12-06 05:05:26 -05:00
|
|
|
open-bytevector-input-port
|
2008-05-06 15:38:05 -04:00
|
|
|
open-string-input-port open-string-input-port/id
|
|
|
|
with-input-from-string
|
2007-12-06 05:05:26 -05:00
|
|
|
make-custom-binary-input-port
|
2007-12-10 10:36:10 -05:00
|
|
|
make-custom-binary-output-port
|
2007-12-10 11:11:59 -05:00
|
|
|
make-custom-textual-input-port
|
|
|
|
make-custom-textual-output-port
|
2007-12-08 14:52:35 -05:00
|
|
|
transcoded-port port-transcoder
|
2007-12-23 22:28:48 -05:00
|
|
|
close-port port-closed? close-input-port close-output-port
|
2007-12-08 14:52:35 -05:00
|
|
|
port-eof?
|
2007-12-09 12:20:13 -05:00
|
|
|
get-char lookahead-char read-char peek-char
|
2008-12-06 12:40:18 -05:00
|
|
|
get-string-n get-string-n! get-string-all get-line read-line
|
2007-12-08 14:52:35 -05:00
|
|
|
get-u8 lookahead-u8
|
|
|
|
get-bytevector-n get-bytevector-n!
|
|
|
|
get-bytevector-some get-bytevector-all
|
2008-12-09 03:00:44 -05:00
|
|
|
port-position port-has-port-position?
|
|
|
|
set-port-position! port-has-set-port-position!?
|
2007-12-08 14:52:35 -05:00
|
|
|
call-with-port
|
2008-12-09 04:46:43 -05:00
|
|
|
flush-output-port
|
2008-01-20 20:30:37 -05:00
|
|
|
put-u8 put-bytevector
|
2007-12-10 07:28:03 -05:00
|
|
|
put-char write-char
|
|
|
|
put-string
|
|
|
|
open-bytevector-output-port
|
|
|
|
call-with-bytevector-output-port
|
2007-12-23 15:12:22 -05:00
|
|
|
open-string-output-port with-output-to-string
|
2008-04-29 00:20:29 -04:00
|
|
|
with-output-to-port
|
2007-12-26 02:16:02 -05:00
|
|
|
call-with-string-output-port
|
|
|
|
open-output-string get-output-string
|
2007-12-10 07:28:03 -05:00
|
|
|
standard-output-port standard-error-port
|
|
|
|
current-output-port current-error-port
|
2007-12-23 15:18:40 -05:00
|
|
|
open-file-output-port open-output-file
|
|
|
|
call-with-output-file with-output-to-file
|
2007-12-10 07:28:03 -05:00
|
|
|
console-output-port
|
2007-12-10 07:40:34 -05:00
|
|
|
console-error-port
|
2007-12-10 07:28:03 -05:00
|
|
|
console-input-port
|
|
|
|
newline
|
|
|
|
port-mode set-port-mode!
|
2008-12-09 04:46:43 -05:00
|
|
|
output-port-buffer-mode
|
2007-12-10 07:28:03 -05:00
|
|
|
reset-input-port!
|
2008-04-06 10:57:56 -04:00
|
|
|
reset-output-port!
|
2007-12-10 10:18:52 -05:00
|
|
|
port-id
|
2007-12-18 17:25:48 -05:00
|
|
|
input-port-byte-position
|
2008-04-30 22:55:59 -04:00
|
|
|
process process-nonblocking
|
2007-12-26 17:35:58 -05:00
|
|
|
|
2007-12-27 22:08:27 -05:00
|
|
|
tcp-connect tcp-connect-nonblocking
|
2008-03-22 19:29:41 -04:00
|
|
|
udp-connect udp-connect-nonblocking
|
2008-03-23 05:02:12 -04:00
|
|
|
tcp-server-socket tcp-server-socket-nonblocking
|
|
|
|
accept-connection accept-connection-nonblocking
|
|
|
|
close-tcp-server-socket
|
|
|
|
register-callback
|
2008-05-21 02:21:37 -04:00
|
|
|
input-socket-buffer-size output-socket-buffer-size)
|
|
|
|
|
2007-12-08 14:52:35 -05:00
|
|
|
|
|
|
|
|
2007-12-05 05:33:31 -05:00
|
|
|
(import
|
2007-12-12 19:18:57 -05:00
|
|
|
(ikarus system $io)
|
2007-12-06 05:05:26 -05:00
|
|
|
(except (ikarus)
|
2007-12-09 17:13:09 -05:00
|
|
|
port? input-port? output-port? textual-port? binary-port?
|
|
|
|
open-file-input-port open-input-file
|
|
|
|
call-with-input-file with-input-from-file
|
|
|
|
standard-input-port current-input-port
|
2007-12-06 05:05:26 -05:00
|
|
|
open-bytevector-input-port
|
2007-12-23 14:44:55 -05:00
|
|
|
open-string-input-port with-input-from-string
|
2007-12-06 05:05:26 -05:00
|
|
|
make-custom-binary-input-port
|
2007-12-10 10:36:10 -05:00
|
|
|
make-custom-binary-output-port
|
2007-12-10 11:11:59 -05:00
|
|
|
make-custom-textual-input-port
|
|
|
|
make-custom-textual-output-port
|
2007-12-08 14:52:35 -05:00
|
|
|
transcoded-port port-transcoder
|
2007-12-23 22:28:48 -05:00
|
|
|
close-port port-closed? close-input-port close-output-port
|
2007-12-08 14:52:35 -05:00
|
|
|
port-eof?
|
2007-12-09 12:20:13 -05:00
|
|
|
get-char lookahead-char read-char peek-char
|
2008-12-06 12:40:18 -05:00
|
|
|
get-string-n get-string-n! get-string-all get-line read-line
|
2007-12-08 14:52:35 -05:00
|
|
|
get-u8 lookahead-u8
|
|
|
|
get-bytevector-n get-bytevector-n!
|
|
|
|
get-bytevector-some get-bytevector-all
|
2008-12-09 03:00:44 -05:00
|
|
|
port-position port-has-port-position?
|
|
|
|
set-port-position! port-has-set-port-position!?
|
2007-12-08 14:52:35 -05:00
|
|
|
call-with-port
|
2007-12-09 17:13:09 -05:00
|
|
|
flush-output-port
|
2008-01-20 20:30:37 -05:00
|
|
|
put-u8 put-bytevector
|
2007-12-10 07:28:03 -05:00
|
|
|
put-char write-char
|
|
|
|
put-string
|
|
|
|
open-bytevector-output-port
|
|
|
|
call-with-bytevector-output-port
|
2007-12-23 15:12:22 -05:00
|
|
|
open-string-output-port with-output-to-string
|
2007-12-10 07:28:03 -05:00
|
|
|
call-with-string-output-port
|
2007-12-26 02:16:02 -05:00
|
|
|
open-output-string get-output-string
|
2007-12-10 07:28:03 -05:00
|
|
|
standard-output-port standard-error-port
|
|
|
|
current-output-port current-error-port
|
2007-12-23 15:18:40 -05:00
|
|
|
open-file-output-port open-output-file
|
|
|
|
call-with-output-file with-output-to-file
|
2008-04-29 00:20:29 -04:00
|
|
|
with-output-to-port
|
2007-12-10 07:28:03 -05:00
|
|
|
console-output-port
|
|
|
|
console-input-port
|
2007-12-10 07:40:34 -05:00
|
|
|
console-error-port
|
2007-12-10 07:28:03 -05:00
|
|
|
newline
|
|
|
|
port-mode set-port-mode!
|
2008-12-09 04:46:43 -05:00
|
|
|
output-port-buffer-mode
|
2007-12-10 07:28:03 -05:00
|
|
|
reset-input-port!
|
2008-04-06 10:57:56 -04:00
|
|
|
reset-output-port!
|
2007-12-10 10:18:52 -05:00
|
|
|
port-id
|
2008-04-30 22:55:59 -04:00
|
|
|
process process-nonblocking
|
2008-03-22 19:29:41 -04:00
|
|
|
tcp-connect tcp-connect-nonblocking
|
|
|
|
udp-connect udp-connect-nonblocking
|
2008-03-23 05:02:12 -04:00
|
|
|
tcp-server-socket tcp-server-socket-nonblocking
|
|
|
|
accept-connection accept-connection-nonblocking
|
|
|
|
close-tcp-server-socket
|
|
|
|
register-callback
|
2008-04-11 14:02:43 -04:00
|
|
|
input-socket-buffer-size output-socket-buffer-size
|
2008-03-22 19:29:41 -04:00
|
|
|
))
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2008-10-18 15:42:11 -04:00
|
|
|
;(define-syntax assert* (identifier-syntax assert))
|
|
|
|
(define-syntax assert* (syntax-rules () [(_ . x) (void)]))
|
|
|
|
|
2007-12-14 01:58:55 -05:00
|
|
|
(module UNSAFE
|
|
|
|
(fx< fx<= fx> fx>= fx= fx+ fx-
|
|
|
|
fxior fxand fxsra fxsll
|
|
|
|
integer->char char->integer
|
|
|
|
string-ref string-set! string-length
|
2008-10-19 23:10:34 -04:00
|
|
|
bytevector-u8-ref bytevector-u8-set!
|
|
|
|
bytevector-u16-ref)
|
2007-12-12 21:22:05 -05:00
|
|
|
(import
|
|
|
|
(rename (ikarus system $strings)
|
|
|
|
($string-length string-length)
|
|
|
|
($string-ref string-ref)
|
|
|
|
($string-set! string-set!))
|
|
|
|
(rename (ikarus system $chars)
|
|
|
|
($char->fixnum char->integer)
|
|
|
|
($fixnum->char integer->char))
|
|
|
|
(rename (ikarus system $bytevectors)
|
|
|
|
($bytevector-set! bytevector-u8-set!)
|
|
|
|
($bytevector-u8-ref bytevector-u8-ref))
|
|
|
|
(rename (ikarus system $fx)
|
|
|
|
($fxsra fxsra)
|
|
|
|
($fxsll fxsll)
|
|
|
|
($fxlogor fxior)
|
|
|
|
($fxlogand fxand)
|
|
|
|
($fx+ fx+)
|
|
|
|
($fx- fx-)
|
|
|
|
($fx< fx<)
|
|
|
|
($fx> fx>)
|
|
|
|
($fx>= fx>=)
|
|
|
|
($fx<= fx<=)
|
2008-10-19 23:10:34 -04:00
|
|
|
($fx= fx=)))
|
|
|
|
(define (bytevector-u16-ref x i endianness)
|
|
|
|
(case endianness
|
|
|
|
[(little)
|
|
|
|
(fxlogor (bytevector-u8-ref x i)
|
|
|
|
(fxsll (bytevector-u8-ref x (fx+ i 1)) 8))]
|
|
|
|
[else
|
|
|
|
(fxlogor (bytevector-u8-ref x (fx+ i 1))
|
|
|
|
(fxsll (bytevector-u8-ref x i) 8))])))
|
|
|
|
|
2007-12-12 21:22:05 -05:00
|
|
|
|
|
|
|
(define (port? x)
|
|
|
|
(import (only (ikarus) port?))
|
|
|
|
(port? x))
|
|
|
|
|
2007-12-08 14:52:35 -05:00
|
|
|
(define-syntax define-rrr
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ name)
|
|
|
|
(define (name . args)
|
2007-12-15 08:22:49 -05:00
|
|
|
(apply die 'name "not implemented" args))]))
|
2007-12-12 19:18:57 -05:00
|
|
|
|
2007-12-12 21:22:05 -05:00
|
|
|
(define-syntax u8?
|
|
|
|
(let ()
|
|
|
|
(import (ikarus system $fx))
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ x)
|
|
|
|
($fxzero? ($fxlogand x -256))])))
|
|
|
|
|
|
|
|
;(define (u8? x) (and (fixnum? x) (fx>= x 0) (fx< x 256)))
|
2007-12-12 18:59:19 -05:00
|
|
|
|
|
|
|
(define (textual-port? x)
|
|
|
|
(fx= (fxand ($port-tag x) textual-port-tag) textual-port-tag))
|
2007-12-05 05:33:31 -05:00
|
|
|
|
2007-12-12 18:59:19 -05:00
|
|
|
(define (binary-port? x)
|
|
|
|
(fx= (fxand ($port-tag x) binary-port-tag) binary-port-tag))
|
2007-12-10 10:18:52 -05:00
|
|
|
|
2007-12-12 18:59:19 -05:00
|
|
|
(define (output-port? x)
|
|
|
|
(fx= (fxand ($port-tag x) output-port-tag) output-port-tag))
|
2007-12-10 10:18:52 -05:00
|
|
|
|
2007-12-12 18:59:19 -05:00
|
|
|
(define (input-port? x)
|
|
|
|
(fx= (fxand ($port-tag x) input-port-tag) input-port-tag))
|
2007-12-10 10:18:52 -05:00
|
|
|
|
2007-12-12 18:59:19 -05:00
|
|
|
;;; everything above this line will turn into primitive
|
|
|
|
;;; ----------------------------------------------------------
|
|
|
|
|
2007-12-12 19:47:03 -05:00
|
|
|
(define input-port-tag #b00000000000001)
|
|
|
|
(define output-port-tag #b00000000000010)
|
|
|
|
(define textual-port-tag #b00000000000100)
|
|
|
|
(define binary-port-tag #b00000000001000)
|
|
|
|
(define fast-char-text-tag #b00000000010000)
|
|
|
|
(define fast-u7-text-tag #b00000000100000)
|
|
|
|
(define fast-u8-text-tag #b00000001100000)
|
2008-10-19 18:43:42 -04:00
|
|
|
(define fast-u16be-text-tag #b00000010000000)
|
|
|
|
(define fast-u16le-text-tag #b00000100000000)
|
2008-10-21 03:31:44 -04:00
|
|
|
(define init-u16-text-tag #b00000110000000)
|
2007-12-12 19:47:03 -05:00
|
|
|
(define r6rs-mode-tag #b01000000000000)
|
|
|
|
(define closed-port-tag #b10000000000000)
|
|
|
|
|
|
|
|
(define port-type-mask #b00000000001111)
|
|
|
|
(define binary-input-port-bits #b00000000001001)
|
|
|
|
(define binary-output-port-bits #b00000000001010)
|
|
|
|
(define textual-input-port-bits #b00000000000101)
|
|
|
|
(define textual-output-port-bits #b00000000000110)
|
|
|
|
|
|
|
|
(define fast-get-byte-tag #b00000000001001)
|
|
|
|
(define fast-get-char-tag #b00000000010101)
|
|
|
|
(define fast-get-utf8-tag #b00000000100101)
|
|
|
|
(define fast-get-latin-tag #b00000001100101)
|
2008-10-19 18:43:42 -04:00
|
|
|
(define fast-get-utf16be-tag #b00000010000101)
|
|
|
|
(define fast-get-utf16le-tag #b00000100000101)
|
2007-12-12 19:47:03 -05:00
|
|
|
|
|
|
|
(define fast-put-byte-tag #b00000000001010)
|
|
|
|
(define fast-put-char-tag #b00000000010110)
|
|
|
|
(define fast-put-utf8-tag #b00000000100110)
|
|
|
|
(define fast-put-latin-tag #b00000001100110)
|
2008-10-21 03:31:44 -04:00
|
|
|
(define fast-put-utf16be-tag #b00000010000110)
|
|
|
|
(define fast-put-utf16le-tag #b00000100000110)
|
|
|
|
(define init-put-utf16-tag #b00000110000110)
|
2007-12-10 10:18:52 -05:00
|
|
|
|
2008-10-19 18:43:42 -04:00
|
|
|
(define fast-attrs-mask #b111111111111)
|
2008-01-19 13:50:53 -05:00
|
|
|
(define-syntax $port-fast-attrs
|
|
|
|
(identifier-syntax
|
|
|
|
(lambda (x)
|
|
|
|
(import (ikarus system $fx))
|
|
|
|
($fxlogand ($port-tag x) fast-attrs-mask))))
|
2007-12-12 21:22:05 -05:00
|
|
|
|
2007-12-10 10:18:52 -05:00
|
|
|
(define (port-id p)
|
|
|
|
(if (port? p)
|
|
|
|
($port-id p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'port-id "not a port" p)))
|
2007-12-10 10:18:52 -05:00
|
|
|
|
2007-12-18 17:25:48 -05:00
|
|
|
(define (input-port-byte-position p)
|
|
|
|
(if (input-port? p)
|
2008-06-18 00:05:01 -04:00
|
|
|
(let ([pos-vec ($port-position p)])
|
|
|
|
(+ (vector-ref pos-vec 0) (fx+ ($port-index p) 1)))
|
2007-12-18 17:25:48 -05:00
|
|
|
(error 'input-port-byte-position "not an input port" p)))
|
|
|
|
|
2008-06-20 00:49:24 -04:00
|
|
|
(define (port-position p)
|
|
|
|
(define who 'port-position)
|
|
|
|
(if (port? p)
|
|
|
|
(let ([pos-vec ($port-position p)]
|
|
|
|
[index ($port-index p)]
|
|
|
|
[get-position ($port-get-position p)])
|
|
|
|
(cond
|
|
|
|
[(procedure? get-position)
|
|
|
|
(let ([pos (get-position)])
|
|
|
|
(if (or (fixnum? pos) (bignum? pos))
|
2008-07-25 01:30:21 -04:00
|
|
|
(if (input-port? p)
|
|
|
|
(- pos (- ($port-size p) index))
|
|
|
|
(+ pos index))
|
|
|
|
(die who "invalid returned value from get-position" p)))]
|
2008-11-11 16:31:35 -05:00
|
|
|
[(eqv? get-position #t)
|
2008-06-20 00:49:24 -04:00
|
|
|
(+ (vector-ref pos-vec 0) index)]
|
|
|
|
[else
|
2008-11-11 16:31:35 -05:00
|
|
|
(die who "port does not support port-position operation" p)]))
|
2008-06-20 00:49:24 -04:00
|
|
|
(die who "not a port" p)))
|
|
|
|
|
2008-12-09 03:00:44 -05:00
|
|
|
|
|
|
|
(define (set-port-position! p pos)
|
|
|
|
(define who 'set-port-position!)
|
2008-12-09 03:41:59 -05:00
|
|
|
(define (set-position! p pos flush?)
|
2008-12-09 03:00:44 -05:00
|
|
|
(let ([setpos! ($port-set-position! p)])
|
2008-12-09 05:27:50 -05:00
|
|
|
(cond
|
|
|
|
[(procedure? setpos!)
|
|
|
|
(when flush? (flush-output-port p))
|
|
|
|
(setpos! pos)
|
|
|
|
($set-port-index! p 0)
|
|
|
|
($set-port-size! p 0)
|
|
|
|
(let ([pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 pos))]
|
|
|
|
[(eqv? setpos! #t)
|
|
|
|
(if (<= pos ($port-size p))
|
|
|
|
($set-port-index! p pos)
|
|
|
|
(die who "position out of range" pos))]
|
|
|
|
[else
|
|
|
|
(die who "port does not support port position" p)])))
|
2008-12-09 03:00:44 -05:00
|
|
|
(unless (and (or (fixnum? pos) (bignum? pos)) (>= pos 0))
|
|
|
|
(die who "position must be a nonnegative exact integer" pos))
|
|
|
|
(cond
|
2008-12-09 03:41:59 -05:00
|
|
|
[(output-port? p) (set-position! p pos #t)]
|
|
|
|
[(input-port? p) (set-position! p pos #f)]
|
2008-12-09 03:00:44 -05:00
|
|
|
[else (die who "not a port" p)]))
|
|
|
|
|
|
|
|
|
2008-06-20 00:49:24 -04:00
|
|
|
(define (port-has-port-position? p)
|
|
|
|
(define who 'port-has-port-position?)
|
|
|
|
(if (port? p)
|
2008-11-11 16:31:35 -05:00
|
|
|
(and ($port-get-position p) #t)
|
2008-06-20 00:49:24 -04:00
|
|
|
(die who "not a port" p)))
|
2008-12-09 03:00:44 -05:00
|
|
|
|
|
|
|
(define (port-has-set-port-position!? p)
|
|
|
|
(define who 'port-has-set-port-position!?)
|
|
|
|
(if (port? p)
|
|
|
|
(and ($port-set-position! p) #t)
|
|
|
|
(die who "not a port" p)))
|
|
|
|
|
2008-12-09 02:06:46 -05:00
|
|
|
|
2007-12-12 01:32:55 -05:00
|
|
|
(define guarded-port
|
|
|
|
(let ([G (make-guardian)])
|
|
|
|
(define (clean-up)
|
|
|
|
(cond
|
|
|
|
[(G) =>
|
|
|
|
(lambda (p)
|
|
|
|
(close-port p)
|
|
|
|
(clean-up))]))
|
|
|
|
(lambda (p)
|
|
|
|
(clean-up)
|
2008-01-12 17:32:43 -05:00
|
|
|
(when (fixnum? ($port-cookie p))
|
2007-12-12 01:32:55 -05:00
|
|
|
(G p))
|
|
|
|
p)))
|
|
|
|
|
2007-12-11 03:25:51 -05:00
|
|
|
(define ($make-custom-binary-port attrs init-size id
|
2007-12-10 10:36:10 -05:00
|
|
|
read! write! get-position set-position! close buffer-size)
|
2007-12-06 05:05:26 -05:00
|
|
|
(let ([bv (make-bytevector buffer-size)])
|
2008-03-23 05:02:12 -04:00
|
|
|
($make-port attrs 0 init-size bv #f id read! write!
|
2008-06-20 00:49:24 -04:00
|
|
|
get-position set-position! close #f (vector 0))))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
2007-12-11 03:25:51 -05:00
|
|
|
(define ($make-custom-textual-port attrs init-size id
|
2007-12-10 11:11:59 -05:00
|
|
|
read! write! get-position set-position! close buffer-size)
|
|
|
|
(let ([bv (make-string buffer-size)])
|
2008-03-23 05:02:12 -04:00
|
|
|
($make-port attrs 0 init-size bv #t id read! write!
|
2008-06-20 00:49:24 -04:00
|
|
|
get-position set-position! close #f (vector 0))))
|
2007-12-10 11:11:59 -05:00
|
|
|
|
2007-12-06 05:05:26 -05:00
|
|
|
(define (make-custom-binary-input-port id
|
|
|
|
read! get-position set-position! close)
|
|
|
|
;;; FIXME: get-position and set-position! are ignored for now
|
|
|
|
(define who 'make-custom-binary-input-port)
|
|
|
|
(unless (string? id)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "id is not a string" id))
|
2007-12-06 05:05:26 -05:00
|
|
|
(unless (procedure? read!)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "read! is not a procedure" read!))
|
2007-12-06 05:05:26 -05:00
|
|
|
(unless (or (procedure? close) (not close))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "close should be either a procedure or #f" close))
|
2008-06-20 00:49:24 -04:00
|
|
|
(unless (or (procedure? get-position)
|
|
|
|
(not get-position))
|
|
|
|
(die who "get-position is not a procedure or #f"
|
|
|
|
get-position))
|
2007-12-10 10:56:10 -05:00
|
|
|
($make-custom-binary-port
|
2007-12-12 18:59:19 -05:00
|
|
|
binary-input-port-bits
|
2007-12-11 03:25:51 -05:00
|
|
|
0
|
2008-06-20 00:49:24 -04:00
|
|
|
id read! #f
|
|
|
|
get-position
|
|
|
|
set-position!
|
|
|
|
close 256))
|
2007-12-10 10:36:10 -05:00
|
|
|
|
|
|
|
(define (make-custom-binary-output-port id
|
|
|
|
write! get-position set-position! close)
|
|
|
|
;;; FIXME: get-position and set-position! are ignored for now
|
|
|
|
(define who 'make-custom-binary-output-port)
|
|
|
|
(unless (string? id)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "id is not a string" id))
|
2007-12-10 10:36:10 -05:00
|
|
|
(unless (procedure? write!)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "write! is not a procedure" write!))
|
2007-12-10 10:36:10 -05:00
|
|
|
(unless (or (procedure? close) (not close))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "close should be either a procedure or #f" close))
|
2008-06-20 00:49:24 -04:00
|
|
|
(unless (or (procedure? get-position)
|
|
|
|
(not get-position))
|
|
|
|
(die who "get-position is not a procedure or #f"
|
|
|
|
get-position))
|
2007-12-10 10:53:17 -05:00
|
|
|
($make-custom-binary-port
|
2007-12-12 18:59:19 -05:00
|
|
|
binary-output-port-bits
|
2007-12-11 03:25:51 -05:00
|
|
|
256
|
2008-06-20 00:49:24 -04:00
|
|
|
id #f write!
|
|
|
|
get-position
|
|
|
|
set-position!
|
|
|
|
close 256))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
2007-12-10 11:11:59 -05:00
|
|
|
(define (make-custom-textual-input-port id
|
|
|
|
read! get-position set-position! close)
|
|
|
|
;;; FIXME: get-position and set-position! are ignored for now
|
|
|
|
(define who 'make-custom-textual-input-port)
|
|
|
|
(unless (string? id)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "id is not a string" id))
|
2007-12-10 11:11:59 -05:00
|
|
|
(unless (procedure? read!)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "read! is not a procedure" read!))
|
2007-12-10 11:11:59 -05:00
|
|
|
(unless (or (procedure? close) (not close))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "close should be either a procedure or #f" close))
|
2008-06-20 00:49:24 -04:00
|
|
|
(unless (or (procedure? get-position)
|
|
|
|
(not get-position))
|
|
|
|
(die who "get-position is not a procedure or #f"
|
|
|
|
get-position))
|
2007-12-10 11:11:59 -05:00
|
|
|
($make-custom-textual-port
|
2007-12-14 19:42:00 -05:00
|
|
|
(fxior textual-input-port-bits fast-char-text-tag)
|
2007-12-11 03:25:51 -05:00
|
|
|
0
|
2007-12-10 11:11:59 -05:00
|
|
|
id read! #f get-position
|
|
|
|
set-position! close 256))
|
|
|
|
|
|
|
|
(define (make-custom-textual-output-port id
|
|
|
|
write! get-position set-position! close)
|
|
|
|
;;; FIXME: get-position and set-position! are ignored for now
|
|
|
|
(define who 'make-custom-textual-output-port)
|
|
|
|
(unless (string? id)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "id is not a string" id))
|
2007-12-10 11:11:59 -05:00
|
|
|
(unless (procedure? write!)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "write! is not a procedure" write!))
|
2007-12-10 11:11:59 -05:00
|
|
|
(unless (or (procedure? close) (not close))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "close should be either a procedure or #f" close))
|
2008-06-20 00:49:24 -04:00
|
|
|
(unless (or (procedure? get-position)
|
|
|
|
(not get-position))
|
|
|
|
(die who "get-position is not a procedure or #f"
|
|
|
|
get-position))
|
2007-12-10 11:11:59 -05:00
|
|
|
($make-custom-textual-port
|
2007-12-14 19:42:00 -05:00
|
|
|
(fxior textual-output-port-bits fast-char-text-tag)
|
2007-12-11 03:25:51 -05:00
|
|
|
256
|
2007-12-10 11:11:59 -05:00
|
|
|
id #f write! get-position
|
|
|
|
set-position! close 256))
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-22 03:30:52 -05:00
|
|
|
(define (input-transcoder-attrs x who)
|
2007-12-06 05:05:26 -05:00
|
|
|
(cond
|
2007-12-06 08:14:05 -05:00
|
|
|
[(not x) ;;; binary input port
|
2007-12-12 18:59:19 -05:00
|
|
|
binary-input-port-bits]
|
2008-01-22 03:30:52 -05:00
|
|
|
[(not (eq? 'none (transcoder-eol-style x)))
|
|
|
|
(die who "unsupported transcoder eol-style"
|
|
|
|
(transcoder-eol-style x))]
|
|
|
|
[(eq? 'latin-1-codec (transcoder-codec x))
|
2007-12-12 18:59:19 -05:00
|
|
|
(fxior textual-input-port-bits fast-u8-text-tag)]
|
2007-12-14 19:45:50 -05:00
|
|
|
;;; attrs for utf-8-codec are set as part of the
|
|
|
|
;;; bom-reading dance when the first char is read.
|
2007-12-12 18:59:19 -05:00
|
|
|
[else textual-input-port-bits]))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
2008-01-22 03:30:52 -05:00
|
|
|
(define (output-transcoder-attrs x who)
|
2007-12-10 07:28:03 -05:00
|
|
|
(cond
|
|
|
|
[(not x) ;;; binary input port
|
2007-12-12 18:59:19 -05:00
|
|
|
binary-output-port-bits]
|
2008-01-22 03:30:52 -05:00
|
|
|
[(not (eq? 'none (transcoder-eol-style x)))
|
|
|
|
(die who "unsupported transcoder eol-style"
|
|
|
|
(transcoder-eol-style x))]
|
|
|
|
[(eq? 'latin-1-codec (transcoder-codec x))
|
2007-12-12 18:59:19 -05:00
|
|
|
(fxior textual-output-port-bits fast-u8-text-tag)]
|
2008-01-22 03:30:52 -05:00
|
|
|
[(eq? 'utf-8-codec (transcoder-codec x))
|
2007-12-12 18:59:19 -05:00
|
|
|
(fxior textual-output-port-bits fast-u7-text-tag)]
|
2008-10-21 03:31:44 -04:00
|
|
|
[(eq? 'utf-16-codec (transcoder-codec x))
|
2008-10-21 23:00:10 -04:00
|
|
|
(fxior textual-output-port-bits fast-u16be-text-tag)]
|
2008-07-30 20:28:33 -04:00
|
|
|
[else (die who "unsupported codec" (transcoder-codec x))]))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
|
|
|
(define open-bytevector-input-port
|
|
|
|
(case-lambda
|
|
|
|
[(bv) (open-bytevector-input-port bv #f)]
|
|
|
|
[(bv maybe-transcoder)
|
|
|
|
(unless (bytevector? bv)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'open-bytevector-input-port
|
2007-12-06 05:05:26 -05:00
|
|
|
"not a bytevector" bv))
|
|
|
|
(when (and maybe-transcoder
|
2007-12-06 08:14:05 -05:00
|
|
|
(not (transcoder? maybe-transcoder)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'open-bytevector-input-port
|
2007-12-06 05:05:26 -05:00
|
|
|
"not a transcoder" maybe-transcoder))
|
2007-12-12 19:18:57 -05:00
|
|
|
($make-port
|
2008-01-22 03:30:52 -05:00
|
|
|
(input-transcoder-attrs maybe-transcoder
|
|
|
|
'open-bytevector-output-port)
|
2007-12-12 19:59:10 -05:00
|
|
|
0 (bytevector-length bv) bv
|
2007-12-06 05:05:26 -05:00
|
|
|
maybe-transcoder
|
|
|
|
"*bytevector-input-port*"
|
|
|
|
(lambda (bv i c) 0) ;;; read!
|
|
|
|
#f ;;; write!
|
2008-11-11 16:31:35 -05:00
|
|
|
#t ;;; get-position
|
2008-12-09 05:27:50 -05:00
|
|
|
#t ;;; set-position!
|
2007-12-06 05:05:26 -05:00
|
|
|
#f ;;; close
|
2008-06-18 00:05:01 -04:00
|
|
|
#f
|
|
|
|
(vector 0))]))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define open-bytevector-output-port
|
|
|
|
(case-lambda
|
|
|
|
[() (open-bytevector-output-port #f)]
|
|
|
|
[(transcoder)
|
|
|
|
(define who 'open-bytevector-output-port)
|
|
|
|
(unless (or (not transcoder) (transcoder? transcoder))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid transcoder value" transcoder))
|
2007-12-10 07:28:03 -05:00
|
|
|
(let ([buf* '()] [buffer-size 256])
|
|
|
|
(let ([p
|
2007-12-12 19:18:57 -05:00
|
|
|
($make-port
|
2008-01-22 03:30:52 -05:00
|
|
|
(output-transcoder-attrs transcoder
|
|
|
|
'open-bytevector-output-port)
|
2007-12-12 19:59:10 -05:00
|
|
|
0 buffer-size (make-bytevector buffer-size)
|
2007-12-10 07:28:03 -05:00
|
|
|
transcoder
|
|
|
|
"*bytevector-output-port*"
|
|
|
|
#f
|
|
|
|
(lambda (bv i c)
|
|
|
|
(unless (= c 0)
|
|
|
|
(let ([x (make-bytevector c)])
|
|
|
|
(bytevector-copy! bv i x 0 c)
|
|
|
|
(set! buf* (cons x buf*))))
|
|
|
|
c)
|
2008-11-11 16:31:35 -05:00
|
|
|
#t ;;; get-position
|
|
|
|
#f ;;; set-position!
|
|
|
|
#f ;;; close
|
|
|
|
#f ;;; cookie
|
2008-06-18 00:05:01 -04:00
|
|
|
(vector 0))])
|
2007-12-10 07:28:03 -05:00
|
|
|
(values
|
|
|
|
p
|
|
|
|
(lambda ()
|
|
|
|
(define (append-bv-buf* ls)
|
|
|
|
(let f ([ls ls] [i 0])
|
|
|
|
(cond
|
|
|
|
[(null? ls)
|
|
|
|
(values (make-bytevector i) 0)]
|
|
|
|
[else
|
|
|
|
(let* ([a (car ls)]
|
|
|
|
[n (bytevector-length a)])
|
|
|
|
(let-values ([(bv i) (f (cdr ls) (fx+ i n))])
|
|
|
|
(bytevector-copy! a 0 bv i n)
|
|
|
|
(values bv (fx+ i n))))])))
|
|
|
|
(unless ($port-closed? p)
|
|
|
|
(flush-output-port p))
|
|
|
|
(let-values ([(bv len) (append-bv-buf* buf*)])
|
|
|
|
(set! buf* '())
|
|
|
|
bv)))))]))
|
|
|
|
|
|
|
|
(define call-with-bytevector-output-port
|
|
|
|
(case-lambda
|
|
|
|
[(proc) (call-with-bytevector-output-port proc #f)]
|
|
|
|
[(proc transcoder)
|
|
|
|
(define who 'call-with-bytevector-output-port)
|
|
|
|
(unless (procedure? proc)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a procedure" proc))
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (or (not transcoder) (transcoder? transcoder))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid transcoder argument" transcoder))
|
2007-12-10 07:28:03 -05:00
|
|
|
(let-values ([(p extract)
|
|
|
|
(open-bytevector-output-port transcoder)])
|
|
|
|
(proc p)
|
|
|
|
(extract))]))
|
|
|
|
|
|
|
|
(define (call-with-string-output-port proc)
|
|
|
|
(define who 'call-with-string-output-port)
|
|
|
|
(unless (procedure? proc)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a procedure" proc))
|
2007-12-10 07:28:03 -05:00
|
|
|
(let-values ([(p extract) (open-string-output-port)])
|
|
|
|
(proc p)
|
|
|
|
(extract)))
|
2007-12-23 15:12:22 -05:00
|
|
|
|
|
|
|
(define (with-output-to-string proc)
|
|
|
|
(define who 'with-output-to-string)
|
|
|
|
(unless (procedure? proc)
|
|
|
|
(die who "not a procedure" proc))
|
|
|
|
(let-values ([(p extract) (open-string-output-port)])
|
2008-11-15 11:21:00 -05:00
|
|
|
(parameterize ([current-output-port p])
|
2007-12-23 15:12:22 -05:00
|
|
|
(proc))
|
|
|
|
(extract)))
|
2008-04-29 00:20:29 -04:00
|
|
|
|
|
|
|
(define (with-output-to-port p proc)
|
|
|
|
(define who 'with-output-to-port)
|
|
|
|
(unless (procedure? proc)
|
|
|
|
(die who "not a procedure" proc))
|
|
|
|
(unless (output-port? p)
|
|
|
|
(die who "not an output port" p))
|
|
|
|
(unless (textual-port? p)
|
|
|
|
(die who "not a textual port" p))
|
2008-11-15 11:21:00 -05:00
|
|
|
(parameterize ([current-output-port p])
|
2008-04-29 00:20:29 -04:00
|
|
|
(proc)))
|
|
|
|
|
2007-12-26 02:16:02 -05:00
|
|
|
(define-struct output-string-cookie (strings))
|
|
|
|
|
|
|
|
|
|
|
|
(define (open-output-string)
|
|
|
|
(define who 'open-output-string)
|
|
|
|
(let ([cookie (make-output-string-cookie '())]
|
|
|
|
[buffer-size 256])
|
|
|
|
($make-port
|
|
|
|
(fxior textual-output-port-bits fast-char-text-tag)
|
|
|
|
0 buffer-size (make-string buffer-size)
|
|
|
|
#t ;;; transcoder
|
|
|
|
"*string-output-port*"
|
|
|
|
#f
|
|
|
|
(lambda (str i c)
|
|
|
|
(unless (= c 0)
|
|
|
|
(let ([x (make-string c)])
|
|
|
|
(string-copy! str i x 0 c)
|
|
|
|
(set-output-string-cookie-strings! cookie
|
|
|
|
(cons x (output-string-cookie-strings cookie)))))
|
|
|
|
c)
|
2008-11-11 16:31:35 -05:00
|
|
|
#t ;;; get-position
|
|
|
|
#f ;;; set-position!
|
|
|
|
#f ;;; close!
|
2008-06-18 00:05:01 -04:00
|
|
|
cookie
|
|
|
|
(vector 0))))
|
2007-12-26 02:16:02 -05:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (open-string-output-port)
|
2007-12-26 02:16:02 -05:00
|
|
|
(let ([p (open-output-string)])
|
|
|
|
(values
|
|
|
|
p
|
|
|
|
(lambda ()
|
|
|
|
(let ([str (get-output-string p)])
|
|
|
|
(set-output-string-cookie-strings! ($port-cookie p) '())
|
|
|
|
str)))))
|
|
|
|
|
|
|
|
(define (get-output-string-cookie-data cookie)
|
|
|
|
(define (append-str-buf* ls)
|
|
|
|
(let f ([ls ls] [i 0])
|
|
|
|
(cond
|
|
|
|
[(null? ls)
|
|
|
|
(values (make-string i) 0)]
|
|
|
|
[else
|
|
|
|
(let* ([a (car ls)]
|
|
|
|
[n (string-length a)])
|
|
|
|
(let-values ([(bv i) (f (cdr ls) (fx+ i n))])
|
|
|
|
(string-copy! a 0 bv i n)
|
|
|
|
(values bv (fx+ i n))))])))
|
|
|
|
(let ([buf* (output-string-cookie-strings cookie)])
|
|
|
|
(let-values ([(bv len) (append-str-buf* buf*)])
|
|
|
|
bv)))
|
|
|
|
|
|
|
|
(define (get-output-string p)
|
|
|
|
(if (port? p)
|
|
|
|
(let ([cookie ($port-cookie p)])
|
|
|
|
(cond
|
|
|
|
[(output-string-cookie? cookie)
|
|
|
|
(unless ($port-closed? p)
|
|
|
|
(flush-output-port p))
|
|
|
|
(get-output-string-cookie-data cookie)]
|
|
|
|
[else
|
|
|
|
(die 'get-output-string "not an output-string port" p)]))
|
|
|
|
(die 'get-output-string "not a port" p)))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
2008-05-06 15:38:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
(define (open-string-input-port/id str id)
|
2007-12-07 07:39:17 -05:00
|
|
|
(unless (string? str)
|
2008-01-23 02:17:25 -05:00
|
|
|
(die 'open-string-input-port "not a string" str))
|
2007-12-12 19:18:57 -05:00
|
|
|
($make-port
|
|
|
|
(fxior textual-input-port-bits fast-char-text-tag)
|
2007-12-12 19:59:10 -05:00
|
|
|
0 (string-length str) str
|
2007-12-11 17:41:48 -05:00
|
|
|
#t ;;; transcoder
|
2008-05-06 15:38:05 -04:00
|
|
|
id
|
2007-12-07 07:39:17 -05:00
|
|
|
(lambda (str i c) 0) ;;; read!
|
|
|
|
#f ;;; write!
|
2008-11-11 16:31:35 -05:00
|
|
|
#t ;;; get-position
|
2008-12-09 05:27:50 -05:00
|
|
|
#t ;;; set-position!
|
2007-12-07 07:39:17 -05:00
|
|
|
#f ;;; close
|
2008-11-11 16:31:35 -05:00
|
|
|
#f ;;; cookie
|
2008-06-18 00:05:01 -04:00
|
|
|
(vector 0)))
|
2008-05-06 15:38:05 -04:00
|
|
|
|
|
|
|
(define (open-string-input-port str)
|
|
|
|
(open-string-input-port/id str "*string-input-port*"))
|
2007-12-07 07:39:17 -05:00
|
|
|
|
2007-12-06 08:14:05 -05:00
|
|
|
(define (transcoded-port p transcoder)
|
|
|
|
(define who 'transcoded-port)
|
|
|
|
(unless (transcoder? transcoder)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a transcoder" transcoder))
|
|
|
|
(unless (port? p) (die who "not a port" p))
|
|
|
|
(when ($port-transcoder p) (die who "not a binary port" p))
|
|
|
|
(when ($port-closed? p) (die who "cannot transcode closed port" p))
|
2007-12-06 08:14:05 -05:00
|
|
|
(let ([read! ($port-read! p)]
|
2007-12-12 19:34:28 -05:00
|
|
|
[write! ($port-write! p)])
|
|
|
|
($mark-port-closed! p)
|
2007-12-12 01:32:55 -05:00
|
|
|
(guarded-port
|
|
|
|
($make-port
|
|
|
|
(cond
|
2008-01-22 03:30:52 -05:00
|
|
|
[read! (input-transcoder-attrs transcoder
|
|
|
|
'transcoded-port)]
|
|
|
|
[write! (output-transcoder-attrs transcoder
|
|
|
|
'transcoded-port)]
|
2007-12-12 01:32:55 -05:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'transcoded-port
|
2007-12-12 01:32:55 -05:00
|
|
|
"port is neither input nor output!")])
|
2007-12-12 19:18:57 -05:00
|
|
|
($port-index p)
|
|
|
|
($port-size p)
|
|
|
|
($port-buffer p)
|
|
|
|
transcoder
|
2007-12-12 01:32:55 -05:00
|
|
|
($port-id p)
|
|
|
|
read!
|
|
|
|
write!
|
|
|
|
($port-get-position p)
|
|
|
|
($port-set-position! p)
|
|
|
|
($port-close p)
|
2008-06-18 00:05:01 -04:00
|
|
|
($port-cookie p)
|
|
|
|
(vector 0)))))
|
2007-12-06 08:14:05 -05:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (reset-input-port! p)
|
|
|
|
(if (input-port? p)
|
2008-04-02 20:28:45 -04:00
|
|
|
(begin
|
|
|
|
($set-port-index! p ($port-size p))
|
|
|
|
(unregister-callback p))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'reset-input-port! "not an input port" p)))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
2008-04-06 10:57:56 -04:00
|
|
|
(define (reset-output-port! p)
|
|
|
|
(if (output-port? p)
|
|
|
|
(begin
|
|
|
|
($set-port-index! p 0)
|
|
|
|
(unregister-callback p))
|
|
|
|
(die 'reset-output-port! "not an output port" p)))
|
|
|
|
|
|
|
|
|
2007-12-08 14:52:35 -05:00
|
|
|
(define (port-transcoder p)
|
2007-12-12 19:18:57 -05:00
|
|
|
(if (port? p)
|
2007-12-08 14:52:35 -05:00
|
|
|
(let ([tr ($port-transcoder p)])
|
|
|
|
(and (transcoder? tr) tr))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'port-transcoder "not a port" p)))
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2007-12-12 19:34:28 -05:00
|
|
|
(define ($port-closed? p)
|
2008-01-19 14:51:14 -05:00
|
|
|
(import UNSAFE)
|
|
|
|
(not (fx= (fxand ($port-attrs p) closed-port-tag) 0)))
|
2007-12-23 22:28:48 -05:00
|
|
|
|
|
|
|
(define (port-closed? p)
|
|
|
|
(if (port? p)
|
|
|
|
($port-closed? p)
|
|
|
|
(error 'port-closed? "not a port" p)))
|
|
|
|
|
2007-12-12 19:34:28 -05:00
|
|
|
(define ($mark-port-closed! p)
|
|
|
|
($set-port-attrs! p
|
|
|
|
(fxior closed-port-tag
|
|
|
|
(fxand ($port-attrs p) port-type-mask))))
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (port-mode p)
|
2007-12-12 19:18:57 -05:00
|
|
|
(if (port? p)
|
2007-12-10 07:28:03 -05:00
|
|
|
(if (fxzero? (fxand ($port-attrs p) r6rs-mode-tag))
|
|
|
|
'ikarus-mode
|
|
|
|
'r6rs-mode)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'port-mode "not a port" p)))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
|
|
|
(define (set-port-mode! p mode)
|
2007-12-12 19:18:57 -05:00
|
|
|
(if (port? p)
|
2007-12-10 07:28:03 -05:00
|
|
|
(case mode
|
|
|
|
[(r6rs-mode)
|
|
|
|
($set-port-attrs! p
|
|
|
|
(fxior ($port-attrs p) r6rs-mode-tag))]
|
|
|
|
[(ikarus-mode)
|
|
|
|
($set-port-attrs! p
|
|
|
|
(fxand ($port-attrs p) (fxnot r6rs-mode-tag)))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'set-port-mode! "invalid mode" mode)])
|
|
|
|
(die 'set-port-mode! "not a port" p)))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
2008-12-12 16:03:10 -05:00
|
|
|
(define (put-byte/unbuffered! p b who)
|
|
|
|
(when ($port-closed? p) (die who "port is closed" p))
|
|
|
|
(let ([bv (make-bytevector 1)])
|
|
|
|
(bytevector-u8-set! bv 0 b)
|
|
|
|
(let ([bytes (($port-write! p) bv 0 1)])
|
|
|
|
(cond
|
|
|
|
[(eq? bytes 1)
|
|
|
|
(let ([pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 (+ (vector-ref pos-vec 0) 1)))]
|
|
|
|
[(eq? bytes 0)
|
|
|
|
($mark-port-closed! p)
|
|
|
|
(die who "could not write bytes to sink")]
|
|
|
|
[else
|
|
|
|
(die who "invalid return value from write! proc" bytes p)]))))
|
|
|
|
|
|
|
|
(define (put-char/unbuffered! p c who)
|
|
|
|
(when ($port-closed? p) (die who "port is closed" p))
|
|
|
|
(let ([str (string c)])
|
|
|
|
(let ([bytes (($port-write! p) str 0 1)])
|
|
|
|
(cond
|
|
|
|
[(eq? bytes 1)
|
|
|
|
(let ([pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 (+ (vector-ref pos-vec 0) 1)))]
|
|
|
|
[(eq? bytes 0)
|
|
|
|
($mark-port-closed! p)
|
|
|
|
(die who "could not write char to sink")]
|
|
|
|
[else
|
|
|
|
(die who "invalid return value from write! proc" bytes p)]))))
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define flush-output-port
|
|
|
|
(case-lambda
|
2008-11-15 11:21:00 -05:00
|
|
|
[() (flush-output-port (current-output-port))]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(p)
|
2008-01-19 14:51:14 -05:00
|
|
|
(import UNSAFE)
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (output-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'flush-output-port "not an output port" p))
|
2007-12-10 07:28:03 -05:00
|
|
|
(when ($port-closed? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'flush-output-port "port is closed" p))
|
2007-12-10 07:28:03 -05:00
|
|
|
(let ([idx ($port-index p)]
|
|
|
|
[buf ($port-buffer p)])
|
|
|
|
(unless (fx= idx 0)
|
|
|
|
(let ([bytes (($port-write! p) buf 0 idx)])
|
|
|
|
(unless (and (fixnum? bytes) (fx>= bytes 0) (fx<= bytes idx))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'flush-output-port
|
2007-12-10 07:28:03 -05:00
|
|
|
"write! returned an invalid value"
|
|
|
|
bytes))
|
2008-06-20 00:49:24 -04:00
|
|
|
(let ([pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 (+ (vector-ref pos-vec 0) bytes)))
|
2007-12-10 07:28:03 -05:00
|
|
|
(cond
|
|
|
|
[(fx= bytes idx)
|
|
|
|
($set-port-index! p 0)]
|
|
|
|
[(fx= bytes 0)
|
2008-04-06 10:57:56 -04:00
|
|
|
($mark-port-closed! p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'flush-output-port "could not write bytes to sink")]
|
2007-12-10 07:28:03 -05:00
|
|
|
[else
|
|
|
|
(bytevector-copy! buf bytes buf 0 (fx- idx bytes))
|
|
|
|
($set-port-index! p (fx- idx bytes))
|
|
|
|
(flush-output-port p)]))))]))
|
2007-12-09 17:13:09 -05:00
|
|
|
|
|
|
|
(define ($close-port p)
|
2007-12-06 05:05:26 -05:00
|
|
|
(cond
|
|
|
|
[($port-closed? p) (void)]
|
|
|
|
[else
|
|
|
|
(when ($port-write! p)
|
|
|
|
(flush-output-port p))
|
2007-12-12 19:34:28 -05:00
|
|
|
($mark-port-closed! p)
|
2007-12-06 05:05:26 -05:00
|
|
|
(let ([close ($port-close p)])
|
|
|
|
(when (procedure? close)
|
|
|
|
(close)))]))
|
2007-12-05 11:36:25 -05:00
|
|
|
|
2007-12-09 17:13:09 -05:00
|
|
|
(define (close-port p)
|
2007-12-12 19:18:57 -05:00
|
|
|
(unless (port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'close-port "not a port" p))
|
2007-12-09 17:13:09 -05:00
|
|
|
($close-port p))
|
|
|
|
|
|
|
|
(define (close-input-port p)
|
|
|
|
(unless (input-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'close-input-port "not an input port" p))
|
2007-12-09 17:13:09 -05:00
|
|
|
($close-port p))
|
|
|
|
|
|
|
|
(define (close-output-port p)
|
|
|
|
(unless (output-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'close-output-port "not an output port" p))
|
2007-12-09 17:13:09 -05:00
|
|
|
($close-port p))
|
|
|
|
|
2008-01-22 10:59:04 -05:00
|
|
|
(define (refill-bv-buffer p who)
|
|
|
|
(when ($port-closed? p) (die who "port is closed" p))
|
|
|
|
(let ([bv ($port-buffer p)] [i ($port-index p)] [j ($port-size p)])
|
|
|
|
(let ([c0 (fx- j i)])
|
|
|
|
(unless (fx= c0 0) (bytevector-copy! bv i bv 0 c0))
|
2008-06-18 00:05:01 -04:00
|
|
|
(let ([pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 (+ (vector-ref pos-vec 0) i)))
|
2008-01-22 10:59:04 -05:00
|
|
|
(let* ([max (fx- (bytevector-length bv) c0)]
|
|
|
|
[c1 (($port-read! p) bv c0 max)])
|
|
|
|
(unless (fixnum? c1)
|
|
|
|
(die who "invalid return value from read! procedure" c1))
|
|
|
|
(cond
|
2008-06-08 18:48:52 -04:00
|
|
|
[(fx>= c1 0)
|
|
|
|
(unless (fx<= c1 max)
|
|
|
|
(die who "read! returned a value out of range" c1))
|
2008-06-08 09:19:29 -04:00
|
|
|
($set-port-index! p 0)
|
2008-01-22 10:59:04 -05:00
|
|
|
($set-port-size! p (fx+ c1 c0))
|
|
|
|
c1]
|
|
|
|
[else
|
|
|
|
(die who "read! returned a value out of range" c1)])))))
|
|
|
|
|
2007-12-05 11:36:25 -05:00
|
|
|
;;; ----------------------------------------------------------
|
2007-12-15 11:43:04 -05:00
|
|
|
(module (read-char get-char lookahead-char)
|
2007-12-12 21:22:05 -05:00
|
|
|
(import UNSAFE)
|
2007-12-18 17:25:48 -05:00
|
|
|
(define (get-char-latin-mode p who inc)
|
|
|
|
(let ([n (refill-bv-buffer p who)])
|
2007-12-06 08:14:05 -05:00
|
|
|
(cond
|
|
|
|
[(fx= n 0) (eof-object)]
|
|
|
|
[else
|
2007-12-18 17:25:48 -05:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
($set-port-index! p (fx+ idx inc))
|
|
|
|
(integer->char (bytevector-u8-ref ($port-buffer p) idx)))])))
|
2007-12-07 04:42:10 -05:00
|
|
|
(define (get-char-utf8-mode p who)
|
|
|
|
(define (do-error p who)
|
|
|
|
(case (transcoder-error-handling-mode ($port-transcoder p))
|
|
|
|
[(ignore) (get-char p)]
|
|
|
|
[(replace) #\xFFFD]
|
|
|
|
[(raise)
|
|
|
|
(raise (make-i/o-decoding-error p))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "cannot happen")]))
|
2007-12-07 04:42:10 -05:00
|
|
|
(let ([i ($port-index p)]
|
|
|
|
[j ($port-size p)]
|
|
|
|
[buf ($port-buffer p)])
|
|
|
|
(cond
|
|
|
|
[(fx= i j) ;;; exhausted
|
2007-12-07 05:34:46 -05:00
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0) (eof-object)]
|
|
|
|
[else (get-char p)]))]
|
2007-12-07 04:42:10 -05:00
|
|
|
[else
|
2007-12-07 05:34:46 -05:00
|
|
|
(let ([b0 (bytevector-u8-ref buf i)])
|
2007-12-07 04:42:10 -05:00
|
|
|
(cond
|
|
|
|
[(fx= (fxsra b0 5) #b110) ;;; two-byte-encoding
|
|
|
|
(let ([i (fx+ i 1)])
|
|
|
|
(cond
|
|
|
|
[(fx< i j)
|
2007-12-07 05:34:46 -05:00
|
|
|
(let ([b1 (bytevector-u8-ref buf i)])
|
2007-12-07 04:42:10 -05:00
|
|
|
(cond
|
|
|
|
[(fx= (fxsra b1 6) #b10)
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(integer->char
|
|
|
|
(fxior (fxand b1 #b111111)
|
|
|
|
(fxsll (fxand b0 #b11111) 6)))]
|
|
|
|
[else
|
2007-12-07 05:34:46 -05:00
|
|
|
($set-port-index! p i)
|
2007-12-07 04:42:10 -05:00
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
($set-port-index! p (fx+ ($port-index p) 1))
|
|
|
|
(do-error p who)]
|
|
|
|
[else (get-char-utf8-mode p who)]))]))]
|
2007-12-07 05:34:46 -05:00
|
|
|
[(fx= (fxsra b0 4) #b1110) ;;; three-byte-encoding
|
|
|
|
(cond
|
|
|
|
[(fx< (fx+ i 2) j)
|
|
|
|
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))]
|
|
|
|
[b2 (bytevector-u8-ref buf (fx+ i 2))])
|
|
|
|
(cond
|
|
|
|
[(fx= (fxsra (fxlogor b1 b2) 6) #b10)
|
|
|
|
(let ([n (fxlogor
|
|
|
|
(fxsll (fxand b0 #b1111) 12)
|
|
|
|
(fxsll (fxand b1 #b111111) 6)
|
|
|
|
(fxand b2 #b111111))])
|
|
|
|
(cond
|
2007-12-12 21:22:05 -05:00
|
|
|
[(and (fx<= #xD800 n) (fx<= n #xDFFF))
|
2007-12-07 05:34:46 -05:00
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(do-error p who)]
|
|
|
|
[else
|
|
|
|
($set-port-index! p (fx+ i 3))
|
|
|
|
(integer->char n)]))]
|
|
|
|
[else
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
($set-port-index! p (fx+ ($port-index p) 1))
|
|
|
|
(do-error p who)]
|
|
|
|
[else (get-char-utf8-mode p who)]))])]
|
|
|
|
[(fx= (fxsra b0 3) #b11110) ;;; four-byte-encoding
|
|
|
|
(cond
|
|
|
|
[(fx< (fx+ i 3) j)
|
|
|
|
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))]
|
|
|
|
[b2 (bytevector-u8-ref buf (fx+ i 2))]
|
|
|
|
[b3 (bytevector-u8-ref buf (fx+ i 3))])
|
|
|
|
(cond
|
|
|
|
[(fx= (fxsra (fxlogor b1 b2 b3) 6) #b10)
|
|
|
|
(let ([n (fxlogor
|
|
|
|
(fxsll (fxand b0 #b111) 18)
|
|
|
|
(fxsll (fxand b1 #b111111) 12)
|
|
|
|
(fxsll (fxand b2 #b111111) 6)
|
|
|
|
(fxand b3 #b111111))])
|
|
|
|
(cond
|
2007-12-12 21:22:05 -05:00
|
|
|
[(and (fx<= #x10000 n) (fx<= n #x10FFFF))
|
2007-12-07 05:34:46 -05:00
|
|
|
($set-port-index! p (fx+ i 4))
|
|
|
|
(integer->char n)]
|
|
|
|
[else
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
($set-port-index! p (fx+ ($port-index p) 1))
|
|
|
|
(do-error p who)]
|
|
|
|
[else (get-char-utf8-mode p who)]))])]
|
|
|
|
[else
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(do-error p who)]))])))
|
2007-12-07 04:42:10 -05:00
|
|
|
|
|
|
|
(define (lookahead-char-utf8-mode p who)
|
|
|
|
(define (do-error p who)
|
|
|
|
(case (transcoder-error-handling-mode ($port-transcoder p))
|
2008-06-08 18:53:42 -04:00
|
|
|
[(ignore) (lookahead-char p)]
|
2007-12-07 04:42:10 -05:00
|
|
|
[(replace) #\xFFFD]
|
|
|
|
[(raise)
|
|
|
|
(raise (make-i/o-decoding-error p))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "cannot happen")]))
|
2007-12-07 04:42:10 -05:00
|
|
|
(let ([i ($port-index p)]
|
|
|
|
[j ($port-size p)]
|
|
|
|
[buf ($port-buffer p)])
|
|
|
|
(cond
|
|
|
|
[(fx= i j) ;;; exhausted
|
2007-12-07 05:34:46 -05:00
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0) (eof-object)]
|
|
|
|
[else (lookahead-char p)]))]
|
2007-12-07 04:42:10 -05:00
|
|
|
[else
|
2007-12-07 05:34:46 -05:00
|
|
|
(let ([b0 (bytevector-u8-ref buf i)])
|
2007-12-07 04:42:10 -05:00
|
|
|
(cond
|
|
|
|
[(fx= (fxsra b0 5) #b110) ;;; two-byte-encoding
|
|
|
|
(let ([i (fx+ i 1)])
|
|
|
|
(cond
|
|
|
|
[(fx< i j)
|
2007-12-07 05:34:46 -05:00
|
|
|
(let ([b1 (bytevector-u8-ref buf i)])
|
2007-12-07 04:42:10 -05:00
|
|
|
(cond
|
|
|
|
[(fx= (fxsra b1 6) #b10)
|
|
|
|
(integer->char
|
|
|
|
(fxior (fxand b1 #b111111)
|
|
|
|
(fxsll (fxand b0 #b11111) 6)))]
|
|
|
|
[else
|
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0) (do-error p who)]
|
|
|
|
[else (lookahead-char-utf8-mode p who)]))]))]
|
2007-12-07 05:34:46 -05:00
|
|
|
[(fx= (fxsra b0 4) #b1110) ;;; three-byte-encoding
|
|
|
|
(cond
|
|
|
|
[(fx< (fx+ i 2) j)
|
|
|
|
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))]
|
|
|
|
[b2 (bytevector-u8-ref buf (fx+ i 2))])
|
|
|
|
(cond
|
|
|
|
[(fx= (fxsra (fxlogor b1 b2) 6) #b10)
|
|
|
|
(let ([n (fxlogor
|
|
|
|
(fxsll (fxand b0 #b1111) 12)
|
|
|
|
(fxsll (fxand b1 #b111111) 6)
|
|
|
|
(fxand b2 #b111111))])
|
|
|
|
(cond
|
2007-12-12 21:22:05 -05:00
|
|
|
[(and (fx<= #xD800 n) (fx<= n #xDFFF))
|
|
|
|
(do-error p who)]
|
2007-12-07 05:34:46 -05:00
|
|
|
[else (integer->char n)]))]
|
|
|
|
[else (do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0) (do-error p who)]
|
|
|
|
[else (lookahead-char-utf8-mode p who)]))])]
|
|
|
|
[(fx= (fxsra b0 3) #b11110) ;;; four-byte-encoding
|
|
|
|
(cond
|
|
|
|
[(fx< (fx+ i 3) j)
|
|
|
|
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))]
|
|
|
|
[b2 (bytevector-u8-ref buf (fx+ i 2))]
|
|
|
|
[b3 (bytevector-u8-ref buf (fx+ i 3))])
|
|
|
|
(cond
|
|
|
|
[(fx= (fxsra (fxlogor b1 b2 b3) 6) #b10)
|
|
|
|
(let ([n (fxlogor
|
|
|
|
(fxsll (fxand b0 #b111) 18)
|
|
|
|
(fxsll (fxand b1 #b111111) 12)
|
|
|
|
(fxsll (fxand b2 #b111111) 6)
|
|
|
|
(fxand b3 #b111111))])
|
|
|
|
(cond
|
2007-12-12 21:22:05 -05:00
|
|
|
[(and (fx<= #x10000 n) (fx<= n #x10FFFF))
|
2007-12-07 05:34:46 -05:00
|
|
|
(integer->char n)]
|
|
|
|
[else
|
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(do-error p who)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
(do-error p who)]
|
2007-12-09 07:20:49 -05:00
|
|
|
[else (lookahead-char-utf8-mode p who)]))])]
|
2007-12-07 05:34:46 -05:00
|
|
|
[else (do-error p who)]))])))
|
2007-12-15 11:43:04 -05:00
|
|
|
;;;
|
2007-12-09 07:20:49 -05:00
|
|
|
(define (advance-bom p who bom-seq)
|
|
|
|
;;; return eof if port is eof,
|
|
|
|
;;; #t if a bom is present, updating the port index to
|
|
|
|
;;; point just past the bom.
|
|
|
|
;;; #f otherwise.
|
|
|
|
(cond
|
|
|
|
[(fx< ($port-index p) ($port-size p))
|
|
|
|
(let f ([i 0] [ls bom-seq])
|
|
|
|
(cond
|
|
|
|
[(null? ls)
|
|
|
|
($set-port-index! p (fx+ ($port-index p) i))
|
|
|
|
#t]
|
|
|
|
[else
|
|
|
|
(let ([idx (fx+ i ($port-index p))])
|
|
|
|
(cond
|
|
|
|
[(fx< idx ($port-size p))
|
|
|
|
(if (fx=? (car ls)
|
|
|
|
(bytevector-u8-ref ($port-buffer p) idx))
|
|
|
|
(f (fx+ i 1) (cdr ls))
|
|
|
|
#f)]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(if (fx= bytes 0)
|
|
|
|
#f
|
|
|
|
(f i ls)))]))]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(if (fx= bytes 0)
|
|
|
|
(eof-object)
|
|
|
|
(advance-bom p who bom-seq)))]))
|
2007-12-15 11:43:04 -05:00
|
|
|
;;;
|
2007-12-07 04:42:10 -05:00
|
|
|
(define (speedup-input-port p who)
|
2007-12-09 07:20:49 -05:00
|
|
|
;;; returns #t if port is eof, #f otherwise
|
2007-12-07 04:42:10 -05:00
|
|
|
(unless (input-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not an input port" p))
|
2008-07-07 04:22:12 -04:00
|
|
|
(when ($port-closed? p)
|
|
|
|
(die who "port is closed" p))
|
2007-12-07 04:42:10 -05:00
|
|
|
(let ([tr ($port-transcoder p)])
|
|
|
|
(unless tr
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a textual port" p))
|
2007-12-07 04:42:10 -05:00
|
|
|
(case (transcoder-codec tr)
|
2007-12-09 07:20:49 -05:00
|
|
|
[(utf-8-codec)
|
2007-12-07 04:42:10 -05:00
|
|
|
($set-port-attrs! p
|
2007-12-12 18:59:19 -05:00
|
|
|
(fxior textual-input-port-bits fast-u7-text-tag))
|
2007-12-09 07:20:49 -05:00
|
|
|
(eof-object? (advance-bom p who '(#xEF #xBB #xBF)))]
|
2008-10-19 18:43:42 -04:00
|
|
|
[(utf-16-codec)
|
|
|
|
(let ([be? (advance-bom p who '(#xFE #xFF))])
|
|
|
|
(case be?
|
|
|
|
[(#t)
|
|
|
|
($set-port-attrs! p
|
|
|
|
(fxior textual-input-port-bits fast-u16be-text-tag))
|
|
|
|
#f]
|
|
|
|
[(#f)
|
|
|
|
(let ([le? (advance-bom p who '(#xFF #xFE))])
|
|
|
|
(case le?
|
|
|
|
[(#t #f) ;;; little by default
|
|
|
|
($set-port-attrs! p
|
|
|
|
(fxior textual-input-port-bits fast-u16le-text-tag))
|
|
|
|
#f]
|
|
|
|
[else #t]))]
|
|
|
|
[else #t]))]
|
2008-01-19 18:15:18 -05:00
|
|
|
[else
|
2008-10-19 18:43:42 -04:00
|
|
|
(die who "BUG: codec not handled" (transcoder-codec tr))])))
|
2007-12-15 11:43:04 -05:00
|
|
|
;;;
|
2007-12-07 07:39:17 -05:00
|
|
|
(define (lookahead-char-char-mode p who)
|
|
|
|
(let ([str ($port-buffer p)]
|
|
|
|
[read! ($port-read! p)])
|
|
|
|
(let ([n (read! str 0 (string-length str))])
|
|
|
|
(unless (fixnum? n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid return value from read!" n))
|
2007-12-10 09:28:48 -05:00
|
|
|
(unless (<= 0 n (string-length str))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "return value from read! is out of range" n))
|
2008-12-09 02:06:46 -05:00
|
|
|
(let ([idx ($port-index p)] [pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 (+ idx (vector-ref pos-vec 0))))
|
2007-12-07 07:39:17 -05:00
|
|
|
($set-port-index! p 0)
|
|
|
|
($set-port-size! p n)
|
|
|
|
(cond
|
|
|
|
[(fx= n 0)
|
|
|
|
(eof-object)]
|
|
|
|
[else
|
|
|
|
(string-ref str 0)]))))
|
2007-12-06 08:14:05 -05:00
|
|
|
;;;
|
|
|
|
(define (lookahead-char p)
|
|
|
|
(define who 'lookahead-char)
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-06 08:14:05 -05:00
|
|
|
(cond
|
|
|
|
[(eq? m fast-get-utf8-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(let ([b (bytevector-u8-ref ($port-buffer p) i)])
|
|
|
|
(cond
|
|
|
|
[(fx< b 128) (integer->char b)]
|
2007-12-07 05:34:46 -05:00
|
|
|
[else (lookahead-char-utf8-mode p who)]))]
|
2007-12-06 08:14:05 -05:00
|
|
|
[else
|
|
|
|
(lookahead-char-utf8-mode p who)]))]
|
|
|
|
[(eq? m fast-get-char-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(string-ref ($port-buffer p) i)]
|
|
|
|
[else
|
|
|
|
(lookahead-char-char-mode p who)]))]
|
|
|
|
[(eq? m fast-get-latin-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(integer->char
|
|
|
|
(bytevector-u8-ref ($port-buffer p) i))]
|
|
|
|
[else
|
|
|
|
(get-char-latin-mode p who 0)]))]
|
2008-10-19 18:43:42 -04:00
|
|
|
[(eq? m fast-get-utf16le-tag) (peek-utf16 p who 'little)]
|
|
|
|
[(eq? m fast-get-utf16be-tag) (peek-utf16 p who 'big)]
|
2007-12-07 04:42:10 -05:00
|
|
|
[else
|
2007-12-09 07:20:49 -05:00
|
|
|
(if (speedup-input-port p who)
|
|
|
|
(eof-object)
|
|
|
|
(lookahead-char p))])))
|
2007-12-05 11:36:25 -05:00
|
|
|
;;;
|
2007-12-07 07:39:17 -05:00
|
|
|
(define (get-char-char-mode p who)
|
|
|
|
(let ([str ($port-buffer p)]
|
|
|
|
[read! ($port-read! p)])
|
|
|
|
(let ([n (read! str 0 (string-length str))])
|
|
|
|
(unless (fixnum? n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid return value from read!" n))
|
2007-12-10 09:28:48 -05:00
|
|
|
(unless (<= 0 n (string-length str))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "return value from read! is out of range" n))
|
2008-12-09 02:06:46 -05:00
|
|
|
(let ([idx ($port-index p)] [pos-vec ($port-position p)])
|
|
|
|
(vector-set! pos-vec 0 (+ idx (vector-ref pos-vec 0))))
|
2007-12-07 07:39:17 -05:00
|
|
|
($set-port-size! p n)
|
|
|
|
(cond
|
|
|
|
[(fx= n 0)
|
|
|
|
($set-port-index! p 0)
|
|
|
|
(eof-object)]
|
|
|
|
[else
|
|
|
|
($set-port-index! p 1)
|
|
|
|
(string-ref str 0)]))))
|
2008-10-19 18:43:42 -04:00
|
|
|
(define (peek-utf16 p who endianness)
|
|
|
|
(define integer->char/invalid
|
|
|
|
(lambda (n)
|
|
|
|
(cond
|
|
|
|
[(fx<= n #xD7FF) (integer->char n)]
|
|
|
|
[(fx< n #xE000) #\xFFFD]
|
|
|
|
[(fx<= n #x10FFFF) (integer->char n)]
|
|
|
|
[else #\xFFFD])))
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx<= (fx+ i 2) ($port-size p))
|
|
|
|
(let ([w1 (bytevector-u16-ref ($port-buffer p) i endianness)])
|
|
|
|
(cond
|
|
|
|
[(or (fx< w1 #xD800) (fx> w1 #xDFFF))
|
|
|
|
(integer->char/invalid w1)]
|
|
|
|
[(not (and (fx<= #xD800 w1) (fx<= w1 #xDBFF)))
|
|
|
|
#\xFFFD]
|
|
|
|
[(fx<= (+ i 4) ($port-size p))
|
|
|
|
(let ([w2 (bytevector-u16-ref
|
|
|
|
($port-buffer p) (+ i 2) endianness)])
|
|
|
|
(cond
|
|
|
|
[(not (and (fx<= #xDC00 w2) (fx<= w2 #xDFFF)))
|
|
|
|
#\xFFFD]
|
|
|
|
[else
|
|
|
|
(integer->char/invalid
|
|
|
|
(fx+ #x10000
|
|
|
|
(fxlogor
|
|
|
|
(fxsll (fxand w1 #x3FF) 10)
|
|
|
|
(fxand w2 #x3FF))))]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
#\xFFFD]
|
|
|
|
[else
|
|
|
|
(peek-utf16 p who endianness)]))]))]
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
#\xFFFD]
|
|
|
|
[else (peek-utf16 p who endianness)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(if (fx= bytes 0)
|
|
|
|
(eof-object)
|
|
|
|
(peek-utf16 p who endianness)))])))
|
|
|
|
(define (get-utf16 p who endianness)
|
|
|
|
(define (invalid p who endianness n)
|
|
|
|
(case (transcoder-error-handling-mode (port-transcoder p))
|
|
|
|
[(ignore) (do-get-char p who endianness)]
|
|
|
|
[(replace) #\xFFFD]
|
|
|
|
[(raise)
|
|
|
|
(raise (make-i/o-decoding-error p n))]
|
|
|
|
[else (die who "BUG: invalid error handling mode" p)]))
|
|
|
|
(define (integer->char/invalid p who endianness n)
|
|
|
|
(cond
|
|
|
|
[(fx<= n #xD7FF) (integer->char n)]
|
|
|
|
[(fx< n #xE000) (invalid p who endianness n)]
|
|
|
|
[(fx<= n #x10FFFF) (integer->char n)]
|
|
|
|
[else (invalid p who endianness n)]))
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx<= (fx+ i 2) ($port-size p))
|
|
|
|
(let ([w1 (bytevector-u16-ref ($port-buffer p) i endianness)])
|
|
|
|
(cond
|
|
|
|
[(or (fx< w1 #xD800) (fx> w1 #xDFFF))
|
|
|
|
($set-port-index! p (fx+ i 2))
|
|
|
|
(integer->char/invalid p who endianness w1)]
|
|
|
|
[(not (and (fx<= #xD800 w1) (fx<= w1 #xDBFF)))
|
|
|
|
($set-port-index! p (fx+ i 2))
|
|
|
|
(invalid p who endianness w1)]
|
|
|
|
[(fx<= (+ i 4) ($port-size p))
|
|
|
|
(let ([w2 (bytevector-u16-ref
|
|
|
|
($port-buffer p) (+ i 2) endianness)])
|
|
|
|
(cond
|
|
|
|
[(not (and (fx<= #xDC00 w2) (fx<= w2 #xDFFF)))
|
|
|
|
($set-port-index! p (fx+ i 2))
|
|
|
|
(invalid p who endianness w1)]
|
|
|
|
[else
|
|
|
|
($set-port-index! p (fx+ i 4))
|
|
|
|
(integer->char/invalid p who endianness
|
|
|
|
(fx+ #x10000
|
|
|
|
(fxlogor
|
|
|
|
(fxsll (fxand w1 #x3FF) 10)
|
|
|
|
(fxand w2 #x3FF))))]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
($set-port-index! p ($port-size p))
|
|
|
|
(invalid p who endianness w1)]
|
|
|
|
[else
|
|
|
|
(get-utf16 p who endianness)]))]))]
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(fx= bytes 0)
|
|
|
|
($set-port-index! p ($port-size p))
|
|
|
|
(invalid p who endianness
|
|
|
|
(bytevector-u8-ref ($port-buffer p) ($port-index p)))]
|
|
|
|
[else (get-utf16 p who endianness)]))]
|
|
|
|
[else
|
|
|
|
(let ([bytes (refill-bv-buffer p who)])
|
|
|
|
(if (fx= bytes 0)
|
|
|
|
(eof-object)
|
|
|
|
(get-utf16 p who endianness)))])))
|
|
|
|
(define (get-char p)
|
|
|
|
(do-get-char p 'get-char))
|
2007-12-15 11:43:04 -05:00
|
|
|
(define read-char
|
|
|
|
(case-lambda
|
2008-10-19 18:43:42 -04:00
|
|
|
[(p) (do-get-char p 'read-char)]
|
|
|
|
[() (do-get-char (current-input-port) 'read-char)]))
|
|
|
|
(define (do-get-char p who)
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-05 11:36:25 -05:00
|
|
|
(cond
|
|
|
|
[(eq? m fast-get-utf8-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(let ([b (bytevector-u8-ref ($port-buffer p) i)])
|
|
|
|
(cond
|
|
|
|
[(fx< b 128)
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(integer->char b)]
|
2007-12-07 04:42:10 -05:00
|
|
|
[else (get-char-utf8-mode p who)]))]
|
2007-12-05 11:36:25 -05:00
|
|
|
[else
|
|
|
|
(get-char-utf8-mode p who)]))]
|
|
|
|
[(eq? m fast-get-char-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(string-ref ($port-buffer p) i)]
|
2007-12-07 07:39:17 -05:00
|
|
|
[else (get-char-char-mode p who)]))]
|
2007-12-05 11:36:25 -05:00
|
|
|
[(eq? m fast-get-latin-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(integer->char
|
|
|
|
(bytevector-u8-ref ($port-buffer p) i))]
|
|
|
|
[else
|
2007-12-06 08:14:05 -05:00
|
|
|
(get-char-latin-mode p who 1)]))]
|
2008-10-19 18:43:42 -04:00
|
|
|
[(eq? m fast-get-utf16le-tag) (get-utf16 p who 'little)]
|
|
|
|
[(eq? m fast-get-utf16be-tag) (get-utf16 p who 'big)]
|
2007-12-07 04:42:10 -05:00
|
|
|
[else
|
2007-12-09 07:20:49 -05:00
|
|
|
(if (speedup-input-port p who)
|
|
|
|
(eof-object)
|
2008-10-19 18:43:42 -04:00
|
|
|
(do-get-char p who))]))))
|
2007-12-05 11:36:25 -05:00
|
|
|
|
|
|
|
;;; ----------------------------------------------------------
|
2007-12-06 05:05:26 -05:00
|
|
|
(define (assert-binary-input-port p who)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (port? p) (die who "not a port" p))
|
|
|
|
(when ($port-closed? p) (die who "port is closed" p))
|
|
|
|
(when ($port-transcoder p) (die who "port is not binary" p))
|
2007-12-06 05:05:26 -05:00
|
|
|
(unless ($port-read! p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "port is not an input port" p)))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
|
|
|
(module (get-u8 lookahead-u8)
|
2007-12-12 21:22:05 -05:00
|
|
|
(import UNSAFE)
|
2007-12-06 05:05:26 -05:00
|
|
|
(define (get-u8-byte-mode p who start)
|
2007-12-15 08:22:49 -05:00
|
|
|
(when ($port-closed? p) (die who "port is closed" p))
|
2008-07-23 14:15:44 -04:00
|
|
|
(let ([cnt (refill-bv-buffer p who)])
|
|
|
|
(cond
|
|
|
|
[(eqv? cnt 0) (eof-object)]
|
|
|
|
[else
|
|
|
|
($set-port-index! p start)
|
|
|
|
(bytevector-u8-ref ($port-buffer p) 0)])))
|
2007-12-06 05:05:26 -05:00
|
|
|
(define (slow-get-u8 p who start)
|
|
|
|
(assert-binary-input-port p who)
|
2007-12-12 18:59:19 -05:00
|
|
|
($set-port-attrs! p fast-get-byte-tag)
|
2007-12-06 05:05:26 -05:00
|
|
|
(get-u8-byte-mode p who start))
|
2007-12-05 11:36:25 -05:00
|
|
|
;;;
|
|
|
|
(define (get-u8 p)
|
|
|
|
(define who 'get-u8)
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-05 11:36:25 -05:00
|
|
|
(cond
|
|
|
|
[(eq? m fast-get-byte-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
($set-port-index! p (fx+ i 1))
|
|
|
|
(bytevector-u8-ref ($port-buffer p) i)]
|
2007-12-06 05:05:26 -05:00
|
|
|
[else (get-u8-byte-mode p who 1)]))]
|
|
|
|
[else (slow-get-u8 p who 1)])))
|
|
|
|
(define (lookahead-u8 p)
|
|
|
|
(define who 'lookahead-u8)
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-06 05:05:26 -05:00
|
|
|
(cond
|
|
|
|
[(eq? m fast-get-byte-tag)
|
|
|
|
(let ([i ($port-index p)])
|
|
|
|
(cond
|
|
|
|
[(fx< i ($port-size p))
|
|
|
|
(bytevector-u8-ref ($port-buffer p) i)]
|
|
|
|
[else (get-u8-byte-mode p who 0)]))]
|
|
|
|
[else (slow-get-u8 p who 0)]))))
|
|
|
|
|
|
|
|
(define (port-eof? p)
|
2007-12-12 21:22:05 -05:00
|
|
|
(import UNSAFE)
|
2007-12-06 05:05:26 -05:00
|
|
|
(define who 'port-eof?)
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-06 05:05:26 -05:00
|
|
|
(cond
|
|
|
|
[(not (eq? m 0))
|
|
|
|
(if (fx< ($port-index p) ($port-size p))
|
|
|
|
#f
|
2007-12-07 04:42:10 -05:00
|
|
|
(if ($port-transcoder p)
|
2007-12-06 05:05:26 -05:00
|
|
|
(eof-object? (lookahead-char p))
|
|
|
|
(eof-object? (lookahead-u8 p))))]
|
|
|
|
[(input-port? p)
|
|
|
|
(when ($port-closed? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'port-eof? "port is closed" p))
|
2007-12-06 05:05:26 -05:00
|
|
|
(if (textual-port? p)
|
|
|
|
(eof-object? (lookahead-char p))
|
|
|
|
(eof-object? (lookahead-u8 p)))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'port-eof? "not an input port" p)])))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
2008-06-06 11:10:17 -04:00
|
|
|
;;; FIXME: these hard coded constants should go away
|
2008-05-31 13:43:55 -04:00
|
|
|
(define EAGAIN-error-code -6) ;;; from ikarus-errno.c
|
2007-12-09 07:20:49 -05:00
|
|
|
|
2008-06-06 11:10:17 -04:00
|
|
|
(define io-error
|
|
|
|
(case-lambda
|
|
|
|
[(who id err base-condition)
|
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
base-condition
|
|
|
|
(make-who-condition who)
|
|
|
|
(make-message-condition (strerror err))
|
|
|
|
(case err
|
|
|
|
;; from ikarus-errno.c: EACCES=-2, EFAULT=-21, EROFS=-71, EEXIST=-20,
|
|
|
|
;; EIO=-29, ENOENT=-45
|
|
|
|
;; Why is EFAULT included here?
|
|
|
|
[(-2 -21) (make-i/o-file-protection-error id)]
|
|
|
|
[(-71) (make-i/o-file-is-read-only-error id)]
|
|
|
|
[(-20) (make-i/o-file-already-exists-error id)]
|
|
|
|
[(-29) (make-i/o-error)]
|
|
|
|
[(-45) (make-i/o-file-does-not-exist-error id)]
|
|
|
|
[else (if id
|
|
|
|
(make-irritants-condition (list id))
|
|
|
|
(condition))])))]
|
|
|
|
[(who id err) (io-error who id err (make-error))]))
|
2007-12-18 03:58:50 -05:00
|
|
|
|
2008-01-19 14:51:14 -05:00
|
|
|
;(define block-size 4096)
|
|
|
|
;(define block-size (* 4 4096))
|
|
|
|
(define input-block-size (* 4 4096))
|
|
|
|
(define output-block-size (* 4 4096))
|
|
|
|
|
|
|
|
(define input-file-buffer-size (+ input-block-size 128))
|
|
|
|
(define output-file-buffer-size output-block-size)
|
2007-12-09 07:20:49 -05:00
|
|
|
|
2008-04-11 14:02:43 -04:00
|
|
|
(define input-socket-buffer-size
|
|
|
|
(make-parameter (+ input-block-size 128)
|
|
|
|
(lambda (x)
|
|
|
|
(import (ikarus system $fx))
|
|
|
|
(if (and (fixnum? x) ($fx>= x 128))
|
|
|
|
x
|
|
|
|
(error 'input-socket-buffer-size
|
|
|
|
"buffer size should be a fixnum >= 128"
|
|
|
|
x)))))
|
|
|
|
|
|
|
|
(define output-socket-buffer-size
|
|
|
|
(make-parameter output-block-size
|
|
|
|
(lambda (x)
|
|
|
|
(import (ikarus system $fx))
|
|
|
|
(if (and (fixnum? x) ($fx> x 0))
|
|
|
|
x
|
|
|
|
(error 'output-socket-buffer-size
|
|
|
|
"buffer size should be a positive fixnum"
|
|
|
|
x)))))
|
|
|
|
|
2008-12-09 03:41:59 -05:00
|
|
|
(define (make-file-set-position-handler fd id)
|
|
|
|
(lambda (pos) ;;; set-position!
|
|
|
|
(let ([err (foreign-call "ikrt_set_position" fd pos)])
|
|
|
|
(when err
|
|
|
|
(io-error 'set-position! id err
|
|
|
|
(make-i/o-invalid-position-error pos))))))
|
|
|
|
|
2008-01-22 03:30:52 -05:00
|
|
|
(define (fh->input-port fd id size transcoder close who)
|
2007-12-27 22:08:27 -05:00
|
|
|
(letrec ([port
|
|
|
|
($make-port
|
2008-01-22 03:30:52 -05:00
|
|
|
(input-transcoder-attrs transcoder who)
|
2007-12-27 22:08:27 -05:00
|
|
|
0 0 (make-bytevector size)
|
|
|
|
transcoder
|
|
|
|
id
|
|
|
|
(letrec ([refill
|
|
|
|
(lambda (bv idx cnt)
|
2008-01-19 13:50:53 -05:00
|
|
|
(import UNSAFE)
|
2007-12-27 22:08:27 -05:00
|
|
|
(let ([bytes
|
|
|
|
(foreign-call "ikrt_read_fd" fd bv idx
|
2008-01-19 14:51:14 -05:00
|
|
|
(if (fx< input-block-size cnt)
|
|
|
|
input-block-size
|
2008-01-19 13:50:53 -05:00
|
|
|
cnt))])
|
2007-12-27 22:08:27 -05:00
|
|
|
(cond
|
|
|
|
[(fx>= bytes 0) bytes]
|
|
|
|
[(fx= bytes EAGAIN-error-code)
|
2008-03-23 02:14:00 -04:00
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
|
|
|
(add-io-event fd k 'r)
|
|
|
|
(process-events)))
|
2007-12-27 22:08:27 -05:00
|
|
|
(refill bv idx cnt)]
|
2008-04-05 05:15:30 -04:00
|
|
|
[else
|
|
|
|
(io-error 'read id bytes
|
2008-06-08 09:25:22 -04:00
|
|
|
(make-i/o-read-error))])))])
|
2007-12-27 22:08:27 -05:00
|
|
|
refill)
|
|
|
|
#f ;;; write!
|
2008-11-11 16:31:35 -05:00
|
|
|
#t ;;; get-position
|
2008-12-09 03:41:59 -05:00
|
|
|
(make-file-set-position-handler fd id)
|
2007-12-27 22:08:27 -05:00
|
|
|
(cond
|
|
|
|
[(procedure? close) close]
|
|
|
|
[(eqv? close #t) (file-close-proc id fd)]
|
|
|
|
[else #f])
|
2008-06-18 00:05:01 -04:00
|
|
|
fd
|
|
|
|
(vector 0))])
|
2007-12-27 22:08:27 -05:00
|
|
|
(guarded-port port)))
|
2007-12-09 07:20:49 -05:00
|
|
|
|
2008-12-09 03:41:59 -05:00
|
|
|
|
2008-01-22 03:30:52 -05:00
|
|
|
(define (fh->output-port fd id size transcoder close who)
|
2007-12-27 22:08:27 -05:00
|
|
|
(letrec ([port
|
|
|
|
($make-port
|
2008-01-22 03:30:52 -05:00
|
|
|
(output-transcoder-attrs transcoder who)
|
2007-12-27 22:08:27 -05:00
|
|
|
0 size (make-bytevector size)
|
|
|
|
transcoder
|
|
|
|
id
|
|
|
|
#f
|
|
|
|
(letrec ([refill
|
|
|
|
(lambda (bv idx cnt)
|
2008-01-19 14:51:14 -05:00
|
|
|
(import UNSAFE)
|
2007-12-27 22:08:27 -05:00
|
|
|
(let ([bytes
|
|
|
|
(foreign-call "ikrt_write_fd" fd bv idx
|
2008-01-19 14:51:14 -05:00
|
|
|
(if (fx< output-block-size cnt)
|
|
|
|
output-block-size
|
|
|
|
cnt))])
|
|
|
|
|
2007-12-27 22:08:27 -05:00
|
|
|
(cond
|
|
|
|
[(fx>= bytes 0) bytes]
|
|
|
|
[(fx= bytes EAGAIN-error-code)
|
2008-03-23 02:14:00 -04:00
|
|
|
(call/cc
|
2008-04-06 10:57:56 -04:00
|
|
|
(lambda (k)
|
2008-03-23 02:14:00 -04:00
|
|
|
(add-io-event fd k 'w)
|
|
|
|
(process-events)))
|
2007-12-27 22:08:27 -05:00
|
|
|
(refill bv idx cnt)]
|
2008-04-05 05:15:30 -04:00
|
|
|
[else
|
|
|
|
(io-error 'write id bytes
|
|
|
|
(make-i/o-write-error))])))])
|
2007-12-27 22:08:27 -05:00
|
|
|
refill)
|
2008-11-11 16:31:35 -05:00
|
|
|
#t ;;; get-position
|
2008-12-09 03:41:59 -05:00
|
|
|
(make-file-set-position-handler fd id)
|
2007-12-27 22:08:27 -05:00
|
|
|
(cond
|
|
|
|
[(procedure? close) close]
|
|
|
|
[(eqv? close #t) (file-close-proc id fd)]
|
|
|
|
[else #f])
|
2008-06-18 00:05:01 -04:00
|
|
|
fd
|
|
|
|
(vector 0))])
|
2007-12-27 22:08:27 -05:00
|
|
|
(guarded-port port)))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
2007-12-26 17:35:58 -05:00
|
|
|
(define (file-close-proc id fd)
|
|
|
|
(lambda ()
|
|
|
|
(cond
|
|
|
|
[(foreign-call "ikrt_close_fd" fd) =>
|
|
|
|
(lambda (err)
|
|
|
|
(io-error 'close id err))])))
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (open-input-file-handle filename who)
|
2007-12-09 17:13:09 -05:00
|
|
|
(let ([fh (foreign-call "ikrt_open_input_fd"
|
|
|
|
(string->utf8 filename))])
|
|
|
|
(cond
|
|
|
|
[(fx< fh 0) (io-error who filename fh)]
|
|
|
|
[else fh])))
|
2007-12-09 07:20:49 -05:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (open-output-file-handle filename file-options who)
|
2008-07-24 21:58:53 -04:00
|
|
|
(define (opt->num x)
|
|
|
|
(bitwise-ior
|
|
|
|
(if (enum-set-member? 'no-create x) 1 0)
|
|
|
|
(if (enum-set-member? 'no-fail x) 2 0)
|
|
|
|
(if (enum-set-member? 'no-truncate x) 4 0)))
|
2008-10-29 14:10:24 -04:00
|
|
|
(let ([opt (if (enum-set? file-options)
|
|
|
|
(opt->num file-options)
|
|
|
|
(die who "file-options is not an enum set"
|
|
|
|
file-options))])
|
2007-12-10 07:28:03 -05:00
|
|
|
(let ([fh (foreign-call "ikrt_open_output_fd"
|
|
|
|
(string->utf8 filename)
|
|
|
|
opt)])
|
|
|
|
(cond
|
|
|
|
[(fx< fh 0) (io-error who filename fh)]
|
|
|
|
[else fh]))))
|
2007-12-09 17:13:09 -05:00
|
|
|
|
|
|
|
(define open-file-input-port
|
|
|
|
(case-lambda
|
|
|
|
[(filename)
|
|
|
|
(open-file-input-port filename (file-options) 'block #f)]
|
2008-10-29 14:10:24 -04:00
|
|
|
[(filename file-options)
|
2007-12-09 17:13:09 -05:00
|
|
|
(open-file-input-port filename file-options 'block #f)]
|
2008-10-29 14:10:24 -04:00
|
|
|
[(filename file-options buffer-mode)
|
2007-12-09 17:13:09 -05:00
|
|
|
(open-file-input-port filename file-options buffer-mode #f)]
|
|
|
|
[(filename file-options buffer-mode transcoder)
|
2008-10-29 14:10:24 -04:00
|
|
|
(define who 'open-file-input-port)
|
2007-12-09 17:13:09 -05:00
|
|
|
(unless (string? filename)
|
2008-10-29 14:10:24 -04:00
|
|
|
(die who "invalid filename" filename))
|
|
|
|
(unless (enum-set? file-options)
|
|
|
|
(die who "file-options is not an enum set" file-options))
|
|
|
|
(unless (or (not transcoder) (transcoder? transcoder))
|
|
|
|
(die who "invalid transcoder" transcoder))
|
2007-12-09 17:13:09 -05:00
|
|
|
; FIXME: file-options ignored
|
|
|
|
; FIXME: buffer-mode ignored
|
|
|
|
(fh->input-port
|
2008-10-29 14:10:24 -04:00
|
|
|
(open-input-file-handle filename who)
|
2007-12-09 17:13:09 -05:00
|
|
|
filename
|
2007-12-12 23:50:05 -05:00
|
|
|
input-file-buffer-size
|
2007-12-10 07:28:03 -05:00
|
|
|
transcoder
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
2008-10-29 14:10:24 -04:00
|
|
|
who)]))
|
2008-10-18 17:01:57 -04:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define open-file-output-port
|
|
|
|
(case-lambda
|
|
|
|
[(filename)
|
|
|
|
(open-file-output-port filename (file-options) 'block #f)]
|
|
|
|
[(filename file-options)
|
|
|
|
(open-file-output-port filename file-options 'block #f)]
|
|
|
|
[(filename file-options buffer-mode)
|
|
|
|
(open-file-output-port filename file-options buffer-mode #f)]
|
|
|
|
[(filename file-options buffer-mode transcoder)
|
2008-10-18 17:01:57 -04:00
|
|
|
(define who 'open-file-output-port)
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (string? filename)
|
2008-10-18 17:01:57 -04:00
|
|
|
(die who "invalid filename" filename))
|
2007-12-10 07:28:03 -05:00
|
|
|
; FIXME: file-options ignored
|
2008-10-18 17:01:57 -04:00
|
|
|
; FIXME: line-buffered output ports are not handled
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (or (not transcoder) (transcoder? transcoder))
|
2008-10-18 17:01:57 -04:00
|
|
|
(die who "invalid transcoder" transcoder))
|
|
|
|
(let ([buffer-size
|
|
|
|
(case buffer-mode
|
2008-12-12 16:03:10 -05:00
|
|
|
[(none) 0]
|
2008-10-18 17:01:57 -04:00
|
|
|
[(block line) output-file-buffer-size]
|
|
|
|
[else (die who "invalid buffer mode" buffer-mode)])])
|
|
|
|
(fh->output-port
|
|
|
|
(open-output-file-handle filename file-options who)
|
|
|
|
filename buffer-size transcoder #t who))]))
|
2007-12-09 17:13:09 -05:00
|
|
|
|
2008-12-09 04:46:43 -05:00
|
|
|
(define (output-port-buffer-mode p)
|
|
|
|
(unless (output-port? p)
|
|
|
|
(die 'output-port-buffer-mode "not an output port" p))
|
2008-12-12 16:03:10 -05:00
|
|
|
(if (fx= 0 ($port-size p)) 'none 'block))
|
2008-12-09 04:46:43 -05:00
|
|
|
|
2007-12-10 10:18:52 -05:00
|
|
|
(define (open-output-file filename)
|
|
|
|
(unless (string? filename)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'open-output-file "invalid filename" filename))
|
2007-12-10 10:18:52 -05:00
|
|
|
(fh->output-port
|
|
|
|
(open-output-file-handle filename (file-options)
|
2007-12-18 03:32:07 -05:00
|
|
|
'open-output-file)
|
2007-12-10 10:18:52 -05:00
|
|
|
filename
|
2007-12-12 23:50:05 -05:00
|
|
|
output-file-buffer-size
|
2007-12-10 10:18:52 -05:00
|
|
|
(native-transcoder)
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
|
|
|
'open-output-file))
|
2007-12-10 10:18:52 -05:00
|
|
|
|
2007-12-09 17:13:09 -05:00
|
|
|
(define (open-input-file filename)
|
|
|
|
(unless (string? filename)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'open-input-file "invalid filename" filename))
|
2007-12-09 17:13:09 -05:00
|
|
|
(fh->input-port
|
2007-12-10 07:28:03 -05:00
|
|
|
(open-input-file-handle filename 'open-input-file)
|
2007-12-09 17:13:09 -05:00
|
|
|
filename
|
2007-12-12 23:50:05 -05:00
|
|
|
input-file-buffer-size
|
2007-12-09 17:13:09 -05:00
|
|
|
(native-transcoder)
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
|
|
|
'open-input-file))
|
2007-12-09 17:13:09 -05:00
|
|
|
|
2007-12-23 15:06:05 -05:00
|
|
|
|
|
|
|
(define (with-output-to-file filename proc)
|
|
|
|
(unless (string? filename)
|
|
|
|
(die 'with-output-to-file "invalid filename" filename))
|
|
|
|
(unless (procedure? proc)
|
|
|
|
(die 'with-output-to-file "not a procedure" proc))
|
|
|
|
(call-with-port
|
|
|
|
(fh->output-port
|
2007-12-23 15:18:40 -05:00
|
|
|
(open-output-file-handle filename (file-options)
|
|
|
|
'with-output-to-file)
|
2007-12-23 15:06:05 -05:00
|
|
|
filename
|
|
|
|
output-file-buffer-size
|
|
|
|
(native-transcoder)
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
|
|
|
'with-output-to-file)
|
2007-12-23 15:06:05 -05:00
|
|
|
(lambda (p)
|
2008-11-15 11:21:00 -05:00
|
|
|
(parameterize ([current-output-port p])
|
2007-12-23 15:06:05 -05:00
|
|
|
(proc)))))
|
|
|
|
|
2007-12-23 15:18:40 -05:00
|
|
|
(define (call-with-output-file filename proc)
|
|
|
|
(unless (string? filename)
|
|
|
|
(die 'call-with-output-file "invalid filename" filename))
|
|
|
|
(unless (procedure? proc)
|
|
|
|
(die 'call-with-output-file "not a procedure" proc))
|
|
|
|
(call-with-port
|
|
|
|
(fh->output-port
|
|
|
|
(open-output-file-handle filename (file-options)
|
|
|
|
'call-with-output-file)
|
|
|
|
filename
|
|
|
|
output-file-buffer-size
|
|
|
|
(native-transcoder)
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
|
|
|
'call-with-output-file)
|
2007-12-23 15:18:40 -05:00
|
|
|
proc))
|
|
|
|
|
2007-12-09 17:13:09 -05:00
|
|
|
(define (call-with-input-file filename proc)
|
|
|
|
(unless (string? filename)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'call-with-input-file "invalid filename" filename))
|
2007-12-09 17:13:09 -05:00
|
|
|
(unless (procedure? proc)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'call-with-input-file "not a procedure" proc))
|
2007-12-09 17:13:09 -05:00
|
|
|
(call-with-port
|
|
|
|
(fh->input-port
|
2007-12-10 07:28:03 -05:00
|
|
|
(open-input-file-handle filename 'call-with-input-file)
|
2007-12-09 17:13:09 -05:00
|
|
|
filename
|
2007-12-12 23:50:05 -05:00
|
|
|
input-file-buffer-size
|
2007-12-09 17:13:09 -05:00
|
|
|
(native-transcoder)
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
|
|
|
'call-with-input-file)
|
2007-12-09 17:13:09 -05:00
|
|
|
proc))
|
|
|
|
|
|
|
|
(define (with-input-from-file filename proc)
|
|
|
|
(unless (string? filename)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'with-input-from-file "invalid filename" filename))
|
2007-12-09 17:13:09 -05:00
|
|
|
(unless (procedure? proc)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'with-input-from-file "not a procedure" proc))
|
2007-12-23 15:06:05 -05:00
|
|
|
(call-with-port
|
|
|
|
(fh->input-port
|
|
|
|
(open-input-file-handle filename 'with-input-from-file)
|
|
|
|
filename
|
|
|
|
input-file-buffer-size
|
|
|
|
(native-transcoder)
|
2008-01-22 03:30:52 -05:00
|
|
|
#t
|
|
|
|
'with-input-from-file)
|
2007-12-23 15:06:05 -05:00
|
|
|
(lambda (p)
|
2008-11-15 11:21:00 -05:00
|
|
|
(parameterize ([current-input-port p])
|
2007-12-23 15:06:05 -05:00
|
|
|
(proc)))))
|
2007-12-09 17:13:09 -05:00
|
|
|
|
2007-12-23 14:44:55 -05:00
|
|
|
(define (with-input-from-string string proc)
|
|
|
|
(unless (string? string)
|
|
|
|
(die 'with-input-from-string "not a string" string))
|
|
|
|
(unless (procedure? proc)
|
|
|
|
(die 'with-input-from-string "not a procedure" proc))
|
2008-11-15 11:21:00 -05:00
|
|
|
(parameterize ([current-input-port
|
2007-12-23 14:44:55 -05:00
|
|
|
(open-string-input-port string)])
|
|
|
|
(proc)))
|
|
|
|
|
2008-01-12 17:32:43 -05:00
|
|
|
(define (standard-input-port)
|
2008-01-22 03:30:52 -05:00
|
|
|
(fh->input-port 0 '*stdin* 256 #f #f 'standard-input-port))
|
2007-12-09 07:20:49 -05:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (standard-output-port)
|
2008-01-22 03:30:52 -05:00
|
|
|
(fh->output-port 1 '*stdout* 256 #f #f 'standard-output-port))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
|
|
|
(define (standard-error-port)
|
2008-01-22 03:30:52 -05:00
|
|
|
(fh->output-port 2 '*stderr* 256 #f #f 'standard-error-port))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
2008-11-15 11:21:00 -05:00
|
|
|
(define current-input-port
|
2007-12-09 17:13:09 -05:00
|
|
|
(make-parameter
|
2007-12-12 19:52:26 -05:00
|
|
|
(transcoded-port
|
2008-01-22 03:30:52 -05:00
|
|
|
(fh->input-port 0 '*stdin* input-file-buffer-size #f #f #f)
|
2008-11-15 11:21:00 -05:00
|
|
|
(native-transcoder))
|
|
|
|
(lambda (x)
|
|
|
|
(if (and (input-port? x) (textual-port? x))
|
|
|
|
x
|
|
|
|
(die 'current-input-port "not a textual input port" x)))))
|
2007-12-09 07:20:49 -05:00
|
|
|
|
2008-11-15 11:21:00 -05:00
|
|
|
(define current-output-port
|
2007-12-10 07:28:03 -05:00
|
|
|
(make-parameter
|
2007-12-12 19:52:26 -05:00
|
|
|
(transcoded-port
|
2008-01-22 03:30:52 -05:00
|
|
|
(fh->output-port 1 '*stdout* output-file-buffer-size #f #f #f)
|
2008-11-15 11:21:00 -05:00
|
|
|
(native-transcoder))
|
|
|
|
(lambda (x)
|
|
|
|
(if (and (output-port? x) (textual-port? x))
|
|
|
|
x
|
|
|
|
(die 'current-output-port "not a textual output port" x)))))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
2008-11-15 11:21:00 -05:00
|
|
|
(define current-error-port
|
2007-12-10 07:28:03 -05:00
|
|
|
(make-parameter
|
2007-12-12 19:52:26 -05:00
|
|
|
(transcoded-port
|
2008-12-12 16:03:10 -05:00
|
|
|
(fh->output-port 2 '*stderr* 0 #f #f #f)
|
2008-11-15 11:21:00 -05:00
|
|
|
(native-transcoder))
|
|
|
|
(lambda (x)
|
|
|
|
(if (and (output-port? x) (textual-port? x))
|
|
|
|
x
|
|
|
|
(die 'current-errorput-port "not a textual output port" x)))))
|
2007-12-10 07:28:03 -05:00
|
|
|
|
|
|
|
(define console-output-port
|
2008-11-15 11:21:00 -05:00
|
|
|
(let ([p (current-output-port)])
|
2007-12-10 07:28:03 -05:00
|
|
|
(lambda () p)))
|
|
|
|
|
2007-12-10 07:40:34 -05:00
|
|
|
(define console-error-port
|
2008-11-15 11:21:00 -05:00
|
|
|
(let ([p (current-error-port)])
|
2007-12-10 07:40:34 -05:00
|
|
|
(lambda () p)))
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define console-input-port
|
2008-11-15 11:21:00 -05:00
|
|
|
(let ([p (current-input-port)])
|
2007-12-10 07:28:03 -05:00
|
|
|
(lambda () p)))
|
|
|
|
|
2007-12-08 14:52:35 -05:00
|
|
|
(define (call-with-port p proc)
|
2007-12-12 19:18:57 -05:00
|
|
|
(if (port? p)
|
2008-07-07 03:56:52 -04:00
|
|
|
(if (procedure? proc)
|
|
|
|
(call-with-values
|
2007-12-08 14:52:35 -05:00
|
|
|
(lambda () (proc p))
|
2008-07-07 03:56:52 -04:00
|
|
|
(lambda vals
|
|
|
|
(close-port p)
|
|
|
|
(apply values vals)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'call-with-port "not a procedure" proc))
|
|
|
|
(die 'call-with-port "not a port" p)))
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2007-12-09 12:20:13 -05:00
|
|
|
;;;
|
|
|
|
(define peek-char
|
|
|
|
(case-lambda
|
2008-11-15 11:21:00 -05:00
|
|
|
[() (lookahead-char (current-input-port))]
|
2007-12-09 12:20:13 -05:00
|
|
|
[(p)
|
|
|
|
(if (input-port? p)
|
|
|
|
(if (textual-port? p)
|
|
|
|
(lookahead-char p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'peek-char "not a textual port" p))
|
|
|
|
(die 'peek-char "not an input-port" p))]))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
|
|
|
(define (get-bytevector-n p n)
|
|
|
|
(import (ikarus system $fx) (ikarus system $bytevectors))
|
|
|
|
(define (subbytevector s n)
|
|
|
|
(let ([p ($make-bytevector n)])
|
|
|
|
(let f ([s s] [n n] [p p])
|
|
|
|
(let ([n ($fx- n 1)])
|
|
|
|
($bytevector-set! p n ($bytevector-u8-ref s n))
|
|
|
|
(if ($fx= n 0)
|
|
|
|
p
|
|
|
|
(f s n p))))))
|
|
|
|
(unless (input-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n "not an input port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (binary-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n "not a binary port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (fixnum? n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n "count is not a fixnum" n))
|
2007-12-09 12:20:13 -05:00
|
|
|
(cond
|
|
|
|
[($fx> n 0)
|
|
|
|
(let ([s ($make-bytevector n)])
|
|
|
|
(let f ([p p] [n n] [s s] [i 0])
|
|
|
|
(let ([x (get-u8 p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x)
|
|
|
|
(if ($fx= i 0)
|
|
|
|
(eof-object)
|
|
|
|
(subbytevector s i))]
|
|
|
|
[else
|
|
|
|
($bytevector-set! s i x)
|
|
|
|
(let ([i ($fxadd1 i)])
|
|
|
|
(if ($fx= i n)
|
|
|
|
s
|
|
|
|
(f p n s i)))]))))]
|
|
|
|
[($fx= n 0) '#vu8()]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'get-bytevector-n "count is negative" n)]))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
|
|
|
(define (get-bytevector-n! p s i c)
|
|
|
|
(import (ikarus system $fx) (ikarus system $bytevectors))
|
|
|
|
(unless (input-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n! "not an input port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (binary-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n! "not a binary port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (bytevector? s)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n! "not a bytevector" s))
|
2007-12-09 12:20:13 -05:00
|
|
|
(let ([len ($bytevector-length s)])
|
|
|
|
(unless (fixnum? i)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n! "starting index is not a fixnum" i))
|
2007-12-09 12:20:13 -05:00
|
|
|
(when (or ($fx< i 0) ($fx> i len))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n!
|
2007-12-09 12:20:13 -05:00
|
|
|
(format "starting index is out of range 0..~a" len)
|
|
|
|
i))
|
|
|
|
(unless (fixnum? c)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n! "count is not a fixnum" c))
|
2007-12-09 12:20:13 -05:00
|
|
|
(cond
|
|
|
|
[($fx> c 0)
|
|
|
|
(let ([j (+ i c)])
|
|
|
|
(when (> j len)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-n!
|
2007-12-09 12:20:13 -05:00
|
|
|
(format "count is out of range 0..~a" (- len i))
|
|
|
|
c))
|
|
|
|
(let ([x (get-u8 p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x) x]
|
|
|
|
[else
|
|
|
|
($bytevector-set! s i x)
|
|
|
|
(let f ([p p] [s s] [start i] [i 1] [c c])
|
|
|
|
(let ([x (get-u8 p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x) i]
|
|
|
|
[else
|
|
|
|
($bytevector-set! s ($fx+ start i) x)
|
|
|
|
(let ([i ($fxadd1 i)])
|
|
|
|
(if ($fx= i c)
|
|
|
|
i
|
|
|
|
(f p s start i c)))])))])))]
|
|
|
|
[($fx= c 0) 0]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'get-bytevector-n! "count is negative" c)])))
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2008-01-22 10:59:04 -05:00
|
|
|
(define (get-bytevector-some p)
|
|
|
|
(define who 'get-bytevector-some)
|
|
|
|
; (import UNSAFE)
|
|
|
|
(let ([m ($port-fast-attrs p)])
|
|
|
|
(cond
|
|
|
|
[(eq? m fast-get-byte-tag)
|
|
|
|
(let ([i ($port-index p)] [j ($port-size p)])
|
|
|
|
(let ([cnt (fx- j i)])
|
|
|
|
(cond
|
|
|
|
[(fx> cnt 0)
|
|
|
|
(let f ([bv (make-bytevector cnt)]
|
|
|
|
[buf ($port-buffer p)]
|
|
|
|
[i i] [j j] [idx 0])
|
|
|
|
(cond
|
|
|
|
[(fx= i j)
|
|
|
|
($set-port-index! p j)
|
|
|
|
bv]
|
|
|
|
[else
|
|
|
|
(bytevector-u8-set! bv idx (bytevector-u8-ref buf i))
|
|
|
|
(f bv buf (fx+ i 1) j (fx+ idx 1))]))]
|
|
|
|
[else
|
|
|
|
(refill-bv-buffer p who)
|
|
|
|
(if (fx= ($port-index p) ($port-size p))
|
|
|
|
(eof-object)
|
|
|
|
(get-bytevector-some p))])))]
|
|
|
|
[else (die who "invalid port argument" p)])))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
|
|
|
(define (get-bytevector-all p)
|
|
|
|
(define (get-it p)
|
|
|
|
(let f ([p p] [n 0] [ac '()])
|
|
|
|
(let ([x (get-u8 p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x)
|
|
|
|
(if (null? ac)
|
|
|
|
(eof-object)
|
|
|
|
(make-it n ac))]
|
|
|
|
[else (f p (+ n 1) (cons x ac))]))))
|
|
|
|
(define (make-it n revls)
|
|
|
|
(let f ([s (make-bytevector n)] [i (- n 1)] [ls revls])
|
|
|
|
(cond
|
|
|
|
[(pair? ls)
|
|
|
|
(bytevector-u8-set! s i (car ls))
|
|
|
|
(f s (- i 1) (cdr ls))]
|
|
|
|
[else s])))
|
|
|
|
(if (input-port? p)
|
|
|
|
(if (binary-port? p)
|
|
|
|
(get-it p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-bytevector-all "not a binary port" p))
|
|
|
|
(die 'get-bytevector-all "not an input port" p)))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
|
|
|
(define (get-string-n p n)
|
|
|
|
(import (ikarus system $fx) (ikarus system $strings))
|
|
|
|
(unless (input-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n "not an input port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (textual-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n "not a textual port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (fixnum? n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n "count is not a fixnum" n))
|
2007-12-09 12:20:13 -05:00
|
|
|
(cond
|
|
|
|
[($fx> n 0)
|
|
|
|
(let ([s ($make-string n)])
|
|
|
|
(let f ([p p] [n n] [s s] [i 0])
|
|
|
|
(let ([x (get-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x)
|
|
|
|
(if ($fx= i 0)
|
|
|
|
(eof-object)
|
|
|
|
(substring s 0 i))]
|
|
|
|
[else
|
|
|
|
($string-set! s i x)
|
|
|
|
(let ([i ($fxadd1 i)])
|
|
|
|
(if ($fx= i n)
|
|
|
|
s
|
|
|
|
(f p n s i)))]))))]
|
|
|
|
[($fx= n 0) ""]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'get-string-n "count is negative" n)]))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
|
|
|
(define (get-string-n! p s i c)
|
|
|
|
(import (ikarus system $fx) (ikarus system $strings))
|
|
|
|
(unless (input-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n! "not an input port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (textual-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n! "not a textual port" p))
|
2007-12-09 12:20:13 -05:00
|
|
|
(unless (string? s)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n! "not a string" s))
|
2007-12-09 12:20:13 -05:00
|
|
|
(let ([len ($string-length s)])
|
|
|
|
(unless (fixnum? i)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n! "starting index is not a fixnum" i))
|
2007-12-09 12:20:13 -05:00
|
|
|
(when (or ($fx< i 0) ($fx> i len))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n!
|
2007-12-09 12:20:13 -05:00
|
|
|
(format "starting index is out of range 0..~a" len)
|
|
|
|
i))
|
|
|
|
(unless (fixnum? c)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n! "count is not a fixnum" c))
|
2007-12-09 12:20:13 -05:00
|
|
|
(cond
|
|
|
|
[($fx> c 0)
|
|
|
|
(let ([j (+ i c)])
|
|
|
|
(when (> j len)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-n!
|
2007-12-09 12:20:13 -05:00
|
|
|
(format "count is out of range 0..~a" (- len i))
|
|
|
|
c))
|
|
|
|
(let ([x (get-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x) x]
|
|
|
|
[else
|
|
|
|
($string-set! s i x)
|
|
|
|
(let f ([p p] [s s] [start i] [i 1] [c c])
|
|
|
|
(let ([x (get-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x) i]
|
|
|
|
[else
|
|
|
|
($string-set! s ($fx+ start i) x)
|
|
|
|
(let ([i ($fxadd1 i)])
|
|
|
|
(if ($fx= i c)
|
|
|
|
i
|
|
|
|
(f p s start i c)))])))])))]
|
|
|
|
[($fx= c 0) 0]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'get-string-n! "count is negative" c)])))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
2008-12-06 12:40:18 -05:00
|
|
|
(define ($get-line p who)
|
2007-12-12 23:50:05 -05:00
|
|
|
(import UNSAFE)
|
2007-12-09 12:20:13 -05:00
|
|
|
(define (get-it p)
|
|
|
|
(let f ([p p] [n 0] [ac '()])
|
|
|
|
(let ([x (get-char p)])
|
|
|
|
(cond
|
|
|
|
[(eqv? x #\newline)
|
|
|
|
(make-it n ac)]
|
|
|
|
[(eof-object? x)
|
|
|
|
(if (null? ac) x (make-it n ac))]
|
2007-12-12 23:50:05 -05:00
|
|
|
[else (f p (fx+ n 1) (cons x ac))]))))
|
2007-12-09 12:20:13 -05:00
|
|
|
(define (make-it n revls)
|
2007-12-12 23:50:05 -05:00
|
|
|
(let f ([s (make-string n)] [i (fx- n 1)] [ls revls])
|
2007-12-09 12:20:13 -05:00
|
|
|
(cond
|
|
|
|
[(pair? ls)
|
|
|
|
(string-set! s i (car ls))
|
2007-12-12 23:50:05 -05:00
|
|
|
(f s (fx- i 1) (cdr ls))]
|
2007-12-09 12:20:13 -05:00
|
|
|
[else s])))
|
|
|
|
(if (input-port? p)
|
|
|
|
(if (textual-port? p)
|
|
|
|
(get-it p)
|
2008-12-06 12:40:18 -05:00
|
|
|
(die who "not a textual port" p))
|
|
|
|
(die who "not an input port" p)))
|
|
|
|
(define (get-line p)
|
|
|
|
($get-line p 'get-line))
|
|
|
|
(define read-line
|
|
|
|
(case-lambda
|
|
|
|
[() ($get-line (current-input-port) 'read-line)]
|
|
|
|
[(p) ($get-line p 'read-line)]))
|
2007-12-09 12:20:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
(define (get-string-all p)
|
|
|
|
(define (get-it p)
|
|
|
|
(let f ([p p] [n 0] [ac '()])
|
|
|
|
(let ([x (get-char p)])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x)
|
|
|
|
(if (null? ac)
|
|
|
|
(eof-object)
|
|
|
|
(make-it n ac))]
|
|
|
|
[else (f p (+ n 1) (cons x ac))]))))
|
|
|
|
(define (make-it n revls)
|
|
|
|
(let f ([s (make-string n)] [i (- n 1)] [ls revls])
|
|
|
|
(cond
|
|
|
|
[(pair? ls)
|
|
|
|
(string-set! s i (car ls))
|
|
|
|
(f s (- i 1) (cdr ls))]
|
|
|
|
[else s])))
|
|
|
|
(if (input-port? p)
|
|
|
|
(if (textual-port? p)
|
|
|
|
(get-it p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'get-string-all "not a textual port" p))
|
|
|
|
(die 'get-string-all "not an input port" p)))
|
2007-12-06 05:05:26 -05:00
|
|
|
|
2007-12-05 11:36:25 -05:00
|
|
|
|
2007-12-06 05:05:26 -05:00
|
|
|
|
|
|
|
;;; ----------------------------------------------------------
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(module (put-char write-char put-string)
|
2007-12-12 21:22:05 -05:00
|
|
|
(import UNSAFE)
|
2008-10-21 03:31:44 -04:00
|
|
|
(define (put-byte! p b who)
|
|
|
|
(let ([i ($port-index p)] [j ($port-size p)])
|
2008-12-12 16:03:10 -05:00
|
|
|
(if (fx< i j)
|
|
|
|
(begin
|
|
|
|
(bytevector-u8-set! ($port-buffer p) i b)
|
|
|
|
($set-port-index! p (fx+ i 1)))
|
|
|
|
(if (fx= j 0)
|
|
|
|
(put-byte/unbuffered! p b who)
|
|
|
|
(begin
|
|
|
|
(flush-output-port p)
|
|
|
|
(put-byte! p b who))))))
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (put-char-utf8-mode p b who)
|
|
|
|
(cond
|
2008-10-18 15:42:11 -04:00
|
|
|
[(fx< b 128)
|
|
|
|
(put-byte! p b who)]
|
|
|
|
[(fx<= b #x7FF)
|
|
|
|
(put-byte! p (fxior #b11000000 (fxsra b 6)) who)
|
|
|
|
(put-byte! p (fxior #b10000000 (fxand b #b111111)) who)]
|
|
|
|
[(fx<= b #xFFFF)
|
|
|
|
(put-byte! p (fxior #b11100000 (fxsra b 12)) who)
|
|
|
|
(put-byte! p (fxior #b10000000 (fxand (fxsra b 6) #b111111)) who)
|
|
|
|
(put-byte! p (fxior #b10000000 (fxand b #b111111)) who)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[else
|
2008-10-18 15:42:11 -04:00
|
|
|
(put-byte! p (fxior #b11110000 (fxsra b 18)) who)
|
|
|
|
(put-byte! p (fxior #b10000000 (fxand (fxsra b 12) #b111111)) who)
|
|
|
|
(put-byte! p (fxior #b10000000 (fxand (fxsra b 6) #b111111)) who)
|
|
|
|
(put-byte! p (fxior #b10000000 (fxand b #b111111)) who)]))
|
2007-12-05 11:36:25 -05:00
|
|
|
;;;
|
2007-12-10 07:28:03 -05:00
|
|
|
(define write-char
|
|
|
|
(case-lambda
|
|
|
|
[(c p) (do-put-char p c 'write-char)]
|
2008-11-15 11:21:00 -05:00
|
|
|
[(c) (do-put-char (current-output-port) c 'write-char)]))
|
2007-12-10 07:28:03 -05:00
|
|
|
(define (put-char p c)
|
|
|
|
(do-put-char p c 'put-char))
|
|
|
|
(define (put-string p str)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (string? str) (die 'put-string "not a string" str))
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (output-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'put-string "not an output port" p))
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (textual-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'put-string "not a textual port" p))
|
2007-12-10 07:28:03 -05:00
|
|
|
(let f ([i 0] [n (string-length str)])
|
|
|
|
(unless (fx= i n)
|
|
|
|
(do-put-char p (string-ref str i) 'put-string)
|
|
|
|
(f (fx+ i 1) n))))
|
|
|
|
(define (do-put-char p c who)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (char? c) (die who "not a char" c))
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-05 11:36:25 -05:00
|
|
|
(cond
|
|
|
|
[(eq? m fast-put-utf8-tag)
|
2008-10-18 15:42:11 -04:00
|
|
|
(let ([i ($port-index p)] [j ($port-size p)])
|
2007-12-05 11:36:25 -05:00
|
|
|
(let ([b (char->integer c)])
|
|
|
|
(cond
|
2008-10-18 15:42:11 -04:00
|
|
|
[(fx< b 128)
|
2008-12-12 16:03:10 -05:00
|
|
|
(if (fx< i j)
|
|
|
|
(begin
|
|
|
|
(bytevector-u8-set! ($port-buffer p) i b)
|
|
|
|
($set-port-index! p (fx+ i 1)))
|
|
|
|
(if (fx= j 0)
|
|
|
|
(put-byte/unbuffered! p b who)
|
|
|
|
(begin
|
|
|
|
(flush-output-port p)
|
|
|
|
(put-byte! p b who))))]
|
2007-12-05 11:36:25 -05:00
|
|
|
[else
|
|
|
|
(put-char-utf8-mode p b who)])))]
|
|
|
|
[(eq? m fast-put-char-tag)
|
2008-10-18 15:42:11 -04:00
|
|
|
(let ([i ($port-index p)] [j ($port-size p)])
|
2008-12-12 16:03:10 -05:00
|
|
|
(if (fx< i j)
|
|
|
|
(begin
|
|
|
|
(string-set! ($port-buffer p) i c)
|
|
|
|
($set-port-index! p (fx+ i 1)))
|
|
|
|
(if (fx= j 0)
|
|
|
|
(put-char/unbuffered! p c who)
|
|
|
|
(begin
|
|
|
|
(flush-output-port p)
|
|
|
|
(do-put-char p c who)))))]
|
2007-12-05 11:36:25 -05:00
|
|
|
[(eq? m fast-put-latin-tag)
|
2008-10-18 15:42:11 -04:00
|
|
|
(let ([i ($port-index p)] [j ($port-size p)])
|
2007-12-05 11:36:25 -05:00
|
|
|
(let ([b (char->integer c)])
|
|
|
|
(cond
|
2008-10-18 15:42:11 -04:00
|
|
|
[(fx< b 256)
|
2008-12-12 16:03:10 -05:00
|
|
|
(if (fx< i j)
|
|
|
|
(begin
|
|
|
|
(bytevector-u8-set! ($port-buffer p) i b)
|
|
|
|
($set-port-index! p (fx+ i 1)))
|
|
|
|
(if (fx= j 0)
|
|
|
|
(put-byte/unbuffered! p b who)
|
|
|
|
(begin
|
|
|
|
(flush-output-port p)
|
|
|
|
(put-byte! p b who))))]
|
2007-12-05 11:36:25 -05:00
|
|
|
[else
|
2008-10-18 15:42:11 -04:00
|
|
|
(case (transcoder-error-handling-mode (port-transcoder p))
|
|
|
|
[(ignore) (void)]
|
2008-12-12 16:03:10 -05:00
|
|
|
[(replace) (do-put-char p #\? who)]
|
2008-10-18 15:42:11 -04:00
|
|
|
[(raise)
|
|
|
|
(raise (make-i/o-encoding-error p c))]
|
|
|
|
[else (die who "BUG: invalid error handling mode" p)])])))]
|
2008-10-21 03:31:44 -04:00
|
|
|
[(eq? m fast-put-utf16be-tag)
|
|
|
|
(let ([n (char->integer c)])
|
|
|
|
(cond
|
|
|
|
[(fx< n #x10000)
|
|
|
|
(put-byte! p (fxsra n 8) who)
|
|
|
|
(put-byte! p (fxand n #xFF) who)]
|
|
|
|
[else
|
|
|
|
(let ([u^ (fx- n #x10000)])
|
|
|
|
(let ([w1 (fxior #xD800 (fxsra u^ 10))])
|
|
|
|
(put-byte! p (fxsra w1 8) who)
|
|
|
|
(put-byte! p (fxand w1 #xFF) who))
|
|
|
|
(let ([w2 (fxior #xDC00 (fxand u^ (- (fxsll 1 10) 1)))])
|
|
|
|
(put-byte! p (fxsra w2 8) who)
|
|
|
|
(put-byte! p (fxand w2 #xFF) who)))]))]
|
2008-10-18 15:42:11 -04:00
|
|
|
[else
|
2007-12-10 07:28:03 -05:00
|
|
|
(if (output-port? p)
|
2007-12-23 22:28:48 -05:00
|
|
|
(if (textual-port? p)
|
2008-01-22 03:30:52 -05:00
|
|
|
(if (port-closed? p)
|
|
|
|
(die who "port is closed" p)
|
|
|
|
(die who "unsupported port" p))
|
2007-12-23 22:28:48 -05:00
|
|
|
(die who "not a textual port" p))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not an output port" p))]))))
|
2007-12-05 11:36:25 -05:00
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define newline
|
|
|
|
(case-lambda
|
|
|
|
[()
|
2008-11-15 11:21:00 -05:00
|
|
|
(put-char (current-output-port) #\newline)
|
|
|
|
(flush-output-port (current-output-port))]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(p)
|
|
|
|
(unless (output-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'newline "not an output port" p))
|
2007-12-10 07:28:03 -05:00
|
|
|
(unless (textual-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'newline "not a textual port" p))
|
2008-01-22 03:30:52 -05:00
|
|
|
(when ($port-closed? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'newline "port is closed" p))
|
2007-12-10 07:28:03 -05:00
|
|
|
(put-char p #\newline)
|
|
|
|
(flush-output-port p)]))
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-20 20:30:37 -05:00
|
|
|
(module (put-u8 put-bytevector)
|
2007-12-12 21:22:05 -05:00
|
|
|
(import UNSAFE)
|
2007-12-05 11:36:25 -05:00
|
|
|
;;;
|
|
|
|
(define (put-u8 p b)
|
|
|
|
(define who 'put-u8)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (u8? b) (die who "not a u8" b))
|
2007-12-12 18:59:19 -05:00
|
|
|
(let ([m ($port-fast-attrs p)])
|
2007-12-05 11:36:25 -05:00
|
|
|
(cond
|
|
|
|
[(eq? m fast-put-byte-tag)
|
2008-10-18 15:42:11 -04:00
|
|
|
(let ([i ($port-index p)] [j ($port-size p)])
|
2008-12-12 16:03:10 -05:00
|
|
|
(if (fx< i j)
|
|
|
|
(begin
|
|
|
|
(bytevector-u8-set! ($port-buffer p) i b)
|
|
|
|
($set-port-index! p (fx+ i 1)))
|
|
|
|
(if (fx= j 0)
|
|
|
|
(put-byte/unbuffered! p b who)
|
|
|
|
(begin
|
|
|
|
(flush-output-port p)
|
|
|
|
(put-u8 p b)))))]
|
2007-12-10 07:28:03 -05:00
|
|
|
[else
|
|
|
|
(if (output-port? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a binary port" p)
|
2008-01-20 20:30:37 -05:00
|
|
|
(die who "not an output port" p))])))
|
|
|
|
;;;
|
|
|
|
(define ($put-bytevector p bv i c)
|
|
|
|
(define who 'put-bytevector)
|
|
|
|
(define (copy! src dst si di c)
|
|
|
|
(when (fx> c 0)
|
|
|
|
(bytevector-u8-set! dst di (bytevector-u8-ref src si))
|
|
|
|
(copy! src dst (fx+ si 1) (fx+ di 1) (fx- c 1))))
|
|
|
|
(let ([m ($port-fast-attrs p)])
|
|
|
|
(cond
|
|
|
|
[(eq? m fast-put-byte-tag)
|
2008-10-18 15:42:11 -04:00
|
|
|
(let ([idx ($port-index p)] [j ($port-size p)])
|
|
|
|
(let ([room (fx- j idx)])
|
2008-01-20 20:30:37 -05:00
|
|
|
(cond
|
|
|
|
[(fx>= room c)
|
|
|
|
;; hurray
|
2008-10-18 15:42:11 -04:00
|
|
|
(copy! bv ($port-buffer p) i idx c)
|
2008-12-17 15:42:28 -05:00
|
|
|
($set-port-index! p (fx+ idx c))]
|
2008-12-12 16:03:10 -05:00
|
|
|
[(fx> room 0)
|
2008-01-20 20:30:37 -05:00
|
|
|
($set-port-index! p (fx+ idx room))
|
|
|
|
(copy! bv ($port-buffer p) i idx room)
|
|
|
|
(flush-output-port p)
|
2008-12-12 16:03:10 -05:00
|
|
|
($put-bytevector p bv (fx+ i room) (fx- c room))]
|
|
|
|
[(fx> j 0)
|
|
|
|
(flush-output-port p)
|
|
|
|
($put-bytevector p bv i c)]
|
|
|
|
[else
|
|
|
|
(let f ([i i] [j (fx+ i c)])
|
|
|
|
(unless (fx= i j)
|
|
|
|
(put-byte/unbuffered! p (bytevector-u8-ref bv i) who)
|
|
|
|
(f (fx+ i 1) j)))])))]
|
2008-01-20 20:30:37 -05:00
|
|
|
[else
|
|
|
|
(if (output-port? p)
|
|
|
|
(die who "not a binary port" p)
|
|
|
|
(die who "not an output port" p))])))
|
|
|
|
(define put-bytevector
|
|
|
|
(case-lambda
|
|
|
|
[(p bv)
|
|
|
|
(if (bytevector? bv)
|
|
|
|
($put-bytevector p bv 0 (bytevector-length bv))
|
|
|
|
(die 'put-bytevector "not a bytevector" bv))]
|
|
|
|
[(p bv i)
|
|
|
|
(if (bytevector? bv)
|
|
|
|
(if (fixnum? i)
|
|
|
|
(let ([n (bytevector-length bv)])
|
|
|
|
(if (and (fx< i n) (fx>= i 0))
|
|
|
|
($put-bytevector p bv i (fx- n i))
|
|
|
|
(die 'put-bytevector "index out of range" i)))
|
|
|
|
(die 'put-bytevector "invalid index" i))
|
|
|
|
(die 'put-bytevector "not a bytevector" bv))]
|
|
|
|
[(p bv i c)
|
|
|
|
(if (bytevector? bv)
|
|
|
|
(if (fixnum? i)
|
|
|
|
(let ([n (bytevector-length bv)])
|
|
|
|
(if (and (fx< i n) (fx>= i 0))
|
|
|
|
(if (fixnum? c)
|
|
|
|
(if (and (fx>= c 0) (fx>= (fx- n c) i))
|
|
|
|
($put-bytevector p bv i c)
|
|
|
|
(die 'put-bytevector "count out of range" c))
|
|
|
|
(die 'put-bytevector "invalid count" c))
|
|
|
|
(die 'put-bytevector "index out of range" i)))
|
|
|
|
(die 'put-bytevector "invalid index" i))
|
|
|
|
(die 'put-bytevector "not a bytevector" bv))]))
|
|
|
|
;;; module
|
|
|
|
)
|
2007-12-10 07:28:03 -05:00
|
|
|
|
|
|
|
|
2007-12-19 22:46:07 -05:00
|
|
|
|
|
|
|
(define (process cmd . args)
|
|
|
|
(define who 'process)
|
|
|
|
(unless (string? cmd)
|
|
|
|
(die who "command is not a string" cmd))
|
|
|
|
(unless (andmap string? args)
|
|
|
|
(die who "all arguments must be strings"))
|
|
|
|
(let ([r (foreign-call "ikrt_process"
|
|
|
|
(make-vector 4)
|
|
|
|
(string->utf8 cmd)
|
|
|
|
(map string->utf8 (cons cmd args)))])
|
|
|
|
(if (fixnum? r)
|
|
|
|
(io-error who cmd r)
|
|
|
|
(values
|
|
|
|
(vector-ref r 0) ; pid
|
|
|
|
(fh->output-port (vector-ref r 1)
|
2008-01-22 03:30:52 -05:00
|
|
|
cmd output-file-buffer-size #f #t
|
|
|
|
'process)
|
2007-12-19 22:46:07 -05:00
|
|
|
(fh->input-port (vector-ref r 2)
|
2008-01-22 03:30:52 -05:00
|
|
|
cmd input-file-buffer-size #f #t
|
|
|
|
'process)
|
2007-12-19 22:46:07 -05:00
|
|
|
(fh->input-port (vector-ref r 3)
|
2008-01-22 03:30:52 -05:00
|
|
|
cmd input-file-buffer-size #f #t
|
|
|
|
'process)))))
|
2007-12-19 22:46:07 -05:00
|
|
|
|
2008-04-30 22:55:59 -04:00
|
|
|
|
|
|
|
(define (process-nonblocking cmd . args)
|
|
|
|
(define who 'process-nonblocking)
|
|
|
|
(unless (string? cmd)
|
|
|
|
(die who "command is not a string" cmd))
|
|
|
|
(unless (andmap string? args)
|
|
|
|
(die who "all arguments must be strings"))
|
|
|
|
(let ([r (foreign-call "ikrt_process"
|
|
|
|
(make-vector 4)
|
|
|
|
(string->utf8 cmd)
|
|
|
|
(map string->utf8 (cons cmd args)))])
|
|
|
|
(if (fixnum? r)
|
|
|
|
(io-error who cmd r)
|
|
|
|
(begin
|
|
|
|
(set-fd-nonblocking (vector-ref r 1) who cmd)
|
|
|
|
(set-fd-nonblocking (vector-ref r 2) who cmd)
|
|
|
|
(set-fd-nonblocking (vector-ref r 3) who cmd)
|
|
|
|
(values
|
|
|
|
(vector-ref r 0) ; pid
|
|
|
|
(fh->output-port (vector-ref r 1)
|
|
|
|
cmd output-file-buffer-size #f #t
|
|
|
|
'process)
|
|
|
|
(fh->input-port (vector-ref r 2)
|
|
|
|
cmd input-file-buffer-size #f #t
|
|
|
|
'process)
|
|
|
|
(fh->input-port (vector-ref r 3)
|
|
|
|
cmd input-file-buffer-size #f #t
|
|
|
|
'process))))))
|
|
|
|
|
|
|
|
|
2008-03-23 02:14:00 -04:00
|
|
|
(define (set-fd-nonblocking fd who id)
|
|
|
|
(let ([rv (foreign-call "ikrt_make_fd_nonblocking" fd)])
|
|
|
|
(unless (eq? rv 0)
|
2008-03-24 00:01:22 -04:00
|
|
|
(io-error who id rv))))
|
2008-03-23 02:14:00 -04:00
|
|
|
|
2008-03-23 00:41:49 -04:00
|
|
|
(define (socket->ports socket who id block?)
|
2007-12-27 22:08:27 -05:00
|
|
|
(if (< socket 0)
|
2008-03-22 19:29:41 -04:00
|
|
|
(io-error who id socket)
|
2007-12-27 22:08:27 -05:00
|
|
|
(let ([close
|
|
|
|
(let ([closed-once? #f])
|
|
|
|
(lambda ()
|
|
|
|
(if closed-once?
|
2007-12-28 00:05:44 -05:00
|
|
|
((file-close-proc id socket))
|
2007-12-27 22:08:27 -05:00
|
|
|
(set! closed-once? #t))))])
|
2008-03-23 00:41:49 -04:00
|
|
|
(unless block?
|
2008-03-23 02:14:00 -04:00
|
|
|
(set-fd-nonblocking socket who id))
|
2007-12-27 22:08:27 -05:00
|
|
|
(values
|
|
|
|
(fh->output-port socket
|
2008-04-11 14:02:43 -04:00
|
|
|
id (output-socket-buffer-size) #f close who)
|
2007-12-27 22:08:27 -05:00
|
|
|
(fh->input-port socket
|
2008-04-11 14:02:43 -04:00
|
|
|
id (input-socket-buffer-size) #f close who)))))
|
2007-12-19 22:46:07 -05:00
|
|
|
|
2008-03-22 19:29:41 -04:00
|
|
|
(define-syntax define-connector
|
|
|
|
(syntax-rules ()
|
2008-03-23 00:41:49 -04:00
|
|
|
[(_ who foreign-name block?)
|
2008-03-22 19:29:41 -04:00
|
|
|
(define (who host srvc)
|
|
|
|
(unless (and (string? host) (string? srvc))
|
|
|
|
(die 'who "host and service must both be strings" host srvc))
|
|
|
|
(socket->ports
|
2008-05-31 13:43:55 -04:00
|
|
|
(or (foreign-call foreign-name
|
|
|
|
(string->utf8 host) (string->utf8 srvc))
|
|
|
|
(die 'who "failed to resolve host name or connect" host srvc))
|
2008-03-22 19:29:41 -04:00
|
|
|
'who
|
2008-03-23 00:41:49 -04:00
|
|
|
(string-append host ":" srvc)
|
|
|
|
block?))]))
|
2008-03-22 19:29:41 -04:00
|
|
|
|
2008-03-23 00:41:49 -04:00
|
|
|
(define-connector tcp-connect "ikrt_tcp_connect" #t)
|
|
|
|
(define-connector udp-connect "ikrt_udp_connect" #t)
|
|
|
|
(define-connector tcp-connect-nonblocking "ikrt_tcp_connect" #f)
|
|
|
|
(define-connector udp-connect-nonblocking "ikrt_udp_connect" #f)
|
2008-03-22 19:29:41 -04:00
|
|
|
|
2008-04-02 20:28:45 -04:00
|
|
|
(module (add-io-event rem-io-event process-events)
|
2008-03-23 02:14:00 -04:00
|
|
|
(define-struct t (fd proc type))
|
|
|
|
;;; callbacks
|
|
|
|
(define pending '())
|
|
|
|
(define out-queue '())
|
|
|
|
(define in-queue '())
|
|
|
|
|
|
|
|
(define (process-events)
|
|
|
|
(if (null? out-queue)
|
|
|
|
(if (null? in-queue)
|
|
|
|
(if (null? pending)
|
|
|
|
(error 'process-events "no more events")
|
|
|
|
(begin
|
|
|
|
(do-select)
|
|
|
|
(process-events)))
|
|
|
|
(begin
|
|
|
|
(set! out-queue (reverse in-queue))
|
|
|
|
(set! in-queue '())
|
|
|
|
(process-events)))
|
2008-04-06 10:57:56 -04:00
|
|
|
(let ([t (car out-queue)])
|
2008-03-23 02:14:00 -04:00
|
|
|
(set! out-queue (cdr out-queue))
|
2008-04-06 10:57:56 -04:00
|
|
|
((t-proc t))
|
2008-03-23 02:14:00 -04:00
|
|
|
(process-events))))
|
|
|
|
|
|
|
|
(define (add-io-event fd proc event-type)
|
|
|
|
(set! pending
|
|
|
|
(cons (make-t fd proc event-type) pending)))
|
2008-04-02 20:28:45 -04:00
|
|
|
|
|
|
|
(define (rem-io-event fd)
|
|
|
|
(define (p x) (eq? (t-fd x) fd))
|
|
|
|
(set! pending (remp p pending))
|
|
|
|
(set! out-queue (remp p out-queue))
|
|
|
|
(set! in-queue (remp p in-queue)))
|
2008-03-23 02:14:00 -04:00
|
|
|
|
|
|
|
(define (get-max-fd)
|
|
|
|
(assert (pair? pending))
|
|
|
|
(let f ([m (t-fd (car pending))]
|
|
|
|
[ls (cdr pending)])
|
|
|
|
(cond
|
|
|
|
[(null? ls) m]
|
|
|
|
[else (f (max m (t-fd (car ls))) (cdr ls))])))
|
|
|
|
|
|
|
|
(define (do-select)
|
|
|
|
(let ([n (add1 (get-max-fd))])
|
2008-06-06 11:22:45 -04:00
|
|
|
(let ([vecsize (div (+ n 7) 8)])
|
|
|
|
(let ([rbv (make-bytevector vecsize 0)]
|
|
|
|
[wbv (make-bytevector vecsize 0)]
|
|
|
|
[xbv (make-bytevector vecsize 0)])
|
|
|
|
;;; add all fds to their bytevectors depending on type
|
|
|
|
(for-each
|
|
|
|
(lambda (t)
|
|
|
|
(let ([fd (t-fd t)])
|
|
|
|
(let ([i (div fd 8)] [j (mod fd 8)])
|
|
|
|
(let ([bv (case (t-type t)
|
|
|
|
[(r) rbv]
|
|
|
|
[(w) wbv]
|
|
|
|
[(x) xbv]
|
|
|
|
[else
|
|
|
|
(error 'do-select "invalid type" t)])])
|
|
|
|
(bytevector-u8-set! bv i
|
|
|
|
(fxlogor (fxsll 1 j)
|
|
|
|
(bytevector-u8-ref bv i)))))))
|
|
|
|
pending)
|
|
|
|
;;; do select
|
|
|
|
(let ([rv (foreign-call "ikrt_select" n rbv wbv xbv)])
|
|
|
|
(when (< rv 0)
|
|
|
|
(io-error 'select #f rv)))
|
|
|
|
;;; go through fds again and see if they're selected
|
|
|
|
(for-each
|
|
|
|
(lambda (t)
|
|
|
|
(let ([fd (t-fd t)])
|
|
|
|
(let ([i (div fd 8)] [j (mod fd 8)])
|
|
|
|
(let ([bv (case (t-type t)
|
|
|
|
[(r) rbv]
|
|
|
|
[(w) wbv]
|
|
|
|
[(x) xbv]
|
|
|
|
[else
|
|
|
|
(error 'do-select "invalid type" t)])])
|
|
|
|
(cond
|
|
|
|
[(fxzero?
|
|
|
|
(fxlogand (fxsll 1 j)
|
|
|
|
(bytevector-u8-ref bv i)))
|
|
|
|
;;; not selected
|
|
|
|
(set! pending (cons t pending))]
|
|
|
|
[else
|
|
|
|
;;; ready
|
|
|
|
(set! in-queue (cons t in-queue))])))))
|
|
|
|
(let ([ls pending])
|
|
|
|
(set! pending '())
|
|
|
|
ls))))))
|
2008-03-23 02:14:00 -04:00
|
|
|
)
|
|
|
|
|
2008-03-23 03:44:20 -04:00
|
|
|
|
|
|
|
(define-struct tcp-server (portnum fd))
|
|
|
|
|
|
|
|
(define (tcp-server-socket portnum)
|
|
|
|
(unless (fixnum? portnum)
|
|
|
|
(error 'tcp-server-socket "not a fixnum" portnum))
|
|
|
|
(let ([sock (foreign-call "ikrt_listen" portnum)])
|
|
|
|
(cond
|
|
|
|
[(fx>= sock 0) (make-tcp-server portnum sock)]
|
|
|
|
[else (die 'tcp-server-socket "failed to start server")])))
|
2008-03-23 05:02:12 -04:00
|
|
|
|
|
|
|
(define (tcp-server-socket-nonblocking portnum)
|
|
|
|
(let ([s (tcp-server-socket portnum)])
|
|
|
|
(set-fd-nonblocking (tcp-server-fd s)
|
|
|
|
'tcp-server-socket-nonblocking
|
|
|
|
'#f)
|
|
|
|
s))
|
2008-03-23 03:44:20 -04:00
|
|
|
|
2008-03-23 05:02:12 -04:00
|
|
|
|
|
|
|
(define (do-accept-connection s who blocking?)
|
2008-04-11 07:01:27 -04:00
|
|
|
(define (make-socket-info x)
|
|
|
|
(unless (= (bytevector-length x) 16)
|
|
|
|
(error who "BUG: unexpected return value" x))
|
|
|
|
(format "~s.~s.~s.~s:~s"
|
|
|
|
(bytevector-u8-ref x 4)
|
|
|
|
(bytevector-u8-ref x 5)
|
|
|
|
(bytevector-u8-ref x 6)
|
|
|
|
(bytevector-u8-ref x 7)
|
|
|
|
(+ (* 256 (bytevector-u8-ref x 2))
|
|
|
|
(bytevector-u8-ref x 3))))
|
2008-03-23 03:44:20 -04:00
|
|
|
(unless (tcp-server? s)
|
|
|
|
(die who "not a tcp server" s))
|
2008-04-11 07:01:27 -04:00
|
|
|
(let ([fd (tcp-server-fd s)] [bv (make-bytevector 16)])
|
2008-03-23 03:44:20 -04:00
|
|
|
(unless fd
|
|
|
|
(die who "server is closed" s))
|
2008-04-11 07:01:27 -04:00
|
|
|
(let ([sock (foreign-call "ikrt_accept" fd bv)])
|
2008-03-23 05:02:12 -04:00
|
|
|
(cond
|
|
|
|
[(eq? sock EAGAIN-error-code)
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
|
|
|
(add-io-event fd k 'r)
|
|
|
|
(process-events)))
|
|
|
|
(do-accept-connection s who blocking?)]
|
2008-04-11 07:01:27 -04:00
|
|
|
[(< sock 0)
|
|
|
|
(io-error who s sock)]
|
2008-03-23 05:02:12 -04:00
|
|
|
[else
|
2008-04-11 07:01:27 -04:00
|
|
|
(socket->ports sock who (make-socket-info bv) blocking?)]))))
|
2008-03-23 05:02:12 -04:00
|
|
|
|
|
|
|
(define (accept-connection s)
|
|
|
|
(do-accept-connection s 'accept-connection #t))
|
|
|
|
|
|
|
|
(define (accept-connection-nonblocking s)
|
|
|
|
(do-accept-connection s 'accept-connection-nonblocking #f))
|
2008-03-23 03:44:20 -04:00
|
|
|
|
|
|
|
(define (close-tcp-server-socket s)
|
|
|
|
(define who 'close-tcp-server-socket)
|
|
|
|
(unless (tcp-server? s)
|
|
|
|
(die who "not a tcp server" s))
|
|
|
|
(let ([fd (tcp-server-fd s)])
|
|
|
|
(unless fd
|
|
|
|
(die who "server is closed" s))
|
|
|
|
(let ([rv (foreign-call "ikrt_shutdown" fd)])
|
|
|
|
(when (fx< rv 0)
|
|
|
|
(die who "failed to shutdown")))))
|
2008-03-23 02:14:00 -04:00
|
|
|
|
2008-04-02 20:28:45 -04:00
|
|
|
(define (unregister-callback what)
|
|
|
|
(define who 'unregister-callback)
|
|
|
|
(cond
|
|
|
|
[(output-port? what)
|
|
|
|
(let ([c ($port-cookie what)])
|
|
|
|
(unless (fixnum? c) (die who "not a file-based port" what))
|
|
|
|
(rem-io-event c))]
|
|
|
|
[(input-port? what)
|
|
|
|
(let ([c ($port-cookie what)])
|
|
|
|
(unless (fixnum? c) (die who "not a file-based port" what))
|
|
|
|
(rem-io-event c))]
|
|
|
|
[(tcp-server? what)
|
|
|
|
(rem-io-event (tcp-server-fd what))]
|
|
|
|
[else (die who "invalid argument" what)]))
|
|
|
|
|
2008-03-23 05:02:12 -04:00
|
|
|
(define (register-callback what proc)
|
|
|
|
(define who 'register-callback)
|
|
|
|
(unless (procedure? proc)
|
|
|
|
(die who "not a procedure" proc))
|
|
|
|
(cond
|
|
|
|
[(output-port? what)
|
|
|
|
(let ([c ($port-cookie what)])
|
|
|
|
(unless (fixnum? c) (die who "not a file-based port" what))
|
|
|
|
(add-io-event c proc 'w))]
|
|
|
|
[(input-port? what)
|
|
|
|
(let ([c ($port-cookie what)])
|
|
|
|
(unless (fixnum? c) (die who "not a file-based port" what))
|
|
|
|
(add-io-event c proc 'r))]
|
|
|
|
[(tcp-server? what)
|
|
|
|
(add-io-event (tcp-server-fd what) proc 'r)]
|
|
|
|
[else (die who "invalid argument" what)]))
|
|
|
|
|
|
|
|
|
2008-05-21 02:21:37 -04:00
|
|
|
|
|
|
|
|
2008-04-06 10:57:56 -04:00
|
|
|
;(set-fd-nonblocking 0 'init '*stdin*)
|
2007-12-10 07:28:03 -05:00
|
|
|
)
|
|
|
|
|