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