Commit | Line | Data |
---|---|---|
574b7be0 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001,2002, 2003, 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd JB |
20 | \f |
21 | ||
dbb605f5 | 22 | #ifdef HAVE_CONFIG_H |
c986274f RB |
23 | # include <config.h> |
24 | #endif | |
25 | ||
a0599745 | 26 | #include "libguile/_scm.h" |
681b9005 MD |
27 | |
28 | #include <stdio.h> | |
29 | #ifdef HAVE_UNISTD_H | |
30 | #include <unistd.h> | |
31 | #endif | |
32 | ||
691fcf66 | 33 | #include "libguile/bytevectors.h" |
a0599745 MD |
34 | #include "libguile/eval.h" |
35 | #include "libguile/ports.h" | |
36 | #include "libguile/read.h" | |
37 | #include "libguile/root.h" | |
38 | #include "libguile/strings.h" | |
07bcf91d | 39 | #include "libguile/modules.h" |
fe78b6c0 | 40 | #include "libguile/validate.h" |
ca13a04a | 41 | #include "libguile/deprecation.h" |
889975e5 | 42 | #include "libguile/srfi-4.h" |
20e6290e | 43 | |
a0599745 | 44 | #include "libguile/strports.h" |
0f2d19dd | 45 | |
95b88819 GH |
46 | #ifdef HAVE_STRING_H |
47 | #include <string.h> | |
48 | #endif | |
49 | ||
0f2d19dd JB |
50 | \f |
51 | ||
52 | /* {Ports - string ports} | |
53 | * | |
54 | */ | |
55 | ||
3fe6190f | 56 | /* NOTES: |
cc95e00a | 57 | |
691fcf66 LC |
58 | write_buf/write_end point to the ends of the allocated bytevector. |
59 | read_buf/read_end in principle point to the part of the bytevector which | |
3fe6190f GH |
60 | has been written to, but this is only updated after a flush. |
61 | read_pos and write_pos in principle should be equal, but this is only true | |
61e452ba | 62 | when rw_active is SCM_PORT_NEITHER. |
bf5ad0da KR |
63 | |
64 | ENHANCE-ME - output blocks: | |
65 | ||
66 | The current code keeps an output string as a single block. That means | |
67 | when the size is increased the entire old contents must be copied. It'd | |
68 | be more efficient to begin a new block when the old one is full, so | |
69 | there's no re-copying of previous data. | |
70 | ||
71 | To make seeking efficient, keeping the pieces in a vector might be best, | |
72 | though appending is probably the most common operation. The size of each | |
73 | block could be progressively increased, so the bigger the string the | |
74 | bigger the blocks. | |
75 | ||
76 | When `get-output-string' is called the blocks have to be coalesced into a | |
77 | string, the result could be kept as a single big block. If blocks were | |
78 | strings then `get-output-string' could notice when there's just one and | |
79 | return that with a copy-on-write (though repeated calls to | |
80 | `get-output-string' are probably unlikely). | |
81 | ||
82 | Another possibility would be to extend the port mechanism to let SCM | |
83 | strings come through directly from `display' and friends. That way if a | |
84 | big string is written it can be kept as a copy-on-write, saving time | |
85 | copying and maybe saving some space. */ | |
86 | ||
1cc91f1b | 87 | |
92c2555f | 88 | scm_t_bits scm_tc16_strport; |
a98bddfd DH |
89 | |
90 | ||
ee149d03 JB |
91 | static int |
92 | stfill_buffer (SCM port) | |
0f2d19dd | 93 | { |
92c2555f | 94 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
ee149d03 | 95 | |
ee149d03 JB |
96 | if (pt->read_pos >= pt->read_end) |
97 | return EOF; | |
98 | else | |
41b0806d | 99 | return scm_return_first_int (*pt->read_pos, port); |
0f2d19dd JB |
100 | } |
101 | ||
691fcf66 LC |
102 | /* Change the size of a port's bytevector to NEW_SIZE. This doesn't |
103 | change `read_buf_size'. */ | |
104 | static void | |
f1ce9199 | 105 | st_resize_port (scm_t_port *pt, scm_t_off new_size) |
754c9491 | 106 | { |
94115ae3 | 107 | SCM old_stream = SCM_PACK (pt->stream); |
691fcf66 LC |
108 | const signed char *src = SCM_BYTEVECTOR_CONTENTS (old_stream); |
109 | SCM new_stream = scm_c_make_bytevector (new_size); | |
110 | signed char *dst = SCM_BYTEVECTOR_CONTENTS (new_stream); | |
111 | unsigned long int old_size = SCM_BYTEVECTOR_LENGTH (old_stream); | |
c014a02e | 112 | unsigned long int min_size = min (old_size, new_size); |
74a16888 | 113 | |
f1ce9199 | 114 | scm_t_off index = pt->write_pos - pt->write_buf; |
3fe6190f GH |
115 | |
116 | pt->write_buf_size = new_size; | |
117 | ||
691fcf66 | 118 | memcpy (dst, src, min_size); |
8824ac88 MV |
119 | |
120 | scm_remember_upto_here_1 (old_stream); | |
754c9491 | 121 | |
94115ae3 | 122 | /* reset buffer. */ |
754c9491 | 123 | { |
729dbac3 | 124 | pt->stream = SCM_UNPACK (new_stream); |
5a6d139b | 125 | pt->read_buf = pt->write_buf = (unsigned char *)dst; |
3fe6190f GH |
126 | pt->read_pos = pt->write_pos = pt->write_buf + index; |
127 | pt->write_end = pt->write_buf + pt->write_buf_size; | |
128 | pt->read_end = pt->read_buf + pt->read_buf_size; | |
754c9491 JB |
129 | } |
130 | } | |
131 | ||
3fe6190f GH |
132 | /* amount by which write_buf is expanded. */ |
133 | #define SCM_WRITE_BLOCK 80 | |
134 | ||
135 | /* ensure that write_pos < write_end by enlarging the buffer when | |
bf5ad0da KR |
136 | necessary. update read_buf to account for written chars. |
137 | ||
138 | The buffer is enlarged by 1.5 times, plus SCM_WRITE_BLOCK. Adding just a | |
139 | fixed amount is no good, because there's a block copy for each increment, | |
140 | and that copying would take quadratic time. In the past it was found to | |
141 | be very slow just adding 80 bytes each time (eg. about 10 seconds for | |
142 | writing a 100kbyte string). */ | |
143 | ||
ee149d03 JB |
144 | static void |
145 | st_flush (SCM port) | |
0f2d19dd | 146 | { |
92c2555f | 147 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
ee149d03 JB |
148 | |
149 | if (pt->write_pos == pt->write_end) | |
150 | { | |
bf5ad0da | 151 | st_resize_port (pt, pt->write_buf_size * 3 / 2 + SCM_WRITE_BLOCK); |
754c9491 JB |
152 | } |
153 | pt->read_pos = pt->write_pos; | |
3fe6190f GH |
154 | if (pt->read_pos > pt->read_end) |
155 | { | |
156 | pt->read_end = (unsigned char *) pt->read_pos; | |
157 | pt->read_buf_size = pt->read_end - pt->read_buf; | |
158 | } | |
61e452ba | 159 | pt->rw_active = SCM_PORT_NEITHER; |
754c9491 JB |
160 | } |
161 | ||
31703ab8 | 162 | static void |
8aa011a1 | 163 | st_write (SCM port, const void *data, size_t size) |
31703ab8 | 164 | { |
92c2555f | 165 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
31703ab8 GH |
166 | const char *input = (char *) data; |
167 | ||
168 | while (size > 0) | |
169 | { | |
170 | int space = pt->write_end - pt->write_pos; | |
171 | int write_len = (size > space) ? space : size; | |
172 | ||
4b8ec619 | 173 | memcpy ((char *) pt->write_pos, input, write_len); |
31703ab8 GH |
174 | pt->write_pos += write_len; |
175 | size -= write_len; | |
176 | input += write_len; | |
177 | if (write_len == space) | |
178 | st_flush (port); | |
179 | } | |
180 | } | |
181 | ||
754c9491 | 182 | static void |
affc96b5 | 183 | st_end_input (SCM port, int offset) |
754c9491 | 184 | { |
92c2555f | 185 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
7dcb364d | 186 | |
cd19d608 GH |
187 | if (pt->read_pos - pt->read_buf < offset) |
188 | scm_misc_error ("st_end_input", "negative position", SCM_EOL); | |
189 | ||
a3c8b9fc | 190 | pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset); |
61e452ba | 191 | pt->rw_active = SCM_PORT_NEITHER; |
754c9491 JB |
192 | } |
193 | ||
f1ce9199 LC |
194 | static scm_t_off |
195 | st_seek (SCM port, scm_t_off offset, int whence) | |
754c9491 | 196 | { |
92c2555f | 197 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
f1ce9199 | 198 | scm_t_off target; |
754c9491 | 199 | |
7dcb364d GH |
200 | if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR) |
201 | /* special case to avoid disturbing the unread-char buffer. */ | |
754c9491 | 202 | { |
7dcb364d GH |
203 | if (pt->read_buf == pt->putback_buf) |
204 | { | |
205 | target = pt->saved_read_pos - pt->saved_read_buf | |
206 | - (pt->read_end - pt->read_pos); | |
207 | } | |
208 | else | |
209 | { | |
210 | target = pt->read_pos - pt->read_buf; | |
211 | } | |
754c9491 | 212 | } |
7dcb364d GH |
213 | else |
214 | /* all other cases. */ | |
754c9491 | 215 | { |
7dcb364d GH |
216 | if (pt->rw_active == SCM_PORT_WRITE) |
217 | st_flush (port); | |
218 | ||
219 | if (pt->rw_active == SCM_PORT_READ) | |
220 | scm_end_input (port); | |
221 | ||
222 | switch (whence) | |
223 | { | |
224 | case SEEK_CUR: | |
225 | target = pt->read_pos - pt->read_buf + offset; | |
226 | break; | |
227 | case SEEK_END: | |
228 | target = pt->read_end - pt->read_buf + offset; | |
229 | break; | |
230 | default: /* SEEK_SET */ | |
231 | target = offset; | |
232 | break; | |
233 | } | |
234 | ||
235 | if (target < 0) | |
236 | scm_misc_error ("st_seek", "negative offset", SCM_EOL); | |
237 | ||
238 | if (target >= pt->write_buf_size) | |
3fe6190f | 239 | { |
206d3de3 | 240 | if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG)) |
3fe6190f | 241 | { |
7dcb364d GH |
242 | if (target > pt->write_buf_size) |
243 | { | |
244 | scm_misc_error ("st_seek", | |
245 | "seek past end of read-only strport", | |
246 | SCM_EOL); | |
247 | } | |
248 | } | |
249 | else | |
250 | { | |
251 | st_resize_port (pt, target + (target == pt->write_buf_size | |
252 | ? SCM_WRITE_BLOCK | |
253 | : 0)); | |
3fe6190f GH |
254 | } |
255 | } | |
7dcb364d GH |
256 | pt->read_pos = pt->write_pos = pt->read_buf + target; |
257 | if (pt->read_pos > pt->read_end) | |
3fe6190f | 258 | { |
7dcb364d GH |
259 | pt->read_end = (unsigned char *) pt->read_pos; |
260 | pt->read_buf_size = pt->read_end - pt->read_buf; | |
3fe6190f | 261 | } |
ee149d03 | 262 | } |
754c9491 JB |
263 | return target; |
264 | } | |
265 | ||
266 | static void | |
f1ce9199 | 267 | st_truncate (SCM port, scm_t_off length) |
754c9491 | 268 | { |
92c2555f | 269 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
3fe6190f GH |
270 | |
271 | if (length > pt->write_buf_size) | |
272 | st_resize_port (pt, length); | |
273 | ||
274 | pt->read_buf_size = length; | |
275 | pt->read_end = pt->read_buf + length; | |
276 | if (pt->read_pos > pt->read_end) | |
277 | pt->read_pos = pt->read_end; | |
754c9491 | 278 | |
3fe6190f GH |
279 | if (pt->write_pos > pt->read_end) |
280 | pt->write_pos = pt->read_end; | |
0f2d19dd JB |
281 | } |
282 | ||
0b2c2ba3 LC |
283 | /* The initial size in bytes of a string port's buffer. */ |
284 | #define INITIAL_BUFFER_SIZE 128 | |
285 | ||
286 | /* Return a new string port with MODES. If STR is #f, a new backing | |
287 | buffer is allocated; otherwise STR must be a string and a copy of it | |
288 | serves as the buffer for the new port. */ | |
7b041912 LC |
289 | SCM |
290 | scm_mkstrport (SCM pos, SCM str, long modes, const char *caller) | |
0f2d19dd | 291 | { |
691fcf66 | 292 | SCM z, buf; |
92c2555f | 293 | scm_t_port *pt; |
7b041912 | 294 | size_t str_len, c_pos; |
691fcf66 | 295 | char *c_buf; |
e11e83f3 | 296 | |
754c9491 JB |
297 | if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG))) |
298 | scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL); | |
5f16b897 | 299 | |
7b041912 LC |
300 | scm_dynwind_begin (0); |
301 | scm_i_dynwind_pthread_mutex_lock (&scm_i_port_table_mutex); | |
302 | ||
da220f27 HWN |
303 | z = scm_new_port_table_entry (scm_tc16_strport); |
304 | pt = SCM_PTAB_ENTRY(z); | |
7b041912 | 305 | |
0b2c2ba3 LC |
306 | if (scm_is_false (str)) |
307 | { | |
308 | /* Allocate a new buffer to write to. */ | |
309 | str_len = INITIAL_BUFFER_SIZE; | |
310 | buf = scm_c_make_bytevector (str_len); | |
311 | c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf); | |
312 | ||
313 | /* Reset `read_buf_size'. It will contain the actual number of | |
314 | bytes written to PT. */ | |
315 | pt->read_buf_size = 0; | |
316 | c_pos = 0; | |
317 | } | |
318 | else | |
319 | { | |
320 | /* STR is a string. */ | |
321 | char *copy; | |
691fcf66 | 322 | |
0b2c2ba3 | 323 | SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller); |
691fcf66 | 324 | |
0b2c2ba3 LC |
325 | /* Create a copy of STR in the encoding of PT. */ |
326 | copy = scm_to_stringn (str, &str_len, pt->encoding, | |
327 | SCM_FAILED_CONVERSION_ERROR); | |
328 | buf = scm_c_make_bytevector (str_len); | |
329 | c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf); | |
330 | memcpy (c_buf, copy, str_len); | |
331 | free (copy); | |
691fcf66 | 332 | |
0b2c2ba3 LC |
333 | c_pos = scm_to_unsigned_integer (pos, 0, str_len); |
334 | pt->read_buf_size = str_len; | |
335 | } | |
691fcf66 LC |
336 | |
337 | SCM_SETSTREAM (z, SCM_UNPACK (buf)); | |
338 | SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes); | |
7b041912 | 339 | |
691fcf66 | 340 | pt->write_buf = pt->read_buf = (unsigned char *) c_buf; |
e11e83f3 | 341 | pt->read_pos = pt->write_pos = pt->read_buf + c_pos; |
691fcf66 | 342 | pt->write_buf_size = str_len; |
754c9491 | 343 | pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size; |
3fe6190f | 344 | |
0de97b83 | 345 | pt->rw_random = 1; |
3fe6190f | 346 | |
7b041912 LC |
347 | scm_dynwind_end (); |
348 | ||
349 | /* Ensure WRITE_POS is writable. */ | |
754c9491 JB |
350 | if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end) |
351 | st_flush (z); | |
25ebc034 | 352 | |
25ebc034 | 353 | scm_i_set_conversion_strategy_x (z, SCM_FAILED_CONVERSION_ERROR); |
0f2d19dd JB |
354 | return z; |
355 | } | |
356 | ||
7b041912 LC |
357 | /* Create a new string from the buffer of PORT, a string port, converting from |
358 | PORT's encoding to the standard string representation. */ | |
359 | SCM | |
360 | scm_strport_to_string (SCM port) | |
3fe6190f | 361 | { |
36284627 | 362 | SCM str; |
7b041912 LC |
363 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
364 | ||
3fe6190f GH |
365 | if (pt->rw_active == SCM_PORT_WRITE) |
366 | st_flush (port); | |
36284627 | 367 | |
fac32b51 MG |
368 | if (pt->read_buf_size == 0) |
369 | return scm_nullstr; | |
370 | ||
371 | if (pt->encoding == NULL) | |
372 | { | |
373 | char *buf; | |
374 | str = scm_i_make_string (pt->read_buf_size, &buf); | |
375 | memcpy (buf, pt->read_buf, pt->read_buf_size); | |
376 | } | |
377 | else | |
f7f4d047 MG |
378 | str = scm_from_stringn ((char *)pt->read_buf, pt->read_buf_size, |
379 | pt->encoding, pt->ilseq_handler); | |
36284627 DH |
380 | scm_remember_upto_here_1 (port); |
381 | return str; | |
3fe6190f GH |
382 | } |
383 | ||
fe78b6c0 KN |
384 | SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0, |
385 | (SCM obj, SCM printer), | |
386 | "Return a Scheme string obtained by printing @var{obj}.\n" | |
387 | "Printing function can be specified by the optional second\n" | |
388 | "argument @var{printer} (default: @code{write}).") | |
1f3908c4 KN |
389 | #define FUNC_NAME s_scm_object_to_string |
390 | { | |
0b2c2ba3 | 391 | SCM port; |
fe78b6c0 KN |
392 | |
393 | if (!SCM_UNBNDP (printer)) | |
394 | SCM_VALIDATE_PROC (2, printer); | |
1f3908c4 | 395 | |
0b2c2ba3 LC |
396 | port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
397 | SCM_OPN | SCM_WRTNG, FUNC_NAME); | |
fe78b6c0 KN |
398 | |
399 | if (SCM_UNBNDP (printer)) | |
400 | scm_write (obj, port); | |
401 | else | |
fdc28395 | 402 | scm_call_2 (printer, obj, port); |
fe78b6c0 | 403 | |
1f3908c4 KN |
404 | return scm_strport_to_string (port); |
405 | } | |
406 | #undef FUNC_NAME | |
407 | ||
3b3b36dd | 408 | SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0, |
1bbd0b84 | 409 | (SCM proc), |
b380b885 MD |
410 | "Calls the one-argument procedure @var{proc} with a newly created output\n" |
411 | "port. When the function returns, the string composed of the characters\n" | |
412 | "written into the port is returned.") | |
1bbd0b84 | 413 | #define FUNC_NAME s_scm_call_with_output_string |
0f2d19dd JB |
414 | { |
415 | SCM p; | |
754c9491 | 416 | |
0b2c2ba3 | 417 | p = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
754c9491 | 418 | SCM_OPN | SCM_WRTNG, |
1bbd0b84 | 419 | FUNC_NAME); |
fdc28395 | 420 | scm_call_1 (proc, p); |
3fe6190f | 421 | |
184b85a3 | 422 | return scm_get_output_string (p); |
0f2d19dd | 423 | } |
1bbd0b84 | 424 | #undef FUNC_NAME |
0f2d19dd | 425 | |
3b3b36dd | 426 | SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0, |
1e6808ea MG |
427 | (SCM string, SCM proc), |
428 | "Calls the one-argument procedure @var{proc} with a newly\n" | |
429 | "created input port from which @var{string}'s contents may be\n" | |
430 | "read. The value yielded by the @var{proc} is returned.") | |
1bbd0b84 | 431 | #define FUNC_NAME s_scm_call_with_input_string |
0f2d19dd | 432 | { |
1e6808ea | 433 | SCM p = scm_mkstrport(SCM_INUM0, string, SCM_OPN | SCM_RDNG, FUNC_NAME); |
fdc28395 | 434 | return scm_call_1 (proc, p); |
0f2d19dd | 435 | } |
1bbd0b84 | 436 | #undef FUNC_NAME |
0f2d19dd | 437 | |
e87a03fc MG |
438 | SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0, |
439 | (SCM str), | |
1e6808ea MG |
440 | "Take a string and return an input port that delivers characters\n" |
441 | "from the string. The port can be closed by\n" | |
e87a03fc MG |
442 | "@code{close-input-port}, though its storage will be reclaimed\n" |
443 | "by the garbage collector if it becomes inaccessible.") | |
444 | #define FUNC_NAME s_scm_open_input_string | |
445 | { | |
446 | SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME); | |
447 | return p; | |
448 | } | |
449 | #undef FUNC_NAME | |
450 | ||
451 | SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0, | |
452 | (void), | |
1e6808ea | 453 | "Return an output port that will accumulate characters for\n" |
e87a03fc MG |
454 | "retrieval by @code{get-output-string}. The port can be closed\n" |
455 | "by the procedure @code{close-output-port}, though its storage\n" | |
456 | "will be reclaimed by the garbage collector if it becomes\n" | |
457 | "inaccessible.") | |
458 | #define FUNC_NAME s_scm_open_output_string | |
459 | { | |
460 | SCM p; | |
461 | ||
0b2c2ba3 | 462 | p = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
e87a03fc MG |
463 | SCM_OPN | SCM_WRTNG, |
464 | FUNC_NAME); | |
465 | return p; | |
466 | } | |
467 | #undef FUNC_NAME | |
468 | ||
469 | SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0, | |
470 | (SCM port), | |
471 | "Given an output port created by @code{open-output-string},\n" | |
1e6808ea | 472 | "return a string consisting of the characters that have been\n" |
e87a03fc MG |
473 | "output to the port so far.") |
474 | #define FUNC_NAME s_scm_get_output_string | |
475 | { | |
476 | SCM_VALIDATE_OPOUTSTRPORT (1, port); | |
477 | return scm_strport_to_string (port); | |
478 | } | |
479 | #undef FUNC_NAME | |
1cc91f1b | 480 | |
cd07a097 | 481 | |
a8aa30d8 MD |
482 | /* Given a null-terminated string EXPR containing a Scheme expression |
483 | read it, and return it as an SCM value. */ | |
484 | SCM | |
ca13a04a | 485 | scm_c_read_string (const char *expr) |
a8aa30d8 | 486 | { |
3fe6190f | 487 | SCM port = scm_mkstrport (SCM_INUM0, |
cc95e00a | 488 | scm_from_locale_string (expr), |
a8aa30d8 | 489 | SCM_OPN | SCM_RDNG, |
ca13a04a | 490 | "scm_c_read_string"); |
a8aa30d8 MD |
491 | SCM form; |
492 | ||
deca31e1 | 493 | form = scm_read (port); |
a8aa30d8 MD |
494 | |
495 | scm_close_port (port); | |
496 | return form; | |
497 | } | |
498 | ||
cd07a097 | 499 | /* Given a null-terminated string EXPR containing Scheme program text, |
a8aa30d8 MD |
500 | evaluate it, and return the result of the last expression evaluated. */ |
501 | SCM | |
ca13a04a | 502 | scm_c_eval_string (const char *expr) |
cd07a097 | 503 | { |
cc95e00a | 504 | return scm_eval_string (scm_from_locale_string (expr)); |
b377f53e JB |
505 | } |
506 | ||
209b52fe MV |
507 | SCM |
508 | scm_c_eval_string_in_module (const char *expr, SCM module) | |
509 | { | |
cc95e00a | 510 | return scm_eval_string_in_module (scm_from_locale_string (expr), module); |
209b52fe MV |
511 | } |
512 | ||
513 | ||
96e83482 MV |
514 | static SCM |
515 | inner_eval_string (void *data) | |
b377f53e | 516 | { |
96e83482 | 517 | SCM port = (SCM)data; |
cd07a097 | 518 | SCM form; |
b377f53e | 519 | SCM ans = SCM_UNSPECIFIED; |
cd07a097 JB |
520 | |
521 | /* Read expressions from that port; ignore the values. */ | |
0c32d76c | 522 | while (!SCM_EOF_OBJECT_P (form = scm_read (port))) |
96e83482 | 523 | ans = scm_primitive_eval_x (form); |
cd07a097 | 524 | |
0d7368d7 JB |
525 | /* Don't close the port here; if we re-enter this function via a |
526 | continuation, then the next time we enter it, we'll get an error. | |
527 | It's a string port anyway, so there's no advantage to closing it | |
528 | early. */ | |
529 | ||
a8aa30d8 | 530 | return ans; |
cd07a097 | 531 | } |
96e83482 | 532 | |
209b52fe MV |
533 | SCM_DEFINE (scm_eval_string_in_module, "eval-string", 1, 1, 0, |
534 | (SCM string, SCM module), | |
96e83482 MV |
535 | "Evaluate @var{string} as the text representation of a Scheme\n" |
536 | "form or forms, and return whatever value they produce.\n" | |
209b52fe MV |
537 | "Evaluation takes place in the given module, or the current\n" |
538 | "module when no module is given.\n" | |
539 | "While the code is evaluated, the given module is made the\n" | |
540 | "current one. The current module is restored when this\n" | |
541 | "procedure returns.") | |
542 | #define FUNC_NAME s_scm_eval_string_in_module | |
96e83482 MV |
543 | { |
544 | SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG, | |
209b52fe MV |
545 | FUNC_NAME); |
546 | if (SCM_UNBNDP (module)) | |
547 | module = scm_current_module (); | |
aeec5be1 MV |
548 | else |
549 | SCM_VALIDATE_MODULE (2, module); | |
209b52fe | 550 | return scm_c_call_with_current_module (module, |
96e83482 MV |
551 | inner_eval_string, (void *)port); |
552 | } | |
1bbd0b84 | 553 | #undef FUNC_NAME |
cd07a097 | 554 | |
209b52fe MV |
555 | SCM |
556 | scm_eval_string (SCM string) | |
557 | { | |
558 | return scm_eval_string_in_module (string, SCM_UNDEFINED); | |
559 | } | |
560 | ||
92c2555f | 561 | static scm_t_bits |
0b6881fa | 562 | scm_make_stptob () |
0f2d19dd | 563 | { |
92c2555f | 564 | scm_t_bits tc = scm_make_port_type ("string", stfill_buffer, st_write); |
a98bddfd | 565 | |
affc96b5 GH |
566 | scm_set_port_end_input (tc, st_end_input); |
567 | scm_set_port_flush (tc, st_flush); | |
6c747373 | 568 | scm_set_port_seek (tc, st_seek); |
affc96b5 | 569 | scm_set_port_truncate (tc, st_truncate); |
a98bddfd DH |
570 | |
571 | return tc; | |
0f2d19dd JB |
572 | } |
573 | ||
0f2d19dd JB |
574 | void |
575 | scm_init_strports () | |
0f2d19dd | 576 | { |
a98bddfd DH |
577 | scm_tc16_strport = scm_make_stptob (); |
578 | ||
a0599745 | 579 | #include "libguile/strports.x" |
0f2d19dd JB |
580 | } |
581 | ||
89e00824 ML |
582 | |
583 | /* | |
584 | Local Variables: | |
585 | c-file-style: "gnu" | |
586 | End: | |
587 | */ |