current-port.scm 2.2 KB

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