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:39:02 -04:00
|
|
|
|
|
|
|
(library (ikarus io input-files)
|
2007-05-05 19:59:28 -04:00
|
|
|
(export open-input-file current-input-port console-input-port
|
2007-11-23 03:44:48 -05:00
|
|
|
standard-input-port with-input-from-file call-with-input-file)
|
2007-05-05 19:39:02 -04:00
|
|
|
(import
|
2007-05-06 18:30:08 -04:00
|
|
|
(ikarus system $ports)
|
|
|
|
(ikarus system $io)
|
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $strings)
|
2007-05-17 06:27:59 -04:00
|
|
|
(ikarus system $bytevectors)
|
2007-05-06 18:30:08 -04:00
|
|
|
(ikarus system $chars)
|
2007-10-12 00:33:19 -04:00
|
|
|
(rnrs bytevectors)
|
2007-05-05 19:39:02 -04:00
|
|
|
(except (ikarus)
|
|
|
|
open-input-file current-input-port console-input-port
|
2007-05-05 19:59:28 -04:00
|
|
|
with-input-from-file call-with-input-file
|
2007-11-23 03:44:48 -05:00
|
|
|
standard-input-port current-input-port))
|
2007-05-05 19:39:02 -04:00
|
|
|
|
|
|
|
(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:39:02 -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:39:02 -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-input-port p)
|
|
|
|
(close-ports))])))
|
|
|
|
|
2007-06-01 22:17:22 -04:00
|
|
|
(define refill-buffer!
|
|
|
|
(lambda (p bytes)
|
|
|
|
(error 'refill-buffer! "not implemented")))
|
|
|
|
|
2007-05-17 06:44:51 -04:00
|
|
|
(define read-multibyte-char
|
2007-06-01 22:17:22 -04:00
|
|
|
(lambda (p b0)
|
2007-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($port-index p)]
|
|
|
|
[size ($port-size p)])
|
2007-06-01 22:17:22 -04:00
|
|
|
(cond
|
|
|
|
[($fx= ($fxlogand b0 #b11100000) #b11000000)
|
|
|
|
;;; 2-byte utf8 sequence
|
|
|
|
(unless ($fx< ($fx+ idx 1) size)
|
|
|
|
(refill-buffer! p 1))
|
|
|
|
(let ([b1 ($bytevector-u8-ref
|
2007-08-25 11:06:30 -04:00
|
|
|
($port-buffer p)
|
2007-06-01 22:17:22 -04:00
|
|
|
($fxadd1 idx))])
|
|
|
|
(unless ($fx= ($fxlogand b1 #b11000000) #b10000000)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read-char "invalid utf8 sequence" b0 b1))
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-index! p ($fx+ idx 2))
|
2007-06-01 22:17:22 -04:00
|
|
|
($fixnum->char
|
|
|
|
($fx+ ($fxsll ($fxlogand b0 #b11111) 6)
|
|
|
|
($fxlogand b1 #b111111))))]
|
|
|
|
[else
|
|
|
|
(error 'read-multibyte
|
2007-10-25 14:32:26 -04:00
|
|
|
"BUG: bytesequence is not supported yet" b0)]))))
|
2007-06-01 22:17:22 -04:00
|
|
|
|
2007-05-17 06:44:51 -04:00
|
|
|
(define peek-multibyte-char
|
|
|
|
(lambda (p)
|
|
|
|
(error 'peek-multibyte-char "not implemented")))
|
|
|
|
(define unread-multibyte-char
|
|
|
|
(lambda (c p)
|
|
|
|
(error 'unread-multibyte-char "not implemented")))
|
|
|
|
|
|
|
|
(define make-input-file-handler
|
|
|
|
(lambda (fd port-name)
|
|
|
|
(let ((open? #t))
|
|
|
|
(lambda (msg . args)
|
|
|
|
(message-case msg args
|
|
|
|
[(read-char p)
|
|
|
|
(unless (input-port? p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read-char "not an input port" p))
|
2007-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
|
|
|
(let ([b ($bytevector-u8-ref ($port-buffer p) idx)])
|
2007-05-17 06:44:51 -04:00
|
|
|
(cond
|
|
|
|
[($fx< b 128)
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-index! p ($fxadd1 idx))
|
2007-05-17 06:44:51 -04:00
|
|
|
($fixnum->char b)]
|
2007-06-01 22:17:22 -04:00
|
|
|
[else (read-multibyte-char p b)]))
|
2007-05-17 06:44:51 -04:00
|
|
|
(if open?
|
|
|
|
(let ([bytes
|
|
|
|
(foreign-call "ikrt_read"
|
2007-08-25 11:06:30 -04:00
|
|
|
fd ($port-buffer p))])
|
2007-05-17 06:44:51 -04:00
|
|
|
(cond
|
|
|
|
[($fx> bytes 0)
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-size! p bytes)
|
2007-05-17 06:44:51 -04:00
|
|
|
($read-char p)]
|
|
|
|
[($fx= bytes 0)
|
|
|
|
(eof-object)]
|
|
|
|
[else
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read-char "Cannot read from file"
|
2007-05-17 06:44:51 -04:00
|
|
|
port-name)]))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'read-char "port is closed" p))))]
|
2007-11-22 14:26:54 -05:00
|
|
|
[(get-u8 p)
|
|
|
|
(unless (input-port? p)
|
|
|
|
(error 'get-u8 "not an input port" p))
|
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
|
|
|
(let ([b ($bytevector-u8-ref ($port-buffer p) idx)])
|
|
|
|
($set-port-index! p ($fxadd1 idx))
|
|
|
|
b)
|
|
|
|
(if open?
|
|
|
|
(let ([bytes
|
|
|
|
(foreign-call "ikrt_read"
|
|
|
|
fd ($port-buffer p))])
|
|
|
|
(cond
|
|
|
|
[($fx> bytes 0)
|
|
|
|
($set-port-size! p bytes)
|
|
|
|
($get-u8 p)]
|
|
|
|
[($fx= bytes 0)
|
|
|
|
(eof-object)]
|
|
|
|
[else
|
|
|
|
(error 'get-u8 "Cannot read from file" port-name)]))
|
|
|
|
(error 'get-u8 "port is closed" p))))]
|
2007-05-17 06:44:51 -04:00
|
|
|
[(peek-char p)
|
|
|
|
(unless (input-port? p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'peek-char "not an input port" p))
|
2007-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
|
|
|
(let ([b ($bytevector-u8-ref ($port-buffer p) idx)])
|
2007-05-17 06:44:51 -04:00
|
|
|
(cond
|
|
|
|
[($fx< b 128) ($fixnum->char b)]
|
|
|
|
[else (peek-multibyte-char p)]))
|
|
|
|
(if open?
|
|
|
|
(let ([bytes
|
|
|
|
(foreign-call "ikrt_read" fd
|
|
|
|
(port-input-buffer p))])
|
|
|
|
(cond
|
|
|
|
[(not bytes)
|
|
|
|
(error 'peek-char
|
2007-10-25 14:32:26 -04:00
|
|
|
"Cannot read from file" port-name)]
|
2007-05-17 06:44:51 -04:00
|
|
|
[($fx= bytes 0)
|
|
|
|
(eof-object)]
|
|
|
|
[else
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-size! p bytes)
|
2007-05-17 06:44:51 -04:00
|
|
|
($peek-char p)]))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'peek-char "port is closed" p))))]
|
2007-05-17 06:44:51 -04:00
|
|
|
[(unread-char c p)
|
|
|
|
(unless (input-port? p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'unread-char "not an input port" p))
|
2007-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($fxsub1 ($port-index p))]
|
2007-05-17 06:44:51 -04:00
|
|
|
[b (if (char? c)
|
|
|
|
($char->fixnum c)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'unread-char "not a char" c))])
|
2007-05-17 06:44:51 -04:00
|
|
|
(if (and ($fx>= idx 0)
|
2007-08-25 11:06:30 -04:00
|
|
|
($fx< idx ($port-size p)))
|
2007-05-17 06:44:51 -04:00
|
|
|
(cond
|
|
|
|
[($fx< b 128)
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-index! p idx)]
|
2007-05-17 06:44:51 -04:00
|
|
|
[else (unread-multibyte-char c p)])
|
|
|
|
(if open?
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'unread-char "port is closed" p)
|
2007-05-17 06:44:51 -04:00
|
|
|
(error 'unread-char "too many unread-chars"))))]
|
|
|
|
[(port-name p) port-name]
|
|
|
|
[(close-port p)
|
|
|
|
(unless (input-port? p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'close-input-port "not an input port" p))
|
2007-05-17 06:44:51 -04:00
|
|
|
(when open?
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-size! p 0)
|
2007-05-17 06:44:51 -04:00
|
|
|
(set! open? #f)
|
|
|
|
(unless (foreign-call "ikrt_close_file" fd)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'close-input-port "cannot close port" port-name)))]
|
2007-05-17 06:44:51 -04:00
|
|
|
[else
|
|
|
|
(error 'input-file-handler
|
2007-10-25 14:32:26 -04:00
|
|
|
"message not handled" (cons msg args))])))))
|
2007-05-17 06:44:51 -04:00
|
|
|
|
2007-05-05 19:39:02 -04:00
|
|
|
(define $open-input-file
|
|
|
|
(lambda (filename)
|
|
|
|
(close-ports)
|
2007-05-18 21:03:14 -04:00
|
|
|
(let ([fd/error (foreign-call "ikrt_open_input_file"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 filename))])
|
2007-05-05 19:39:02 -04:00
|
|
|
(if (fixnum? fd/error)
|
|
|
|
(let ([port (make-input-port
|
|
|
|
(make-input-file-handler fd/error filename)
|
2007-05-17 06:44:51 -04:00
|
|
|
($make-bytevector 4096))])
|
|
|
|
(set-port-input-size! port 0)
|
2007-05-05 19:39:02 -04:00
|
|
|
(guardian port)
|
|
|
|
port)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'open-input-file "cannot open file"
|
|
|
|
filename fd/error)))))
|
2007-05-05 19:39:02 -04:00
|
|
|
|
|
|
|
(define open-input-file
|
|
|
|
(lambda (filename)
|
|
|
|
(if (string? filename)
|
|
|
|
($open-input-file filename)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'open-input-file "not a string" filename))))
|
2007-05-05 19:39:02 -04:00
|
|
|
|
2007-05-05 19:59:28 -04:00
|
|
|
(define with-input-from-file
|
|
|
|
(lambda (name proc)
|
|
|
|
(unless (string? name)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'with-input-from-file "not a string" name))
|
2007-05-05 19:59:28 -04:00
|
|
|
(unless (procedure? proc)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'with-input-from-file "not a procedure" proc))
|
2007-05-05 19:59:28 -04:00
|
|
|
(let ([p ($open-input-file name)])
|
|
|
|
(call-with-values
|
|
|
|
(lambda ()
|
|
|
|
(parameterize ([current-input-port p])
|
|
|
|
(proc)))
|
|
|
|
(case-lambda
|
|
|
|
[(v) (close-input-port p) v]
|
|
|
|
[v*
|
|
|
|
(close-input-port p)
|
|
|
|
(apply values v*)])))))
|
|
|
|
|
|
|
|
(define call-with-input-file
|
|
|
|
(lambda (name proc)
|
|
|
|
(unless (string? name)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'call-with-input-file "not a string" name))
|
2007-05-05 19:59:28 -04:00
|
|
|
(unless (procedure? proc)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'call-with-input-file "not a procedure" proc))
|
2007-05-05 19:59:28 -04:00
|
|
|
(let ([p ($open-input-file name)])
|
|
|
|
(call-with-values (lambda () (proc p))
|
|
|
|
(case-lambda
|
|
|
|
[(v) (close-input-port p) v]
|
|
|
|
[v*
|
|
|
|
(close-input-port p)
|
|
|
|
(apply values v*)])))))
|
|
|
|
|
2007-05-05 19:39:02 -04:00
|
|
|
(define *standard-input-port* #f)
|
|
|
|
(define *current-input-port* #f)
|
|
|
|
|
|
|
|
(define console-input-port
|
|
|
|
(lambda () *standard-input-port*))
|
|
|
|
|
2007-11-23 03:44:48 -05:00
|
|
|
(define standard-input-port
|
|
|
|
(lambda () *standard-input-port*))
|
|
|
|
|
2007-05-05 19:39:02 -04:00
|
|
|
(define current-input-port
|
|
|
|
(case-lambda
|
|
|
|
[() *current-input-port*]
|
|
|
|
[(p)
|
|
|
|
(if (input-port? p)
|
|
|
|
(set! *current-input-port* p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'current-input-port "not an input-port" p))]))
|
2007-05-05 19:39:02 -04:00
|
|
|
|
|
|
|
(set! *standard-input-port*
|
|
|
|
(let ([p (make-input-port
|
|
|
|
(make-input-file-handler 0 '*stdin*)
|
2007-05-17 06:44:51 -04:00
|
|
|
($make-bytevector 4096))])
|
|
|
|
(set-port-input-size! p 0)
|
2007-05-05 19:39:02 -04:00
|
|
|
p))
|
|
|
|
(set! *current-input-port* *standard-input-port*)
|
|
|
|
)
|