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