(scm_char_set_xor): bug fix: characters should only be included if
[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
e39bbe80 4;;;; Copyright (C) 1996, 1997, 1998, 2000, 2001 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
a482f2cc
MV
20;;;;
21;;;; As a special exception, the Free Software Foundation gives permission
22;;;; for additional uses of the text contained in its release of GUILE.
23;;;;
24;;;; The exception is that, if you link the GUILE library with other files
25;;;; to produce an executable, this does not by itself cause the
26;;;; resulting executable to be covered by the GNU General Public License.
27;;;; Your use of that executable is in no way restricted on account of
28;;;; linking the GUILE library code into it.
29;;;;
30;;;; This exception does not however invalidate any other reasons why
31;;;; the executable file might be covered by the GNU General Public License.
32;;;;
33;;;; This exception applies only to the code released by the
34;;;; Free Software Foundation under the name GUILE. If you copy
35;;;; code from other Free Software Foundation releases into a copy of
36;;;; GUILE, as the General Public License permits, the exception does
37;;;; not apply to the code that you add in this way. To avoid misleading
38;;;; anyone as to the status of such modified files, you must delete
39;;;; this exception notice from them.
40;;;;
41;;;; If you write modifications of your own for GUILE, it is your choice
42;;;; whether to permit this exception to apply to your modifications.
43;;;; If you do not wish that, delete this exception notice.
98c27b65
JB
44
45\f
46;;;; apply and call-with-current-continuation
47
06a02069
MD
48;;; We want these to be tail-recursive, so instead of using primitive
49;;; procedures, we define them as closures in terms of the primitive
50;;; macros @apply and @call-with-current-continuation.
98c27b65 51(set! apply (lambda (fun . args) (@apply fun (apply:nconc2last args))))
8fa5faad 52(set-procedure-property! apply 'name 'apply)
98c27b65
JB
53(define (call-with-current-continuation proc)
54 (@call-with-current-continuation proc))
218da2b9
MV
55(define (call-with-values producer consumer)
56 (@call-with-values producer consumer))
98c27b65
JB
57
58\f
59;;;; Basic Port Code
60
61;;; Specifically, the parts of the low-level port code that are written in
62;;; Scheme rather than C.
63;;;
64;;; WARNING: the parts of this interface that refer to file ports
65;;; are going away. It would be gone already except that it is used
66;;; "internally" in a few places.
67
68
670600bd
JB
69;;; OPEN_READ, OPEN_WRITE, and OPEN_BOTH are used to request the
70;;; proper mode to open files in.
71;;;
72;;; If we want to support systems that do CRLF->LF translation, like
73;;; Windows, then we should have a symbol in scmconfig.h made visible
74;;; to the Scheme level that we can test here, and autoconf magic to
75;;; #define it when appropriate. Windows will probably just have a
76;;; hand-generated scmconfig.h file.
77(define OPEN_READ "r")
78(define OPEN_WRITE "w")
79(define OPEN_BOTH "r+")
98c27b65
JB
80
81(define *null-device* "/dev/null")
82
83(define (open-input-file str)
e3d8af60
GB
84 "Takes a string naming an existing file and returns an input port
85capable of delivering characters from the file. If the file
86cannot be opened, an error is signalled."
98c27b65
JB
87 (open-file str OPEN_READ))
88
89(define (open-output-file str)
e3d8af60
GB
90 "Takes a string naming an output file to be created and returns an
91output port capable of writing characters to a new file by that
92name. If the file cannot be opened, an error is signalled. If a
93file with the given name already exists, the effect is unspecified."
98c27b65
JB
94 (open-file str OPEN_WRITE))
95
e3d8af60
GB
96(define (open-io-file str)
97 "Open file with name STR for both input and output."
98 (open-file str OPEN_BOTH))
99
98c27b65
JB
100(define close-io-port close-port)
101
102(define (call-with-input-file str proc)
e3d8af60
GB
103 "PROC should be a procedure of one argument, and STR should be a
104string naming a file. The file must
105already exist. These procedures call PROC
106with one argument: the port obtained by opening the named file for
107input or output. If the file cannot be opened, an error is
108signalled. If the procedure returns, then the port is closed
109automatically and the value yielded by the procedure is returned.
110If the procedure does not return, then the port will not be closed
111automatically unless it is possible to prove that the port will
112never again be used for a read or write operation."
98c27b65
JB
113 (let* ((file (open-input-file str))
114 (ans (proc file)))
115 (close-input-port file)
116 ans))
117
118(define (call-with-output-file str proc)
e3d8af60
GB
119 "PROC should be a procedure of one argument, and STR should be a
120string naming a file. The behaviour is unspecified if the file
121already exists. These procedures call PROC
122with one argument: the port obtained by opening the named file for
123input or output. If the file cannot be opened, an error is
124signalled. If the procedure returns, then the port is closed
125automatically and the value yielded by the procedure is returned.
126If the procedure does not return, then the port will not be closed
127automatically unless it is possible to prove that the port will
128never again be used for a read or write operation."
98c27b65
JB
129 (let* ((file (open-output-file str))
130 (ans (proc file)))
131 (close-output-port file)
132 ans))
133
134(define (with-input-from-port port thunk)
135 (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
136 (dynamic-wind swaports thunk swaports)))
137
138(define (with-output-to-port port thunk)
139 (let* ((swaports (lambda () (set! port (set-current-output-port port)))))
140 (dynamic-wind swaports thunk swaports)))
141
142(define (with-error-to-port port thunk)
143 (let* ((swaports (lambda () (set! port (set-current-error-port port)))))
144 (dynamic-wind swaports thunk swaports)))
145
146(define (with-input-from-file file thunk)
e3d8af60
GB
147 "THUNK must be a procedure of no arguments, and FILE must be a
148string naming a file. The file must already exist. The file is opened for
149input, an input port connected to it is made
150the default value returned by `current-input-port',
151and the THUNK is called with no arguments.
152When the THUNK returns, the port is closed and the previous
153default is restored. Returns the value yielded by THUNK. If an
154escape procedure is used to escape from the continuation of these
155procedures, their behavior is implementation dependent."
98c27b65
JB
156 (let* ((nport (open-input-file file))
157 (ans (with-input-from-port nport thunk)))
158 (close-port nport)
159 ans))
160
161(define (with-output-to-file file thunk)
e3d8af60
GB
162 "THUNK must be a procedure of no arguments, and FILE must be a
163string naming a file. The effect is unspecified if the file already exists.
164The file is opened for output, an output port connected to it is made
165the default value returned by `current-output-port',
166and the THUNK is called with no arguments.
167When the THUNK returns, the port is closed and the previous
168default is restored. Returns the value yielded by THUNK. If an
169escape procedure is used to escape from the continuation of these
170procedures, their behavior is implementation dependent."
98c27b65
JB
171 (let* ((nport (open-output-file file))
172 (ans (with-output-to-port nport thunk)))
173 (close-port nport)
174 ans))
175
176(define (with-error-to-file file thunk)
e3d8af60
GB
177 "THUNK must be a procedure of no arguments, and FILE must be a
178string naming a file. The effect is unspecified if the file already exists.
179The file is opened for output, an output port connected to it is made
180the default value returned by `current-error-port',
181and the THUNK is called with no arguments.
182When the THUNK returns, the port is closed and the previous
183default is restored. Returns the value yielded by THUNK. If an
184escape procedure is used to escape from the continuation of these
185procedures, their behavior is implementation dependent."
98c27b65
JB
186 (let* ((nport (open-output-file file))
187 (ans (with-error-to-port nport thunk)))
188 (close-port nport)
189 ans))
190
191(define (with-input-from-string string thunk)
e3d8af60
GB
192 "THUNK must be a procedure of no arguments.
193The test of STRING is opened for
194input, an input port connected to it is made,
195and the THUNK is called with no arguments.
196When the THUNK returns, the port is closed.
197Returns the value yielded by THUNK. If an
198escape procedure is used to escape from the continuation of these
199procedures, their behavior is implementation dependent."
98c27b65
JB
200 (call-with-input-string string
201 (lambda (p) (with-input-from-port p thunk))))
202
203(define (with-output-to-string thunk)
e3d8af60 204 "Calls THUNK and returns its output as a string."
98c27b65
JB
205 (call-with-output-string
206 (lambda (p) (with-output-to-port p thunk))))
207
208(define (with-error-to-string thunk)
e3d8af60 209 "Calls THUNK and returns its error output as a string."
98c27b65
JB
210 (call-with-output-string
211 (lambda (p) (with-error-to-port p thunk))))
212
213(define the-eof-object (call-with-input-string "" (lambda (p) (read-char p))))
214
215\f
216;;;; Loading
217
0065d90e 218(if (not (defined? '%load-verbosely))
98c27b65
JB
219 (define %load-verbosely #f))
220(define (assert-load-verbosity v) (set! %load-verbosely v))
221
222(define (%load-announce file)
223 (if %load-verbosely
224 (with-output-to-port (current-error-port)
225 (lambda ()
226 (display ";;; ")
227 (display "loading ")
228 (display file)
229 (newline)
230 (force-output)))))
231
232(set! %load-hook %load-announce)
233
98c27b65
JB
234(define (load name)
235 (start-stack 'load-stack
75a97b92 236 (primitive-load name)))