temporarily disable elisp exception tests
[bpt/guile.git] / libguile / strports.c
CommitLineData
478848cb 1/* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
475772ea 2 * 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
478848cb 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>
681b9005 30#include <unistd.h>
681b9005 31
691fcf66 32#include "libguile/bytevectors.h"
a0599745
MD
33#include "libguile/eval.h"
34#include "libguile/ports.h"
35#include "libguile/read.h"
36#include "libguile/root.h"
37#include "libguile/strings.h"
07bcf91d 38#include "libguile/modules.h"
fe78b6c0 39#include "libguile/validate.h"
ca13a04a 40#include "libguile/deprecation.h"
889975e5 41#include "libguile/srfi-4.h"
20e6290e 42
a0599745 43#include "libguile/strports.h"
0f2d19dd 44
95b88819
GH
45#ifdef HAVE_STRING_H
46#include <string.h>
47#endif
48
0f2d19dd
JB
49\f
50
51/* {Ports - string ports}
52 *
53 */
54
3fe6190f 55/* NOTES:
cc95e00a 56
691fcf66 57 write_buf/write_end point to the ends of the allocated bytevector.
ca2ec018
AW
58 read_buf/read_end point to the part of the bytevector which has been
59 written to. read_pos and write_pos are always equal.
bf5ad0da
KR
60
61 ENHANCE-ME - output blocks:
62
63 The current code keeps an output string as a single block. That means
64 when the size is increased the entire old contents must be copied. It'd
65 be more efficient to begin a new block when the old one is full, so
66 there's no re-copying of previous data.
67
68 To make seeking efficient, keeping the pieces in a vector might be best,
69 though appending is probably the most common operation. The size of each
70 block could be progressively increased, so the bigger the string the
71 bigger the blocks.
72
73 When `get-output-string' is called the blocks have to be coalesced into a
74 string, the result could be kept as a single big block. If blocks were
75 strings then `get-output-string' could notice when there's just one and
76 return that with a copy-on-write (though repeated calls to
77 `get-output-string' are probably unlikely).
78
79 Another possibility would be to extend the port mechanism to let SCM
80 strings come through directly from `display' and friends. That way if a
81 big string is written it can be kept as a copy-on-write, saving time
82 copying and maybe saving some space. */
83
1cc91f1b 84
92c2555f 85scm_t_bits scm_tc16_strport;
a98bddfd
DH
86
87
ee149d03 88static int
ca2ec018 89st_fill_input (SCM port)
0f2d19dd 90{
92c2555f 91 scm_t_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 92
ee149d03
JB
93 if (pt->read_pos >= pt->read_end)
94 return EOF;
95 else
ca2ec018 96 return *pt->read_pos;
0f2d19dd
JB
97}
98
691fcf66
LC
99/* Change the size of a port's bytevector to NEW_SIZE. This doesn't
100 change `read_buf_size'. */
101static void
f1ce9199 102st_resize_port (scm_t_port *pt, scm_t_off new_size)
754c9491 103{
94115ae3 104 SCM old_stream = SCM_PACK (pt->stream);
691fcf66
LC
105 const signed char *src = SCM_BYTEVECTOR_CONTENTS (old_stream);
106 SCM new_stream = scm_c_make_bytevector (new_size);
107 signed char *dst = SCM_BYTEVECTOR_CONTENTS (new_stream);
108 unsigned long int old_size = SCM_BYTEVECTOR_LENGTH (old_stream);
c014a02e 109 unsigned long int min_size = min (old_size, new_size);
74a16888 110
ca2ec018 111 scm_t_off offset = pt->write_pos - pt->write_buf;
3fe6190f
GH
112
113 pt->write_buf_size = new_size;
114
691fcf66 115 memcpy (dst, src, min_size);
8824ac88
MV
116
117 scm_remember_upto_here_1 (old_stream);
754c9491 118
94115ae3 119 /* reset buffer. */
754c9491 120 {
729dbac3 121 pt->stream = SCM_UNPACK (new_stream);
5a6d139b 122 pt->read_buf = pt->write_buf = (unsigned char *)dst;
ca2ec018 123 pt->read_pos = pt->write_pos = pt->write_buf + offset;
3fe6190f
GH
124 pt->write_end = pt->write_buf + pt->write_buf_size;
125 pt->read_end = pt->read_buf + pt->read_buf_size;
754c9491
JB
126 }
127}
128
ee149d03 129static void
ca2ec018 130st_write (SCM port, const void *data, size_t size)
0f2d19dd 131{
92c2555f 132 scm_t_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 133
ca2ec018
AW
134 if (size > pt->write_end - pt->write_pos)
135 st_resize_port (pt, max (pt->write_buf_size * 2,
136 pt->write_end - pt->write_pos + size));
137
138 memcpy ((char *) pt->write_pos, data, size);
139 pt->read_pos = (pt->write_pos += size);
d8f1c216 140
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 }
31703ab8
GH
146}
147
754c9491 148static void
affc96b5 149st_end_input (SCM port, int offset)
754c9491 150{
92c2555f 151 scm_t_port *pt = SCM_PTAB_ENTRY (port);
7dcb364d 152
cd19d608
GH
153 if (pt->read_pos - pt->read_buf < offset)
154 scm_misc_error ("st_end_input", "negative position", SCM_EOL);
155
a3c8b9fc 156 pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
61e452ba 157 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
158}
159
f1ce9199
LC
160static scm_t_off
161st_seek (SCM port, scm_t_off offset, int whence)
754c9491 162{
92c2555f 163 scm_t_port *pt = SCM_PTAB_ENTRY (port);
f1ce9199 164 scm_t_off target;
754c9491 165
7dcb364d
GH
166 if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
167 /* special case to avoid disturbing the unread-char buffer. */
754c9491 168 {
7dcb364d
GH
169 if (pt->read_buf == pt->putback_buf)
170 {
171 target = pt->saved_read_pos - pt->saved_read_buf
172 - (pt->read_end - pt->read_pos);
173 }
174 else
175 {
176 target = pt->read_pos - pt->read_buf;
177 }
754c9491 178 }
7dcb364d
GH
179 else
180 /* all other cases. */
754c9491 181 {
7dcb364d 182 if (pt->rw_active == SCM_PORT_READ)
4251ae2e 183 scm_end_input_unlocked (port);
7dcb364d 184
ca2ec018
AW
185 pt->rw_active = SCM_PORT_NEITHER;
186
7dcb364d
GH
187 switch (whence)
188 {
189 case SEEK_CUR:
190 target = pt->read_pos - pt->read_buf + offset;
191 break;
192 case SEEK_END:
193 target = pt->read_end - pt->read_buf + offset;
194 break;
195 default: /* SEEK_SET */
196 target = offset;
197 break;
198 }
199
200 if (target < 0)
201 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
202
203 if (target >= pt->write_buf_size)
3fe6190f 204 {
206d3de3 205 if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
3fe6190f 206 {
7dcb364d
GH
207 if (target > pt->write_buf_size)
208 {
209 scm_misc_error ("st_seek",
210 "seek past end of read-only strport",
211 SCM_EOL);
212 }
213 }
d8f1c216
LC
214 else if (target == pt->write_buf_size)
215 st_resize_port (pt, target * 2);
3fe6190f 216 }
7dcb364d
GH
217 pt->read_pos = pt->write_pos = pt->read_buf + target;
218 if (pt->read_pos > pt->read_end)
3fe6190f 219 {
7dcb364d
GH
220 pt->read_end = (unsigned char *) pt->read_pos;
221 pt->read_buf_size = pt->read_end - pt->read_buf;
3fe6190f 222 }
ee149d03 223 }
754c9491
JB
224 return target;
225}
226
227static void
f1ce9199 228st_truncate (SCM port, scm_t_off length)
754c9491 229{
92c2555f 230 scm_t_port *pt = SCM_PTAB_ENTRY (port);
3fe6190f
GH
231
232 if (length > pt->write_buf_size)
233 st_resize_port (pt, length);
234
235 pt->read_buf_size = length;
236 pt->read_end = pt->read_buf + length;
237 if (pt->read_pos > pt->read_end)
ca2ec018 238 pt->read_pos = pt->write_pos = pt->read_end;
0f2d19dd
JB
239}
240
0b2c2ba3
LC
241/* The initial size in bytes of a string port's buffer. */
242#define INITIAL_BUFFER_SIZE 128
243
244/* Return a new string port with MODES. If STR is #f, a new backing
245 buffer is allocated; otherwise STR must be a string and a copy of it
246 serves as the buffer for the new port. */
7b041912
LC
247SCM
248scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
0f2d19dd 249{
691fcf66 250 SCM z, buf;
92c2555f 251 scm_t_port *pt;
6dce942c 252 size_t read_buf_size, num_bytes, c_byte_pos;
691fcf66 253 char *c_buf;
e11e83f3 254
754c9491
JB
255 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
256 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
5f16b897 257
0b2c2ba3
LC
258 if (scm_is_false (str))
259 {
260 /* Allocate a new buffer to write to. */
6dce942c
MW
261 num_bytes = INITIAL_BUFFER_SIZE;
262 buf = scm_c_make_bytevector (num_bytes);
0b2c2ba3
LC
263 c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf);
264
265 /* Reset `read_buf_size'. It will contain the actual number of
2721f918
AW
266 bytes written to the port. */
267 read_buf_size = 0;
6dce942c 268 c_byte_pos = 0;
0b2c2ba3
LC
269 }
270 else
271 {
0b2c2ba3 272 char *copy;
691fcf66 273
0b2c2ba3 274 SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller);
691fcf66 275
6dce942c
MW
276 /* STR is a string. */
277 /* Create a copy of STR in UTF-8. */
278 copy = scm_to_utf8_stringn (str, &num_bytes);
279 buf = scm_c_make_bytevector (num_bytes);
0b2c2ba3 280 c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf);
6dce942c 281 memcpy (c_buf, copy, num_bytes);
0b2c2ba3 282 free (copy);
691fcf66 283
6dce942c
MW
284 read_buf_size = num_bytes;
285
286 if (scm_is_eq (pos, SCM_INUM0))
287 c_byte_pos = 0;
288 else
289 /* Inefficient but simple way to convert the character position
290 POS into a byte position C_BYTE_POS. */
291 free (scm_to_utf8_stringn (scm_substring (str, SCM_INUM0, pos),
292 &c_byte_pos));
0b2c2ba3 293 }
691fcf66 294
2721f918 295 z = scm_c_make_port_with_encoding (scm_tc16_strport, modes,
6dce942c 296 "UTF-8",
0dd7c540 297 scm_i_default_port_conversion_handler (),
789dd40b 298 SCM_UNPACK (buf));
7b041912 299
2721f918 300 pt = SCM_PTAB_ENTRY (z);
03fcf93b 301
691fcf66 302 pt->write_buf = pt->read_buf = (unsigned char *) c_buf;
6dce942c 303 pt->read_pos = pt->write_pos = pt->read_buf + c_byte_pos;
2721f918 304 pt->read_buf_size = read_buf_size;
6dce942c 305 pt->write_buf_size = num_bytes;
754c9491 306 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
0de97b83 307 pt->rw_random = 1;
3fe6190f 308
0f2d19dd
JB
309 return z;
310}
311
7b041912
LC
312/* Create a new string from the buffer of PORT, a string port, converting from
313 PORT's encoding to the standard string representation. */
314SCM
315scm_strport_to_string (SCM port)
3fe6190f 316{
7b041912
LC
317 scm_t_port *pt = SCM_PTAB_ENTRY (port);
318
fac32b51
MG
319 if (pt->read_buf_size == 0)
320 return scm_nullstr;
321
08467a7e 322 return scm_from_port_stringn ((char *)pt->read_buf, pt->read_buf_size, port);
3fe6190f
GH
323}
324
fe78b6c0
KN
325SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
326 (SCM obj, SCM printer),
327 "Return a Scheme string obtained by printing @var{obj}.\n"
328 "Printing function can be specified by the optional second\n"
329 "argument @var{printer} (default: @code{write}).")
1f3908c4
KN
330#define FUNC_NAME s_scm_object_to_string
331{
8b263377 332 SCM port, result;
fe78b6c0
KN
333
334 if (!SCM_UNBNDP (printer))
335 SCM_VALIDATE_PROC (2, printer);
1f3908c4 336
0b2c2ba3
LC
337 port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
338 SCM_OPN | SCM_WRTNG, FUNC_NAME);
fe78b6c0
KN
339
340 if (SCM_UNBNDP (printer))
341 scm_write (obj, port);
342 else
fdc28395 343 scm_call_2 (printer, obj, port);
fe78b6c0 344
8b263377
LC
345 result = scm_strport_to_string (port);
346
347 /* Explicitly close PORT so that the iconv CDs associated with it are
348 deallocated right away. This is important because CDs use a lot of
349 memory that's not visible to the GC, so not freeing them can lead
350 to almost large heap usage. See
351 <http://wingolog.org/archives/2011/02/25/ports-weaks-gc-and-dark-matter>
352 for details. */
353 scm_close_port (port);
354
355 return result;
1f3908c4
KN
356}
357#undef FUNC_NAME
358
a62b5c3d
AW
359SCM
360scm_call_with_output_string (SCM proc)
0f2d19dd 361{
a62b5c3d 362 static SCM var = SCM_BOOL_F;
754c9491 363
a62b5c3d
AW
364 if (scm_is_false (var))
365 var = scm_c_private_lookup ("guile", "call-with-output-string");
3fe6190f 366
a62b5c3d 367 return scm_call_1 (scm_variable_ref (var), proc);
0f2d19dd
JB
368}
369
a62b5c3d
AW
370SCM
371scm_call_with_input_string (SCM string, SCM proc)
0f2d19dd 372{
a62b5c3d
AW
373 static SCM var = SCM_BOOL_F;
374
375 if (scm_is_false (var))
376 var = scm_c_private_lookup ("guile", "call-with-input-string");
377
378 return scm_call_2 (scm_variable_ref (var), string, proc);
0f2d19dd
JB
379}
380
e87a03fc
MG
381SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0,
382 (SCM str),
1e6808ea
MG
383 "Take a string and return an input port that delivers characters\n"
384 "from the string. The port can be closed by\n"
e87a03fc
MG
385 "@code{close-input-port}, though its storage will be reclaimed\n"
386 "by the garbage collector if it becomes inaccessible.")
387#define FUNC_NAME s_scm_open_input_string
388{
389 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
390 return p;
391}
392#undef FUNC_NAME
393
394SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0,
395 (void),
1e6808ea 396 "Return an output port that will accumulate characters for\n"
e87a03fc
MG
397 "retrieval by @code{get-output-string}. The port can be closed\n"
398 "by the procedure @code{close-output-port}, though its storage\n"
399 "will be reclaimed by the garbage collector if it becomes\n"
400 "inaccessible.")
401#define FUNC_NAME s_scm_open_output_string
402{
403 SCM p;
404
0b2c2ba3 405 p = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
e87a03fc
MG
406 SCM_OPN | SCM_WRTNG,
407 FUNC_NAME);
408 return p;
409}
410#undef FUNC_NAME
411
412SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0,
413 (SCM port),
414 "Given an output port created by @code{open-output-string},\n"
1e6808ea 415 "return a string consisting of the characters that have been\n"
e87a03fc
MG
416 "output to the port so far.")
417#define FUNC_NAME s_scm_get_output_string
418{
419 SCM_VALIDATE_OPOUTSTRPORT (1, port);
420 return scm_strport_to_string (port);
421}
422#undef FUNC_NAME
1cc91f1b 423
cd07a097 424
a8aa30d8
MD
425/* Given a null-terminated string EXPR containing a Scheme expression
426 read it, and return it as an SCM value. */
427SCM
ca13a04a 428scm_c_read_string (const char *expr)
a8aa30d8 429{
3fe6190f 430 SCM port = scm_mkstrport (SCM_INUM0,
cc95e00a 431 scm_from_locale_string (expr),
a8aa30d8 432 SCM_OPN | SCM_RDNG,
ca13a04a 433 "scm_c_read_string");
a8aa30d8
MD
434 SCM form;
435
deca31e1 436 form = scm_read (port);
a8aa30d8
MD
437
438 scm_close_port (port);
439 return form;
440}
441
cd07a097 442/* Given a null-terminated string EXPR containing Scheme program text,
a8aa30d8
MD
443 evaluate it, and return the result of the last expression evaluated. */
444SCM
ca13a04a 445scm_c_eval_string (const char *expr)
cd07a097 446{
cc95e00a 447 return scm_eval_string (scm_from_locale_string (expr));
b377f53e
JB
448}
449
209b52fe
MV
450SCM
451scm_c_eval_string_in_module (const char *expr, SCM module)
452{
cc95e00a 453 return scm_eval_string_in_module (scm_from_locale_string (expr), module);
209b52fe
MV
454}
455
456
60617d81
MW
457static SCM eval_string_var;
458static SCM k_module;
459
460static void
461init_eval_string_var_and_k_module (void)
462{
463 eval_string_var = scm_c_public_variable ("ice-9 eval-string", "eval-string");
464 k_module = scm_from_locale_keyword ("module");
465}
466
209b52fe
MV
467SCM_DEFINE (scm_eval_string_in_module, "eval-string", 1, 1, 0,
468 (SCM string, SCM module),
96e83482
MV
469 "Evaluate @var{string} as the text representation of a Scheme\n"
470 "form or forms, and return whatever value they produce.\n"
209b52fe
MV
471 "Evaluation takes place in the given module, or the current\n"
472 "module when no module is given.\n"
473 "While the code is evaluated, the given module is made the\n"
474 "current one. The current module is restored when this\n"
475 "procedure returns.")
476#define FUNC_NAME s_scm_eval_string_in_module
96e83482 477{
60617d81
MW
478 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
479 scm_i_pthread_once (&once, init_eval_string_var_and_k_module);
0b0e066a 480
209b52fe
MV
481 if (SCM_UNBNDP (module))
482 module = scm_current_module ();
aeec5be1
MV
483 else
484 SCM_VALIDATE_MODULE (2, module);
0b0e066a 485
60617d81
MW
486 return scm_call_3 (scm_variable_ref (eval_string_var),
487 string, k_module, module);
96e83482 488}
1bbd0b84 489#undef FUNC_NAME
cd07a097 490
209b52fe
MV
491SCM
492scm_eval_string (SCM string)
493{
494 return scm_eval_string_in_module (string, SCM_UNDEFINED);
495}
496
92c2555f 497static scm_t_bits
0b6881fa 498scm_make_stptob ()
0f2d19dd 499{
ca2ec018 500 scm_t_bits tc = scm_make_port_type ("string", st_fill_input, st_write);
a98bddfd 501
affc96b5 502 scm_set_port_end_input (tc, st_end_input);
6c747373 503 scm_set_port_seek (tc, st_seek);
affc96b5 504 scm_set_port_truncate (tc, st_truncate);
a98bddfd
DH
505
506 return tc;
0f2d19dd
JB
507}
508
0f2d19dd
JB
509void
510scm_init_strports ()
0f2d19dd 511{
a98bddfd
DH
512 scm_tc16_strport = scm_make_stptob ();
513
a0599745 514#include "libguile/strports.x"
0f2d19dd
JB
515}
516
89e00824
ML
517
518/*
519 Local Variables:
520 c-file-style: "gnu"
521 End:
522*/