* r4rs.scm (close-input-port, close-output-port): Removed.
[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))
31
32\f
33;;;; Basic Port Code
34
35;;; Specifically, the parts of the low-level port code that are written in
36;;; Scheme rather than C.
37;;;
38;;; WARNING: the parts of this interface that refer to file ports
39;;; are going away. It would be gone already except that it is used
40;;; "internally" in a few places.
41
42
670600bd
JB
43;;; OPEN_READ, OPEN_WRITE, and OPEN_BOTH are used to request the
44;;; proper mode to open files in.
45;;;
46;;; If we want to support systems that do CRLF->LF translation, like
47;;; Windows, then we should have a symbol in scmconfig.h made visible
48;;; to the Scheme level that we can test here, and autoconf magic to
49;;; #define it when appropriate. Windows will probably just have a
50;;; hand-generated scmconfig.h file.
51(define OPEN_READ "r")
52(define OPEN_WRITE "w")
53(define OPEN_BOTH "r+")
98c27b65
JB
54
55(define *null-device* "/dev/null")
56
57(define (open-input-file str)
e3d8af60
GB
58 "Takes a string naming an existing file and returns an input port
59capable of delivering characters from the file. If the file
60cannot be opened, an error is signalled."
98c27b65
JB
61 (open-file str OPEN_READ))
62
63(define (open-output-file str)
e3d8af60
GB
64 "Takes a string naming an output file to be created and returns an
65output port capable of writing characters to a new file by that
66name. If the file cannot be opened, an error is signalled. If a
67file with the given name already exists, the effect is unspecified."
98c27b65
JB
68 (open-file str OPEN_WRITE))
69
e3d8af60
GB
70(define (open-io-file str)
71 "Open file with name STR for both input and output."
72 (open-file str OPEN_BOTH))
73
98c27b65
JB
74(define close-io-port close-port)
75
76(define (call-with-input-file str proc)
e3d8af60
GB
77 "PROC should be a procedure of one argument, and STR should be a
78string naming a file. The file must
79already exist. These procedures call PROC
80with one argument: the port obtained by opening the named file for
81input or output. If the file cannot be opened, an error is
82signalled. If the procedure returns, then the port is closed
83automatically and the value yielded by the procedure is returned.
84If the procedure does not return, then the port will not be closed
85automatically unless it is possible to prove that the port will
86never again be used for a read or write operation."
98c27b65
JB
87 (let* ((file (open-input-file str))
88 (ans (proc file)))
89 (close-input-port file)
90 ans))
91
92(define (call-with-output-file str proc)
e3d8af60
GB
93 "PROC should be a procedure of one argument, and STR should be a
94string naming a file. The behaviour is unspecified if the file
95already exists. These procedures call PROC
96with one argument: the port obtained by opening the named file for
97input or output. If the file cannot be opened, an error is
98signalled. If the procedure returns, then the port is closed
99automatically and the value yielded by the procedure is returned.
100If the procedure does not return, then the port will not be closed
101automatically unless it is possible to prove that the port will
102never again be used for a read or write operation."
98c27b65
JB
103 (let* ((file (open-output-file str))
104 (ans (proc file)))
105 (close-output-port file)
106 ans))
107
108(define (with-input-from-port port thunk)
109 (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
110 (dynamic-wind swaports thunk swaports)))
111
112(define (with-output-to-port port thunk)
113 (let* ((swaports (lambda () (set! port (set-current-output-port port)))))
114 (dynamic-wind swaports thunk swaports)))
115
116(define (with-error-to-port port thunk)
117 (let* ((swaports (lambda () (set! port (set-current-error-port port)))))
118 (dynamic-wind swaports thunk swaports)))
119
120(define (with-input-from-file file thunk)
e3d8af60
GB
121 "THUNK must be a procedure of no arguments, and FILE must be a
122string naming a file. The file must already exist. The file is opened for
123input, an input port connected to it is made
124the default value returned by `current-input-port',
125and the THUNK is called with no arguments.
126When the THUNK returns, the port is closed and the previous
127default is restored. Returns the value yielded by THUNK. If an
128escape procedure is used to escape from the continuation of these
129procedures, their behavior is implementation dependent."
98c27b65
JB
130 (let* ((nport (open-input-file file))
131 (ans (with-input-from-port nport thunk)))
132 (close-port nport)
133 ans))
134
135(define (with-output-to-file file thunk)
e3d8af60
GB
136 "THUNK must be a procedure of no arguments, and FILE must be a
137string naming a file. The effect is unspecified if the file already exists.
138The file is opened for output, an output port connected to it is made
139the default value returned by `current-output-port',
140and the THUNK is called with no arguments.
141When the THUNK returns, the port is closed and the previous
142default is restored. Returns the value yielded by THUNK. If an
143escape procedure is used to escape from the continuation of these
144procedures, their behavior is implementation dependent."
98c27b65
JB
145 (let* ((nport (open-output-file file))
146 (ans (with-output-to-port nport thunk)))
147 (close-port nport)
148 ans))
149
150(define (with-error-to-file file thunk)
e3d8af60
GB
151 "THUNK must be a procedure of no arguments, and FILE must be a
152string naming a file. The effect is unspecified if the file already exists.
153The file is opened for output, an output port connected to it is made
154the default value returned by `current-error-port',
155and the THUNK is called with no arguments.
156When the THUNK returns, the port is closed and the previous
157default is restored. Returns the value yielded by THUNK. If an
158escape procedure is used to escape from the continuation of these
159procedures, their behavior is implementation dependent."
98c27b65
JB
160 (let* ((nport (open-output-file file))
161 (ans (with-error-to-port nport thunk)))
162 (close-port nport)
163 ans))
164
165(define (with-input-from-string string thunk)
e3d8af60
GB
166 "THUNK must be a procedure of no arguments.
167The test of STRING is opened for
168input, an input port connected to it is made,
169and the THUNK is called with no arguments.
170When the THUNK returns, the port is closed.
171Returns the value yielded by THUNK. If an
172escape procedure is used to escape from the continuation of these
173procedures, their behavior is implementation dependent."
98c27b65
JB
174 (call-with-input-string string
175 (lambda (p) (with-input-from-port p thunk))))
176
177(define (with-output-to-string thunk)
e3d8af60 178 "Calls THUNK and returns its output as a string."
98c27b65
JB
179 (call-with-output-string
180 (lambda (p) (with-output-to-port p thunk))))
181
182(define (with-error-to-string thunk)
e3d8af60 183 "Calls THUNK and returns its error output as a string."
98c27b65
JB
184 (call-with-output-string
185 (lambda (p) (with-error-to-port p thunk))))
186
187(define the-eof-object (call-with-input-string "" (lambda (p) (read-char p))))
188
189\f
190;;;; Loading
191
0065d90e 192(if (not (defined? '%load-verbosely))
98c27b65
JB
193 (define %load-verbosely #f))
194(define (assert-load-verbosity v) (set! %load-verbosely v))
195
196(define (%load-announce file)
197 (if %load-verbosely
198 (with-output-to-port (current-error-port)
199 (lambda ()
200 (display ";;; ")
201 (display "loading ")
202 (display file)
203 (newline)
204 (force-output)))))
205
206(set! %load-hook %load-announce)
207
98c27b65
JB
208(define (load name)
209 (start-stack 'load-stack
75a97b92 210 (primitive-load name)))