current-port.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  3. ; Current input, output, error, and noise ports.
  4. ; These two ports are needed by the VM for the READ-BYTE and WRITE-BYTE
  5. ; opcodes.
  6. (define $current-input-port (enum current-port-marker current-input-port))
  7. (define $current-output-port (enum current-port-marker current-output-port))
  8. (define $current-error-port (make-fluid #f))
  9. (define $current-noise-port (make-fluid #f)) ; defaults to the error port
  10. (define (current-input-port)
  11. (fluid $current-input-port))
  12. (define (current-output-port)
  13. (fluid $current-output-port))
  14. (define (current-error-port)
  15. (fluid $current-error-port))
  16. (define (current-noise-port)
  17. (fluid $current-noise-port))
  18. (define (initialize-i/o input output error thunk)
  19. (with-current-ports input output error thunk))
  20. (define (with-current-ports in out error thunk)
  21. (let-fluids $current-input-port in
  22. $current-output-port out
  23. $current-error-port error
  24. $current-noise-port error
  25. thunk))
  26. (define (call-with-current-input-port port thunk)
  27. (let-fluid $current-input-port port thunk))
  28. (define (call-with-current-output-port port thunk)
  29. (let-fluid $current-output-port port thunk))
  30. (define (call-with-current-noise-port port thunk)
  31. (let-fluid $current-noise-port port thunk))
  32. (define (silently thunk)
  33. (call-with-current-noise-port (make-null-output-port) thunk))
  34. ;----------------
  35. ; Procedures with default port arguments.
  36. ; We probably lose a lot of speed here as compared with the
  37. ; specialized VM instructions.
  38. (define (newline . port-option)
  39. (write-char #\newline (output-port-option port-option)))
  40. (define (byte-ready? . port-option)
  41. (real-byte-ready? (input-port-option port-option)))
  42. ; CHAR-READY? sucks
  43. (define (char-ready? . port-option)
  44. (real-char-ready? (input-port-option port-option)))
  45. (define (output-port-option port-option)
  46. (cond ((null? port-option) (current-output-port))
  47. ((null? (cdr port-option)) (car port-option))
  48. (else
  49. (assertion-violation 'write-mumble
  50. "too many arguments" port-option))))
  51. (define (input-port-option port-option)
  52. (cond ((null? port-option) (current-input-port))
  53. ((null? (cdr port-option)) (car port-option))
  54. (else
  55. (assertion-violation 'read-mumble
  56. "read-mumble: too many arguments" port-option))))