| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus.io) |
| 18 |
|
| 19 |
(export |
| 20 |
port? input-port? output-port? textual-port? binary-port? |
| 21 |
open-file-input-port open-input-file |
| 22 |
call-with-input-file with-input-from-file |
| 23 |
standard-input-port current-input-port |
| 24 |
open-bytevector-input-port |
| 25 |
open-string-input-port open-string-input-port/id |
| 26 |
with-input-from-string |
| 27 |
make-custom-binary-input-port |
| 28 |
make-custom-binary-output-port |
| 29 |
make-custom-textual-input-port |
| 30 |
make-custom-textual-output-port |
| 31 |
transcoded-port port-transcoder |
| 32 |
close-port port-closed? close-input-port close-output-port |
| 33 |
port-eof? |
| 34 |
get-char lookahead-char read-char peek-char |
| 35 |
get-string-n get-string-n! get-string-all get-line read-line |
| 36 |
get-u8 lookahead-u8 |
| 37 |
get-bytevector-n get-bytevector-n! |
| 38 |
get-bytevector-some get-bytevector-all |
| 39 |
port-position port-has-port-position? |
| 40 |
set-port-position! port-has-set-port-position!? |
| 41 |
call-with-port |
| 42 |
flush-output-port |
| 43 |
put-u8 put-bytevector |
| 44 |
put-char write-char |
| 45 |
put-string |
| 46 |
open-bytevector-output-port |
| 47 |
call-with-bytevector-output-port |
| 48 |
open-string-output-port with-output-to-string |
| 49 |
with-output-to-port |
| 50 |
call-with-string-output-port |
| 51 |
open-output-string get-output-string |
| 52 |
standard-output-port standard-error-port |
| 53 |
current-output-port current-error-port |
| 54 |
open-file-output-port open-output-file |
| 55 |
call-with-output-file with-output-to-file |
| 56 |
console-output-port |
| 57 |
console-error-port |
| 58 |
console-input-port |
| 59 |
newline |
| 60 |
port-mode set-port-mode! |
| 61 |
output-port-buffer-mode |
| 62 |
reset-input-port! |
| 63 |
reset-output-port! |
| 64 |
port-id |
| 65 |
input-port-byte-position |
| 66 |
input-port-column-number input-port-row-number |
| 67 |
process process-nonblocking |
| 68 |
process* |
| 69 |
|
| 70 |
tcp-connect tcp-connect-nonblocking |
| 71 |
udp-connect udp-connect-nonblocking |
| 72 |
tcp-server-socket tcp-server-socket-nonblocking |
| 73 |
accept-connection accept-connection-nonblocking |
| 74 |
close-tcp-server-socket |
| 75 |
register-callback |
| 76 |
input-socket-buffer-size output-socket-buffer-size |
| 77 |
|
| 78 |
open-directory-stream directory-stream? |
| 79 |
read-directory-stream close-directory-stream) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
(import |
| 84 |
(ikarus system $io) |
| 85 |
(except (ikarus) |
| 86 |
port? input-port? output-port? textual-port? binary-port? |
| 87 |
open-file-input-port open-input-file |
| 88 |
call-with-input-file with-input-from-file |
| 89 |
standard-input-port current-input-port |
| 90 |
open-bytevector-input-port |
| 91 |
open-string-input-port with-input-from-string |
| 92 |
make-custom-binary-input-port |
| 93 |
make-custom-binary-output-port |
| 94 |
make-custom-textual-input-port |
| 95 |
make-custom-textual-output-port |
| 96 |
transcoded-port port-transcoder |
| 97 |
close-port port-closed? close-input-port close-output-port |
| 98 |
port-eof? |
| 99 |
get-char lookahead-char read-char peek-char |
| 100 |
get-string-n get-string-n! get-string-all get-line read-line |
| 101 |
get-u8 lookahead-u8 |
| 102 |
get-bytevector-n get-bytevector-n! |
| 103 |
get-bytevector-some get-bytevector-all |
| 104 |
port-position port-has-port-position? |
| 105 |
set-port-position! port-has-set-port-position!? |
| 106 |
call-with-port |
| 107 |
flush-output-port |
| 108 |
put-u8 put-bytevector |
| 109 |
put-char write-char |
| 110 |
put-string |
| 111 |
open-bytevector-output-port |
| 112 |
call-with-bytevector-output-port |
| 113 |
open-string-output-port with-output-to-string |
| 114 |
call-with-string-output-port |
| 115 |
open-output-string get-output-string |
| 116 |
standard-output-port standard-error-port |
| 117 |
current-output-port current-error-port |
| 118 |
open-file-output-port open-output-file |
| 119 |
call-with-output-file with-output-to-file |
| 120 |
with-output-to-port |
| 121 |
console-output-port |
| 122 |
console-input-port |
| 123 |
console-error-port |
| 124 |
newline |
| 125 |
port-mode set-port-mode! |
| 126 |
output-port-buffer-mode |
| 127 |
reset-input-port! |
| 128 |
reset-output-port! |
| 129 |
port-id |
| 130 |
process process-nonblocking |
| 131 |
tcp-connect tcp-connect-nonblocking |
| 132 |
udp-connect udp-connect-nonblocking |
| 133 |
tcp-server-socket tcp-server-socket-nonblocking |
| 134 |
accept-connection accept-connection-nonblocking |
| 135 |
close-tcp-server-socket |
| 136 |
register-callback |
| 137 |
input-socket-buffer-size output-socket-buffer-size |
| 138 |
input-port-column-number input-port-row-number |
| 139 |
|
| 140 |
open-directory-stream directory-stream? |
| 141 |
read-directory-stream close-directory-stream |
| 142 |
process* |
| 143 |
)) |
| 144 |
|
| 145 |
;(define-syntax assert* (identifier-syntax assert)) |
| 146 |
|
| 147 |
;;; all-data-in-buffer is used in place of the read! |
| 148 |
;;; procedure to mark ports whose buffer is all the |
| 149 |
;;; data there is. |
| 150 |
(define all-data-in-buffer 'all-data-in-buffer) |
| 151 |
|
| 152 |
(define-syntax assert* (syntax-rules () [(_ . x) (void)])) |
| 153 |
|
| 154 |
(module UNSAFE |
| 155 |
(fx< fx<= fx> fx>= fx= fx+ fx- |
| 156 |
fxior fxand fxsra fxsll |
| 157 |
integer->char char->integer |
| 158 |
string-ref string-set! string-length |
| 159 |
bytevector-u8-ref bytevector-u8-set! |
| 160 |
bytevector-u16-ref) |
| 161 |
(import |
| 162 |
(rename (ikarus system $strings) |
| 163 |
($string-length string-length) |
| 164 |
($string-ref string-ref) |
| 165 |
($string-set! string-set!)) |
| 166 |
(rename (ikarus system $chars) |
| 167 |
($char->fixnum char->integer) |
| 168 |
($fixnum->char integer->char)) |
| 169 |
(rename (ikarus system $bytevectors) |
| 170 |
($bytevector-set! bytevector-u8-set!) |
| 171 |
($bytevector-u8-ref bytevector-u8-ref)) |
| 172 |
(rename (ikarus system $fx) |
| 173 |
($fxsra fxsra) |
| 174 |
($fxsll fxsll) |
| 175 |
($fxlogor fxior) |
| 176 |
($fxlogand fxand) |
| 177 |
($fx+ fx+) |
| 178 |
($fx- fx-) |
| 179 |
($fx< fx<) |
| 180 |
($fx> fx>) |
| 181 |
($fx>= fx>=) |
| 182 |
($fx<= fx<=) |
| 183 |
($fx= fx=))) |
| 184 |
(define (bytevector-u16-ref x i endianness) |
| 185 |
(case endianness |
| 186 |
[(little) |
| 187 |
(fxlogor (bytevector-u8-ref x i) |
| 188 |
(fxsll (bytevector-u8-ref x (fx+ i 1)) 8))] |
| 189 |
[else |
| 190 |
(fxlogor (bytevector-u8-ref x (fx+ i 1)) |
| 191 |
(fxsll (bytevector-u8-ref x i) 8))]))) |
| 192 |
|
| 193 |
|
| 194 |
(define (port? x) |
| 195 |
(import (only (ikarus) port?)) |
| 196 |
(port? x)) |
| 197 |
|
| 198 |
(define-syntax define-rrr |
| 199 |
(syntax-rules () |
| 200 |
[(_ name) |
| 201 |
(define (name . args) |
| 202 |
(apply die 'name "not implemented" args))])) |
| 203 |
|
| 204 |
(define-syntax u8? |
| 205 |
(let () |
| 206 |
(import (ikarus system $fx)) |
| 207 |
(syntax-rules () |
| 208 |
[(_ x) |
| 209 |
($fxzero? ($fxlogand x -256))]))) |
| 210 |
|
| 211 |
;(define (u8? x) (and (fixnum? x) (fx>= x 0) (fx< x 256))) |
| 212 |
|
| 213 |
(define (textual-port? x) |
| 214 |
(fx= (fxand ($port-tag x) textual-port-tag) textual-port-tag)) |
| 215 |
|
| 216 |
(define (binary-port? x) |
| 217 |
(fx= (fxand ($port-tag x) binary-port-tag) binary-port-tag)) |
| 218 |
|
| 219 |
(define (output-port? x) |
| 220 |
(fx= (fxand ($port-tag x) output-port-tag) output-port-tag)) |
| 221 |
|
| 222 |
(define (input-port? x) |
| 223 |
(fx= (fxand ($port-tag x) input-port-tag) input-port-tag)) |
| 224 |
|
| 225 |
;;; everything above this line will turn into primitive |
| 226 |
;;; ---------------------------------------------------------- |
| 227 |
|
| 228 |
(define input-port-tag #b00000000000001) |
| 229 |
(define output-port-tag #b00000000000010) |
| 230 |
(define textual-port-tag #b00000000000100) |
| 231 |
(define binary-port-tag #b00000000001000) |
| 232 |
(define fast-char-text-tag #b00000000010000) |
| 233 |
(define fast-u7-text-tag #b00000000100000) |
| 234 |
(define fast-u8-text-tag #b00000001100000) |
| 235 |
(define fast-u16be-text-tag #b00000010000000) |
| 236 |
(define fast-u16le-text-tag #b00000100000000) |
| 237 |
(define init-u16-text-tag #b00000110000000) |
| 238 |
; (define r6rs-mode-tag #b01000000000000) |
| 239 |
(define closed-port-tag #b10000000000000) |
| 240 |
|
| 241 |
(define port-type-mask #b00000000001111) |
| 242 |
(define binary-input-port-bits #b00000000001001) |
| 243 |
(define binary-output-port-bits #b00000000001010) |
| 244 |
(define textual-input-port-bits #b00000000000101) |
| 245 |
(define textual-output-port-bits #b00000000000110) |
| 246 |
|
| 247 |
(define fast-get-byte-tag #b00000000001001) |
| 248 |
(define fast-get-char-tag #b00000000010101) |
| 249 |
(define fast-get-utf8-tag #b00000000100101) |
| 250 |
(define fast-get-latin-tag #b00000001100101) |
| 251 |
(define fast-get-utf16be-tag #b00000010000101) |
| 252 |
(define fast-get-utf16le-tag #b00000100000101) |
| 253 |
|
| 254 |
(define fast-put-byte-tag #b00000000001010) |
| 255 |
(define fast-put-char-tag #b00000000010110) |
| 256 |
(define fast-put-utf8-tag #b00000000100110) |
| 257 |
(define fast-put-latin-tag #b00000001100110) |
| 258 |
(define fast-put-utf16be-tag #b00000010000110) |
| 259 |
(define fast-put-utf16le-tag #b00000100000110) |
| 260 |
(define init-put-utf16-tag #b00000110000110) |
| 261 |
|
| 262 |
(define fast-attrs-mask #b111111111111) |
| 263 |
(define-syntax $port-fast-attrs |
| 264 |
(identifier-syntax |
| 265 |
(lambda (x) |
| 266 |
(import (ikarus system $fx)) |
| 267 |
($fxlogand ($port-tag x) fast-attrs-mask)))) |
| 268 |
|
| 269 |
(define-struct cookie (dest mode pos row-num newline-pos)) |
| 270 |
|
| 271 |
(define (default-cookie fd) (make-cookie fd 'ikarus-mode 0 0 0)) |
| 272 |
|
| 273 |
(define (port-id p) |
| 274 |
(if (port? p) |
| 275 |
($port-id p) |
| 276 |
(die 'port-id "not a port" p))) |
| 277 |
|
| 278 |
(define (input-port-byte-position p) |
| 279 |
(if (input-port? p) |
| 280 |
(let ([cookie ($port-cookie p)]) |
| 281 |
(+ (cookie-pos cookie) (fx+ ($port-index p) 1))) |
| 282 |
(error 'input-port-byte-position "not an input port" p))) |
| 283 |
|
| 284 |
(define (mark/return-newline p) |
| 285 |
(let ([cookie ($port-cookie p)]) |
| 286 |
(set-cookie-row-num! cookie (+ (cookie-row-num cookie) 1)) |
| 287 |
(set-cookie-newline-pos! cookie |
| 288 |
(+ (cookie-pos cookie) ($port-index p)))) |
| 289 |
#\newline) |
| 290 |
|
| 291 |
(define (input-port-column-number p) |
| 292 |
(if (input-port? p) |
| 293 |
(let ([cookie ($port-cookie p)]) |
| 294 |
(- (+ (cookie-pos cookie) ($port-index p)) |
| 295 |
(cookie-newline-pos cookie))) |
| 296 |
(die 'input-port-column-number "not an input port" p))) |
| 297 |
|
| 298 |
(define (input-port-row-number p) |
| 299 |
(if (input-port? p) |
| 300 |
(cookie-row-num ($port-cookie p)) |
| 301 |
(die 'input-port-row-number "not an input port" p))) |
| 302 |
|
| 303 |
(define (port-position p) |
| 304 |
(define who 'port-position) |
| 305 |
(if (port? p) |
| 306 |
(let ([cookie ($port-cookie p)] |
| 307 |
[index ($port-index p)] |
| 308 |
[get-position ($port-get-position p)]) |
| 309 |
(cond |
| 310 |
[(procedure? get-position) |
| 311 |
(let ([pos (get-position)]) |
| 312 |
(if (or (fixnum? pos) (bignum? pos)) |
| 313 |
(if (input-port? p) |
| 314 |
(- pos (- ($port-size p) index)) |
| 315 |
(+ pos index)) |
| 316 |
(die who "invalid returned value from get-position" p)))] |
| 317 |
[(eqv? get-position #t) |
| 318 |
(+ (cookie-pos cookie) index)] |
| 319 |
[else |
| 320 |
(die who "port does not support port-position operation" p)])) |
| 321 |
(die who "not a port" p))) |
| 322 |
|
| 323 |
|
| 324 |
(define (set-port-position! p pos) |
| 325 |
(define who 'set-port-position!) |
| 326 |
(define (set-position! p pos flush?) |
| 327 |
(let ([setpos! ($port-set-position! p)]) |
| 328 |
(cond |
| 329 |
[(procedure? setpos!) |
| 330 |
(when flush? (flush-output-port p)) |
| 331 |
(setpos! pos) |
| 332 |
($set-port-index! p 0) |
| 333 |
($set-port-size! p 0) |
| 334 |
(let ([cookie ($port-cookie p)]) |
| 335 |
(set-cookie-pos! cookie pos))] |
| 336 |
[(eqv? setpos! #t) |
| 337 |
(if (<= pos ($port-size p)) |
| 338 |
($set-port-index! p pos) |
| 339 |
(die who "position out of range" pos))] |
| 340 |
[else |
| 341 |
(die who "port does not support port position" p)]))) |
| 342 |
(unless (and (or (fixnum? pos) (bignum? pos)) (>= pos 0)) |
| 343 |
(die who "position must be a nonnegative exact integer" pos)) |
| 344 |
(cond |
| 345 |
[(output-port? p) (set-position! p pos #t)] |
| 346 |
[(input-port? p) (set-position! p pos #f)] |
| 347 |
[else (die who "not a port" p)])) |
| 348 |
|
| 349 |
|
| 350 |
(define (port-has-port-position? p) |
| 351 |
(define who 'port-has-port-position?) |
| 352 |
(if (port? p) |
| 353 |
(and ($port-get-position p) #t) |
| 354 |
(die who "not a port" p))) |
| 355 |
|
| 356 |
(define (port-has-set-port-position!? p) |
| 357 |
(define who 'port-has-set-port-position!?) |
| 358 |
(if (port? p) |
| 359 |
(and ($port-set-position! p) #t) |
| 360 |
(die who "not a port" p))) |
| 361 |
|
| 362 |
(define guarded-port |
| 363 |
(let ([G (make-guardian)]) |
| 364 |
(define (clean-up) |
| 365 |
(cond |
| 366 |
[(G) => |
| 367 |
(lambda (p) |
| 368 |
(close-port p) |
| 369 |
(clean-up))])) |
| 370 |
(lambda (p) |
| 371 |
(clean-up) |
| 372 |
(when (fixnum? (cookie-dest ($port-cookie p))) |
| 373 |
(G p)) |
| 374 |
p))) |
| 375 |
|
| 376 |
(define ($make-custom-binary-port attrs init-size id |
| 377 |
read! write! get-position set-position! close buffer-size) |
| 378 |
(let ([bv (make-bytevector buffer-size)]) |
| 379 |
($make-port attrs 0 init-size bv #f id read! write! |
| 380 |
get-position set-position! close |
| 381 |
(default-cookie #f)))) |
| 382 |
|
| 383 |
(define ($make-custom-textual-port attrs init-size id |
| 384 |
read! write! get-position set-position! close buffer-size) |
| 385 |
(let ([bv (make-string buffer-size)]) |
| 386 |
($make-port attrs 0 init-size bv #t id read! write! |
| 387 |
get-position set-position! close |
| 388 |
(default-cookie #f)))) |
| 389 |
|
| 390 |
(define (make-custom-binary-input-port id |
| 391 |
read! get-position set-position! close) |
| 392 |
;;; FIXME: get-position and set-position! are ignored for now |
| 393 |
(define who 'make-custom-binary-input-port) |
| 394 |
(unless (string? id) |
| 395 |
(die who "id is not a string" id)) |
| 396 |
(unless (procedure? read!) |
| 397 |
(die who "read! is not a procedure" read!)) |
| 398 |
(unless (or (procedure? close) (not close)) |
| 399 |
(die who "close should be either a procedure or #f" close)) |
| 400 |
(unless (or (procedure? get-position) |
| 401 |
(not get-position)) |
| 402 |
(die who "get-position is not a procedure or #f" |
| 403 |
get-position)) |
| 404 |
($make-custom-binary-port |
| 405 |
binary-input-port-bits |
| 406 |
0 |
| 407 |
id read! #f |
| 408 |
get-position |
| 409 |
set-position! |
| 410 |
close 256)) |
| 411 |
|
| 412 |
(define (make-custom-binary-output-port id |
| 413 |
write! get-position set-position! close) |
| 414 |
;;; FIXME: get-position and set-position! are ignored for now |
| 415 |
(define who 'make-custom-binary-output-port) |
| 416 |
(unless (string? id) |
| 417 |
(die who "id is not a string" id)) |
| 418 |
(unless (procedure? write!) |
| 419 |
(die who "write! is not a procedure" write!)) |
| 420 |
(unless (or (procedure? close) (not close)) |
| 421 |
(die who "close should be either a procedure or #f" close)) |
| 422 |
(unless (or (procedure? get-position) |
| 423 |
(not get-position)) |
| 424 |
(die who "get-position is not a procedure or #f" |
| 425 |
get-position)) |
| 426 |
($make-custom-binary-port |
| 427 |
binary-output-port-bits |
| 428 |
256 |
| 429 |
id #f write! |
| 430 |
get-position |
| 431 |
set-position! |
| 432 |
close 256)) |
| 433 |
|
| 434 |
(define (make-custom-textual-input-port id |
| 435 |
read! get-position set-position! close) |
| 436 |
;;; FIXME: get-position and set-position! are ignored for now |
| 437 |
(define who 'make-custom-textual-input-port) |
| 438 |
(unless (string? id) |
| 439 |
(die who "id is not a string" id)) |
| 440 |
(unless (procedure? read!) |
| 441 |
(die who "read! is not a procedure" read!)) |
| 442 |
(unless (or (procedure? close) (not close)) |
| 443 |
(die who "close should be either a procedure or #f" close)) |
| 444 |
(unless (or (procedure? get-position) |
| 445 |
(not get-position)) |
| 446 |
(die who "get-position is not a procedure or #f" |
| 447 |
get-position)) |
| 448 |
($make-custom-textual-port |
| 449 |
(fxior textual-input-port-bits fast-char-text-tag) |
| 450 |
0 |
| 451 |
id read! #f get-position |
| 452 |
set-position! close 256)) |
| 453 |
|
| 454 |
(define (make-custom-textual-output-port id |
| 455 |
write! get-position set-position! close) |
| 456 |
;;; FIXME: get-position and set-position! are ignored for now |
| 457 |
(define who 'make-custom-textual-output-port) |
| 458 |
(unless (string? id) |
| 459 |
(die who "id is not a string" id)) |
| 460 |
(unless (procedure? write!) |
| 461 |
(die who "write! is not a procedure" write!)) |
| 462 |
(unless (or (procedure? close) (not close)) |
| 463 |
(die who "close should be either a procedure or #f" close)) |
| 464 |
(unless (or (procedure? get-position) |
| 465 |
(not get-position)) |
| 466 |
(die who "get-position is not a procedure or #f" |
| 467 |
get-position)) |
| 468 |
($make-custom-textual-port |
| 469 |
(fxior textual-output-port-bits fast-char-text-tag) |
| 470 |
256 |
| 471 |
id #f write! get-position |
| 472 |
set-position! close 256)) |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
(define (input-transcoder-attrs x who) |
| 477 |
(cond |
| 478 |
[(not x) ;;; binary input port |
| 479 |
binary-input-port-bits] |
| 480 |
[(not (eq? 'none (transcoder-eol-style x))) |
| 481 |
(die who "unsupported transcoder eol-style" |
| 482 |
(transcoder-eol-style x))] |
| 483 |
[(eq? 'latin-1-codec (transcoder-codec x)) |
| 484 |
(fxior textual-input-port-bits fast-u8-text-tag)] |
| 485 |
;;; attrs for utf-8-codec are set as part of the |
| 486 |
;;; bom-reading dance when the first char is read. |
| 487 |
[else textual-input-port-bits])) |
| 488 |
|
| 489 |
(define (output-transcoder-attrs x who) |
| 490 |
(cond |
| 491 |
[(not x) ;;; binary input port |
| 492 |
binary-output-port-bits] |
| 493 |
[(not (eq? 'none (transcoder-eol-style x))) |
| 494 |
(die who "unsupported transcoder eol-style" |
| 495 |
(transcoder-eol-style x))] |
| 496 |
[(eq? 'latin-1-codec (transcoder-codec x)) |
| 497 |
(fxior textual-output-port-bits fast-u8-text-tag)] |
| 498 |
[(eq? 'utf-8-codec (transcoder-codec x)) |
| 499 |
(fxior textual-output-port-bits fast-u7-text-tag)] |
| 500 |
[(eq? 'utf-16-codec (transcoder-codec x)) |
| 501 |
(fxior textual-output-port-bits fast-u16be-text-tag)] |
| 502 |
[else (die who "unsupported codec" (transcoder-codec x))])) |
| 503 |
|
| 504 |
(define open-bytevector-input-port |
| 505 |
(case-lambda |
| 506 |
[(bv) (open-bytevector-input-port bv #f)] |
| 507 |
[(bv maybe-transcoder) |
| 508 |
(unless (bytevector? bv) |
| 509 |
(die 'open-bytevector-input-port |
| 510 |
"not a bytevector" bv)) |
| 511 |
(when (and maybe-transcoder |
| 512 |
(not (transcoder? maybe-transcoder))) |
| 513 |
(die 'open-bytevector-input-port |
| 514 |
"not a transcoder" maybe-transcoder)) |
| 515 |
($make-port |
| 516 |
(input-transcoder-attrs maybe-transcoder |
| 517 |
'open-bytevector-output-port) |
| 518 |
0 (bytevector-length bv) bv |
| 519 |
maybe-transcoder |
| 520 |
"*bytevector-input-port*" |
| 521 |
all-data-in-buffer ;;; read! |
| 522 |
#f ;;; write! |
| 523 |
#t ;;; get-position |
| 524 |
#t ;;; set-position! |
| 525 |
#f ;;; close |
| 526 |
(default-cookie #f))])) |
| 527 |
|
| 528 |
(define open-bytevector-output-port |
| 529 |
(case-lambda |
| 530 |
[() (open-bytevector-output-port #f)] |
| 531 |
[(transcoder) |
| 532 |
(define who 'open-bytevector-output-port) |
| 533 |
(unless (or (not transcoder) (transcoder? transcoder)) |
| 534 |
(die who "invalid transcoder value" transcoder)) |
| 535 |
(let ([buf* '()] [buffer-size 256]) |
| 536 |
(let ([p |
| 537 |
($make-port |
| 538 |
(output-transcoder-attrs transcoder |
| 539 |
'open-bytevector-output-port) |
| 540 |
0 buffer-size (make-bytevector buffer-size) |
| 541 |
transcoder |
| 542 |
"*bytevector-output-port*" |
| 543 |
#f |
| 544 |
(lambda (bv i c) |
| 545 |
(unless (= c 0) |
| 546 |
(let ([x (make-bytevector c)]) |
| 547 |
(bytevector-copy! bv i x 0 c) |
| 548 |
(set! buf* (cons x buf*)))) |
| 549 |
c) |
| 550 |
#t ;;; get-position |
| 551 |
#f ;;; set-position! |
| 552 |
#f ;;; close |
| 553 |
(default-cookie #f))]) |
| 554 |
(values |
| 555 |
p |
| 556 |
(lambda () |
| 557 |
(define (append-bv-buf* ls) |
| 558 |
(let f ([ls ls] [i 0]) |
| 559 |
(cond |
| 560 |
[(null? ls) |
| 561 |
(values (make-bytevector i) 0)] |
| 562 |
[else |
| 563 |
(let* ([a (car ls)] |
| 564 |
[n (bytevector-length a)]) |
| 565 |
(let-values ([(bv i) (f (cdr ls) (fx+ i n))]) |
| 566 |
(bytevector-copy! a 0 bv i n) |
| 567 |
(values bv (fx+ i n))))]))) |
| 568 |
(unless ($port-closed? p) |
| 569 |
(flush-output-port p)) |
| 570 |
(let-values ([(bv len) (append-bv-buf* buf*)]) |
| 571 |
(set! buf* '()) |
| 572 |
bv)))))])) |
| 573 |
|
| 574 |
(define call-with-bytevector-output-port |
| 575 |
(case-lambda |
| 576 |
[(proc) (call-with-bytevector-output-port proc #f)] |
| 577 |
[(proc transcoder) |
| 578 |
(define who 'call-with-bytevector-output-port) |
| 579 |
(unless (procedure? proc) |
| 580 |
(die who "not a procedure" proc)) |
| 581 |
(unless (or (not transcoder) (transcoder? transcoder)) |
| 582 |
(die who "invalid transcoder argument" transcoder)) |
| 583 |
(let-values ([(p extract) |
| 584 |
(open-bytevector-output-port transcoder)]) |
| 585 |
(proc p) |
| 586 |
(extract))])) |
| 587 |
|
| 588 |
(define (call-with-string-output-port proc) |
| 589 |
(define who 'call-with-string-output-port) |
| 590 |
(unless (procedure? proc) |
| 591 |
(die who "not a procedure" proc)) |
| 592 |
(let-values ([(p extract) (open-string-output-port)]) |
| 593 |
(proc p) |
| 594 |
(extract))) |
| 595 |
|
| 596 |
(define (with-output-to-string proc) |
| 597 |
(define who 'with-output-to-string) |
| 598 |
(unless (procedure? proc) |
| 599 |
(die who "not a procedure" proc)) |
| 600 |
(let-values ([(p extract) (open-string-output-port)]) |
| 601 |
(parameterize ([current-output-port p]) |
| 602 |
(proc)) |
| 603 |
(extract))) |
| 604 |
|
| 605 |
(define (with-output-to-port p proc) |
| 606 |
(define who 'with-output-to-port) |
| 607 |
(unless (procedure? proc) |
| 608 |
(die who "not a procedure" proc)) |
| 609 |
(unless (output-port? p) |
| 610 |
(die who "not an output port" p)) |
| 611 |
(unless (textual-port? p) |
| 612 |
(die who "not a textual port" p)) |
| 613 |
(parameterize ([current-output-port p]) |
| 614 |
(proc))) |
| 615 |
|
| 616 |
(define (open-output-string) |
| 617 |
(define who 'open-output-string) |
| 618 |
(let ([cookie (default-cookie '())] |
| 619 |
[buffer-size 256]) |
| 620 |
($make-port |
| 621 |
(fxior textual-output-port-bits fast-char-text-tag) |
| 622 |
0 buffer-size (make-string buffer-size) |
| 623 |
#t ;;; transcoder |
| 624 |
"*string-output-port*" |
| 625 |
#f |
| 626 |
(lambda (str i c) |
| 627 |
(unless (= c 0) |
| 628 |
(let ([x (make-string c)]) |
| 629 |
(string-copy! str i x 0 c) |
| 630 |
(set-cookie-dest! cookie (cons x (cookie-dest cookie))))) |
| 631 |
c) |
| 632 |
#t ;;; get-position |
| 633 |
#f ;;; set-position! |
| 634 |
#f ;;; close! |
| 635 |
cookie))) |
| 636 |
|
| 637 |
(define (open-string-output-port) |
| 638 |
(let ([p (open-output-string)]) |
| 639 |
(values |
| 640 |
p |
| 641 |
(lambda () |
| 642 |
(let ([str (get-output-string p)]) |
| 643 |
(set-cookie-dest! ($port-cookie p) '()) |
| 644 |
str))))) |
| 645 |
|
| 646 |
(define (get-output-string-cookie-data cookie) |
| 647 |
(define (append-str-buf* ls) |
| 648 |
(let f ([ls ls] [i 0]) |
| 649 |
(cond |
| 650 |
[(null? ls) |
| 651 |
(values (make-string i) 0)] |
| 652 |
[else |
| 653 |
(let* ([a (car ls)] |
| 654 |
[n (string-length a)]) |
| 655 |
(let-values ([(bv i) (f (cdr ls) (fx+ i n))]) |
| 656 |
(string-copy! a 0 bv i n) |
| 657 |
(values bv (fx+ i n))))]))) |
| 658 |
(let ([buf* (cookie-dest cookie)]) |
| 659 |
(let-values ([(bv len) (append-str-buf* buf*)]) |
| 660 |
bv))) |
| 661 |
|
| 662 |
(define (get-output-string p) |
| 663 |
(if (port? p) |
| 664 |
(let ([cookie ($port-cookie p)]) |
| 665 |
(cond |
| 666 |
[(and (cookie? cookie) |
| 667 |
(let ([x (cookie-dest cookie)]) |
| 668 |
(or (null? x) (pair? x)))) |
| 669 |
(unless ($port-closed? p) |
| 670 |
(flush-output-port p)) |
| 671 |
(get-output-string-cookie-data cookie)] |
| 672 |
[else |
| 673 |
(die 'get-output-string "not an output-string port" p)])) |
| 674 |
(die 'get-output-string "not a port" p))) |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
(define (open-string-input-port/id str id) |
| 679 |
(unless (string? str) |
| 680 |
(die 'open-string-input-port "not a string" str)) |
| 681 |
($make-port |
| 682 |
(fxior textual-input-port-bits fast-char-text-tag) |
| 683 |
0 (string-length str) str |
| 684 |
#t ;;; transcoder |
| 685 |
id |
| 686 |
all-data-in-buffer ;;; read! |
| 687 |
#f ;;; write! |
| 688 |
#t ;;; get-position |
| 689 |
#t ;;; set-position! |
| 690 |
#f ;;; close |
| 691 |
(default-cookie #f))) |
| 692 |
|
| 693 |
(define (open-string-input-port str) |
| 694 |
(open-string-input-port/id str "*string-input-port*")) |
| 695 |
|
| 696 |
(define (transcoded-port p transcoder) |
| 697 |
(define who 'transcoded-port) |
| 698 |
(unless (transcoder? transcoder) |
| 699 |
(die who "not a transcoder" transcoder)) |
| 700 |
(unless (port? p) (die who "not a port" p)) |
| 701 |
(when ($port-transcoder p) (die who "not a binary port" p)) |
| 702 |
(when ($port-closed? p) (die who "cannot transcode closed port" p)) |
| 703 |
(let ([read! ($port-read! p)] |
| 704 |
[write! ($port-write! p)]) |
| 705 |
($mark-port-closed! p) |
| 706 |
(guarded-port |
| 707 |
($make-port |
| 708 |
(cond |
| 709 |
[read! (input-transcoder-attrs transcoder |
| 710 |
'transcoded-port)] |
| 711 |
[write! (output-transcoder-attrs transcoder |
| 712 |
'transcoded-port)] |
| 713 |
[else |
| 714 |
(die 'transcoded-port |
| 715 |
"port is neither input nor output!")]) |
| 716 |
($port-index p) |
| 717 |
($port-size p) |
| 718 |
($port-buffer p) |
| 719 |
transcoder |
| 720 |
($port-id p) |
| 721 |
read! |
| 722 |
write! |
| 723 |
($port-get-position p) |
| 724 |
($port-set-position! p) |
| 725 |
($port-close p) |
| 726 |
($port-cookie p))))) |
| 727 |
|
| 728 |
(define (reset-input-port! p) |
| 729 |
(if (input-port? p) |
| 730 |
(begin |
| 731 |
($set-port-index! p ($port-size p)) |
| 732 |
(unregister-callback p)) |
| 733 |
(die 'reset-input-port! "not an input port" p))) |
| 734 |
|
| 735 |
(define (reset-output-port! p) |
| 736 |
(if (output-port? p) |
| 737 |
(begin |
| 738 |
($set-port-index! p 0) |
| 739 |
(unregister-callback p)) |
| 740 |
(die 'reset-output-port! "not an output port" p))) |
| 741 |
|
| 742 |
|
| 743 |
(define (port-transcoder p) |
| 744 |
(if (port? p) |
| 745 |
(let ([tr ($port-transcoder p)]) |
| 746 |
(and (transcoder? tr) tr)) |
| 747 |
(die 'port-transcoder "not a port" p))) |
| 748 |
|
| 749 |
(define ($port-closed? p) |
| 750 |
(import UNSAFE) |
| 751 |
(not (fx= (fxand ($port-attrs p) closed-port-tag) 0))) |
| 752 |
|
| 753 |
(define (port-closed? p) |
| 754 |
(if (port? p) |
| 755 |
($port-closed? p) |
| 756 |
(error 'port-closed? "not a port" p))) |
| 757 |
|
| 758 |
(define ($mark-port-closed! p) |
| 759 |
($set-port-attrs! p |
| 760 |
(fxior closed-port-tag |
| 761 |
(fxand ($port-attrs p) port-type-mask)))) |
| 762 |
|
| 763 |
(define (port-mode p) |
| 764 |
(if (port? p) |
| 765 |
(cookie-mode ($port-cookie p)) |
| 766 |
(die 'port-mode "not a port" p))) |
| 767 |
|
| 768 |
(define (set-port-mode! p mode) |
| 769 |
(if (port? p) |
| 770 |
(case mode |
| 771 |
[(r6rs-mode ikarus-mode) |
| 772 |
(set-cookie-mode! ($port-cookie p) mode)] |
| 773 |
[else (die 'set-port-mode! "invalid mode" mode)]) |
| 774 |
(die 'set-port-mode! "not a port" p))) |
| 775 |
|
| 776 |
(define (put-byte/unbuffered! p b who) |
| 777 |
(when ($port-closed? p) (die who "port is closed" p)) |
| 778 |
(let ([bv (make-bytevector 1)]) |
| 779 |
(bytevector-u8-set! bv 0 b) |
| 780 |
(let ([bytes (($port-write! p) bv 0 1)]) |
| 781 |
(cond |
| 782 |
[(eq? bytes 1) |
| 783 |
(let ([cookie ($port-cookie p)]) |
| 784 |
(set-cookie-pos! cookie (+ (cookie-pos cookie) 1)))] |
| 785 |
[(eq? bytes 0) |
| 786 |
($mark-port-closed! p) |
| 787 |
(die who "could not write bytes to sink")] |
| 788 |
[else |
| 789 |
(die who "invalid return value from write! proc" bytes p)])))) |
| 790 |
|
| 791 |
(define (put-char/unbuffered! p c who) |
| 792 |
(when ($port-closed? p) (die who "port is closed" p)) |
| 793 |
(let ([str (string c)]) |
| 794 |
(let ([bytes (($port-write! p) str 0 1)]) |
| 795 |
(cond |
| 796 |
[(eq? bytes 1) |
| 797 |
(let ([cookie ($port-cookie p)]) |
| 798 |
(set-cookie-pos! cookie (+ (cookie-pos cookie) 1)))] |
| 799 |
[(eq? bytes 0) |
| 800 |
($mark-port-closed! p) |
| 801 |
(die who "could not write char to sink")] |
| 802 |
[else |
| 803 |
(die who "invalid return value from write! proc" bytes p)])))) |
| 804 |
|
| 805 |
(define flush-output-port |
| 806 |
(case-lambda |
| 807 |
[() (flush-output-port (current-output-port))] |
| 808 |
[(p) |
| 809 |
(import UNSAFE) |
| 810 |
(unless (output-port? p) |
| 811 |
(die 'flush-output-port "not an output port" p)) |
| 812 |
(when ($port-closed? p) |
| 813 |
(die 'flush-output-port "port is closed" p)) |
| 814 |
(let ([idx ($port-index p)] |
| 815 |
[buf ($port-buffer p)]) |
| 816 |
(unless (fx= idx 0) |
| 817 |
(let ([bytes (($port-write! p) buf 0 idx)]) |
| 818 |
(unless (and (fixnum? bytes) (fx>= bytes 0) (fx<= bytes idx)) |
| 819 |
(die 'flush-output-port |
| 820 |
"write! returned an invalid value" |
| 821 |
bytes)) |
| 822 |
(let ([cookie ($port-cookie p)]) |
| 823 |
(set-cookie-pos! cookie (+ (cookie-pos cookie) bytes))) |
| 824 |
(cond |
| 825 |
[(fx= bytes idx) |
| 826 |
($set-port-index! p 0)] |
| 827 |
[(fx= bytes 0) |
| 828 |
($mark-port-closed! p) |
| 829 |
(die 'flush-output-port "could not write bytes to sink")] |
| 830 |
[else |
| 831 |
(bytevector-copy! buf bytes buf 0 (fx- idx bytes)) |
| 832 |
($set-port-index! p (fx- idx bytes)) |
| 833 |
(flush-output-port p)]))))])) |
| 834 |
|
| 835 |
(define ($close-port p) |
| 836 |
(cond |
| 837 |
[($port-closed? p) (void)] |
| 838 |
[else |
| 839 |
(when ($port-write! p) |
| 840 |
(flush-output-port p)) |
| 841 |
($mark-port-closed! p) |
| 842 |
(let ([close ($port-close p)]) |
| 843 |
(when (procedure? close) |
| 844 |
(close)))])) |
| 845 |
|
| 846 |
(define (close-port p) |
| 847 |
(unless (port? p) |
| 848 |
(die 'close-port "not a port" p)) |
| 849 |
($close-port p)) |
| 850 |
|
| 851 |
(define (close-input-port p) |
| 852 |
(unless (input-port? p) |
| 853 |
(die 'close-input-port "not an input port" p)) |
| 854 |
($close-port p)) |
| 855 |
|
| 856 |
(define (close-output-port p) |
| 857 |
(unless (output-port? p) |
| 858 |
(die 'close-output-port "not an output port" p)) |
| 859 |
($close-port p)) |
| 860 |
|
| 861 |
(define (refill-bv-buffer p who) |
| 862 |
(when ($port-closed? p) (die who "port is closed" p)) |
| 863 |
(let ([read! ($port-read! p)]) |
| 864 |
(if (eq? read! all-data-in-buffer) |
| 865 |
0 |
| 866 |
(let ([bv ($port-buffer p)] [i ($port-index p)] [j ($port-size p)]) |
| 867 |
(let ([c0 (fx- j i)]) |
| 868 |
(unless (fx= c0 0) (bytevector-copy! bv i bv 0 c0)) |
| 869 |
(let ([cookie ($port-cookie p)]) |
| 870 |
(set-cookie-pos! cookie (+ (cookie-pos cookie) i))) |
| 871 |
(let* ([max (fx- (bytevector-length bv) c0)] |
| 872 |
[c1 (read! bv c0 max)]) |
| 873 |
(unless (fixnum? c1) |
| 874 |
(die who "invalid return value from read! procedure" c1)) |
| 875 |
(cond |
| 876 |
[(fx>= c1 0) |
| 877 |
(unless (fx<= c1 max) |
| 878 |
(die who "read! returned a value out of range" c1)) |
| 879 |
($set-port-index! p 0) |
| 880 |
($set-port-size! p (fx+ c1 c0)) |
| 881 |
c1] |
| 882 |
[else |
| 883 |
(die who "read! returned a value out of range" c1)]))))))) |
| 884 |
|
| 885 |
;;; ---------------------------------------------------------- |
| 886 |
(module (read-char get-char lookahead-char) |
| 887 |
(import UNSAFE) |
| 888 |
(define (get-char-latin-mode p who inc) |
| 889 |
(let ([n (refill-bv-buffer p who)]) |
| 890 |
(cond |
| 891 |
[(fx= n 0) (eof-object)] |
| 892 |
[else |
| 893 |
(let ([idx ($port-index p)]) |
| 894 |
($set-port-index! p (fx+ idx inc)) |
| 895 |
(integer->char (bytevector-u8-ref ($port-buffer p) idx)))]))) |
| 896 |
(define (get-char-utf8-mode p who) |
| 897 |
(define (do-error p who) |
| 898 |
(case (transcoder-error-handling-mode ($port-transcoder p)) |
| 899 |
[(ignore) (get-char p)] |
| 900 |
[(replace) #\xFFFD] |
| 901 |
[(raise) |
| 902 |
(raise (make-i/o-decoding-error p))] |
| 903 |
[else (die who "cannot happen")])) |
| 904 |
(let ([i ($port-index p)] |
| 905 |
[j ($port-size p)] |
| 906 |
[buf ($port-buffer p)]) |
| 907 |
(cond |
| 908 |
[(fx= i j) ;;; exhausted |
| 909 |
(let ([bytes (refill-bv-buffer p who)]) |
| 910 |
(cond |
| 911 |
[(fx= bytes 0) (eof-object)] |
| 912 |
[else (get-char p)]))] |
| 913 |
[else |
| 914 |
(let ([b0 (bytevector-u8-ref buf i)]) |
| 915 |
(cond |
| 916 |
[(fx= (fxsra b0 5) #b110) ;;; two-byte-encoding |
| 917 |
(let ([i (fx+ i 1)]) |
| 918 |
(cond |
| 919 |
[(fx< i j) |
| 920 |
(let ([b1 (bytevector-u8-ref buf i)]) |
| 921 |
(cond |
| 922 |
[(fx= (fxsra b1 6) #b10) |
| 923 |
($set-port-index! p (fx+ i 1)) |
| 924 |
(integer->char |
| 925 |
(fxior (fxand b1 #b111111) |
| 926 |
(fxsll (fxand b0 #b11111) 6)))] |
| 927 |
[else |
| 928 |
($set-port-index! p i) |
| 929 |
(do-error p who)]))] |
| 930 |
[else |
| 931 |
(let ([bytes (refill-bv-buffer p who)]) |
| 932 |
(cond |
| 933 |
[(fx= bytes 0) |
| 934 |
($set-port-index! p (fx+ ($port-index p) 1)) |
| 935 |
(do-error p who)] |
| 936 |
[else (get-char-utf8-mode p who)]))]))] |
| 937 |
[(fx= (fxsra b0 4) #b1110) ;;; three-byte-encoding |
| 938 |
(cond |
| 939 |
[(fx< (fx+ i 2) j) |
| 940 |
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))] |
| 941 |
[b2 (bytevector-u8-ref buf (fx+ i 2))]) |
| 942 |
(cond |
| 943 |
[(fx= (fxsra (fxlogor b1 b2) 6) #b10) |
| 944 |
(let ([n (fxlogor |
| 945 |
(fxsll (fxand b0 #b1111) 12) |
| 946 |
(fxsll (fxand b1 #b111111) 6) |
| 947 |
(fxand b2 #b111111))]) |
| 948 |
(cond |
| 949 |
[(and (fx<= #xD800 n) (fx<= n #xDFFF)) |
| 950 |
($set-port-index! p (fx+ i 1)) |
| 951 |
(do-error p who)] |
| 952 |
[else |
| 953 |
($set-port-index! p (fx+ i 3)) |
| 954 |
(integer->char n)]))] |
| 955 |
[else |
| 956 |
($set-port-index! p (fx+ i 1)) |
| 957 |
(do-error p who)]))] |
| 958 |
[else |
| 959 |
(let ([bytes (refill-bv-buffer p who)]) |
| 960 |
(cond |
| 961 |
[(fx= bytes 0) |
| 962 |
($set-port-index! p (fx+ ($port-index p) 1)) |
| 963 |
(do-error p who)] |
| 964 |
[else (get-char-utf8-mode p who)]))])] |
| 965 |
[(fx= (fxsra b0 3) #b11110) ;;; four-byte-encoding |
| 966 |
(cond |
| 967 |
[(fx< (fx+ i 3) j) |
| 968 |
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))] |
| 969 |
[b2 (bytevector-u8-ref buf (fx+ i 2))] |
| 970 |
[b3 (bytevector-u8-ref buf (fx+ i 3))]) |
| 971 |
(cond |
| 972 |
[(fx= (fxsra (fxlogor b1 b2 b3) 6) #b10) |
| 973 |
(let ([n (fxlogor |
| 974 |
(fxsll (fxand b0 #b111) 18) |
| 975 |
(fxsll (fxand b1 #b111111) 12) |
| 976 |
(fxsll (fxand b2 #b111111) 6) |
| 977 |
(fxand b3 #b111111))]) |
| 978 |
(cond |
| 979 |
[(and (fx<= #x10000 n) (fx<= n #x10FFFF)) |
| 980 |
($set-port-index! p (fx+ i 4)) |
| 981 |
(integer->char n)] |
| 982 |
[else |
| 983 |
($set-port-index! p (fx+ i 1)) |
| 984 |
(do-error p who)]))] |
| 985 |
[else |
| 986 |
($set-port-index! p (fx+ i 1)) |
| 987 |
(do-error p who)]))] |
| 988 |
[else |
| 989 |
(let ([bytes (refill-bv-buffer p who)]) |
| 990 |
(cond |
| 991 |
[(fx= bytes 0) |
| 992 |
($set-port-index! p (fx+ ($port-index p) 1)) |
| 993 |
(do-error p who)] |
| 994 |
[else (get-char-utf8-mode p who)]))])] |
| 995 |
[else |
| 996 |
($set-port-index! p (fx+ i 1)) |
| 997 |
(do-error p who)]))]))) |
| 998 |
|
| 999 |
(define (lookahead-char-utf8-mode p who) |
| 1000 |
(define (do-error p who) |
| 1001 |
(case (transcoder-error-handling-mode ($port-transcoder p)) |
| 1002 |
[(ignore) (lookahead-char p)] |
| 1003 |
[(replace) #\xFFFD] |
| 1004 |
[(raise) |
| 1005 |
(raise (make-i/o-decoding-error p))] |
| 1006 |
[else (die who "cannot happen")])) |
| 1007 |
(let ([i ($port-index p)] |
| 1008 |
[j ($port-size p)] |
| 1009 |
[buf ($port-buffer p)]) |
| 1010 |
(cond |
| 1011 |
[(fx= i j) ;;; exhausted |
| 1012 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1013 |
(cond |
| 1014 |
[(fx= bytes 0) (eof-object)] |
| 1015 |
[else (lookahead-char p)]))] |
| 1016 |
[else |
| 1017 |
(let ([b0 (bytevector-u8-ref buf i)]) |
| 1018 |
(cond |
| 1019 |
[(fx= (fxsra b0 5) #b110) ;;; two-byte-encoding |
| 1020 |
(let ([i (fx+ i 1)]) |
| 1021 |
(cond |
| 1022 |
[(fx< i j) |
| 1023 |
(let ([b1 (bytevector-u8-ref buf i)]) |
| 1024 |
(cond |
| 1025 |
[(fx= (fxsra b1 6) #b10) |
| 1026 |
(integer->char |
| 1027 |
(fxior (fxand b1 #b111111) |
| 1028 |
(fxsll (fxand b0 #b11111) 6)))] |
| 1029 |
[else |
| 1030 |
(do-error p who)]))] |
| 1031 |
[else |
| 1032 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1033 |
(cond |
| 1034 |
[(fx= bytes 0) (do-error p who)] |
| 1035 |
[else (lookahead-char-utf8-mode p who)]))]))] |
| 1036 |
[(fx= (fxsra b0 4) #b1110) ;;; three-byte-encoding |
| 1037 |
(cond |
| 1038 |
[(fx< (fx+ i 2) j) |
| 1039 |
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))] |
| 1040 |
[b2 (bytevector-u8-ref buf (fx+ i 2))]) |
| 1041 |
(cond |
| 1042 |
[(fx= (fxsra (fxlogor b1 b2) 6) #b10) |
| 1043 |
(let ([n (fxlogor |
| 1044 |
(fxsll (fxand b0 #b1111) 12) |
| 1045 |
(fxsll (fxand b1 #b111111) 6) |
| 1046 |
(fxand b2 #b111111))]) |
| 1047 |
(cond |
| 1048 |
[(and (fx<= #xD800 n) (fx<= n #xDFFF)) |
| 1049 |
(do-error p who)] |
| 1050 |
[else (integer->char n)]))] |
| 1051 |
[else (do-error p who)]))] |
| 1052 |
[else |
| 1053 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1054 |
(cond |
| 1055 |
[(fx= bytes 0) (do-error p who)] |
| 1056 |
[else (lookahead-char-utf8-mode p who)]))])] |
| 1057 |
[(fx= (fxsra b0 3) #b11110) ;;; four-byte-encoding |
| 1058 |
(cond |
| 1059 |
[(fx< (fx+ i 3) j) |
| 1060 |
(let ([b1 (bytevector-u8-ref buf (fx+ i 1))] |
| 1061 |
[b2 (bytevector-u8-ref buf (fx+ i 2))] |
| 1062 |
[b3 (bytevector-u8-ref buf (fx+ i 3))]) |
| 1063 |
(cond |
| 1064 |
[(fx= (fxsra (fxlogor b1 b2 b3) 6) #b10) |
| 1065 |
(let ([n (fxlogor |
| 1066 |
(fxsll (fxand b0 #b111) 18) |
| 1067 |
(fxsll (fxand b1 #b111111) 12) |
| 1068 |
(fxsll (fxand b2 #b111111) 6) |
| 1069 |
(fxand b3 #b111111))]) |
| 1070 |
(cond |
| 1071 |
[(and (fx<= #x10000 n) (fx<= n #x10FFFF)) |
| 1072 |
(integer->char n)] |
| 1073 |
[else |
| 1074 |
(do-error p who)]))] |
| 1075 |
[else |
| 1076 |
(do-error p who)]))] |
| 1077 |
[else |
| 1078 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1079 |
(cond |
| 1080 |
[(fx= bytes 0) |
| 1081 |
(do-error p who)] |
| 1082 |
[else (lookahead-char-utf8-mode p who)]))])] |
| 1083 |
[else (do-error p who)]))]))) |
| 1084 |
;;; |
| 1085 |
(define (advance-bom p who bom-seq) |
| 1086 |
;;; return eof if port is eof, |
| 1087 |
;;; #t if a bom is present, updating the port index to |
| 1088 |
;;; point just past the bom. |
| 1089 |
;;; #f otherwise. |
| 1090 |
(cond |
| 1091 |
[(fx< ($port-index p) ($port-size p)) |
| 1092 |
(let f ([i 0] [ls bom-seq]) |
| 1093 |
(cond |
| 1094 |
[(null? ls) |
| 1095 |
($set-port-index! p (fx+ ($port-index p) i)) |
| 1096 |
#t] |
| 1097 |
[else |
| 1098 |
(let ([idx (fx+ i ($port-index p))]) |
| 1099 |
(cond |
| 1100 |
[(fx< idx ($port-size p)) |
| 1101 |
(if (fx=? (car ls) |
| 1102 |
(bytevector-u8-ref ($port-buffer p) idx)) |
| 1103 |
(f (fx+ i 1) (cdr ls)) |
| 1104 |
#f)] |
| 1105 |
[else |
| 1106 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1107 |
(if (fx= bytes 0) |
| 1108 |
#f |
| 1109 |
(f i ls)))]))]))] |
| 1110 |
[else |
| 1111 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1112 |
(if (fx= bytes 0) |
| 1113 |
(eof-object) |
| 1114 |
(advance-bom p who bom-seq)))])) |
| 1115 |
;;; |
| 1116 |
(define (speedup-input-port p who) |
| 1117 |
;;; returns #t if port is eof, #f otherwise |
| 1118 |
(unless (input-port? p) |
| 1119 |
(die who "not an input port" p)) |
| 1120 |
(when ($port-closed? p) |
| 1121 |
(die who "port is closed" p)) |
| 1122 |
(let ([tr ($port-transcoder p)]) |
| 1123 |
(unless tr |
| 1124 |
(die who "not a textual port" p)) |
| 1125 |
(case (transcoder-codec tr) |
| 1126 |
[(utf-8-codec) |
| 1127 |
($set-port-attrs! p |
| 1128 |
(fxior textual-input-port-bits fast-u7-text-tag)) |
| 1129 |
(eof-object? (advance-bom p who '(#xEF #xBB #xBF)))] |
| 1130 |
[(utf-16-codec) |
| 1131 |
(let ([be? (advance-bom p who '(#xFE #xFF))]) |
| 1132 |
(case be? |
| 1133 |
[(#t) |
| 1134 |
($set-port-attrs! p |
| 1135 |
(fxior textual-input-port-bits fast-u16be-text-tag)) |
| 1136 |
#f] |
| 1137 |
[(#f) |
| 1138 |
(let ([le? (advance-bom p who '(#xFF #xFE))]) |
| 1139 |
(case le? |
| 1140 |
[(#t #f) ;;; little by default |
| 1141 |
($set-port-attrs! p |
| 1142 |
(fxior textual-input-port-bits fast-u16le-text-tag)) |
| 1143 |
#f] |
| 1144 |
[else #t]))] |
| 1145 |
[else #t]))] |
| 1146 |
[else |
| 1147 |
(die who "BUG: codec not handled" (transcoder-codec tr))]))) |
| 1148 |
;;; |
| 1149 |
(define (lookahead-char-char-mode p who) |
| 1150 |
(let ([str ($port-buffer p)] |
| 1151 |
[read! ($port-read! p)]) |
| 1152 |
(if (eq? read! all-data-in-buffer) |
| 1153 |
(eof-object) |
| 1154 |
(let ([n (read! str 0 (string-length str))]) |
| 1155 |
(unless (fixnum? n) |
| 1156 |
(die who "invalid return value from read!" n)) |
| 1157 |
(unless (<= 0 n (string-length str)) |
| 1158 |
(die who "return value from read! is out of range" n)) |
| 1159 |
(let ([idx ($port-index p)] [cookie ($port-cookie p)]) |
| 1160 |
(set-cookie-pos! cookie (+ idx (cookie-pos cookie)))) |
| 1161 |
($set-port-index! p 0) |
| 1162 |
($set-port-size! p n) |
| 1163 |
(cond |
| 1164 |
[(fx= n 0) |
| 1165 |
(eof-object)] |
| 1166 |
[else |
| 1167 |
(string-ref str 0)]))))) |
| 1168 |
;;; |
| 1169 |
(define (lookahead-char p) |
| 1170 |
(define who 'lookahead-char) |
| 1171 |
(let ([m ($port-fast-attrs p)]) |
| 1172 |
(cond |
| 1173 |
[(eq? m fast-get-utf8-tag) |
| 1174 |
(let ([i ($port-index p)]) |
| 1175 |
(cond |
| 1176 |
[(fx< i ($port-size p)) |
| 1177 |
(let ([b (bytevector-u8-ref ($port-buffer p) i)]) |
| 1178 |
(cond |
| 1179 |
[(fx< b 128) (integer->char b)] |
| 1180 |
[else (lookahead-char-utf8-mode p who)]))] |
| 1181 |
[else |
| 1182 |
(lookahead-char-utf8-mode p who)]))] |
| 1183 |
[(eq? m fast-get-char-tag) |
| 1184 |
(let ([i ($port-index p)]) |
| 1185 |
(cond |
| 1186 |
[(fx< i ($port-size p)) |
| 1187 |
(string-ref ($port-buffer p) i)] |
| 1188 |
[else |
| 1189 |
(lookahead-char-char-mode p who)]))] |
| 1190 |
[(eq? m fast-get-latin-tag) |
| 1191 |
(let ([i ($port-index p)]) |
| 1192 |
(cond |
| 1193 |
[(fx< i ($port-size p)) |
| 1194 |
(integer->char |
| 1195 |
(bytevector-u8-ref ($port-buffer p) i))] |
| 1196 |
[else |
| 1197 |
(get-char-latin-mode p who 0)]))] |
| 1198 |
[(eq? m fast-get-utf16le-tag) (peek-utf16 p who 'little)] |
| 1199 |
[(eq? m fast-get-utf16be-tag) (peek-utf16 p who 'big)] |
| 1200 |
[else |
| 1201 |
(if (speedup-input-port p who) |
| 1202 |
(eof-object) |
| 1203 |
(lookahead-char p))]))) |
| 1204 |
;;; |
| 1205 |
(define (get-char-char-mode p who) |
| 1206 |
(let ([str ($port-buffer p)] |
| 1207 |
[read! ($port-read! p)]) |
| 1208 |
(if (eq? read! all-data-in-buffer) |
| 1209 |
(eof-object) |
| 1210 |
(let ([n (read! str 0 (string-length str))]) |
| 1211 |
(unless (fixnum? n) |
| 1212 |
(die who "invalid return value from read!" n)) |
| 1213 |
(unless (<= 0 n (string-length str)) |
| 1214 |
(die who "return value from read! is out of range" n)) |
| 1215 |
(let ([idx ($port-index p)] [cookie ($port-cookie p)]) |
| 1216 |
(set-cookie-pos! cookie (+ idx (cookie-pos cookie)))) |
| 1217 |
($set-port-size! p n) |
| 1218 |
(cond |
| 1219 |
[(fx= n 0) |
| 1220 |
($set-port-index! p 0) |
| 1221 |
(eof-object)] |
| 1222 |
[else |
| 1223 |
($set-port-index! p 1) |
| 1224 |
(string-ref str 0)]))))) |
| 1225 |
(define (peek-utf16 p who endianness) |
| 1226 |
(define integer->char/invalid |
| 1227 |
(lambda (n) |
| 1228 |
(cond |
| 1229 |
[(fx<= n #xD7FF) (integer->char n)] |
| 1230 |
[(fx< n #xE000) #\xFFFD] |
| 1231 |
[(fx<= n #x10FFFF) (integer->char n)] |
| 1232 |
[else #\xFFFD]))) |
| 1233 |
(let ([i ($port-index p)]) |
| 1234 |
(cond |
| 1235 |
[(fx<= (fx+ i 2) ($port-size p)) |
| 1236 |
(let ([w1 (bytevector-u16-ref ($port-buffer p) i endianness)]) |
| 1237 |
(cond |
| 1238 |
[(or (fx< w1 #xD800) (fx> w1 #xDFFF)) |
| 1239 |
(integer->char/invalid w1)] |
| 1240 |
[(not (and (fx<= #xD800 w1) (fx<= w1 #xDBFF))) |
| 1241 |
#\xFFFD] |
| 1242 |
[(fx<= (+ i 4) ($port-size p)) |
| 1243 |
(let ([w2 (bytevector-u16-ref |
| 1244 |
($port-buffer p) (+ i 2) endianness)]) |
| 1245 |
(cond |
| 1246 |
[(not (and (fx<= #xDC00 w2) (fx<= w2 #xDFFF))) |
| 1247 |
#\xFFFD] |
| 1248 |
[else |
| 1249 |
(integer->char/invalid |
| 1250 |
(fx+ #x10000 |
| 1251 |
(fxlogor |
| 1252 |
(fxsll (fxand w1 #x3FF) 10) |
| 1253 |
(fxand w2 #x3FF))))]))] |
| 1254 |
[else |
| 1255 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1256 |
(cond |
| 1257 |
[(fx= bytes 0) |
| 1258 |
#\xFFFD] |
| 1259 |
[else |
| 1260 |
(peek-utf16 p who endianness)]))]))] |
| 1261 |
[(fx< i ($port-size p)) |
| 1262 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1263 |
(cond |
| 1264 |
[(fx= bytes 0) |
| 1265 |
#\xFFFD] |
| 1266 |
[else (peek-utf16 p who endianness)]))] |
| 1267 |
[else |
| 1268 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1269 |
(if (fx= bytes 0) |
| 1270 |
(eof-object) |
| 1271 |
(peek-utf16 p who endianness)))]))) |
| 1272 |
(define (get-utf16 p who endianness) |
| 1273 |
(define (invalid p who endianness n) |
| 1274 |
(case (transcoder-error-handling-mode (port-transcoder p)) |
| 1275 |
[(ignore) (do-get-char p who endianness)] |
| 1276 |
[(replace) #\xFFFD] |
| 1277 |
[(raise) |
| 1278 |
(raise (make-i/o-decoding-error p n))] |
| 1279 |
[else (die who "BUG: invalid error handling mode" p)])) |
| 1280 |
(define (integer->char/invalid p who endianness n) |
| 1281 |
(cond |
| 1282 |
[(fx<= n #xD7FF) (integer->char n)] |
| 1283 |
[(fx< n #xE000) (invalid p who endianness n)] |
| 1284 |
[(fx<= n #x10FFFF) (integer->char n)] |
| 1285 |
[else (invalid p who endianness n)])) |
| 1286 |
(let ([i ($port-index p)]) |
| 1287 |
(cond |
| 1288 |
[(fx<= (fx+ i 2) ($port-size p)) |
| 1289 |
(let ([w1 (bytevector-u16-ref ($port-buffer p) i endianness)]) |
| 1290 |
(cond |
| 1291 |
[(or (fx< w1 #xD800) (fx> w1 #xDFFF)) |
| 1292 |
($set-port-index! p (fx+ i 2)) |
| 1293 |
(integer->char/invalid p who endianness w1)] |
| 1294 |
[(not (and (fx<= #xD800 w1) (fx<= w1 #xDBFF))) |
| 1295 |
($set-port-index! p (fx+ i 2)) |
| 1296 |
(invalid p who endianness w1)] |
| 1297 |
[(fx<= (+ i 4) ($port-size p)) |
| 1298 |
(let ([w2 (bytevector-u16-ref |
| 1299 |
($port-buffer p) (+ i 2) endianness)]) |
| 1300 |
(cond |
| 1301 |
[(not (and (fx<= #xDC00 w2) (fx<= w2 #xDFFF))) |
| 1302 |
($set-port-index! p (fx+ i 2)) |
| 1303 |
(invalid p who endianness w1)] |
| 1304 |
[else |
| 1305 |
($set-port-index! p (fx+ i 4)) |
| 1306 |
(integer->char/invalid p who endianness |
| 1307 |
(fx+ #x10000 |
| 1308 |
(fxlogor |
| 1309 |
(fxsll (fxand w1 #x3FF) 10) |
| 1310 |
(fxand w2 #x3FF))))]))] |
| 1311 |
[else |
| 1312 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1313 |
(cond |
| 1314 |
[(fx= bytes 0) |
| 1315 |
($set-port-index! p ($port-size p)) |
| 1316 |
(invalid p who endianness w1)] |
| 1317 |
[else |
| 1318 |
(get-utf16 p who endianness)]))]))] |
| 1319 |
[(fx< i ($port-size p)) |
| 1320 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1321 |
(cond |
| 1322 |
[(fx= bytes 0) |
| 1323 |
($set-port-index! p ($port-size p)) |
| 1324 |
(invalid p who endianness |
| 1325 |
(bytevector-u8-ref ($port-buffer p) ($port-index p)))] |
| 1326 |
[else (get-utf16 p who endianness)]))] |
| 1327 |
[else |
| 1328 |
(let ([bytes (refill-bv-buffer p who)]) |
| 1329 |
(if (fx= bytes 0) |
| 1330 |
(eof-object) |
| 1331 |
(get-utf16 p who endianness)))]))) |
| 1332 |
(define (get-char p) |
| 1333 |
(do-get-char p 'get-char)) |
| 1334 |
(define read-char |
| 1335 |
(case-lambda |
| 1336 |
[(p) (do-get-char p 'read-char)] |
| 1337 |
[() (do-get-char (current-input-port) 'read-char)])) |
| 1338 |
(define (do-get-char p who) |
| 1339 |
(let ([m ($port-fast-attrs p)]) |
| 1340 |
(cond |
| 1341 |
[(eq? m fast-get-utf8-tag) |
| 1342 |
(let ([i ($port-index p)]) |
| 1343 |
(cond |
| 1344 |
[(fx< i ($port-size p)) |
| 1345 |
(let ([b (bytevector-u8-ref ($port-buffer p) i)]) |
| 1346 |
(cond |
| 1347 |
[(fx< b 128) |
| 1348 |
($set-port-index! p (fx+ i 1)) |
| 1349 |
(if (eqv? b (char->integer #\newline)) |
| 1350 |
(mark/return-newline p) |
| 1351 |
(integer->char b))] |
| 1352 |
[else (get-char-utf8-mode p who)]))] |
| 1353 |
[else |
| 1354 |
(get-char-utf8-mode p who)]))] |
| 1355 |
[(eq? m fast-get-char-tag) |
| 1356 |
(let ([i ($port-index p)]) |
| 1357 |
(cond |
| 1358 |
[(fx< i ($port-size p)) |
| 1359 |
($set-port-index! p (fx+ i 1)) |
| 1360 |
(let ([c (string-ref ($port-buffer p) i)]) |
| 1361 |
(if (eqv? c #\newline) |
| 1362 |
(mark/return-newline p) |
| 1363 |
c))] |
| 1364 |
[else (get-char-char-mode p who)]))] |
| 1365 |
[(eq? m fast-get-latin-tag) |
| 1366 |
(let ([i ($port-index p)]) |
| 1367 |
(cond |
| 1368 |
[(fx< i ($port-size p)) |
| 1369 |
($set-port-index! p (fx+ i 1)) |
| 1370 |
(let ([b (bytevector-u8-ref ($port-buffer p) i)]) |
| 1371 |
(if (eqv? b (char->integer #\newline)) |
| 1372 |
(mark/return-newline p) |
| 1373 |
(integer->char b)))] |
| 1374 |
[else |
| 1375 |
(get-char-latin-mode p who 1)]))] |
| 1376 |
[(eq? m fast-get-utf16le-tag) (get-utf16 p who 'little)] |
| 1377 |
[(eq? m fast-get-utf16be-tag) (get-utf16 p who 'big)] |
| 1378 |
[else |
| 1379 |
(if (speedup-input-port p who) |
| 1380 |
(eof-object) |
| 1381 |
(do-get-char p who))])))) |
| 1382 |
|
| 1383 |
|
| 1384 |
;;; ---------------------------------------------------------- |
| 1385 |
(define (assert-binary-input-port p who) |
| 1386 |
(unless (port? p) (die who "not a port" p)) |
| 1387 |
(when ($port-closed? p) (die who "port is closed" p)) |
| 1388 |
(when ($port-transcoder p) (die who "port is not binary" p)) |
| 1389 |
(unless ($port-read! p) |
| 1390 |
(die who "port is not an input port" p))) |
| 1391 |
|
| 1392 |
(module (get-u8 lookahead-u8) |
| 1393 |
(import UNSAFE) |
| 1394 |
(define (get-u8-byte-mode p who start) |
| 1395 |
(when ($port-closed? p) (die who "port is closed" p)) |
| 1396 |
(let ([cnt (refill-bv-buffer p who)]) |
| 1397 |
(cond |
| 1398 |
[(eqv? cnt 0) (eof-object)] |
| 1399 |
[else |
| 1400 |
($set-port-index! p start) |
| 1401 |
(bytevector-u8-ref ($port-buffer p) 0)]))) |
| 1402 |
(define (slow-get-u8 p who start) |
| 1403 |
(assert-binary-input-port p who) |
| 1404 |
($set-port-attrs! p fast-get-byte-tag) |
| 1405 |
(get-u8-byte-mode p who start)) |
| 1406 |
;;; |
| 1407 |
(define (get-u8 p) |
| 1408 |
(define who 'get-u8) |
| 1409 |
(let ([m ($port-fast-attrs p)]) |
| 1410 |
(cond |
| 1411 |
[(eq? m fast-get-byte-tag) |
| 1412 |
(let ([i ($port-index p)]) |
| 1413 |
(cond |
| 1414 |
[(fx< i ($port-size p)) |
| 1415 |
($set-port-index! p (fx+ i 1)) |
| 1416 |
(bytevector-u8-ref ($port-buffer p) i)] |
| 1417 |
[else (get-u8-byte-mode p who 1)]))] |
| 1418 |
[else (slow-get-u8 p who 1)]))) |
| 1419 |
(define (lookahead-u8 p) |
| 1420 |
(define who 'lookahead-u8) |
| 1421 |
(let ([m ($port-fast-attrs p)]) |
| 1422 |
(cond |
| 1423 |
[(eq? m fast-get-byte-tag) |
| 1424 |
(let ([i ($port-index p)]) |
| 1425 |
(cond |
| 1426 |
[(fx< i ($port-size p)) |
| 1427 |
(bytevector-u8-ref ($port-buffer p) i)] |
| 1428 |
[else (get-u8-byte-mode p who 0)]))] |
| 1429 |
[else (slow-get-u8 p who 0)])))) |
| 1430 |
|
| 1431 |
(define (port-eof? p) |
| 1432 |
(import UNSAFE) |
| 1433 |
(define who 'port-eof?) |
| 1434 |
(let ([m ($port-fast-attrs p)]) |
| 1435 |
(cond |
| 1436 |
[(not (eq? m 0)) |
| 1437 |
(if (fx< ($port-index p) ($port-size p)) |
| 1438 |
#f |
| 1439 |
(if ($port-transcoder p) |
| 1440 |
(eof-object? (lookahead-char p)) |
| 1441 |
(eof-object? (lookahead-u8 p))))] |
| 1442 |
[(input-port? p) |
| 1443 |
(when ($port-closed? p) |
| 1444 |
(die 'port-eof? "port is closed" p)) |
| 1445 |
(if (textual-port? p) |
| 1446 |
(eof-object? (lookahead-char p)) |
| 1447 |
(eof-object? (lookahead-u8 p)))] |
| 1448 |
[else (die 'port-eof? "not an input port" p)]))) |
| 1449 |
|
| 1450 |
;;; FIXME: these hard coded constants should go away |
| 1451 |
(define EAGAIN-error-code -6) ;;; from ikarus-errno.c |
| 1452 |
|
| 1453 |
(define io-error |
| 1454 |
(case-lambda |
| 1455 |
[(who id err base-condition) |
| 1456 |
(raise |
| 1457 |
(condition |
| 1458 |
base-condition |
| 1459 |
(make-who-condition who) |
| 1460 |
(make-message-condition (strerror err)) |
| 1461 |
(case err |
| 1462 |
;; from ikarus-errno.c: EACCES=-2, EFAULT=-21, EROFS=-71, EEXIST=-20, |
| 1463 |
;; EIO=-29, ENOENT=-45 |
| 1464 |
;; Why is EFAULT included here? |
| 1465 |
[(-2 -21) (make-i/o-file-protection-error id)] |
| 1466 |
[(-71) (make-i/o-file-is-read-only-error id)] |
| 1467 |
[(-20) (make-i/o-file-already-exists-error id)] |
| 1468 |
[(-29) (make-i/o-error)] |
| 1469 |
[(-45) (make-i/o-file-does-not-exist-error id)] |
| 1470 |
[else (if id |
| 1471 |
(make-irritants-condition (list id)) |
| 1472 |
(condition))])))] |
| 1473 |
[(who id err) (io-error who id err (make-error))])) |
| 1474 |
|
| 1475 |
;(define block-size 4096) |
| 1476 |
;(define block-size (* 4 4096)) |
| 1477 |
(define input-block-size (* 4 4096)) |
| 1478 |
(define output-block-size (* 4 4096)) |
| 1479 |
|
| 1480 |
(define input-file-buffer-size (+ input-block-size 128)) |
| 1481 |
(define output-file-buffer-size output-block-size) |
| 1482 |
|
| 1483 |
(define input-socket-buffer-size |
| 1484 |
(make-parameter (+ input-block-size 128) |
| 1485 |
(lambda (x) |
| 1486 |
(import (ikarus system $fx)) |
| 1487 |
(if (and (fixnum? x) ($fx>= x 128)) |
| 1488 |
x |
| 1489 |
(error 'input-socket-buffer-size |
| 1490 |
"buffer size should be a fixnum >= 128" |
| 1491 |
x))))) |
| 1492 |
|
| 1493 |
(define output-socket-buffer-size |
| 1494 |
(make-parameter output-block-size |
| 1495 |
(lambda (x) |
| 1496 |
(import (ikarus system $fx)) |
| 1497 |
(if (and (fixnum? x) ($fx> x 0)) |
| 1498 |
x |
| 1499 |
(error 'output-socket-buffer-size |
| 1500 |
"buffer size should be a positive fixnum" |
| 1501 |
x))))) |
| 1502 |
|
| 1503 |
(define (make-file-set-position-handler fd id) |
| 1504 |
(lambda (pos) ;;; set-position! |
| 1505 |
(let ([err (foreign-call "ikrt_set_position" fd pos)]) |
| 1506 |
(when err |
| 1507 |
(io-error 'set-position! id err |
| 1508 |
(make-i/o-invalid-position-error pos)))))) |
| 1509 |
|
| 1510 |
(define (fh->input-port fd id size transcoder close who) |
| 1511 |
(letrec ([port |
| 1512 |
($make-port |
| 1513 |
(input-transcoder-attrs transcoder who) |
| 1514 |
0 0 (make-bytevector size) |
| 1515 |
transcoder |
| 1516 |
id |
| 1517 |
(letrec ([refill |
| 1518 |
(lambda (bv idx cnt) |
| 1519 |
(import UNSAFE) |
| 1520 |
(let ([bytes |
| 1521 |
(foreign-call "ikrt_read_fd" fd bv idx |
| 1522 |
(if (fx< input-block-size cnt) |
| 1523 |
input-block-size |
| 1524 |
cnt))]) |
| 1525 |
(cond |
| 1526 |
[(fx>= bytes 0) bytes] |
| 1527 |
[(fx= bytes EAGAIN-error-code) |
| 1528 |
(call/cc |
| 1529 |
(lambda (k) |
| 1530 |
(add-io-event fd k 'r) |
| 1531 |
(process-events))) |
| 1532 |
(refill bv idx cnt)] |
| 1533 |
[else |
| 1534 |
(io-error 'read id bytes |
| 1535 |
(make-i/o-read-error))])))]) |
| 1536 |
refill) |
| 1537 |
#f ;;; write! |
| 1538 |
#t ;;; get-position |
| 1539 |
(make-file-set-position-handler fd id) |
| 1540 |
(cond |
| 1541 |
[(procedure? close) close] |
| 1542 |
[(eqv? close #t) (file-close-proc id fd)] |
| 1543 |
[else #f]) |
| 1544 |
(default-cookie fd))]) |
| 1545 |
(guarded-port port))) |
| 1546 |
|
| 1547 |
|
| 1548 |
(define (fh->output-port fd id size transcoder close who) |
| 1549 |
(letrec ([port |
| 1550 |
($make-port |
| 1551 |
(output-transcoder-attrs transcoder who) |
| 1552 |
0 size (make-bytevector size) |
| 1553 |
transcoder |
| 1554 |
id |
| 1555 |
#f |
| 1556 |
(letrec ([refill |
| 1557 |
(lambda (bv idx cnt) |
| 1558 |
(import UNSAFE) |
| 1559 |
(let ([bytes |
| 1560 |
(foreign-call "ikrt_write_fd" fd bv idx |
| 1561 |
(if (fx< output-block-size cnt) |
| 1562 |
output-block-size |
| 1563 |
cnt))]) |
| 1564 |
|
| 1565 |
(cond |
| 1566 |
[(fx>= bytes 0) bytes] |
| 1567 |
[(fx= bytes EAGAIN-error-code) |
| 1568 |
(call/cc |
| 1569 |
(lambda (k) |
| 1570 |
(add-io-event fd k 'w) |
| 1571 |
(process-events))) |
| 1572 |
(refill bv idx cnt)] |
| 1573 |
[else |
| 1574 |
(io-error 'write id bytes |
| 1575 |
(make-i/o-write-error))])))]) |
| 1576 |
refill) |
| 1577 |
#t ;;; get-position |
| 1578 |
(make-file-set-position-handler fd id) |
| 1579 |
(cond |
| 1580 |
[(procedure? close) close] |
| 1581 |
[(eqv? close #t) (file-close-proc id fd)] |
| 1582 |
[else #f]) |
| 1583 |
(default-cookie fd))]) |
| 1584 |
(guarded-port port))) |
| 1585 |
|
| 1586 |
(define (file-close-proc id fd) |
| 1587 |
(lambda () |
| 1588 |
(cond |
| 1589 |
[(foreign-call "ikrt_close_fd" fd) => |
| 1590 |
(lambda (err) |
| 1591 |
(io-error 'close id err))]))) |
| 1592 |
|
| 1593 |
(define (open-input-file-handle filename who) |
| 1594 |
(let ([fh (foreign-call "ikrt_open_input_fd" |
| 1595 |
(string->utf8 filename))]) |
| 1596 |
(cond |
| 1597 |
[(fx< fh 0) (io-error who filename fh)] |
| 1598 |
[else fh]))) |
| 1599 |
|
| 1600 |
(define (open-output-file-handle filename file-options who) |
| 1601 |
(define (opt->num x) |
| 1602 |
(bitwise-ior |
| 1603 |
(if (enum-set-member? 'no-create x) 1 0) |
| 1604 |
(if (enum-set-member? 'no-fail x) 2 0) |
| 1605 |
(if (enum-set-member? 'no-truncate x) 4 0))) |
| 1606 |
(let ([opt (if (enum-set? file-options) |
| 1607 |
(opt->num file-options) |
| 1608 |
(die who "file-options is not an enum set" |
| 1609 |
file-options))]) |
| 1610 |
(let ([fh (foreign-call "ikrt_open_output_fd" |
| 1611 |
(string->utf8 filename) |
| 1612 |
opt)]) |
| 1613 |
(cond |
| 1614 |
[(fx< fh 0) (io-error who filename fh)] |
| 1615 |
[else fh])))) |
| 1616 |
|
| 1617 |
(define open-file-input-port |
| 1618 |
(case-lambda |
| 1619 |
[(filename) |
| 1620 |
(open-file-input-port filename (file-options) 'block #f)] |
| 1621 |
[(filename file-options) |
| 1622 |
(open-file-input-port filename file-options 'block #f)] |
| 1623 |
[(filename file-options buffer-mode) |
| 1624 |
(open-file-input-port filename file-options buffer-mode #f)] |
| 1625 |
[(filename file-options buffer-mode transcoder) |
| 1626 |
(define who 'open-file-input-port) |
| 1627 |
(unless (string? filename) |
| 1628 |
(die who "invalid filename" filename)) |
| 1629 |
(unless (enum-set? file-options) |
| 1630 |
(die who "file-options is not an enum set" file-options)) |
| 1631 |
(unless (or (not transcoder) (transcoder? transcoder)) |
| 1632 |
(die who "invalid transcoder" transcoder)) |
| 1633 |
; FIXME: file-options ignored |
| 1634 |
; FIXME: buffer-mode ignored |
| 1635 |
(fh->input-port |
| 1636 |
(open-input-file-handle filename who) |
| 1637 |
filename |
| 1638 |
input-file-buffer-size |
| 1639 |
transcoder |
| 1640 |
#t |
| 1641 |
who)])) |
| 1642 |
|
| 1643 |
(define open-file-output-port |
| 1644 |
(case-lambda |
| 1645 |
[(filename) |
| 1646 |
(open-file-output-port filename (file-options) 'block #f)] |
| 1647 |
[(filename file-options) |
| 1648 |
(open-file-output-port filename file-options 'block #f)] |
| 1649 |
[(filename file-options buffer-mode) |
| 1650 |
(open-file-output-port filename file-options buffer-mode #f)] |
| 1651 |
[(filename file-options buffer-mode transcoder) |
| 1652 |
(define who 'open-file-output-port) |
| 1653 |
(unless (string? filename) |
| 1654 |
(die who "invalid filename" filename)) |
| 1655 |
; FIXME: file-options ignored |
| 1656 |
; FIXME: line-buffered output ports are not handled |
| 1657 |
(unless (or (not transcoder) (transcoder? transcoder)) |
| 1658 |
(die who "invalid transcoder" transcoder)) |
| 1659 |
(let ([buffer-size |
| 1660 |
(case buffer-mode |
| 1661 |
[(none) 0] |
| 1662 |
[(block line) output-file-buffer-size] |
| 1663 |
[else (die who "invalid buffer mode" buffer-mode)])]) |
| 1664 |
(fh->output-port |
| 1665 |
(open-output-file-handle filename file-options who) |
| 1666 |
filename buffer-size transcoder #t who))])) |
| 1667 |
|
| 1668 |
(define (output-port-buffer-mode p) |
| 1669 |
(unless (output-port? p) |
| 1670 |
(die 'output-port-buffer-mode "not an output port" p)) |
| 1671 |
(if (fx= 0 ($port-size p)) 'none 'block)) |
| 1672 |
|
| 1673 |
(define (open-output-file filename) |
| 1674 |
(unless (string? filename) |
| 1675 |
(die 'open-output-file "invalid filename" filename)) |
| 1676 |
(fh->output-port |
| 1677 |
(open-output-file-handle filename (file-options) |
| 1678 |
'open-output-file) |
| 1679 |
filename |
| 1680 |
output-file-buffer-size |
| 1681 |
(native-transcoder) |
| 1682 |
#t |
| 1683 |
'open-output-file)) |
| 1684 |
|
| 1685 |
(define (open-input-file filename) |
| 1686 |
(unless (string? filename) |
| 1687 |
(die 'open-input-file "invalid filename" filename)) |
| 1688 |
(fh->input-port |
| 1689 |
(open-input-file-handle filename 'open-input-file) |
| 1690 |
filename |
| 1691 |
input-file-buffer-size |
| 1692 |
(native-transcoder) |
| 1693 |
#t |
| 1694 |
'open-input-file)) |
| 1695 |
|
| 1696 |
|
| 1697 |
(define (with-output-to-file filename proc) |
| 1698 |
(unless (string? filename) |
| 1699 |
(die 'with-output-to-file "invalid filename" filename)) |
| 1700 |
(unless (procedure? proc) |
| 1701 |
(die 'with-output-to-file "not a procedure" proc)) |
| 1702 |
(call-with-port |
| 1703 |
(fh->output-port |
| 1704 |
(open-output-file-handle filename (file-options) |
| 1705 |
'with-output-to-file) |
| 1706 |
filename |
| 1707 |
output-file-buffer-size |
| 1708 |
(native-transcoder) |
| 1709 |
#t |
| 1710 |
'with-output-to-file) |
| 1711 |
(lambda (p) |
| 1712 |
(parameterize ([current-output-port p]) |
| 1713 |
(proc))))) |
| 1714 |
|
| 1715 |
(define (call-with-output-file filename proc) |
| 1716 |
(unless (string? filename) |
| 1717 |
(die 'call-with-output-file "invalid filename" filename)) |
| 1718 |
(unless (procedure? proc) |
| 1719 |
(die 'call-with-output-file "not a procedure" proc)) |
| 1720 |
(call-with-port |
| 1721 |
(fh->output-port |
| 1722 |
(open-output-file-handle filename (file-options) |
| 1723 |
'call-with-output-file) |
| 1724 |
filename |
| 1725 |
output-file-buffer-size |
| 1726 |
(native-transcoder) |
| 1727 |
#t |
| 1728 |
'call-with-output-file) |
| 1729 |
proc)) |
| 1730 |
|
| 1731 |
(define (call-with-input-file filename proc) |
| 1732 |
(unless (string? filename) |
| 1733 |
(die 'call-with-input-file "invalid filename" filename)) |
| 1734 |
(unless (procedure? proc) |
| 1735 |
(die 'call-with-input-file "not a procedure" proc)) |
| 1736 |
(call-with-port |
| 1737 |
(fh->input-port |
| 1738 |
(open-input-file-handle filename 'call-with-input-file) |
| 1739 |
filename |
| 1740 |
input-file-buffer-size |
| 1741 |
(native-transcoder) |
| 1742 |
#t |
| 1743 |
'call-with-input-file) |
| 1744 |
proc)) |
| 1745 |
|
| 1746 |
(define (with-input-from-file filename proc) |
| 1747 |
(unless (string? filename) |
| 1748 |
(die 'with-input-from-file "invalid filename" filename)) |
| 1749 |
(unless (procedure? proc) |
| 1750 |
(die 'with-input-from-file "not a procedure" proc)) |
| 1751 |
(call-with-port |
| 1752 |
(fh->input-port |
| 1753 |
(open-input-file-handle filename 'with-input-from-file) |
| 1754 |
filename |
| 1755 |
input-file-buffer-size |
| 1756 |
(native-transcoder) |
| 1757 |
#t |
| 1758 |
'with-input-from-file) |
| 1759 |
(lambda (p) |
| 1760 |
(parameterize ([current-input-port p]) |
| 1761 |
(proc))))) |
| 1762 |
|
| 1763 |
(define (with-input-from-string string proc) |
| 1764 |
(unless (string? string) |
| 1765 |
(die 'with-input-from-string "not a string" string)) |
| 1766 |
(unless (procedure? proc) |
| 1767 |
(die 'with-input-from-string "not a procedure" proc)) |
| 1768 |
(parameterize ([current-input-port |
| 1769 |
(open-string-input-port string)]) |
| 1770 |
(proc))) |
| 1771 |
|
| 1772 |
(define (standard-input-port) |
| 1773 |
(fh->input-port 0 '*stdin* 256 #f #f 'standard-input-port)) |
| 1774 |
|
| 1775 |
(define (standard-output-port) |
| 1776 |
(fh->output-port 1 '*stdout* 256 #f #f 'standard-output-port)) |
| 1777 |
|
| 1778 |
(define (standard-error-port) |
| 1779 |
(fh->output-port 2 '*stderr* 256 #f #f 'standard-error-port)) |
| 1780 |
|
| 1781 |
(define current-input-port |
| 1782 |
(make-parameter |
| 1783 |
(transcoded-port |
| 1784 |
(fh->input-port 0 '*stdin* input-file-buffer-size #f #f #f) |
| 1785 |
(native-transcoder)) |
| 1786 |
(lambda (x) |
| 1787 |
(if (and (input-port? x) (textual-port? x)) |
| 1788 |
x |
| 1789 |
(die 'current-input-port "not a textual input port" x))))) |
| 1790 |
|
| 1791 |
(define current-output-port |
| 1792 |
(make-parameter |
| 1793 |
(transcoded-port |
| 1794 |
(fh->output-port 1 '*stdout* output-file-buffer-size #f #f #f) |
| 1795 |
(native-transcoder)) |
| 1796 |
(lambda (x) |
| 1797 |
(if (and (output-port? x) (textual-port? x)) |
| 1798 |
x |
| 1799 |
(die 'current-output-port "not a textual output port" x))))) |
| 1800 |
|
| 1801 |
(define current-error-port |
| 1802 |
(make-parameter |
| 1803 |
(transcoded-port |
| 1804 |
(fh->output-port 2 '*stderr* 0 #f #f #f) |
| 1805 |
(native-transcoder)) |
| 1806 |
(lambda (x) |
| 1807 |
(if (and (output-port? x) (textual-port? x)) |
| 1808 |
x |
| 1809 |
(die 'current-errorput-port "not a textual output port" x))))) |
| 1810 |
|
| 1811 |
(define console-output-port |
| 1812 |
(let ([p (current-output-port)]) |
| 1813 |
(lambda () p))) |
| 1814 |
|
| 1815 |
(define console-error-port |
| 1816 |
(let ([p (current-error-port)]) |
| 1817 |
(lambda () p))) |
| 1818 |
|
| 1819 |
(define console-input-port |
| 1820 |
(let ([p (current-input-port)]) |
| 1821 |
(lambda () p))) |
| 1822 |
|
| 1823 |
(define (call-with-port p proc) |
| 1824 |
(if (port? p) |
| 1825 |
(if (procedure? proc) |
| 1826 |
(call-with-values |
| 1827 |
(lambda () (proc p)) |
| 1828 |
(lambda vals |
| 1829 |
(close-port p) |
| 1830 |
(apply values vals))) |
| 1831 |
(die 'call-with-port "not a procedure" proc)) |
| 1832 |
(die 'call-with-port "not a port" p))) |
| 1833 |
|
| 1834 |
;;; |
| 1835 |
(define peek-char |
| 1836 |
(case-lambda |
| 1837 |
[() (lookahead-char (current-input-port))] |
| 1838 |
[(p) |
| 1839 |
(if (input-port? p) |
| 1840 |
(if (textual-port? p) |
| 1841 |
(lookahead-char p) |
| 1842 |
(die 'peek-char "not a textual port" p)) |
| 1843 |
(die 'peek-char "not an input-port" p))])) |
| 1844 |
|
| 1845 |
(define (get-bytevector-n p n) |
| 1846 |
(import (ikarus system $fx) (ikarus system $bytevectors)) |
| 1847 |
(define (subbytevector s n) |
| 1848 |
(let ([p ($make-bytevector n)]) |
| 1849 |
(let f ([s s] [n n] [p p]) |
| 1850 |
(let ([n ($fx- n 1)]) |
| 1851 |
($bytevector-set! p n ($bytevector-u8-ref s n)) |
| 1852 |
(if ($fx= n 0) |
| 1853 |
p |
| 1854 |
(f s n p)))))) |
| 1855 |
(unless (input-port? p) |
| 1856 |
(die 'get-bytevector-n "not an input port" p)) |
| 1857 |
(unless (binary-port? p) |
| 1858 |
(die 'get-bytevector-n "not a binary port" p)) |
| 1859 |
(unless (fixnum? n) |
| 1860 |
(die 'get-bytevector-n "count is not a fixnum" n)) |
| 1861 |
(cond |
| 1862 |
[($fx> n 0) |
| 1863 |
(let ([s ($make-bytevector n)]) |
| 1864 |
(let f ([p p] [n n] [s s] [i 0]) |
| 1865 |
(let ([x (get-u8 p)]) |
| 1866 |
(cond |
| 1867 |
[(eof-object? x) |
| 1868 |
(if ($fx= i 0) |
| 1869 |
(eof-object) |
| 1870 |
(subbytevector s i))] |
| 1871 |
[else |
| 1872 |
($bytevector-set! s i x) |
| 1873 |
(let ([i ($fxadd1 i)]) |
| 1874 |
(if ($fx= i n) |
| 1875 |
s |
| 1876 |
(f p n s i)))]))))] |
| 1877 |
[($fx= n 0) '#vu8()] |
| 1878 |
[else (die 'get-bytevector-n "count is negative" n)])) |
| 1879 |
|
| 1880 |
(define (get-bytevector-n! p s i c) |
| 1881 |
(import (ikarus system $fx) (ikarus system $bytevectors)) |
| 1882 |
(unless (input-port? p) |
| 1883 |
(die 'get-bytevector-n! "not an input port" p)) |
| 1884 |
(unless (binary-port? p) |
| 1885 |
(die 'get-bytevector-n! "not a binary port" p)) |
| 1886 |
(unless (bytevector? s) |
| 1887 |
(die 'get-bytevector-n! "not a bytevector" s)) |
| 1888 |
(let ([len ($bytevector-length s)]) |
| 1889 |
(unless (fixnum? i) |
| 1890 |
(die 'get-bytevector-n! "starting index is not a fixnum" i)) |
| 1891 |
(when (or ($fx< i 0) ($fx> i len)) |
| 1892 |
(die 'get-bytevector-n! |
| 1893 |
(format "starting index is out of range 0..~a" len) |
| 1894 |
i)) |
| 1895 |
(unless (fixnum? c) |
| 1896 |
(die 'get-bytevector-n! "count is not a fixnum" c)) |
| 1897 |
(cond |
| 1898 |
[($fx> c 0) |
| 1899 |
(let ([j (+ i c)]) |
| 1900 |
(when (> j len) |
| 1901 |
(die 'get-bytevector-n! |
| 1902 |
(format "count is out of range 0..~a" (- len i)) |
| 1903 |
c)) |
| 1904 |
(let ([x (get-u8 p)]) |
| 1905 |
(cond |
| 1906 |
[(eof-object? x) x] |
| 1907 |
[else |
| 1908 |
($bytevector-set! s i x) |
| 1909 |
(let f ([p p] [s s] [start i] [i 1] [c c]) |
| 1910 |
(cond |
| 1911 |
[($fx= i c) i] |
| 1912 |
[else |
| 1913 |
(let ([x (get-u8 p)]) |
| 1914 |
(cond |
| 1915 |
[(eof-object? x) i] |
| 1916 |
[else |
| 1917 |
($bytevector-set! s ($fx+ start i) x) |
| 1918 |
(f p s start ($fx+ i 1) c)]))]))])))] |
| 1919 |
[($fx= c 0) 0] |
| 1920 |
[else (die 'get-bytevector-n! "count is negative" c)]))) |
| 1921 |
|
| 1922 |
(define (get-bytevector-some p) |
| 1923 |
(define who 'get-bytevector-some) |
| 1924 |
; (import UNSAFE) |
| 1925 |
(let ([m ($port-fast-attrs p)]) |
| 1926 |
(cond |
| 1927 |
[(eq? m fast-get-byte-tag) |
| 1928 |
(let ([i ($port-index p)] [j ($port-size p)]) |
| 1929 |
(let ([cnt (fx- j i)]) |
| 1930 |
(cond |
| 1931 |
[(fx> cnt 0) |
| 1932 |
(let f ([bv (make-bytevector cnt)] |
| 1933 |
[buf ($port-buffer p)] |
| 1934 |
[i i] [j j] [idx 0]) |
| 1935 |
(cond |
| 1936 |
[(fx= i j) |
| 1937 |
($set-port-index! p j) |
| 1938 |
bv] |
| 1939 |
[else |
| 1940 |
(bytevector-u8-set! bv idx (bytevector-u8-ref buf i)) |
| 1941 |
(f bv buf (fx+ i 1) j (fx+ idx 1))]))] |
| 1942 |
[else |
| 1943 |
(refill-bv-buffer p who) |
| 1944 |
(if (fx= ($port-index p) ($port-size p)) |
| 1945 |
(eof-object) |
| 1946 |
(get-bytevector-some p))])))] |
| 1947 |
[else (die who "invalid port argument" p)]))) |
| 1948 |
|
| 1949 |
(define (get-bytevector-all p) |
| 1950 |
(define (get-it p) |
| 1951 |
(let f ([p p] [n 0] [ac '()]) |
| 1952 |
(let ([x (get-u8 p)]) |
| 1953 |
(cond |
| 1954 |
[(eof-object? x) |
| 1955 |
(if (null? ac) |
| 1956 |
(eof-object) |
| 1957 |
(make-it n ac))] |
| 1958 |
[else (f p (+ n 1) (cons x ac))])))) |
| 1959 |
(define (make-it n revls) |
| 1960 |
(let f ([s (make-bytevector n)] [i (- n 1)] [ls revls]) |
| 1961 |
(cond |
| 1962 |
[(pair? ls) |
| 1963 |
(bytevector-u8-set! s i (car ls)) |
| 1964 |
(f s (- i 1) (cdr ls))] |
| 1965 |
[else s]))) |
| 1966 |
(if (input-port? p) |
| 1967 |
(if (binary-port? p) |
| 1968 |
(get-it p) |
| 1969 |
(die 'get-bytevector-all "not a binary port" p)) |
| 1970 |
(die 'get-bytevector-all "not an input port" p))) |
| 1971 |
|
| 1972 |
(define (get-string-n p n) |
| 1973 |
(import (ikarus system $fx) (ikarus system $strings)) |
| 1974 |
(unless (input-port? p) |
| 1975 |
(die 'get-string-n "not an input port" p)) |
| 1976 |
(unless (textual-port? p) |
| 1977 |
(die 'get-string-n "not a textual port" p)) |
| 1978 |
(unless (fixnum? n) |
| 1979 |
(die 'get-string-n "count is not a fixnum" n)) |
| 1980 |
(cond |
| 1981 |
[($fx> n 0) |
| 1982 |
(let ([s ($make-string n)]) |
| 1983 |
(let f ([p p] [n n] [s s] [i 0]) |
| 1984 |
(let ([x (get-char p)]) |
| 1985 |
(cond |
| 1986 |
[(eof-object? x) |
| 1987 |
(if ($fx= i 0) |
| 1988 |
(eof-object) |
| 1989 |
(substring s 0 i))] |
| 1990 |
[else |
| 1991 |
($string-set! s i x) |
| 1992 |
(let ([i ($fxadd1 i)]) |
| 1993 |
(if ($fx= i n) |
| 1994 |
s |
| 1995 |
(f p n s i)))]))))] |
| 1996 |
[($fx= n 0) ""] |
| 1997 |
[else (die 'get-string-n "count is negative" n)])) |
| 1998 |
|
| 1999 |
(define (get-string-n! p s i c) |
| 2000 |
(import (ikarus system $fx) (ikarus system $strings)) |
| 2001 |
(unless (input-port? p) |
| 2002 |
(die 'get-string-n! "not an input port" p)) |
| 2003 |
(unless (textual-port? p) |
| 2004 |
(die 'get-string-n! "not a textual port" p)) |
| 2005 |
(unless (string? s) |
| 2006 |
(die 'get-string-n! "not a string" s)) |
| 2007 |
(let ([len ($string-length s)]) |
| 2008 |
(unless (fixnum? i) |
| 2009 |
(die 'get-string-n! "starting index is not a fixnum" i)) |
| 2010 |
(when (or ($fx< i 0) ($fx> i len)) |
| 2011 |
(die 'get-string-n! |
| 2012 |
(format "starting index is out of range 0..~a" len) |
| 2013 |
i)) |
| 2014 |
(unless (fixnum? c) |
| 2015 |
(die 'get-string-n! "count is not a fixnum" c)) |
| 2016 |
(cond |
| 2017 |
[($fx> c 0) |
| 2018 |
(let ([j (+ i c)]) |
| 2019 |
(when (> j len) |
| 2020 |
(die 'get-string-n! |
| 2021 |
(format "count is out of range 0..~a" (- len i)) |
| 2022 |
c)) |
| 2023 |
(let ([x (get-char p)]) |
| 2024 |
(cond |
| 2025 |
[(eof-object? x) x] |
| 2026 |
[else |
| 2027 |
($string-set! s i x) |
| 2028 |
(let f ([p p] [s s] [start i] [i 1] [c c]) |
| 2029 |
(let ([x (get-char p)]) |
| 2030 |
(cond |
| 2031 |
[(eof-object? x) i] |
| 2032 |
[else |
| 2033 |
($string-set! s ($fx+ start i) x) |
| 2034 |
(let ([i ($fxadd1 i)]) |
| 2035 |
(if ($fx= i c) |
| 2036 |
i |
| 2037 |
(f p s start i c)))])))])))] |
| 2038 |
[($fx= c 0) 0] |
| 2039 |
[else (die 'get-string-n! "count is negative" c)]))) |
| 2040 |
|
| 2041 |
(define ($get-line p who) |
| 2042 |
(import UNSAFE) |
| 2043 |
(define (get-it p) |
| 2044 |
(let f ([p p] [n 0] [ac '()]) |
| 2045 |
(let ([x (get-char p)]) |
| 2046 |
(cond |
| 2047 |
[(eqv? x #\newline) |
| 2048 |
(make-it n ac)] |
| 2049 |
[(eof-object? x) |
| 2050 |
(if (null? ac) x (make-it n ac))] |
| 2051 |
[else (f p (fx+ n 1) (cons x ac))])))) |
| 2052 |
(define (make-it n revls) |
| 2053 |
(let f ([s (make-string n)] [i (fx- n 1)] [ls revls]) |
| 2054 |
(cond |
| 2055 |
[(pair? ls) |
| 2056 |
(string-set! s i (car ls)) |
| 2057 |
(f s (fx- i 1) (cdr ls))] |
| 2058 |
[else s]))) |
| 2059 |
(if (input-port? p) |
| 2060 |
(if (textual-port? p) |
| 2061 |
(get-it p) |
| 2062 |
(die who "not a textual port" p)) |
| 2063 |
(die who "not an input port" p))) |
| 2064 |
(define (get-line p) |
| 2065 |
($get-line p 'get-line)) |
| 2066 |
(define read-line |
| 2067 |
(case-lambda |
| 2068 |
[() ($get-line (current-input-port) 'read-line)] |
| 2069 |
[(p) ($get-line p 'read-line)])) |
| 2070 |
|
| 2071 |
|
| 2072 |
(define (get-string-all p) |
| 2073 |
(define (get-it p) |
| 2074 |
(let f ([p p] [n 0] [ac '()]) |
| 2075 |
(let ([x (get-char p)]) |
| 2076 |
(cond |
| 2077 |
[(eof-object? x) |
| 2078 |
(if (null? ac) |
| 2079 |
(eof-object) |
| 2080 |
(make-it n ac))] |
| 2081 |
[else (f p (+ n 1) (cons x ac))])))) |
| 2082 |
(define (make-it n revls) |
| 2083 |
(let f ([s (make-string n)] [i (- n 1)] [ls revls]) |
| 2084 |
(cond |
| 2085 |
[(pair? ls) |
| 2086 |
(string-set! s i (car ls)) |
| 2087 |
(f s (- i 1) (cdr ls))] |
| 2088 |
[else s]))) |
| 2089 |
(if (input-port? p) |
| 2090 |
(if (textual-port? p) |
| 2091 |
(get-it p) |
| 2092 |
(die 'get-string-all "not a textual port" p)) |
| 2093 |
(die 'get-string-all "not an input port" p))) |
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
;;; ---------------------------------------------------------- |
| 2098 |
|
| 2099 |
(define-syntax put-string/bv |
| 2100 |
(syntax-rules () |
| 2101 |
[(_ who not-a-what pred? len $put) |
| 2102 |
(case-lambda |
| 2103 |
[(p bv) |
| 2104 |
(if (pred? bv) |
| 2105 |
($put p bv 0 (len bv)) |
| 2106 |
(die who not-a-what bv))] |
| 2107 |
[(p bv i) |
| 2108 |
(if (pred? bv) |
| 2109 |
(if (fixnum? i) |
| 2110 |
(let ([n (len bv)]) |
| 2111 |
(if (and (fx<= i n) (fx>= i 0)) |
| 2112 |
($put p bv i (fx- n i)) |
| 2113 |
(die who "index out of range" i))) |
| 2114 |
(die who "invalid index" i)) |
| 2115 |
(die who not-a-what bv))] |
| 2116 |
[(p bv i c) |
| 2117 |
(if (pred? bv) |
| 2118 |
(if (fixnum? i) |
| 2119 |
(let ([n (len bv)]) |
| 2120 |
(if (and (fx<= i n) (fx>= i 0)) |
| 2121 |
(if (fixnum? c) |
| 2122 |
(if (and (fx>= c 0) (fx>= (fx- n c) i)) |
| 2123 |
($put p bv i c) |
| 2124 |
(die who "count out of range" c)) |
| 2125 |
(die who "invalid count" c)) |
| 2126 |
(die who "index out of range" i))) |
| 2127 |
(die who "invalid index" i)) |
| 2128 |
(die who not-a-what bv))])])) |
| 2129 |
|
| 2130 |
|
| 2131 |
(module (put-char write-char put-string) |
| 2132 |
(import UNSAFE) |
| 2133 |
(define (put-byte! p b who) |
| 2134 |
(let ([i ($port-index p)] [j ($port-size p)]) |
| 2135 |
(if (fx< i j) |
| 2136 |
(begin |
| 2137 |
(bytevector-u8-set! ($port-buffer p) i b) |
| 2138 |
($set-port-index! p (fx+ i 1))) |
| 2139 |
(if (fx= j 0) |
| 2140 |
(put-byte/unbuffered! p b who) |
| 2141 |
(begin |
| 2142 |
(flush-output-port p) |
| 2143 |
(put-byte! p b who)))))) |
| 2144 |
(define (put-char-utf8-mode p b who) |
| 2145 |
(cond |
| 2146 |
[(fx< b 128) |
| 2147 |
(put-byte! p b who)] |
| 2148 |
[(fx<= b #x7FF) |
| 2149 |
(put-byte! p (fxior #b11000000 (fxsra b 6)) who) |
| 2150 |
(put-byte! p (fxior #b10000000 (fxand b #b111111)) who)] |
| 2151 |
[(fx<= b #xFFFF) |
| 2152 |
(put-byte! p (fxior #b11100000 (fxsra b 12)) who) |
| 2153 |
(put-byte! p (fxior #b10000000 (fxand (fxsra b 6) #b111111)) who) |
| 2154 |
(put-byte! p (fxior #b10000000 (fxand b #b111111)) who)] |
| 2155 |
[else |
| 2156 |
(put-byte! p (fxior #b11110000 (fxsra b 18)) who) |
| 2157 |
(put-byte! p (fxior #b10000000 (fxand (fxsra b 12) #b111111)) who) |
| 2158 |
(put-byte! p (fxior #b10000000 (fxand (fxsra b 6) #b111111)) who) |
| 2159 |
(put-byte! p (fxior #b10000000 (fxand b #b111111)) who)])) |
| 2160 |
;;; |
| 2161 |
(define write-char |
| 2162 |
(case-lambda |
| 2163 |
[(c p) (do-put-char p c 'write-char)] |
| 2164 |
[(c) (do-put-char (current-output-port) c 'write-char)])) |
| 2165 |
(define (put-char p c) |
| 2166 |
(do-put-char p c 'put-char)) |
| 2167 |
(define ($put-string p str start count) |
| 2168 |
(unless (output-port? p) |
| 2169 |
(die 'put-string "not an output port" p)) |
| 2170 |
(unless (textual-port? p) |
| 2171 |
(die 'put-string "not a textual port" p)) |
| 2172 |
(let f ([i start] [j (fx+ start count)]) |
| 2173 |
(unless (fx= i j) |
| 2174 |
(do-put-char p (string-ref str i) 'put-string) |
| 2175 |
(f (fx+ i 1) j)))) |
| 2176 |
(define put-string |
| 2177 |
(put-string/bv 'put-string "not a string" |
| 2178 |
string? string-length $put-string)) |
| 2179 |
(define (do-put-char p c who) |
| 2180 |
(unless (char? c) (die who "not a char" c)) |
| 2181 |
(let ([m ($port-fast-attrs p)]) |
| 2182 |
(cond |
| 2183 |
[(eq? m fast-put-utf8-tag) |
| 2184 |
(let ([i ($port-index p)] [j ($port-size p)]) |
| 2185 |
(let ([b (char->integer c)]) |
| 2186 |
(cond |
| 2187 |
[(fx< b 128) |
| 2188 |
(if (fx< i j) |
| 2189 |
(begin |
| 2190 |
(bytevector-u8-set! ($port-buffer p) i b) |
| 2191 |
($set-port-index! p (fx+ i 1))) |
| 2192 |
(if (fx= j 0) |
| 2193 |
(put-byte/unbuffered! p b who) |
| 2194 |
(begin |
| 2195 |
(flush-output-port p) |
| 2196 |
(put-byte! p b who))))] |
| 2197 |
[else |
| 2198 |
(put-char-utf8-mode p b who)])))] |
| 2199 |
[(eq? m fast-put-char-tag) |
| 2200 |
(let ([i ($port-index p)] [j ($port-size p)]) |
| 2201 |
(if (fx< i j) |
| 2202 |
(begin |
| 2203 |
(string-set! ($port-buffer p) i c) |
| 2204 |
($set-port-index! p (fx+ i 1))) |
| 2205 |
(if (fx= j 0) |
| 2206 |
(put-char/unbuffered! p c who) |
| 2207 |
(begin |
| 2208 |
(flush-output-port p) |
| 2209 |
(do-put-char p c who)))))] |
| 2210 |
[(eq? m fast-put-latin-tag) |
| 2211 |
(let ([i ($port-index p)] [j ($port-size p)]) |
| 2212 |
(let ([b (char->integer c)]) |
| 2213 |
(cond |
| 2214 |
[(fx< b 256) |
| 2215 |
(if (fx< i j) |
| 2216 |
(begin |
| 2217 |
(bytevector-u8-set! ($port-buffer p) i b) |
| 2218 |
($set-port-index! p (fx+ i 1))) |
| 2219 |
(if (fx= j 0) |
| 2220 |
(put-byte/unbuffered! p b who) |
| 2221 |
(begin |
| 2222 |
(flush-output-port p) |
| 2223 |
(put-byte! p b who))))] |
| 2224 |
[else |
| 2225 |
(case (transcoder-error-handling-mode (port-transcoder p)) |
| 2226 |
[(ignore) (void)] |
| 2227 |
[(replace) (do-put-char p #\? who)] |
| 2228 |
[(raise) |
| 2229 |
(raise (make-i/o-encoding-error p c))] |
| 2230 |
[else (die who "BUG: invalid error handling mode" p)])])))] |
| 2231 |
[(eq? m fast-put-utf16be-tag) |
| 2232 |
(let ([n (char->integer c)]) |
| 2233 |
(cond |
| 2234 |
[(fx< n #x10000) |
| 2235 |
(put-byte! p (fxsra n 8) who) |
| 2236 |
(put-byte! p (fxand n #xFF) who)] |
| 2237 |
[else |
| 2238 |
(let ([u^ (fx- n #x10000)]) |
| 2239 |
(let ([w1 (fxior #xD800 (fxsra u^ 10))]) |
| 2240 |
(put-byte! p (fxsra w1 8) who) |
| 2241 |
(put-byte! p (fxand w1 #xFF) who)) |
| 2242 |
(let ([w2 (fxior #xDC00 (fxand u^ (- (fxsll 1 10) 1)))]) |
| 2243 |
(put-byte! p (fxsra w2 8) who) |
| 2244 |
(put-byte! p (fxand w2 #xFF) who)))]))] |
| 2245 |
[else |
| 2246 |
(if (output-port? p) |
| 2247 |
(if (textual-port? p) |
| 2248 |
(if (port-closed? p) |
| 2249 |
(die who "port is closed" p) |
| 2250 |
(die who "unsupported port" p)) |
| 2251 |
(die who "not a textual port" p)) |
| 2252 |
(die who "not an output port" p))])))) |
| 2253 |
|
| 2254 |
(define newline |
| 2255 |
(case-lambda |
| 2256 |
[() |
| 2257 |
(put-char (current-output-port) #\newline) |
| 2258 |
(flush-output-port (current-output-port))] |
| 2259 |
[(p) |
| 2260 |
(unless (output-port? p) |
| 2261 |
(die 'newline "not an output port" p)) |
| 2262 |
(unless (textual-port? p) |
| 2263 |
(die 'newline "not a textual port" p)) |
| 2264 |
(when ($port-closed? p) |
| 2265 |
(die 'newline "port is closed" p)) |
| 2266 |
(put-char p #\newline) |
| 2267 |
(flush-output-port p)])) |
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
(module (put-u8 put-bytevector) |
| 2272 |
(import UNSAFE) |
| 2273 |
;;; |
| 2274 |
(define (put-u8 p b) |
| 2275 |
(define who 'put-u8) |
| 2276 |
(unless (u8? b) (die who "not a u8" b)) |
| 2277 |
(let ([m ($port-fast-attrs p)]) |
| 2278 |
(cond |
| 2279 |
[(eq? m fast-put-byte-tag) |
| 2280 |
(let ([i ($port-index p)] [j ($port-size p)]) |
| 2281 |
(if (fx< i j) |
| 2282 |
(begin |
| 2283 |
(bytevector-u8-set! ($port-buffer p) i b) |
| 2284 |
($set-port-index! p (fx+ i 1))) |
| 2285 |
(if (fx= j 0) |
| 2286 |
(put-byte/unbuffered! p b who) |
| 2287 |
(begin |
| 2288 |
(flush-output-port p) |
| 2289 |
(put-u8 p b)))))] |
| 2290 |
[else |
| 2291 |
(if (output-port? p) |
| 2292 |
(die who "not a binary port" p) |
| 2293 |
(die who "not an output port" p))]))) |
| 2294 |
;;; |
| 2295 |
(define ($put-bytevector p bv i c) |
| 2296 |
(define who 'put-bytevector) |
| 2297 |
(define (copy! src dst si di c) |
| 2298 |
(when (fx> c 0) |
| 2299 |
(bytevector-u8-set! dst di (bytevector-u8-ref src si)) |
| 2300 |
(copy! src dst (fx+ si 1) (fx+ di 1) (fx- c 1)))) |
| 2301 |
(let ([m ($port-fast-attrs p)]) |
| 2302 |
(cond |
| 2303 |
[(eq? m fast-put-byte-tag) |
| 2304 |
(let ([idx ($port-index p)] [j ($port-size p)]) |
| 2305 |
(let ([room (fx- j idx)]) |
| 2306 |
(cond |
| 2307 |
[(fx>= room c) |
| 2308 |
;; hurray |
| 2309 |
(copy! bv ($port-buffer p) i idx c) |
| 2310 |
($set-port-index! p (fx+ idx c))] |
| 2311 |
[(fx> room 0) |
| 2312 |
($set-port-index! p (fx+ idx room)) |
| 2313 |
(copy! bv ($port-buffer p) i idx room) |
| 2314 |
(flush-output-port p) |
| 2315 |
($put-bytevector p bv (fx+ i room) (fx- c room))] |
| 2316 |
[(fx> j 0) |
| 2317 |
(flush-output-port p) |
| 2318 |
($put-bytevector p bv i c)] |
| 2319 |
[else |
| 2320 |
(let f ([i i] [j (fx+ i c)]) |
| 2321 |
(unless (fx= i j) |
| 2322 |
(put-byte/unbuffered! p (bytevector-u8-ref bv i) who) |
| 2323 |
(f (fx+ i 1) j)))])))] |
| 2324 |
[else |
| 2325 |
(if (output-port? p) |
| 2326 |
(die who "not a binary port" p) |
| 2327 |
(die who "not an output port" p))]))) |
| 2328 |
|
| 2329 |
(define put-bytevector |
| 2330 |
(put-string/bv 'put-bytevector "not a bytevector" |
| 2331 |
bytevector? bytevector-length $put-bytevector)) |
| 2332 |
|
| 2333 |
;;; module |
| 2334 |
) |
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
(define (pair->env-utf8 pair) |
| 2339 |
(let* ((key-utf8 (string->utf8 (car pair))) |
| 2340 |
(val-utf8 (string->utf8 (cdr pair))) |
| 2341 |
(key-len (bytevector-length key-utf8)) |
| 2342 |
(val-len (bytevector-length val-utf8)) |
| 2343 |
(result (make-bytevector (+ key-len val-len 2)))) |
| 2344 |
(bytevector-copy! key-utf8 0 result 0 key-len) |
| 2345 |
(bytevector-u8-set! result key-len (char->integer #\=)) |
| 2346 |
(bytevector-copy! val-utf8 0 result (+ key-len 1) val-len) |
| 2347 |
(bytevector-u8-set! result (+ key-len val-len 1) 0) |
| 2348 |
result)) |
| 2349 |
|
| 2350 |
(define (spawn-process who search? blocking? env stdin stdout stderr cmd args) |
| 2351 |
(define (port->fd port port-pred arg-name port-type) |
| 2352 |
(cond ((eqv? port #f) -1) |
| 2353 |
((port-pred port) |
| 2354 |
(let ((fd (cookie-dest ($port-cookie port)))) |
| 2355 |
(unless (fixnum? fd) |
| 2356 |
(die who |
| 2357 |
(string-append arg-name " is not a file-based port") |
| 2358 |
stdin)) |
| 2359 |
fd)) |
| 2360 |
(else |
| 2361 |
(die who |
| 2362 |
(string-append arg-name " is neither false nor an " port-type) |
| 2363 |
stdin)))) |
| 2364 |
(let ((stdin-fd (port->fd stdin input-port? "stdin" "input port")) |
| 2365 |
(stdout-fd (port->fd stdout output-port? "stdout" "output port")) |
| 2366 |
(stderr-fd (port->fd stderr output-port? "stderr" "output port"))) |
| 2367 |
(unless (string? cmd) |
| 2368 |
(die who "command is not a string" cmd)) |
| 2369 |
(unless (andmap string? args) |
| 2370 |
(die who "all command arguments must be strings")) |
| 2371 |
(let ([r (foreign-call "ikrt_process" |
| 2372 |
(vector search? stdin-fd stdout-fd stderr-fd) |
| 2373 |
(and env (map pair->env-utf8 env)) |
| 2374 |
(string->utf8 cmd) |
| 2375 |
(map string->utf8 (cons cmd args)))]) |
| 2376 |
(cond ((fixnum? r) |
| 2377 |
(io-error who cmd r)) |
| 2378 |
(else |
| 2379 |
(unless blocking? |
| 2380 |
(or stdin (set-fd-nonblocking (vector-ref r 1) who cmd)) |
| 2381 |
(or stdout (set-fd-nonblocking (vector-ref r 2) who cmd)) |
| 2382 |
(or stderr (set-fd-nonblocking (vector-ref r 3) who cmd))) |
| 2383 |
(values |
| 2384 |
(vector-ref r 0) ; pid |
| 2385 |
(and (not stdin) |
| 2386 |
(fh->output-port (vector-ref r 1) |
| 2387 |
cmd output-file-buffer-size #f #t |
| 2388 |
'process)) |
| 2389 |
(and (not stdout) |
| 2390 |
(fh->input-port (vector-ref r 2) |
| 2391 |
cmd input-file-buffer-size #f #t |
| 2392 |
'process)) |
| 2393 |
(and (not stderr) |
| 2394 |
(fh->input-port (vector-ref r 3) |
| 2395 |
cmd input-file-buffer-size #f #t |
| 2396 |
'process)))))))) |
| 2397 |
|
| 2398 |
(define (process cmd . args) |
| 2399 |
(spawn-process 'process #t #t #f #f #f #f cmd args)) |
| 2400 |
|
| 2401 |
(define (process* search? env stdin stdout stderr cmd . args) |
| 2402 |
(spawn-process 'process* search? #t env stdin stdout stderr cmd args)) |
| 2403 |
|
| 2404 |
(define (process-nonblocking cmd . args) |
| 2405 |
(spawn-process 'process-nonblocking #t #f #f #f #f cmd args)) |
| 2406 |
|
| 2407 |
(define (set-fd-nonblocking fd who id) |
| 2408 |
(let ([rv (foreign-call "ikrt_make_fd_nonblocking" fd)]) |
| 2409 |
(unless (eq? rv 0) |
| 2410 |
(io-error who id rv)))) |
| 2411 |
|
| 2412 |
(define (socket->ports socket who id block?) |
| 2413 |
(if (< socket 0) |
| 2414 |
(io-error who id socket) |
| 2415 |
(let ([close |
| 2416 |
(let ([closed-once? #f]) |
| 2417 |
(lambda () |
| 2418 |
(if closed-once? |
| 2419 |
((file-close-proc id socket)) |
| 2420 |
(set! closed-once? #t))))]) |
| 2421 |
(unless block? |
| 2422 |
(set-fd-nonblocking socket who id)) |
| 2423 |
(values |
| 2424 |
(fh->input-port socket |
| 2425 |
id (input-socket-buffer-size) #f close who) |
| 2426 |
(fh->output-port socket |
| 2427 |
id (output-socket-buffer-size) #f close who))))) |
| 2428 |
|
| 2429 |
(define-syntax define-connector |
| 2430 |
(syntax-rules () |
| 2431 |
[(_ who foreign-name block?) |
| 2432 |
(define (who host srvc) |
| 2433 |
(unless (and (string? host) (string? srvc)) |
| 2434 |
(die 'who "host and service must both be strings" host srvc)) |
| 2435 |
(socket->ports |
| 2436 |
(or (foreign-call foreign-name |
| 2437 |
(string->utf8 host) (string->utf8 srvc)) |
| 2438 |
(die 'who "failed to resolve host name or connect" host srvc)) |
| 2439 |
'who |
| 2440 |
(string-append host ":" srvc) |
| 2441 |
block?))])) |
| 2442 |
|
| 2443 |
(define-connector tcp-connect "ikrt_tcp_connect" #t) |
| 2444 |
(define-connector udp-connect "ikrt_udp_connect" #t) |
| 2445 |
(define-connector tcp-connect-nonblocking "ikrt_tcp_connect" #f) |
| 2446 |
(define-connector udp-connect-nonblocking "ikrt_udp_connect" #f) |
| 2447 |
|
| 2448 |
(module (add-io-event rem-io-event process-events) |
| 2449 |
(define-struct t (fd proc type)) |
| 2450 |
;;; callbacks |
| 2451 |
(define pending '()) |
| 2452 |
(define out-queue '()) |
| 2453 |
(define in-queue '()) |
| 2454 |
|
| 2455 |
(define (process-events) |
| 2456 |
(if (null? out-queue) |
| 2457 |
(if (null? in-queue) |
| 2458 |
(if (null? pending) |
| 2459 |
(error 'process-events "no more events") |
| 2460 |
(begin |
| 2461 |
(do-select) |
| 2462 |
(process-events))) |
| 2463 |
(begin |
| 2464 |
(set! out-queue (reverse in-queue)) |
| 2465 |
(set! in-queue '()) |
| 2466 |
(process-events))) |
| 2467 |
(let ([t (car out-queue)]) |
| 2468 |
(set! out-queue (cdr out-queue)) |
| 2469 |
((t-proc t)) |
| 2470 |
(process-events)))) |
| 2471 |
|
| 2472 |
(define (add-io-event fd proc event-type) |
| 2473 |
(set! pending |
| 2474 |
(cons (make-t fd proc event-type) pending))) |
| 2475 |
|
| 2476 |
(define (rem-io-event fd) |
| 2477 |
(define (p x) (eq? (t-fd x) fd)) |
| 2478 |
(set! pending (remp p pending)) |
| 2479 |
(set! out-queue (remp p out-queue)) |
| 2480 |
(set! in-queue (remp p in-queue))) |
| 2481 |
|
| 2482 |
(define (get-max-fd) |
| 2483 |
(assert (pair? pending)) |
| 2484 |
(let f ([m (t-fd (car pending))] |
| 2485 |
[ls (cdr pending)]) |
| 2486 |
(cond |
| 2487 |
[(null? ls) m] |
| 2488 |
[else (f (max m (t-fd (car ls))) (cdr ls))]))) |
| 2489 |
|
| 2490 |
(define (do-select) |
| 2491 |
(let ([n (add1 (get-max-fd))]) |
| 2492 |
(let ([vecsize (div (+ n 7) 8)]) |
| 2493 |
(let ([rbv (make-bytevector vecsize 0)] |
| 2494 |
[wbv (make-bytevector vecsize 0)] |
| 2495 |
[xbv (make-bytevector vecsize 0)]) |
| 2496 |
;;; add all fds to their bytevectors depending on type |
| 2497 |
(for-each |
| 2498 |
(lambda (t) |
| 2499 |
(let ([fd (t-fd t)]) |
| 2500 |
(let ([i (div fd 8)] [j (mod fd 8)]) |
| 2501 |
(let ([bv (case (t-type t) |
| 2502 |
[(r) rbv] |
| 2503 |
[(w) wbv] |
| 2504 |
[(x) xbv] |
| 2505 |
[else |
| 2506 |
(error 'do-select "invalid type" t)])]) |
| 2507 |
(bytevector-u8-set! bv i |
| 2508 |
(fxlogor (fxsll 1 j) |
| 2509 |
(bytevector-u8-ref bv i))))))) |
| 2510 |
pending) |
| 2511 |
;;; do select |
| 2512 |
(let ([rv (foreign-call "ikrt_select" n rbv wbv xbv)]) |
| 2513 |
(when (< rv 0) |
| 2514 |
(io-error 'select #f rv))) |
| 2515 |
;;; go through fds again and see if they're selected |
| 2516 |
(for-each |
| 2517 |
(lambda (t) |
| 2518 |
(let ([fd (t-fd t)]) |
| 2519 |
(let ([i (div fd 8)] [j (mod fd 8)]) |
| 2520 |
(let ([bv (case (t-type t) |
| 2521 |
[(r) rbv] |
| 2522 |
[(w) wbv] |
| 2523 |
[(x) xbv] |
| 2524 |
[else |
| 2525 |
(error 'do-select "invalid type" t)])]) |
| 2526 |
(cond |
| 2527 |
[(fxzero? |
| 2528 |
(fxlogand (fxsll 1 j) |
| 2529 |
(bytevector-u8-ref bv i))) |
| 2530 |
;;; not selected |
| 2531 |
(set! pending (cons t pending))] |
| 2532 |
[else |
| 2533 |
;;; ready |
| 2534 |
(set! in-queue (cons t in-queue))]))))) |
| 2535 |
(let ([ls pending]) |
| 2536 |
(set! pending '()) |
| 2537 |
ls)))))) |
| 2538 |
) |
| 2539 |
|
| 2540 |
|
| 2541 |
(define-struct tcp-server (portnum fd)) |
| 2542 |
|
| 2543 |
(define (tcp-server-socket portnum) |
| 2544 |
(unless (fixnum? portnum) |
| 2545 |
(error 'tcp-server-socket "not a fixnum" portnum)) |
| 2546 |
(let ([sock (foreign-call "ikrt_listen" portnum)]) |
| 2547 |
(cond |
| 2548 |
[(fx>= sock 0) (make-tcp-server portnum sock)] |
| 2549 |
[else (die 'tcp-server-socket "failed to start server")]))) |
| 2550 |
|
| 2551 |
(define (tcp-server-socket-nonblocking portnum) |
| 2552 |
(let ([s (tcp-server-socket portnum)]) |
| 2553 |
(set-fd-nonblocking (tcp-server-fd s) |
| 2554 |
'tcp-server-socket-nonblocking |
| 2555 |
'#f) |
| 2556 |
s)) |
| 2557 |
|
| 2558 |
|
| 2559 |
(define (do-accept-connection s who blocking?) |
| 2560 |
(define (make-socket-info x) |
| 2561 |
(unless (= (bytevector-length x) 16) |
| 2562 |
(error who "BUG: unexpected return value" x)) |
| 2563 |
(format "~s.~s.~s.~s:~s" |
| 2564 |
(bytevector-u8-ref x 4) |
| 2565 |
(bytevector-u8-ref x 5) |
| 2566 |
(bytevector-u8-ref x 6) |
| 2567 |
(bytevector-u8-ref x 7) |
| 2568 |
(+ (* 256 (bytevector-u8-ref x 2)) |
| 2569 |
(bytevector-u8-ref x 3)))) |
| 2570 |
(unless (tcp-server? s) |
| 2571 |
(die who "not a tcp server" s)) |
| 2572 |
(let ([fd (tcp-server-fd s)] [bv (make-bytevector 16)]) |
| 2573 |
(unless fd |
| 2574 |
(die who "server is closed" s)) |
| 2575 |
(let ([sock (foreign-call "ikrt_accept" fd bv)]) |
| 2576 |
(cond |
| 2577 |
[(eq? sock EAGAIN-error-code) |
| 2578 |
(call/cc |
| 2579 |
(lambda (k) |
| 2580 |
(add-io-event fd k 'r) |
| 2581 |
(process-events))) |
| 2582 |
(do-accept-connection s who blocking?)] |
| 2583 |
[(< sock 0) |
| 2584 |
(io-error who s sock)] |
| 2585 |
[else |
| 2586 |
(socket->ports sock who (make-socket-info bv) blocking?)])))) |
| 2587 |
|
| 2588 |
(define (accept-connection s) |
| 2589 |
(do-accept-connection s 'accept-connection #t)) |
| 2590 |
|
| 2591 |
(define (accept-connection-nonblocking s) |
| 2592 |
(do-accept-connection s 'accept-connection-nonblocking #f)) |
| 2593 |
|
| 2594 |
(define (close-tcp-server-socket s) |
| 2595 |
(define who 'close-tcp-server-socket) |
| 2596 |
(unless (tcp-server? s) |
| 2597 |
(die who "not a tcp server" s)) |
| 2598 |
(let ([fd (tcp-server-fd s)]) |
| 2599 |
(unless fd |
| 2600 |
(die who "server is closed" s)) |
| 2601 |
(let ([rv (foreign-call "ikrt_shutdown" fd)]) |
| 2602 |
(when (fx< rv 0) |
| 2603 |
(die who "failed to shutdown"))))) |
| 2604 |
|
| 2605 |
(define (unregister-callback what) |
| 2606 |
(define who 'unregister-callback) |
| 2607 |
(cond |
| 2608 |
[(output-port? what) |
| 2609 |
(let ([c (cookie-dest ($port-cookie what))]) |
| 2610 |
(unless (fixnum? c) (die who "not a file-based port" what)) |
| 2611 |
(rem-io-event c))] |
| 2612 |
[(input-port? what) |
| 2613 |
(let ([c (cookie-dest ($port-cookie what))]) |
| 2614 |
(unless (fixnum? c) (die who "not a file-based port" what)) |
| 2615 |
(rem-io-event c))] |
| 2616 |
[(tcp-server? what) |
| 2617 |
(rem-io-event (tcp-server-fd what))] |
| 2618 |
[else (die who "invalid argument" what)])) |
| 2619 |
|
| 2620 |
(define (register-callback what proc) |
| 2621 |
(define who 'register-callback) |
| 2622 |
(unless (procedure? proc) |
| 2623 |
(die who "not a procedure" proc)) |
| 2624 |
(cond |
| 2625 |
[(output-port? what) |
| 2626 |
(let ([c (cookie-dest ($port-cookie what))]) |
| 2627 |
(unless (fixnum? c) (die who "not a file-based port" what)) |
| 2628 |
(add-io-event c proc 'w))] |
| 2629 |
[(input-port? what) |
| 2630 |
(let ([c (cookie-dest ($port-cookie what))]) |
| 2631 |
(unless (fixnum? c) (die who "not a file-based port" what)) |
| 2632 |
(add-io-event c proc 'r))] |
| 2633 |
[(tcp-server? what) |
| 2634 |
(add-io-event (tcp-server-fd what) proc 'r)] |
| 2635 |
[else (die who "invalid argument" what)])) |
| 2636 |
|
| 2637 |
|
| 2638 |
(module (directory-stream? open-directory-stream |
| 2639 |
read-directory-stream close-directory-stream) |
| 2640 |
|
| 2641 |
(define-struct directory-stream (filename pointer closed?)) |
| 2642 |
|
| 2643 |
(define G (make-guardian)) |
| 2644 |
|
| 2645 |
(define (clean-up) |
| 2646 |
(cond |
| 2647 |
[(G) => |
| 2648 |
(lambda (x) |
| 2649 |
(close-directory-stream x #f) |
| 2650 |
(clean-up))])) |
| 2651 |
|
| 2652 |
(define (open-directory-stream filename) |
| 2653 |
(define who 'open-directory-stream) |
| 2654 |
(unless (string? filename) |
| 2655 |
(die who "not a string" filename)) |
| 2656 |
(clean-up) |
| 2657 |
(let ([rv (foreign-call "ikrt_opendir" (string->utf8 filename))]) |
| 2658 |
(if (fixnum? rv) |
| 2659 |
(io-error who filename rv) |
| 2660 |
(let ([stream (make-directory-stream filename rv #f)]) |
| 2661 |
(G stream) |
| 2662 |
stream)))) |
| 2663 |
|
| 2664 |
(define (read-directory-stream x) |
| 2665 |
(define who 'read-directory-stream) |
| 2666 |
(unless (directory-stream? x) |
| 2667 |
(die who "not a directory stream" x)) |
| 2668 |
(when (directory-stream-closed? x) |
| 2669 |
(die who "directory stream is closed" x)) |
| 2670 |
(let ([rv (foreign-call "ikrt_readdir" |
| 2671 |
(directory-stream-pointer x))]) |
| 2672 |
(cond |
| 2673 |
[(fixnum? rv) |
| 2674 |
(close-directory-stream x #f) |
| 2675 |
(io-error who (directory-stream-filename x) rv)] |
| 2676 |
[(not rv) #f] |
| 2677 |
[else (utf8->string rv)]))) |
| 2678 |
|
| 2679 |
(define close-directory-stream |
| 2680 |
(case-lambda |
| 2681 |
[(x wanterror?) |
| 2682 |
(define who 'close-directory-stream) |
| 2683 |
(clean-up) |
| 2684 |
(unless (directory-stream? x) |
| 2685 |
(die who "not a directory stream" x)) |
| 2686 |
(unless (directory-stream-closed? x) |
| 2687 |
(set-directory-stream-closed?! x #t) |
| 2688 |
(let ([rv (foreign-call "ikrt_closedir" |
| 2689 |
(directory-stream-pointer x))]) |
| 2690 |
(when (and wanterror? (not (eqv? rv 0))) |
| 2691 |
(io-error who (directory-stream-filename x) rv))))] |
| 2692 |
[(x) (close-directory-stream x #t)])) |
| 2693 |
|
| 2694 |
(set-rtd-printer! (type-descriptor directory-stream) |
| 2695 |
(lambda (x p wr) |
| 2696 |
(fprintf p "#<directory-stream ~a>" |
| 2697 |
(directory-stream-filename x))))) |
| 2698 |
|
| 2699 |
|
| 2700 |
;(set-fd-nonblocking 0 'init '*stdin*) |
| 2701 |
) |
| 2702 |
|