2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
|
|
|
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum
|
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 19:53:16 -04:00
|
|
|
|
|
|
|
(library (ikarus io output-files)
|
|
|
|
(export standard-output-port standard-error-port
|
|
|
|
console-output-port current-output-port
|
|
|
|
open-output-file with-output-to-file call-with-output-file)
|
|
|
|
(import
|
2007-05-06 18:43:04 -04:00
|
|
|
(ikarus system $ports)
|
|
|
|
(ikarus system $io)
|
|
|
|
(ikarus system $strings)
|
2007-05-18 15:47:06 -04:00
|
|
|
(ikarus system $chars)
|
|
|
|
(ikarus system $bytevectors)
|
2007-05-06 18:43:04 -04:00
|
|
|
(ikarus system $fx)
|
2007-10-12 00:33:19 -04:00
|
|
|
(rnrs bytevectors)
|
2007-05-05 19:53:16 -04:00
|
|
|
(except (ikarus)
|
|
|
|
standard-output-port standard-error-port
|
|
|
|
console-output-port current-output-port
|
|
|
|
*standard-output-port* *standard-error-port*
|
|
|
|
*current-output-port*
|
|
|
|
open-output-file with-output-to-file
|
|
|
|
call-with-output-file))
|
|
|
|
|
|
|
|
(define-syntax message-case
|
|
|
|
(syntax-rules (else)
|
|
|
|
[(_ msg args
|
|
|
|
[(msg-name msg-arg* ...) b b* ...] ...
|
|
|
|
[else else1 else2 ...])
|
|
|
|
(let ([tmsg msg] [targs args])
|
|
|
|
(define-syntax match-and-bind
|
|
|
|
(syntax-rules ()
|
|
|
|
[(__ y () body)
|
|
|
|
(if (null? y)
|
|
|
|
body
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'message-case "unmatched" (cons tmsg targs)))]
|
2007-05-05 19:53:16 -04:00
|
|
|
[(__ y (a a* (... ...)) body)
|
|
|
|
(if (pair? y)
|
|
|
|
(let ([a (car y)] [d (cdr y)])
|
|
|
|
(match-and-bind d (a* (... ...)) body))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'message-case "unmatched" (cons tmsg targs)))]))
|
2007-05-05 19:53:16 -04:00
|
|
|
(case tmsg
|
|
|
|
[(msg-name)
|
|
|
|
(match-and-bind targs (msg-arg* ...) (begin b b* ...))] ...
|
|
|
|
[else else1 else2 ...]))]))
|
|
|
|
|
|
|
|
(define guardian (make-guardian))
|
|
|
|
|
|
|
|
(define close-ports
|
|
|
|
(lambda ()
|
|
|
|
(cond
|
|
|
|
[(guardian) =>
|
|
|
|
(lambda (p)
|
|
|
|
(close-output-port p)
|
|
|
|
(close-ports))])))
|
|
|
|
|
2007-05-18 15:47:06 -04:00
|
|
|
(define do-write-buffer
|
|
|
|
(lambda (fd port-name buff idx caller)
|
|
|
|
(let ([bytes (foreign-call "ikrt_write_file" fd buff idx)])
|
|
|
|
(if (fixnum? bytes)
|
|
|
|
bytes
|
2007-10-25 14:32:26 -04:00
|
|
|
(error caller "cannot write to file" port-name bytes)))))
|
2007-05-18 15:47:06 -04:00
|
|
|
|
|
|
|
(define make-output-file-handler
|
|
|
|
(lambda (fd port-name)
|
|
|
|
(define open? #t)
|
|
|
|
(define output-file-handler
|
|
|
|
(lambda (msg . args)
|
|
|
|
(message-case msg args
|
|
|
|
[(write-byte b p)
|
|
|
|
(if (and (fixnum? b) ($fx<= 0 b) ($fx<= b 255))
|
|
|
|
(if (output-port? p)
|
2007-08-25 11:24:05 -04:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
2007-05-18 16:07:58 -04:00
|
|
|
(begin
|
2007-08-25 11:24:05 -04:00
|
|
|
($bytevector-set! ($port-buffer p) idx b)
|
|
|
|
($set-port-index! p ($fxadd1 idx)))
|
2007-05-18 16:07:58 -04:00
|
|
|
(if open?
|
|
|
|
(let ([bytes (do-write-buffer fd port-name
|
2007-08-25 11:24:05 -04:00
|
|
|
($port-buffer p) idx 'write-char)])
|
|
|
|
($set-port-index! p 0)
|
2007-05-18 16:07:58 -04:00
|
|
|
($write-byte b p))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'write-byte "port is closed" p))))
|
|
|
|
(error 'write-byte "not an output-port" p))
|
|
|
|
(error 'write-byte "not a byte" b))]
|
2007-05-18 15:47:06 -04:00
|
|
|
[(write-char c p)
|
|
|
|
(if (char? c)
|
|
|
|
(if (output-port? p)
|
|
|
|
(let ([b ($char->fixnum c)])
|
|
|
|
(if ($fx<= b 255)
|
|
|
|
($write-byte b p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'write-char
|
|
|
|
"BUG: multibyte write of not implemented" c)))
|
|
|
|
(error 'write-char "not an output-port" p))
|
|
|
|
(error 'write-char "not a character" c))]
|
2007-05-18 15:47:06 -04:00
|
|
|
[(flush-output-port p)
|
|
|
|
(if (output-port? p)
|
|
|
|
(if open?
|
2007-05-18 16:07:58 -04:00
|
|
|
(let ([bytes (do-write-buffer fd port-name
|
2007-08-25 11:24:05 -04:00
|
|
|
($port-buffer p)
|
|
|
|
($port-index p)
|
2007-05-18 16:07:58 -04:00
|
|
|
'flush-output-port)])
|
2007-08-25 11:24:05 -04:00
|
|
|
($set-port-index! p 0))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'flush-output-port "port is closed" p))
|
|
|
|
(error 'flush-output-port "not an output-port" p))]
|
2007-05-18 15:47:06 -04:00
|
|
|
[(close-port p)
|
|
|
|
(when open?
|
|
|
|
(flush-output-port p)
|
2007-08-25 11:24:05 -04:00
|
|
|
($set-port-size! p 0)
|
2007-05-18 15:47:06 -04:00
|
|
|
(set! open? #f)
|
|
|
|
(unless (foreign-call "ikrt_close_file" fd)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'close-output-port "cannot close" port-name)))]
|
2007-05-18 15:47:06 -04:00
|
|
|
[(port-name p) port-name]
|
|
|
|
[else (error 'output-file-handler
|
2007-10-25 14:32:26 -04:00
|
|
|
"unhandled message" (cons msg args))])))
|
2007-05-18 15:47:06 -04:00
|
|
|
output-file-handler))
|
2007-05-05 19:53:16 -04:00
|
|
|
(define (option-id x)
|
|
|
|
(case x
|
|
|
|
[(error) 0]
|
|
|
|
[(replace) 1]
|
|
|
|
[(truncate) 2]
|
|
|
|
[(append) 3]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else (error 'open-output-file "not a valid mode" x)]))
|
2007-05-05 19:53:16 -04:00
|
|
|
|
|
|
|
(define $open-output-file
|
|
|
|
(lambda (filename options)
|
|
|
|
(close-ports)
|
|
|
|
(let ([fd/error
|
|
|
|
(foreign-call "ikrt_open_output_file"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 filename)
|
2007-05-05 19:53:16 -04:00
|
|
|
(option-id options))])
|
|
|
|
(if (fixnum? fd/error)
|
|
|
|
(let ([port
|
|
|
|
(make-output-port
|
|
|
|
(make-output-file-handler fd/error filename)
|
2007-05-18 16:07:58 -04:00
|
|
|
($make-bytevector 4096))])
|
2007-05-05 19:53:16 -04:00
|
|
|
(guardian port)
|
|
|
|
port)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'open-output-file "cannot open file" filename fd/error)))))
|
2007-05-05 19:53:16 -04:00
|
|
|
|
|
|
|
(define *standard-output-port* #f)
|
|
|
|
|
|
|
|
(define *standard-error-port* #f)
|
|
|
|
|
|
|
|
(define *current-output-port* #f)
|
|
|
|
|
|
|
|
(define standard-output-port
|
|
|
|
(lambda () *standard-output-port*))
|
|
|
|
|
|
|
|
(define standard-error-port
|
|
|
|
(lambda () *standard-error-port*))
|
|
|
|
|
|
|
|
(define console-output-port
|
|
|
|
(lambda () *standard-output-port*))
|
|
|
|
|
|
|
|
(define current-output-port
|
|
|
|
(case-lambda
|
|
|
|
[() *current-output-port*]
|
|
|
|
[(p)
|
|
|
|
(if (output-port? p)
|
|
|
|
(set! *current-output-port* p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'current-output-port "not an output port" p))]))
|
2007-05-05 19:53:16 -04:00
|
|
|
|
|
|
|
(define open-output-file
|
|
|
|
(case-lambda
|
|
|
|
[(filename)
|
|
|
|
(if (string? filename)
|
|
|
|
($open-output-file filename 'error)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'open-output-file "not a string" filename))]
|
2007-05-05 19:53:16 -04:00
|
|
|
[(filename options)
|
|
|
|
(if (string? filename)
|
|
|
|
($open-output-file filename options)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'open-output-file "not a string" filename))]))
|
2007-05-05 19:53:16 -04:00
|
|
|
|
|
|
|
(define with-output-to-file
|
|
|
|
(lambda (name proc . args)
|
|
|
|
(unless (string? name)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'with-output-to-file "not a string" name))
|
2007-05-05 19:53:16 -04:00
|
|
|
(unless (procedure? proc)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'with-output-to-file "not a procedure" proc))
|
2007-05-05 19:53:16 -04:00
|
|
|
(let ([p (apply open-output-file name args)]
|
|
|
|
[shot #f])
|
|
|
|
(call-with-values
|
|
|
|
(lambda ()
|
|
|
|
(parameterize ([current-output-port p])
|
|
|
|
(proc)))
|
|
|
|
(case-lambda
|
|
|
|
[(v) (close-output-port p) v]
|
|
|
|
[v*
|
|
|
|
(close-output-port p)
|
|
|
|
(apply values v*)])))))
|
|
|
|
|
|
|
|
(define call-with-output-file
|
|
|
|
(lambda (name proc . args)
|
|
|
|
(unless (string? name)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'call-with-output-file "not a string" name))
|
2007-05-05 19:53:16 -04:00
|
|
|
(unless (procedure? proc)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'call-with-output-file "not a procedure" proc))
|
2007-05-05 19:53:16 -04:00
|
|
|
(let ([p (apply open-output-file name args)])
|
|
|
|
(call-with-values (lambda () (proc p))
|
|
|
|
(case-lambda
|
|
|
|
[(v) (close-output-port p) v]
|
|
|
|
[v*
|
|
|
|
(close-output-port p)
|
|
|
|
(apply values v*)])))))
|
|
|
|
|
|
|
|
(set! *standard-output-port*
|
|
|
|
(make-output-port
|
|
|
|
(make-output-file-handler 1 '*stdout*)
|
2007-05-18 16:07:58 -04:00
|
|
|
($make-bytevector 4096)))
|
2007-05-05 19:53:16 -04:00
|
|
|
(set! *current-output-port* *standard-output-port*)
|
|
|
|
(set! *standard-error-port*
|
|
|
|
(make-output-port
|
|
|
|
(make-output-file-handler 2 '*stderr*)
|
2007-05-18 16:07:58 -04:00
|
|
|
($make-bytevector 4096))) )
|