(scm_init_rw_builtins): Renamed from scm_init_rw. Return
[bpt/guile.git] / ice-9 / r4rs.scm
CommitLineData
98c27b65
JB
1;;;; r4rs.scm --- definitions needed for libguile to be R4RS compliant
2;;;; Jim Blandy <jimb@cyclic.com> --- October 1996
3
483525f1 4;;;; Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
98c27b65
JB
5;;;;
6;;;; This program is free software; you can redistribute it and/or modify
7;;;; it under the terms of the GNU General Public License as published by
8;;;; the Free Software Foundation; either version 2, or (at your option)
9;;;; any later version.
10;;;;
11;;;; This program is distributed in the hope that it will be useful,
12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;;; GNU General Public License for more details.
15;;;;
16;;;; You should have received a copy of the GNU General Public License
17;;;; along with this software; see the file COPYING. If not, write to
15328041
JB
18;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19;;;; Boston, MA 02111-1307 USA
98c27b65
JB
20
21\f
22;;;; apply and call-with-current-continuation
23
06a02069
MD
24;;; We want these to be tail-recursive, so instead of using primitive
25;;; procedures, we define them as closures in terms of the primitive
26;;; macros @apply and @call-with-current-continuation.
98c27b65 27(set! apply (lambda (fun . args) (@apply fun (apply:nconc2last args))))
8fa5faad 28(set-procedure-property! apply 'name 'apply)
98c27b65
JB
29(define (call-with-current-continuation proc)
30 (@call-with-current-continuation proc))
218da2b9
MV
31(define (call-with-values producer consumer)
32 (@call-with-values producer consumer))
98c27b65
JB
33
34\f
35;;;; Basic Port Code
36
37;;; Specifically, the parts of the low-level port code that are written in
38;;; Scheme rather than C.
39;;;
40;;; WARNING: the parts of this interface that refer to file ports
41;;; are going away. It would be gone already except that it is used
42;;; "internally" in a few places.
43
44
670600bd
JB
45;;; OPEN_READ, OPEN_WRITE, and OPEN_BOTH are used to request the
46;;; proper mode to open files in.
47;;;
48;;; If we want to support systems that do CRLF->LF translation, like
49;;; Windows, then we should have a symbol in scmconfig.h made visible
50;;; to the Scheme level that we can test here, and autoconf magic to
51;;; #define it when appropriate. Windows will probably just have a
52;;; hand-generated scmconfig.h file.
53(define OPEN_READ "r")
54(define OPEN_WRITE "w")
55(define OPEN_BOTH "r+")
98c27b65
JB
56
57(define *null-device* "/dev/null")
58
59(define (open-input-file str)
e3d8af60
GB
60 "Takes a string naming an existing file and returns an input port
61capable of delivering characters from the file. If the file
62cannot be opened, an error is signalled."
98c27b65
JB
63 (open-file str OPEN_READ))
64
65(define (open-output-file str)
e3d8af60
GB
66 "Takes a string naming an output file to be created and returns an
67output port capable of writing characters to a new file by that
68name. If the file cannot be opened, an error is signalled. If a
69file with the given name already exists, the effect is unspecified."
98c27b65
JB
70 (open-file str OPEN_WRITE))
71
e3d8af60
GB
72(define (open-io-file str)
73 "Open file with name STR for both input and output."
74 (open-file str OPEN_BOTH))
75
98c27b65
JB
76(define close-io-port close-port)
77
78(define (call-with-input-file str proc)
e3d8af60
GB
79 "PROC should be a procedure of one argument, and STR should be a
80string naming a file. The file must
81already exist. These procedures call PROC
82with one argument: the port obtained by opening the named file for
83input or output. If the file cannot be opened, an error is
84signalled. If the procedure returns, then the port is closed
85automatically and the value yielded by the procedure is returned.
86If the procedure does not return, then the port will not be closed
87automatically unless it is possible to prove that the port will
88never again be used for a read or write operation."
98c27b65
JB
89 (let* ((file (open-input-file str))
90 (ans (proc file)))
91 (close-input-port file)
92 ans))
93
94(define (call-with-output-file str proc)
e3d8af60
GB
95 "PROC should be a procedure of one argument, and STR should be a
96string naming a file. The behaviour is unspecified if the file
97already exists. These procedures call PROC
98with one argument: the port obtained by opening the named file for
99input or output. If the file cannot be opened, an error is
100signalled. If the procedure returns, then the port is closed
101automatically and the value yielded by the procedure is returned.
102If the procedure does not return, then the port will not be closed
103automatically unless it is possible to prove that the port will
104never again be used for a read or write operation."
98c27b65
JB
105 (let* ((file (open-output-file str))
106 (ans (proc file)))
107 (close-output-port file)
108 ans))
109
110(define (with-input-from-port port thunk)
111 (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
112 (dynamic-wind swaports thunk swaports)))
113
114(define (with-output-to-port port thunk)
115 (let* ((swaports (lambda () (set! port (set-current-output-port port)))))
116 (dynamic-wind swaports thunk swaports)))
117
118(define (with-error-to-port port thunk)
119 (let* ((swaports (lambda () (set! port (set-current-error-port port)))))
120 (dynamic-wind swaports thunk swaports)))
121
122(define (with-input-from-file file thunk)
e3d8af60
GB
123 "THUNK must be a procedure of no arguments, and FILE must be a
124string naming a file. The file must already exist. The file is opened for
125input, an input port connected to it is made
126the default value returned by `current-input-port',
127and the THUNK is called with no arguments.
128When the THUNK returns, the port is closed and the previous
129default is restored. Returns the value yielded by THUNK. If an
130escape procedure is used to escape from the continuation of these
131procedures, their behavior is implementation dependent."
98c27b65
JB
132 (let* ((nport (open-input-file file))
133 (ans (with-input-from-port nport thunk)))
134 (close-port nport)
135 ans))
136
137(define (with-output-to-file file thunk)
e3d8af60
GB
138 "THUNK must be a procedure of no arguments, and FILE must be a
139string naming a file. The effect is unspecified if the file already exists.
140The file is opened for output, an output port connected to it is made
141the default value returned by `current-output-port',
142and the THUNK is called with no arguments.
143When the THUNK returns, the port is closed and the previous
144default is restored. Returns the value yielded by THUNK. If an
145escape procedure is used to escape from the continuation of these
146procedures, their behavior is implementation dependent."
98c27b65
JB
147 (let* ((nport (open-output-file file))
148 (ans (with-output-to-port nport thunk)))
149 (close-port nport)
150 ans))
151
152(define (with-error-to-file file thunk)
e3d8af60
GB
153 "THUNK must be a procedure of no arguments, and FILE must be a
154string naming a file. The effect is unspecified if the file already exists.
155The file is opened for output, an output port connected to it is made
156the default value returned by `current-error-port',
157and the THUNK is called with no arguments.
158When the THUNK returns, the port is closed and the previous
159default is restored. Returns the value yielded by THUNK. If an
160escape procedure is used to escape from the continuation of these
161procedures, their behavior is implementation dependent."
98c27b65
JB
162 (let* ((nport (open-output-file file))
163 (ans (with-error-to-port nport thunk)))
164 (close-port nport)
165 ans))
166
167(define (with-input-from-string string thunk)
e3d8af60
GB
168 "THUNK must be a procedure of no arguments.
169The test of STRING is opened for
170input, an input port connected to it is made,
171and the THUNK is called with no arguments.
172When the THUNK returns, the port is closed.
173Returns the value yielded by THUNK. If an
174escape procedure is used to escape from the continuation of these
175procedures, their behavior is implementation dependent."
98c27b65
JB
176 (call-with-input-string string
177 (lambda (p) (with-input-from-port p thunk))))
178
179(define (with-output-to-string thunk)
e3d8af60 180 "Calls THUNK and returns its output as a string."
98c27b65
JB
181 (call-with-output-string
182 (lambda (p) (with-output-to-port p thunk))))
183
184(define (with-error-to-string thunk)
e3d8af60 185 "Calls THUNK and returns its error output as a string."
98c27b65
JB
186 (call-with-output-string
187 (lambda (p) (with-error-to-port p thunk))))
188
189(define the-eof-object (call-with-input-string "" (lambda (p) (read-char p))))
190
191\f
192;;;; Loading
193
0065d90e 194(if (not (defined? '%load-verbosely))
98c27b65
JB
195 (define %load-verbosely #f))
196(define (assert-load-verbosity v) (set! %load-verbosely v))
197
198(define (%load-announce file)
199 (if %load-verbosely
200 (with-output-to-port (current-error-port)
201 (lambda ()
202 (display ";;; ")
203 (display "loading ")
204 (display file)
205 (newline)
206 (force-output)))))
207
208(set! %load-hook %load-announce)
209
98c27b65
JB
210(define (load name)
211 (start-stack 'load-stack
75a97b92 212 (primitive-load name)))