rpc-io.scm
(module rpc-io mzscheme
        (provide write*
                 read*
                 newline*
                 io-ok?
                 io-error?
                 rpc-notify?
                 wrong-input?
                 )

        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;; Exported functions/macros
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        
        ;;;; * read*
        (define (read* port)
          (with-handlers ((exn:fail? (lambda (exn) 
                                       (cons '%rpc-wrong-input% (exn-message exn)))))
                         (read port)))
        
        ;;;; write*
        (define (write* any port)
          (with-handlers ((exn:fail? (lambda (exn) 
                                       (cons '%rpc-io-error% (exn-message exn)))))
                         (begin
                           (write any port)
                           (flush-output port)
                           '%rpc-ok%)))
        
        ;;;; * newline
        (define (newline* port)
          (with-handlers ((exn:fail? (lambda (exn) 
                                       (cons '%rpc-io-error% (exn-message exn)))))
                         (begin
                           (display "\r\n" port)
                           (flush-output port))))
        
        
        ;;;; * io-error?
        (define (io-error? r)
          (if (pair? r)
              (eq? (car r) '%rpc-io-error%)
              #f))
        
        ;;;; * wong-input?
        (define (wrong-input? r)
          (if (pair? r)
              (eq? (car r) '%rpc-wrong-input%)
              #f))
        
        ;;;; * io-ok?
        (define (io-ok? r)
          (eq? r '%rpc-io-ok%))
        
        ;;;; * rpc-notify?
        (define (rpc-notify? r)
          (if (pair? r)
              (eq? (car r) '%rpc-notify%)
              #f))
        
        );;;; module-end