1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
; Part of Scheme 48 1.9. See file COPYING for notices and license.
; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
; I/O primitives
; See doc/io.txt for a description of Scheme 48's I/O system.
; Argument specs
(define open-input-port->
(input-type (lambda (x)
(and (port? x)
(port-has-status? x (enum port-status-options
open-for-input))))
no-coercion))
(define open-output-port->
(input-type (lambda (x)
(and (port? x)
(port-has-status? x (enum port-status-options
open-for-output))))
no-coercion))
(define channel-> (input-type channel? no-coercion))
(define (port-has-status? port status)
(not (= 0 (bitwise-and (extract-fixnum (port-status port))
(shift-left 1 status)))))
;; Must be a C-level string, as a byte vector
(define (extract-filename filename)
(extract-low-string filename))
; Check SPEC type and then call OPEN-CHANNEL.
(define-consing-primitive open-channel (any-> any-> fixnum-> any->)
(lambda (ignore) channel-size)
(lambda (spec id mode close-silently? key)
(let* ((lose (lambda (reason)
(raise-exception* reason 0 spec (enter-fixnum mode))))
(win (lambda (index)
(receive (channel reason)
(make-registered-channel mode id index close-silently? key)
(cond ((false? channel)
(if (code-vector? spec)
(close-channel-index! index spec mode))
(lose reason))
(else
(goto return channel)))))))
(cond ((not (open-channel-status? mode))
(lose (enum exception wrong-type-argument)))
((fixnum? spec)
(if (<= 0 (extract-fixnum spec))
(win (extract-fixnum spec))
(lose (enum exception wrong-type-argument))))
((code-vector? spec)
(receive (channel status)
(let ((filename (extract-filename spec)))
(if (or (= mode (enum channel-status-option input))
(= mode (enum channel-status-option special-input)))
(open-input-file-channel filename)
(open-output-file-channel filename)))
(cond ((eq? status (enum errors no-errors))
(win channel))
(else
(raise-exception os-error 0
spec
(enter-fixnum mode)
(enter-fixnum status))))))
(else
(lose (enum exception wrong-type-argument)))))))
(define (open-channel-status? mode)
(or (= mode (enum channel-status-option input))
(= mode (enum channel-status-option output))
(= mode (enum channel-status-option special-input))
(= mode (enum channel-status-option special-output))))
(define-primitive close-channel (channel->)
(lambda (channel)
(if (open? channel)
(let ((status (close-channel! channel)))
(if (error? status)
(raise-exception os-error 0 channel status)
(goto no-result)))
(raise-exception wrong-type-argument 0 channel))))
(define-primitive channel-ready? (channel->)
(lambda (channel)
(if (open? channel)
(receive (ready? status)
(channel-ready? (extract-channel channel)
(input-channel? channel))
(if (error? status)
(raise-exception os-error 0 channel status)
(goto return-boolean ready?)))
(raise-exception wrong-type-argument 0 channel))))
;----------------
; Reading from and writing to channels.
;
; This is a wrapper around CHANNEL-READ-BLOCK. We check argument
; types and interpret the return value. We either return a
; number---the number of bytes read---or a cell containing the OS
; error code in the case of an I/O error.
(define-consing-primitive channel-maybe-read
(channel-> any-> fixnum-> fixnum-> boolean->)
(lambda (ignore) cell-size)
(lambda (channel buffer start count wait? key)
(if (and (input-channel? channel)
(buffer? buffer)
(not (immutable? buffer))
(<= (+ start count)
(buffer-length buffer)))
(receive (got eof? pending? status)
(channel-read-block (extract-channel channel)
(address+ (address-after-header buffer)
start)
count
wait?)
(goto return
(cond ((error? status)
(make-cell (enter-fixnum status) key))
(eof?
;; possible on Windows
(if pending?
(set-channel-os-status! channel true))
vm-eof-object)
(pending?
(set-channel-os-status! channel true)
false)
(else
(enter-fixnum got)))))
(raise-exception wrong-type-argument 0
channel
buffer
(enter-fixnum start)
(enter-fixnum count)
(enter-boolean wait?)))))
; This is a wrapper around CHANNEL-WRITE-BLOCK. We check argument
; types and interpret the return value. We either return a
; number---the number of bytes written---or a cell containing the OS
; error code in the case of an I/O error.
(define-consing-primitive channel-maybe-write
(channel-> any-> fixnum-> fixnum->)
(lambda (ignore) cell-size)
(lambda (channel buffer start count key)
(if (and (output-channel? channel)
(buffer? buffer)
(<= (+ start count)
(buffer-length buffer)))
(receive (got pending? status)
(channel-write-block (extract-channel channel)
(address+ (address-after-header buffer)
start)
count)
(goto return
(cond
((error? status)
(make-cell (enter-fixnum status) key))
(pending?
(set-channel-os-status! channel true)
false)
(else
(enter-fixnum got)))))
(raise-exception wrong-type-argument 0
channel
buffer
(enter-fixnum start)
(enter-fixnum count)))))
;----------------
; Utilities for the above two opcodes.
(define (buffer? thing)
(code-vector? thing))
(define (buffer-length buffer)
(code-vector-length buffer))
(define (extract-channel channel)
(extract-fixnum (channel-os-index channel)))
;----------------
;; random stuff
(define-primitive channel-parameter (fixnum->)
(lambda (param)
(enum-case channel-parameter-option param
((buffer-size)
(goto return-fixnum (channel-buffer-size)))
((crlf?)
(goto return-boolean (channel-crlf?)))
(else
(raise-exception bad-option 0 (enter-fixnum param))))))
(define-primitive channel-abort (channel->)
(lambda (channel)
(goto return (vm-channel-abort channel))))
(define-primitive open-channels-list ()
(lambda ()
(goto return (open-channels-list))))
;----------------------------------------------------------------
; Port instructions.
;
; These are only for speed. If no port was supplied by the user they have
; to look up the appropriate port in the current dynamic environments.
; This is a complete hack, also done for speed. See rts/current-port.scm
; for the other end.
(define (read-or-peek-byte read?)
(lambda ()
(let ((port (if (= (code-byte 0) 0)
(val)
(get-current-port
(enter-fixnum
(enum current-port-marker current-input-port))))))
(if (and (port? port)
(port-has-status? port
(enum port-status-options open-for-input)))
(let ((b (port-buffer port)))
(if (false? b)
(raise-exception buffer-full/empty 1 port)
(let ((i (extract-fixnum (port-index port)))
(l (extract-fixnum (port-limit port))))
(cond ((= i l)
(raise-exception buffer-full/empty 1 port))
(else
(if read?
(set-port-index! port (enter-fixnum (+ i 1))))
(goto continue-with-value
(enter-fixnum (code-vector-ref b i))
1))))))
(raise-exception wrong-type-argument 1 port)))))
(let ((do-it (read-or-peek-byte #t)))
(define-primitive read-byte () do-it))
(let ((do-it (read-or-peek-byte #f)))
(define-primitive peek-byte () do-it))
(define (read-or-peek-char read?)
(lambda ()
(let ((port (if (= (code-byte 0) 0)
(val)
(get-current-port
(enter-fixnum
(enum current-port-marker current-input-port))))))
(if (and (port? port)
(port-has-status? port
(enum port-status-options open-for-input)))
(let ((b (port-buffer port)))
(if (false? b)
(raise-exception buffer-full/empty 1 port)
(let loop ((i (extract-fixnum (port-index port))))
(let ((l (extract-fixnum (port-limit port)))
(codec (port-text-codec-spec port))
(lose
(lambda ()
;; we may have gotten out of synch because of CR/LF conversion
(if read?
(set-port-index! port (enter-fixnum i)))
(raise-exception buffer-full/empty 1 port))))
(cond ((= i l)
(lose))
((not (fixnum? codec))
(lose))
(else
(call-with-values
(lambda ()
(decode-scalar-value (extract-fixnum codec)
(address+ (address-after-header b) i)
(- l i)))
(lambda (encoding-ok? ok? incomplete? value count)
(define (deliver)
(if read?
(begin
(set-port-pending-cr?! port false)
(set-port-index! port (enter-fixnum (+ i count)))))
(goto continue-with-value
(scalar-value->vm-char value)
1))
(cond
((not encoding-ok?)
(raise-exception wrong-type-argument 1 port))
((or (not ok?) incomplete?)
(lose))
((not (false? (port-crlf? port)))
;; CR/LF handling. Great.
(cond
((= value cr-code)
(if read?
(begin
(set-port-pending-cr?! port true)
(set-port-index! port (enter-fixnum (+ i count)))))
(goto continue-with-value (scalar-value->vm-char lf-code) 1))
((and (= value lf-code)
(not (false? (port-pending-cr? port))))
(if read?
(set-port-pending-cr?! port false))
(loop (+ i count)))
(else
(deliver))))
(else
(deliver)))))))))))
(raise-exception wrong-type-argument 1 port)))))
(let ((do-it (read-or-peek-char #t)))
(define-primitive read-char () do-it))
(let ((do-it (read-or-peek-char #f)))
(define-primitive peek-char () do-it))
(define-primitive write-byte ()
(lambda ()
(receive (byte port)
(if (= (code-byte 0) 0)
(values (pop)
(val))
(values (val)
(get-current-port (enter-fixnum
(enum current-port-marker
current-output-port)))))
(cond
((not (and (fixnum? byte)
(port? port)
(port-has-status? port
(enum port-status-options open-for-output))))
(raise-exception wrong-type-argument 1 byte port))
((false? (port-limit port)) ; unbuffered
(raise-exception buffer-full/empty 1 byte port))
(else
(let ((b (port-buffer port))
(i (extract-fixnum (port-index port))))
(cond ((= i (code-vector-length b))
(raise-exception buffer-full/empty 1 byte port))
(else
(set-port-index! port (enter-fixnum (+ i 1)))
(code-vector-set! b i (extract-fixnum byte))
(goto continue-with-value unspecific-value 1)))))))))
(define cr-code 13)
(define lf-code 10)
(define-primitive write-char ()
(lambda ()
(receive (char port)
(if (= (code-byte 0) 0)
(values (pop)
(val))
(values (val)
(get-current-port (enter-fixnum
(enum current-port-marker
current-output-port)))))
(cond
((not (and (vm-char? char)
(port? port)
(port-has-status? port
(enum port-status-options open-for-output))))
(raise-exception wrong-type-argument 1 char port))
((false? (port-limit port)) ; unbuffered
(raise-exception buffer-full/empty 1 char port))
(else
(let ((codec (port-text-codec-spec port))
(lose
;; #### this isn't really the right exception
(lambda () (raise-exception buffer-full/empty 1 char port))))
(if (not (fixnum? codec))
(lose)
(let* ((b (port-buffer port))
(i (extract-fixnum (port-index port)))
(l (code-vector-length b)))
(cond
((= i l) (lose))
;; CR/LF handling is atrocious
((and (not (false? (port-crlf? port)))
(= (vm-char->scalar-value char) lf-code))
(call-with-values
(lambda ()
(encode-scalar-value (extract-fixnum codec) cr-code
(address+ (address-after-header b) i)
(- l i)))
(lambda (codec-ok? encoding-ok? out-of-space? count)
(cond
((not codec-ok?)
(raise-exception wrong-type-argument 1 char port))
((or (not encoding-ok?) out-of-space?)
(lose))
(else
(let ((i (+ i count)))
(if (= i l)
(lose)
(call-with-values
(lambda ()
(encode-scalar-value (extract-fixnum codec) lf-code
(address+ (address-after-header b) i)
(- l i)))
(lambda (codec-ok? encoding-ok? out-of-space? count)
(cond
;; the codec is the same as before, so it must be OK
((or (not encoding-ok?) out-of-space?)
(lose))
(else
(set-port-index! port (enter-fixnum (+ i count)))
(goto continue-with-value unspecific-value 1))))))))))))
(else
(call-with-values
(lambda ()
(encode-scalar-value (extract-fixnum codec) (vm-char->scalar-value char)
(address+ (address-after-header b) i)
(- l i)))
(lambda (codec-ok? encoding-ok? out-of-space? count)
(cond
((not codec-ok?)
(raise-exception wrong-type-argument 1 char port))
((or (not encoding-ok?) out-of-space?)
(lose))
(else
(set-port-index! port (enter-fixnum (+ i count)))
(goto continue-with-value unspecific-value 1)))))))))))))))
; Do an ASSQ-like walk up the current dynamic environment, looking for
; MARKER.
(define (get-current-port marker)
(let ((thread (current-thread)))
(if (and (record? thread)
(< 1 (record-length thread)))
(let loop ((env (record-ref thread 1)))
(cond ((not (and (vm-pair? env)
(vm-pair? (vm-car env))))
(if (vm-eq? env null)
(error (if (eq? (extract-fixnum marker)
(enum current-port-marker
current-output-port))
"dynamic environment doesn't have current-output-port"
"dynamic environment doesn't have current-input-port"))
(error "dynamic environment is not a proper list")))
((vm-eq? marker (vm-car (vm-car env)))
(vm-cdr (vm-car env)))
(else
(loop (vm-cdr env)))))
(error "current thread is not a record"))))
(define-primitive os-error-message (fixnum->)
(lambda (status)
(let* ((raw (error-string status))
(len (+ (string-length raw) 1))
(vector (maybe-make-b-vector+gc (enum stob byte-vector) len)))
(if (false? vector)
(raise-exception heap-overflow
0
(enter-fixnum len))
(do ((i 0 (+ 1 i)))
((>= i len)
(goto return vector))
(code-vector-set! vector i (char->ascii (string-ref raw i))))))))
;----------------
; A poor man's WRITE for use in debugging.
(define-primitive message (any->)
(lambda (stuff)
(let ((out (current-error-port)))
(let loop ((stuff stuff))
(if (vm-pair? stuff)
(begin
(message-element (vm-car stuff) out)
(loop (vm-cdr stuff)))))
(newline out)))
return-unspecific)
(define (message-element thing out)
(cond ((fixnum? thing)
(write-integer (extract-fixnum thing) out))
((vm-char? thing)
(write-string "#\\" out)
(write-char (ascii->char (vm-char->scalar-value thing)) out)) ; ####
((typed-record? thing)
(write-string "#{" out)
(write-vm-string (record-type-name thing) out)
(write-char #\} out))
((vm-string? thing)
(write-vm-string thing out))
((vm-symbol? thing)
(write-vm-string (vm-symbol->string thing) out))
(else
(write-string (cond ((vm-boolean? thing)
(if (extract-boolean thing) "#t" "#f"))
((vm-eq? thing null)
"()")
((vm-pair? thing)
"(...)")
((vm-vector? thing)
"#(...)")
((closure? thing)
"#{procedure}")
((template? thing)
"#{template}")
((location? thing)
"#{location}")
((code-vector? thing)
"#{code-vector}")
((continuation? thing)
"#{continuation}")
(else
"???"))
out))))
;----------------------------------------------------------------
; RESUME-PROC is called when the image is resumed.
; This does a garbage collection rooting from RESUME-PROC, writes the heap
; into a file, and then aborts the garbage collection (which didn't modify
; any VM registers or the stack).
; Bug: finalizers for things in the image are ignored.
(define-primitive write-image-low (code-vector-> any-> code-vector-> vector->)
(lambda (filename resume-proc comment-string undumpables)
(let* ((lose (lambda (status)
(raise-exception* (enum exception os-error) 0
(enter-fixnum status)
filename
resume-proc comment-string)))
(port-lose (lambda (status port)
(let ((close-status (close-output-port port)))
(if (error? close-status)
(lose close-status)
(lose status))))))
(receive (port status)
(open-output-file (extract-filename filename))
(if (error? status)
(lose status)
(let ((status (write-string (extract-low-string comment-string) port)))
(if (error? status)
(port-lose status port)
(let ((status (s48-write-image resume-proc
undumpables
port)))
(if (error? status)
(port-lose status port)
(let ((status (close-output-port port)))
(if (error? status)
(lose status)
(goto no-result))))))))))))
; READ-IMAGE needs to protect some values against GCs (this can't be with
; READ-IMAGE as that is compiled separately.)
(add-gc-root! s48-initializing-gc-root)
|