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