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