* ioext.c (scm_do_read_line, scm_read_line): Use scm_must_malloc,
[bpt/guile.git] / libguile / strports.c
CommitLineData
754c9491 1/* Copyright (C) 1995,1996,1998,1999 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. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e
JB
45#include "unif.h"
46#include "eval.h"
cd07a097 47#include "read.h"
20e6290e
JB
48
49#include "strports.h"
0f2d19dd 50
95b88819
GH
51#ifdef HAVE_STRING_H
52#include <string.h>
53#endif
54
0f2d19dd
JB
55\f
56
57/* {Ports - string ports}
58 *
59 */
60
3fe6190f
GH
61/* NOTES:
62 write_buf/write_end point to the ends of the allocated string.
63 read_buf/read_end in principle point to the part of the string which
64 has been written to, but this is only updated after a flush.
65 read_pos and write_pos in principle should be equal, but this is only true
66 when rw_active is 0.
67*/
1cc91f1b 68
ee149d03
JB
69static int
70stfill_buffer (SCM port)
0f2d19dd 71{
754c9491 72 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 73
ee149d03
JB
74 if (pt->read_pos >= pt->read_end)
75 return EOF;
76 else
5c070ca7 77 return scm_return_first (*pt->read_pos, port);
0f2d19dd
JB
78}
79
3fe6190f
GH
80/* change the size of a port's string to new_size. this doesn't
81 change read_buf_size. */
754c9491 82static void
3fe6190f 83st_resize_port (scm_port *pt, off_t new_size)
754c9491 84{
3fe6190f
GH
85 off_t index = pt->write_pos - pt->write_buf;
86
87 pt->write_buf_size = new_size;
88
89 scm_vector_set_length_x (pt->stream, SCM_MAKINUM (new_size));
754c9491 90
754c9491
JB
91 /* reset buffer in case reallocation moved the string. */
92 {
754c9491 93 pt->read_buf = pt->write_buf = SCM_CHARS (pt->stream);
3fe6190f
GH
94 pt->read_pos = pt->write_pos = pt->write_buf + index;
95 pt->write_end = pt->write_buf + pt->write_buf_size;
96 pt->read_end = pt->read_buf + pt->read_buf_size;
754c9491
JB
97 }
98}
99
3fe6190f
GH
100/* amount by which write_buf is expanded. */
101#define SCM_WRITE_BLOCK 80
102
103/* ensure that write_pos < write_end by enlarging the buffer when
104 necessary. update read_buf to account for written chars. */
ee149d03
JB
105static void
106st_flush (SCM port)
0f2d19dd 107{
754c9491 108 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03
JB
109
110 if (pt->write_pos == pt->write_end)
111 {
3fe6190f 112 st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK);
754c9491
JB
113 }
114 pt->read_pos = pt->write_pos;
3fe6190f
GH
115 if (pt->read_pos > pt->read_end)
116 {
117 pt->read_end = (unsigned char *) pt->read_pos;
118 pt->read_buf_size = pt->read_end - pt->read_buf;
119 }
754c9491
JB
120 pt->rw_active = 0;
121}
122
31703ab8
GH
123static void
124st_write (SCM port, void *data, size_t size)
125{
126 scm_port *pt = SCM_PTAB_ENTRY (port);
127 const char *input = (char *) data;
128
129 while (size > 0)
130 {
131 int space = pt->write_end - pt->write_pos;
132 int write_len = (size > space) ? space : size;
133
134 strncpy (pt->write_pos, input, write_len);
135 pt->write_pos += write_len;
136 size -= write_len;
137 input += write_len;
138 if (write_len == space)
139 st_flush (port);
140 }
141}
142
754c9491 143static void
affc96b5 144st_end_input (SCM port, int offset)
754c9491
JB
145{
146 scm_port *pt = SCM_PTAB_ENTRY (port);
147
148 pt->write_pos = (unsigned char *) pt->read_pos;
149 pt->rw_active = 0;
150}
151
152static off_t
153st_seek (SCM port, off_t offset, int whence)
154{
155 scm_port *pt = SCM_PTAB_ENTRY (port);
156 off_t target;
157
3fe6190f 158 /* we can assume at this point that pt->write_pos == pt->read_pos. */
754c9491
JB
159 switch (whence)
160 {
161 case SEEK_CUR:
3fe6190f 162 target = pt->read_pos - pt->read_buf + offset;
754c9491
JB
163 break;
164 case SEEK_END:
3fe6190f 165 target = pt->read_end - pt->read_buf + offset;
754c9491
JB
166 break;
167 default: /* SEEK_SET */
168 target = offset;
169 break;
170 }
171 if (target < 0)
3fe6190f
GH
172 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
173 if (target >= pt->write_buf_size)
754c9491 174 {
3fe6190f
GH
175 if (!(SCM_CAR (port) & SCM_WRTNG))
176 {
177 if (target > pt->write_buf_size)
178 {
179 scm_misc_error ("st_seek", "seek past end of read-only strport",
180 SCM_EOL);
181 }
182 }
183 else
184 {
185 st_resize_port (pt, target + (target == pt->write_buf_size
186 ? SCM_WRITE_BLOCK
187 : 0));
188 }
ee149d03 189 }
754c9491 190 pt->read_pos = pt->write_pos = pt->read_buf + target;
3fe6190f
GH
191 if (pt->read_pos > pt->read_end)
192 {
193 pt->read_end = (unsigned char *) pt->read_pos;
194 pt->read_buf_size = pt->read_end - pt->read_buf;
195 }
754c9491
JB
196 return target;
197}
198
199static void
affc96b5 200st_truncate (SCM port, off_t length)
754c9491
JB
201{
202 scm_port *pt = SCM_PTAB_ENTRY (port);
3fe6190f
GH
203
204 if (length > pt->write_buf_size)
205 st_resize_port (pt, length);
206
207 pt->read_buf_size = length;
208 pt->read_end = pt->read_buf + length;
209 if (pt->read_pos > pt->read_end)
210 pt->read_pos = pt->read_end;
754c9491 211
3fe6190f
GH
212 if (pt->write_pos > pt->read_end)
213 pt->write_pos = pt->read_end;
0f2d19dd
JB
214}
215
0f2d19dd
JB
216SCM
217scm_mkstrport (pos, str, modes, caller)
218 SCM pos;
219 SCM str;
220 long modes;
3eeba8d4 221 const char * caller;
0f2d19dd
JB
222{
223 SCM z;
754c9491
JB
224 scm_port *pt;
225 int str_len;
226
227 SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
228 SCM_ASSERT (SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, caller);
229 str_len = SCM_ROLENGTH (str);
230 if (SCM_INUM (pos) > str_len)
231 scm_out_of_range (caller, pos);
232 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
233 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
0f2d19dd
JB
234 SCM_NEWCELL (z);
235 SCM_DEFER_INTS;
236 pt = scm_add_to_port_table (z);
a6c64c3c 237 SCM_SETCAR (z, scm_tc16_strport | modes);
0f2d19dd 238 SCM_SETPTAB_ENTRY (z, pt);
754c9491 239 SCM_SETSTREAM (z, str);
ee149d03 240 pt->write_buf = pt->read_buf = SCM_ROCHARS (str);
754c9491
JB
241 pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
242 pt->write_buf_size = pt->read_buf_size = str_len;
243 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
3fe6190f
GH
244
245 /* doesn't check (modes & SCM_RDNG), since the read_buf must be
246 maintained even for output-only ports. */
247 pt->rw_random = modes & SCM_WRTNG;
248
0f2d19dd 249 SCM_ALLOW_INTS;
3fe6190f
GH
250
251 /* ensure write_pos is writable. */
754c9491
JB
252 if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
253 st_flush (z);
0f2d19dd
JB
254 return z;
255}
256
3fe6190f
GH
257/* create a new string from a string port's buffer. */
258SCM scm_strport_to_string (SCM port)
259{
260 scm_port *pt = SCM_PTAB_ENTRY (port);
261
262 if (pt->rw_active == SCM_PORT_WRITE)
263 st_flush (port);
264 return scm_makfromstr (SCM_CHARS (SCM_STREAM (port)),
265 pt->read_buf_size, 0);
266}
267
0f2d19dd 268SCM_PROC(s_call_with_output_string, "call-with-output-string", 1, 0, 0, scm_call_with_output_string);
1cc91f1b 269
0f2d19dd
JB
270SCM
271scm_call_with_output_string (proc)
272 SCM proc;
0f2d19dd
JB
273{
274 SCM p;
754c9491
JB
275
276 p = scm_mkstrport (SCM_INUM0,
277 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
278 SCM_OPN | SCM_WRTNG,
279 s_call_with_output_string);
0f2d19dd 280 scm_apply (proc, p, scm_listofnull);
3fe6190f
GH
281
282 return scm_strport_to_string (p);
0f2d19dd
JB
283}
284
285
286
287/* Return a Scheme string obtained by printing a given object.
288 */
289
1cc91f1b 290
0f2d19dd
JB
291SCM
292scm_strprint_obj (obj)
293 SCM obj;
0f2d19dd
JB
294{
295 SCM str;
296 SCM port;
297
3fe6190f
GH
298 str = scm_makstr (0, 0);
299 port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj");
87818069 300 scm_prin1 (obj, port, 1);
0f2d19dd 301 {
3fe6190f 302 return scm_strport_to_string (port);
0f2d19dd
JB
303 }
304}
305
306
307
308
309SCM_PROC(s_call_with_input_string, "call-with-input-string", 2, 0, 0, scm_call_with_input_string);
1cc91f1b 310
0f2d19dd
JB
311SCM
312scm_call_with_input_string (str, proc)
313 SCM str;
314 SCM proc;
0f2d19dd
JB
315{
316 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, s_call_with_input_string);
317 return scm_apply (proc, p, scm_listofnull);
318}
319
1cc91f1b 320
cd07a097 321
a8aa30d8
MD
322/* Given a null-terminated string EXPR containing a Scheme expression
323 read it, and return it as an SCM value. */
324SCM
325scm_read_0str (expr)
326 char *expr;
327{
3fe6190f 328 SCM port = scm_mkstrport (SCM_INUM0,
a8aa30d8
MD
329 scm_makfrom0str (expr),
330 SCM_OPN | SCM_RDNG,
331 "scm_eval_0str");
332 SCM form;
333
334 /* Read expressions from that port; ignore the values. */
deca31e1 335 form = scm_read (port);
a8aa30d8
MD
336
337 scm_close_port (port);
338 return form;
339}
340
cd07a097 341/* Given a null-terminated string EXPR containing Scheme program text,
a8aa30d8
MD
342 evaluate it, and return the result of the last expression evaluated. */
343SCM
cd07a097
JB
344scm_eval_0str (expr)
345 char *expr;
346{
b377f53e
JB
347 return scm_eval_string (scm_makfrom0str (expr));
348}
349
350
351SCM_PROC (s_eval_string, "eval-string", 1, 0, 0, scm_eval_string);
352
353SCM
354scm_eval_string (string)
355 SCM string;
356{
3fe6190f 357 SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
cd07a097
JB
358 "scm_eval_0str");
359 SCM form;
b377f53e 360 SCM ans = SCM_UNSPECIFIED;
cd07a097
JB
361
362 /* Read expressions from that port; ignore the values. */
0c32d76c 363 while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
a8aa30d8 364 ans = scm_eval_x (form);
cd07a097 365
0d7368d7
JB
366 /* Don't close the port here; if we re-enter this function via a
367 continuation, then the next time we enter it, we'll get an error.
368 It's a string port anyway, so there's no advantage to closing it
369 early. */
370
a8aa30d8 371 return ans;
cd07a097
JB
372}
373
0b6881fa 374void scm_make_stptob (void); /* Called from ports.c */
cd07a097 375
0b6881fa
MD
376void
377scm_make_stptob ()
0f2d19dd 378{
affc96b5 379 long tc = scm_make_port_type ("string", stfill_buffer, st_write);
6c747373 380 scm_set_port_mark (tc, scm_markstream);
affc96b5
GH
381 scm_set_port_end_input (tc, st_end_input);
382 scm_set_port_flush (tc, st_flush);
6c747373 383 scm_set_port_seek (tc, st_seek);
affc96b5 384 scm_set_port_truncate (tc, st_truncate);
0f2d19dd
JB
385}
386
0f2d19dd
JB
387void
388scm_init_strports ()
0f2d19dd
JB
389{
390#include "strports.x"
391}
392