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 | ||
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 |
61 | capable of delivering characters from the file. If the file | |
62 | cannot 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 |
67 | output port capable of writing characters to a new file by that | |
68 | name. If the file cannot be opened, an error is signalled. If a | |
69 | file 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 |
80 | string naming a file. The file must | |
81 | already exist. These procedures call PROC | |
82 | with one argument: the port obtained by opening the named file for | |
83 | input or output. If the file cannot be opened, an error is | |
84 | signalled. If the procedure returns, then the port is closed | |
85 | automatically and the value yielded by the procedure is returned. | |
86 | If the procedure does not return, then the port will not be closed | |
87 | automatically unless it is possible to prove that the port will | |
88 | never 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 |
96 | string naming a file. The behaviour is unspecified if the file | |
97 | already exists. These procedures call PROC | |
98 | with one argument: the port obtained by opening the named file for | |
99 | input or output. If the file cannot be opened, an error is | |
100 | signalled. If the procedure returns, then the port is closed | |
101 | automatically and the value yielded by the procedure is returned. | |
102 | If the procedure does not return, then the port will not be closed | |
103 | automatically unless it is possible to prove that the port will | |
104 | never 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 |
124 | string naming a file. The file must already exist. The file is opened for | |
125 | input, an input port connected to it is made | |
126 | the default value returned by `current-input-port', | |
127 | and the THUNK is called with no arguments. | |
128 | When the THUNK returns, the port is closed and the previous | |
129 | default is restored. Returns the value yielded by THUNK. If an | |
130 | escape procedure is used to escape from the continuation of these | |
131 | procedures, 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 |
139 | string naming a file. The effect is unspecified if the file already exists. | |
140 | The file is opened for output, an output port connected to it is made | |
141 | the default value returned by `current-output-port', | |
142 | and the THUNK is called with no arguments. | |
143 | When the THUNK returns, the port is closed and the previous | |
144 | default is restored. Returns the value yielded by THUNK. If an | |
145 | escape procedure is used to escape from the continuation of these | |
146 | procedures, 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 |
154 | string naming a file. The effect is unspecified if the file already exists. | |
155 | The file is opened for output, an output port connected to it is made | |
156 | the default value returned by `current-error-port', | |
157 | and the THUNK is called with no arguments. | |
158 | When the THUNK returns, the port is closed and the previous | |
159 | default is restored. Returns the value yielded by THUNK. If an | |
160 | escape procedure is used to escape from the continuation of these | |
161 | procedures, 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. |
169 | The test of STRING is opened for | |
170 | input, an input port connected to it is made, | |
171 | and the THUNK is called with no arguments. | |
172 | When the THUNK returns, the port is closed. | |
173 | Returns the value yielded by THUNK. If an | |
174 | escape procedure is used to escape from the continuation of these | |
175 | procedures, 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))) |