Commit | Line | Data |
---|---|---|
be54b15d | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
a0599745 | 47 | #include "libguile/_scm.h" |
681b9005 MD |
48 | |
49 | #include <stdio.h> | |
50 | #ifdef HAVE_UNISTD_H | |
51 | #include <unistd.h> | |
52 | #endif | |
53 | ||
a0599745 MD |
54 | #include "libguile/unif.h" |
55 | #include "libguile/eval.h" | |
56 | #include "libguile/ports.h" | |
57 | #include "libguile/read.h" | |
58 | #include "libguile/root.h" | |
59 | #include "libguile/strings.h" | |
07bcf91d | 60 | #include "libguile/modules.h" |
fe78b6c0 | 61 | #include "libguile/validate.h" |
ca13a04a | 62 | #include "libguile/deprecation.h" |
20e6290e | 63 | |
a0599745 | 64 | #include "libguile/strports.h" |
0f2d19dd | 65 | |
95b88819 GH |
66 | #ifdef HAVE_STRING_H |
67 | #include <string.h> | |
68 | #endif | |
69 | ||
0f2d19dd JB |
70 | \f |
71 | ||
72 | /* {Ports - string ports} | |
73 | * | |
74 | */ | |
75 | ||
3fe6190f GH |
76 | /* NOTES: |
77 | write_buf/write_end point to the ends of the allocated string. | |
78 | read_buf/read_end in principle point to the part of the string which | |
79 | has been written to, but this is only updated after a flush. | |
80 | read_pos and write_pos in principle should be equal, but this is only true | |
61e452ba | 81 | when rw_active is SCM_PORT_NEITHER. |
3fe6190f | 82 | */ |
1cc91f1b | 83 | |
92c2555f | 84 | scm_t_bits scm_tc16_strport; |
a98bddfd DH |
85 | |
86 | ||
ee149d03 JB |
87 | static int |
88 | stfill_buffer (SCM port) | |
0f2d19dd | 89 | { |
92c2555f | 90 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
ee149d03 | 91 | |
ee149d03 JB |
92 | if (pt->read_pos >= pt->read_end) |
93 | return EOF; | |
94 | else | |
41b0806d | 95 | return scm_return_first_int (*pt->read_pos, port); |
0f2d19dd JB |
96 | } |
97 | ||
3fe6190f GH |
98 | /* change the size of a port's string to new_size. this doesn't |
99 | change read_buf_size. */ | |
754c9491 | 100 | static void |
92c2555f | 101 | st_resize_port (scm_t_port *pt, off_t new_size) |
754c9491 | 102 | { |
94115ae3 | 103 | SCM old_stream = SCM_PACK (pt->stream); |
be54b15d | 104 | SCM new_stream = scm_allocate_string (new_size); |
c014a02e ML |
105 | unsigned long int old_size = SCM_STRING_LENGTH (old_stream); |
106 | unsigned long int min_size = min (old_size, new_size); | |
107 | unsigned long int i; | |
74a16888 | 108 | |
3fe6190f GH |
109 | off_t index = pt->write_pos - pt->write_buf; |
110 | ||
111 | pt->write_buf_size = new_size; | |
112 | ||
94115ae3 DH |
113 | for (i = 0; i != min_size; ++i) |
114 | SCM_STRING_CHARS (new_stream) [i] = SCM_STRING_CHARS (old_stream) [i]; | |
754c9491 | 115 | |
94115ae3 | 116 | /* reset buffer. */ |
754c9491 | 117 | { |
729dbac3 | 118 | pt->stream = SCM_UNPACK (new_stream); |
94115ae3 | 119 | pt->read_buf = pt->write_buf = SCM_STRING_UCHARS (new_stream); |
3fe6190f GH |
120 | pt->read_pos = pt->write_pos = pt->write_buf + index; |
121 | pt->write_end = pt->write_buf + pt->write_buf_size; | |
122 | pt->read_end = pt->read_buf + pt->read_buf_size; | |
754c9491 JB |
123 | } |
124 | } | |
125 | ||
3fe6190f GH |
126 | /* amount by which write_buf is expanded. */ |
127 | #define SCM_WRITE_BLOCK 80 | |
128 | ||
129 | /* ensure that write_pos < write_end by enlarging the buffer when | |
130 | necessary. update read_buf to account for written chars. */ | |
ee149d03 JB |
131 | static void |
132 | st_flush (SCM port) | |
0f2d19dd | 133 | { |
92c2555f | 134 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
ee149d03 JB |
135 | |
136 | if (pt->write_pos == pt->write_end) | |
137 | { | |
3fe6190f | 138 | st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK); |
754c9491 JB |
139 | } |
140 | pt->read_pos = pt->write_pos; | |
3fe6190f GH |
141 | if (pt->read_pos > pt->read_end) |
142 | { | |
143 | pt->read_end = (unsigned char *) pt->read_pos; | |
144 | pt->read_buf_size = pt->read_end - pt->read_buf; | |
145 | } | |
61e452ba | 146 | pt->rw_active = SCM_PORT_NEITHER; |
754c9491 JB |
147 | } |
148 | ||
31703ab8 | 149 | static void |
8aa011a1 | 150 | st_write (SCM port, const void *data, size_t size) |
31703ab8 | 151 | { |
92c2555f | 152 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
31703ab8 GH |
153 | const char *input = (char *) data; |
154 | ||
155 | while (size > 0) | |
156 | { | |
157 | int space = pt->write_end - pt->write_pos; | |
158 | int write_len = (size > space) ? space : size; | |
159 | ||
a4c7145b | 160 | strncpy ((char *) pt->write_pos, input, write_len); |
31703ab8 GH |
161 | pt->write_pos += write_len; |
162 | size -= write_len; | |
163 | input += write_len; | |
164 | if (write_len == space) | |
165 | st_flush (port); | |
166 | } | |
167 | } | |
168 | ||
754c9491 | 169 | static void |
affc96b5 | 170 | st_end_input (SCM port, int offset) |
754c9491 | 171 | { |
92c2555f | 172 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
7dcb364d | 173 | |
cd19d608 GH |
174 | if (pt->read_pos - pt->read_buf < offset) |
175 | scm_misc_error ("st_end_input", "negative position", SCM_EOL); | |
176 | ||
a3c8b9fc | 177 | pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset); |
61e452ba | 178 | pt->rw_active = SCM_PORT_NEITHER; |
754c9491 JB |
179 | } |
180 | ||
181 | static off_t | |
182 | st_seek (SCM port, off_t offset, int whence) | |
183 | { | |
92c2555f | 184 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
754c9491 JB |
185 | off_t target; |
186 | ||
7dcb364d GH |
187 | if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR) |
188 | /* special case to avoid disturbing the unread-char buffer. */ | |
754c9491 | 189 | { |
7dcb364d GH |
190 | if (pt->read_buf == pt->putback_buf) |
191 | { | |
192 | target = pt->saved_read_pos - pt->saved_read_buf | |
193 | - (pt->read_end - pt->read_pos); | |
194 | } | |
195 | else | |
196 | { | |
197 | target = pt->read_pos - pt->read_buf; | |
198 | } | |
754c9491 | 199 | } |
7dcb364d GH |
200 | else |
201 | /* all other cases. */ | |
754c9491 | 202 | { |
7dcb364d GH |
203 | if (pt->rw_active == SCM_PORT_WRITE) |
204 | st_flush (port); | |
205 | ||
206 | if (pt->rw_active == SCM_PORT_READ) | |
207 | scm_end_input (port); | |
208 | ||
209 | switch (whence) | |
210 | { | |
211 | case SEEK_CUR: | |
212 | target = pt->read_pos - pt->read_buf + offset; | |
213 | break; | |
214 | case SEEK_END: | |
215 | target = pt->read_end - pt->read_buf + offset; | |
216 | break; | |
217 | default: /* SEEK_SET */ | |
218 | target = offset; | |
219 | break; | |
220 | } | |
221 | ||
222 | if (target < 0) | |
223 | scm_misc_error ("st_seek", "negative offset", SCM_EOL); | |
224 | ||
225 | if (target >= pt->write_buf_size) | |
3fe6190f | 226 | { |
206d3de3 | 227 | if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG)) |
3fe6190f | 228 | { |
7dcb364d GH |
229 | if (target > pt->write_buf_size) |
230 | { | |
231 | scm_misc_error ("st_seek", | |
232 | "seek past end of read-only strport", | |
233 | SCM_EOL); | |
234 | } | |
235 | } | |
236 | else | |
237 | { | |
238 | st_resize_port (pt, target + (target == pt->write_buf_size | |
239 | ? SCM_WRITE_BLOCK | |
240 | : 0)); | |
3fe6190f GH |
241 | } |
242 | } | |
7dcb364d GH |
243 | pt->read_pos = pt->write_pos = pt->read_buf + target; |
244 | if (pt->read_pos > pt->read_end) | |
3fe6190f | 245 | { |
7dcb364d GH |
246 | pt->read_end = (unsigned char *) pt->read_pos; |
247 | pt->read_buf_size = pt->read_end - pt->read_buf; | |
3fe6190f | 248 | } |
ee149d03 | 249 | } |
754c9491 JB |
250 | return target; |
251 | } | |
252 | ||
253 | static void | |
affc96b5 | 254 | st_truncate (SCM port, off_t length) |
754c9491 | 255 | { |
92c2555f | 256 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
3fe6190f GH |
257 | |
258 | if (length > pt->write_buf_size) | |
259 | st_resize_port (pt, length); | |
260 | ||
261 | pt->read_buf_size = length; | |
262 | pt->read_end = pt->read_buf + length; | |
263 | if (pt->read_pos > pt->read_end) | |
264 | pt->read_pos = pt->read_end; | |
754c9491 | 265 | |
3fe6190f GH |
266 | if (pt->write_pos > pt->read_end) |
267 | pt->write_pos = pt->read_end; | |
0f2d19dd JB |
268 | } |
269 | ||
0f2d19dd | 270 | SCM |
1bbd0b84 | 271 | scm_mkstrport (SCM pos, SCM str, long modes, const char *caller) |
0f2d19dd JB |
272 | { |
273 | SCM z; | |
92c2555f | 274 | scm_t_port *pt; |
1be6b49c | 275 | size_t str_len; |
754c9491 JB |
276 | |
277 | SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller); | |
a6d9e5ab DH |
278 | SCM_ASSERT (SCM_STRINGP (str), str, SCM_ARG1, caller); |
279 | str_len = SCM_STRING_LENGTH (str); | |
754c9491 JB |
280 | if (SCM_INUM (pos) > str_len) |
281 | scm_out_of_range (caller, pos); | |
282 | if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG))) | |
283 | scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL); | |
0f2d19dd JB |
284 | SCM_NEWCELL (z); |
285 | SCM_DEFER_INTS; | |
286 | pt = scm_add_to_port_table (z); | |
54778cd3 | 287 | SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes); |
0f2d19dd | 288 | SCM_SETPTAB_ENTRY (z, pt); |
74a16888 | 289 | SCM_SETSTREAM (z, SCM_UNPACK (str)); |
34f0f2b8 | 290 | pt->write_buf = pt->read_buf = SCM_STRING_UCHARS (str); |
754c9491 JB |
291 | pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos); |
292 | pt->write_buf_size = pt->read_buf_size = str_len; | |
293 | pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size; | |
3fe6190f | 294 | |
0de97b83 | 295 | pt->rw_random = 1; |
3fe6190f | 296 | |
0f2d19dd | 297 | SCM_ALLOW_INTS; |
3fe6190f GH |
298 | |
299 | /* ensure write_pos is writable. */ | |
754c9491 JB |
300 | if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end) |
301 | st_flush (z); | |
0f2d19dd JB |
302 | return z; |
303 | } | |
304 | ||
3fe6190f GH |
305 | /* create a new string from a string port's buffer. */ |
306 | SCM scm_strport_to_string (SCM port) | |
307 | { | |
92c2555f | 308 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
36284627 | 309 | SCM str; |
3fe6190f GH |
310 | |
311 | if (pt->rw_active == SCM_PORT_WRITE) | |
312 | st_flush (port); | |
36284627 DH |
313 | |
314 | str = scm_mem2string ((char *) pt->read_buf, pt->read_buf_size); | |
315 | scm_remember_upto_here_1 (port); | |
316 | return str; | |
3fe6190f GH |
317 | } |
318 | ||
fe78b6c0 KN |
319 | SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0, |
320 | (SCM obj, SCM printer), | |
321 | "Return a Scheme string obtained by printing @var{obj}.\n" | |
322 | "Printing function can be specified by the optional second\n" | |
323 | "argument @var{printer} (default: @code{write}).") | |
1f3908c4 KN |
324 | #define FUNC_NAME s_scm_object_to_string |
325 | { | |
fe78b6c0 KN |
326 | SCM str, port; |
327 | ||
328 | if (!SCM_UNBNDP (printer)) | |
329 | SCM_VALIDATE_PROC (2, printer); | |
1f3908c4 | 330 | |
be54b15d | 331 | str = scm_allocate_string (0); |
1a92274c | 332 | port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, FUNC_NAME); |
fe78b6c0 KN |
333 | |
334 | if (SCM_UNBNDP (printer)) | |
335 | scm_write (obj, port); | |
336 | else | |
fdc28395 | 337 | scm_call_2 (printer, obj, port); |
fe78b6c0 | 338 | |
1f3908c4 KN |
339 | return scm_strport_to_string (port); |
340 | } | |
341 | #undef FUNC_NAME | |
342 | ||
343 | #if (SCM_DEBUG_DEPRECATED == 0) | |
344 | ||
345 | SCM | |
346 | scm_strprint_obj (SCM obj) | |
347 | { | |
fe78b6c0 | 348 | return scm_object_to_string (obj, SCM_UNDEFINED); |
1f3908c4 KN |
349 | } |
350 | ||
351 | #endif /* (SCM_DEBUG_DEPRECATED == 0) */ | |
352 | ||
3b3b36dd | 353 | SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0, |
1bbd0b84 | 354 | (SCM proc), |
b380b885 MD |
355 | "Calls the one-argument procedure @var{proc} with a newly created output\n" |
356 | "port. When the function returns, the string composed of the characters\n" | |
357 | "written into the port is returned.") | |
1bbd0b84 | 358 | #define FUNC_NAME s_scm_call_with_output_string |
0f2d19dd JB |
359 | { |
360 | SCM p; | |
754c9491 JB |
361 | |
362 | p = scm_mkstrport (SCM_INUM0, | |
363 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
364 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 365 | FUNC_NAME); |
fdc28395 | 366 | scm_call_1 (proc, p); |
3fe6190f GH |
367 | |
368 | return scm_strport_to_string (p); | |
0f2d19dd | 369 | } |
1bbd0b84 | 370 | #undef FUNC_NAME |
0f2d19dd | 371 | |
3b3b36dd | 372 | SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0, |
1e6808ea MG |
373 | (SCM string, SCM proc), |
374 | "Calls the one-argument procedure @var{proc} with a newly\n" | |
375 | "created input port from which @var{string}'s contents may be\n" | |
376 | "read. The value yielded by the @var{proc} is returned.") | |
1bbd0b84 | 377 | #define FUNC_NAME s_scm_call_with_input_string |
0f2d19dd | 378 | { |
1e6808ea | 379 | SCM p = scm_mkstrport(SCM_INUM0, string, SCM_OPN | SCM_RDNG, FUNC_NAME); |
fdc28395 | 380 | return scm_call_1 (proc, p); |
0f2d19dd | 381 | } |
1bbd0b84 | 382 | #undef FUNC_NAME |
0f2d19dd | 383 | |
e87a03fc MG |
384 | SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0, |
385 | (SCM str), | |
1e6808ea MG |
386 | "Take a string and return an input port that delivers characters\n" |
387 | "from the string. The port can be closed by\n" | |
e87a03fc MG |
388 | "@code{close-input-port}, though its storage will be reclaimed\n" |
389 | "by the garbage collector if it becomes inaccessible.") | |
390 | #define FUNC_NAME s_scm_open_input_string | |
391 | { | |
392 | SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME); | |
393 | return p; | |
394 | } | |
395 | #undef FUNC_NAME | |
396 | ||
397 | SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0, | |
398 | (void), | |
1e6808ea | 399 | "Return an output port that will accumulate characters for\n" |
e87a03fc MG |
400 | "retrieval by @code{get-output-string}. The port can be closed\n" |
401 | "by the procedure @code{close-output-port}, though its storage\n" | |
402 | "will be reclaimed by the garbage collector if it becomes\n" | |
403 | "inaccessible.") | |
404 | #define FUNC_NAME s_scm_open_output_string | |
405 | { | |
406 | SCM p; | |
407 | ||
408 | p = scm_mkstrport (SCM_INUM0, | |
409 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
410 | SCM_OPN | SCM_WRTNG, | |
411 | FUNC_NAME); | |
412 | return p; | |
413 | } | |
414 | #undef FUNC_NAME | |
415 | ||
416 | SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0, | |
417 | (SCM port), | |
418 | "Given an output port created by @code{open-output-string},\n" | |
1e6808ea | 419 | "return a string consisting of the characters that have been\n" |
e87a03fc MG |
420 | "output to the port so far.") |
421 | #define FUNC_NAME s_scm_get_output_string | |
422 | { | |
423 | SCM_VALIDATE_OPOUTSTRPORT (1, port); | |
424 | return scm_strport_to_string (port); | |
425 | } | |
426 | #undef FUNC_NAME | |
1cc91f1b | 427 | |
cd07a097 | 428 | |
a8aa30d8 MD |
429 | /* Given a null-terminated string EXPR containing a Scheme expression |
430 | read it, and return it as an SCM value. */ | |
431 | SCM | |
ca13a04a | 432 | scm_c_read_string (const char *expr) |
a8aa30d8 | 433 | { |
3fe6190f | 434 | SCM port = scm_mkstrport (SCM_INUM0, |
a8aa30d8 MD |
435 | scm_makfrom0str (expr), |
436 | SCM_OPN | SCM_RDNG, | |
ca13a04a | 437 | "scm_c_read_string"); |
a8aa30d8 MD |
438 | SCM form; |
439 | ||
440 | /* Read expressions from that port; ignore the values. */ | |
deca31e1 | 441 | form = scm_read (port); |
a8aa30d8 MD |
442 | |
443 | scm_close_port (port); | |
444 | return form; | |
445 | } | |
446 | ||
cd07a097 | 447 | /* Given a null-terminated string EXPR containing Scheme program text, |
a8aa30d8 MD |
448 | evaluate it, and return the result of the last expression evaluated. */ |
449 | SCM | |
ca13a04a | 450 | scm_c_eval_string (const char *expr) |
cd07a097 | 451 | { |
b377f53e JB |
452 | return scm_eval_string (scm_makfrom0str (expr)); |
453 | } | |
454 | ||
ca13a04a MV |
455 | #if SCM_DEBUG_DEPRECATED == 0 |
456 | ||
457 | SCM | |
458 | scm_read_0str (char *expr) | |
459 | { | |
460 | scm_c_issue_deprecation_warning | |
461 | ("scm_read_0str is deprecated. Use scm_c_read_string instead."); | |
462 | ||
71c9d8eb | 463 | return scm_c_read_string (expr); |
ca13a04a MV |
464 | } |
465 | ||
466 | SCM | |
467 | scm_eval_0str (const char *expr) | |
468 | { | |
469 | scm_c_issue_deprecation_warning | |
470 | ("scm_eval_0str is deprecated. Use scm_c_eval_string instead."); | |
471 | ||
71c9d8eb | 472 | return scm_c_eval_string (expr); |
ca13a04a MV |
473 | } |
474 | ||
475 | #endif | |
476 | ||
96e83482 MV |
477 | static SCM |
478 | inner_eval_string (void *data) | |
b377f53e | 479 | { |
96e83482 | 480 | SCM port = (SCM)data; |
cd07a097 | 481 | SCM form; |
b377f53e | 482 | SCM ans = SCM_UNSPECIFIED; |
cd07a097 JB |
483 | |
484 | /* Read expressions from that port; ignore the values. */ | |
0c32d76c | 485 | while (!SCM_EOF_OBJECT_P (form = scm_read (port))) |
96e83482 | 486 | ans = scm_primitive_eval_x (form); |
cd07a097 | 487 | |
0d7368d7 JB |
488 | /* Don't close the port here; if we re-enter this function via a |
489 | continuation, then the next time we enter it, we'll get an error. | |
490 | It's a string port anyway, so there's no advantage to closing it | |
491 | early. */ | |
492 | ||
a8aa30d8 | 493 | return ans; |
cd07a097 | 494 | } |
96e83482 MV |
495 | |
496 | SCM_DEFINE (scm_eval_string, "eval-string", 1, 0, 0, | |
497 | (SCM string), | |
498 | "Evaluate @var{string} as the text representation of a Scheme\n" | |
499 | "form or forms, and return whatever value they produce.\n" | |
500 | "Evaluation takes place in the environment returned by the\n" | |
501 | "procedure @code{interaction-environment}.") | |
502 | #define FUNC_NAME s_scm_eval_string | |
503 | { | |
504 | SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG, | |
ca13a04a | 505 | "eval-string"); |
96e83482 MV |
506 | return scm_c_call_with_current_module (scm_interaction_environment (), |
507 | inner_eval_string, (void *)port); | |
508 | } | |
1bbd0b84 | 509 | #undef FUNC_NAME |
cd07a097 | 510 | |
92c2555f | 511 | static scm_t_bits |
0b6881fa | 512 | scm_make_stptob () |
0f2d19dd | 513 | { |
92c2555f | 514 | scm_t_bits tc = scm_make_port_type ("string", stfill_buffer, st_write); |
a98bddfd | 515 | |
6c747373 | 516 | scm_set_port_mark (tc, scm_markstream); |
affc96b5 GH |
517 | scm_set_port_end_input (tc, st_end_input); |
518 | scm_set_port_flush (tc, st_flush); | |
6c747373 | 519 | scm_set_port_seek (tc, st_seek); |
affc96b5 | 520 | scm_set_port_truncate (tc, st_truncate); |
a98bddfd DH |
521 | |
522 | return tc; | |
0f2d19dd JB |
523 | } |
524 | ||
0f2d19dd JB |
525 | void |
526 | scm_init_strports () | |
0f2d19dd | 527 | { |
a98bddfd DH |
528 | scm_tc16_strport = scm_make_stptob (); |
529 | ||
8dc9439f | 530 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 531 | #include "libguile/strports.x" |
8dc9439f | 532 | #endif |
0f2d19dd JB |
533 | } |
534 | ||
89e00824 ML |
535 | |
536 | /* | |
537 | Local Variables: | |
538 | c-file-style: "gnu" | |
539 | End: | |
540 | */ |