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