Commit | Line | Data |
---|---|---|
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 |
85 | capable of delivering characters from the file. If the file | |
86 | cannot 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 |
91 | output port capable of writing characters to a new file by that | |
92 | name. If the file cannot be opened, an error is signalled. If a | |
93 | file 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 |
104 | string naming a file. The file must | |
105 | already exist. These procedures call PROC | |
106 | with one argument: the port obtained by opening the named file for | |
107 | input or output. If the file cannot be opened, an error is | |
108 | signalled. If the procedure returns, then the port is closed | |
109 | automatically and the value yielded by the procedure is returned. | |
110 | If the procedure does not return, then the port will not be closed | |
111 | automatically unless it is possible to prove that the port will | |
112 | never 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 |
120 | string naming a file. The behaviour is unspecified if the file | |
121 | already exists. These procedures call PROC | |
122 | with one argument: the port obtained by opening the named file for | |
123 | input or output. If the file cannot be opened, an error is | |
124 | signalled. If the procedure returns, then the port is closed | |
125 | automatically and the value yielded by the procedure is returned. | |
126 | If the procedure does not return, then the port will not be closed | |
127 | automatically unless it is possible to prove that the port will | |
128 | never 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 |
148 | string naming a file. The file must already exist. The file is opened for | |
149 | input, an input port connected to it is made | |
150 | the default value returned by `current-input-port', | |
151 | and the THUNK is called with no arguments. | |
152 | When the THUNK returns, the port is closed and the previous | |
153 | default is restored. Returns the value yielded by THUNK. If an | |
154 | escape procedure is used to escape from the continuation of these | |
155 | procedures, 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 |
163 | string naming a file. The effect is unspecified if the file already exists. | |
164 | The file is opened for output, an output port connected to it is made | |
165 | the default value returned by `current-output-port', | |
166 | and the THUNK is called with no arguments. | |
167 | When the THUNK returns, the port is closed and the previous | |
168 | default is restored. Returns the value yielded by THUNK. If an | |
169 | escape procedure is used to escape from the continuation of these | |
170 | procedures, 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 |
178 | string naming a file. The effect is unspecified if the file already exists. | |
179 | The file is opened for output, an output port connected to it is made | |
180 | the default value returned by `current-error-port', | |
181 | and the THUNK is called with no arguments. | |
182 | When the THUNK returns, the port is closed and the previous | |
183 | default is restored. Returns the value yielded by THUNK. If an | |
184 | escape procedure is used to escape from the continuation of these | |
185 | procedures, 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. |
193 | The test of STRING is opened for | |
194 | input, an input port connected to it is made, | |
195 | and the THUNK is called with no arguments. | |
196 | When the THUNK returns, the port is closed. | |
197 | Returns the value yielded by THUNK. If an | |
198 | escape procedure is used to escape from the continuation of these | |
199 | procedures, 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))) |