Fix: Don't cast SCM values to pointer. Use SCM2PTR instead.
[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. */
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
0f2d19dd 47#include "_scm.h"
681b9005
MD
48
49#include <stdio.h>
50#ifdef HAVE_UNISTD_H
51#include <unistd.h>
52#endif
53
20e6290e
JB
54#include "unif.h"
55#include "eval.h"
f04d8caf 56#include "ports.h"
cd07a097 57#include "read.h"
ba11fd4c 58#include "root.h"
7ab3fdd5 59#include "strings.h"
003d1fd0 60#include "vectors.h"
20e6290e
JB
61
62#include "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
ee149d03
JB
82static int
83stfill_buffer (SCM port)
0f2d19dd 84{
754c9491 85 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 86
ee149d03
JB
87 if (pt->read_pos >= pt->read_end)
88 return EOF;
89 else
41b0806d 90 return scm_return_first_int (*pt->read_pos, port);
0f2d19dd
JB
91}
92
3fe6190f
GH
93/* change the size of a port's string to new_size. this doesn't
94 change read_buf_size. */
754c9491 95static void
3fe6190f 96st_resize_port (scm_port *pt, off_t new_size)
754c9491 97{
3fe6190f
GH
98 off_t index = pt->write_pos - pt->write_buf;
99
100 pt->write_buf_size = new_size;
101
102 scm_vector_set_length_x (pt->stream, SCM_MAKINUM (new_size));
754c9491 103
754c9491
JB
104 /* reset buffer in case reallocation moved the string. */
105 {
754c9491 106 pt->read_buf = pt->write_buf = SCM_CHARS (pt->stream);
3fe6190f
GH
107 pt->read_pos = pt->write_pos = pt->write_buf + index;
108 pt->write_end = pt->write_buf + pt->write_buf_size;
109 pt->read_end = pt->read_buf + pt->read_buf_size;
754c9491
JB
110 }
111}
112
3fe6190f
GH
113/* amount by which write_buf is expanded. */
114#define SCM_WRITE_BLOCK 80
115
116/* ensure that write_pos < write_end by enlarging the buffer when
117 necessary. update read_buf to account for written chars. */
ee149d03
JB
118static void
119st_flush (SCM port)
0f2d19dd 120{
754c9491 121 scm_port *pt = SCM_PTAB_ENTRY (port);
ee149d03
JB
122
123 if (pt->write_pos == pt->write_end)
124 {
3fe6190f 125 st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK);
754c9491
JB
126 }
127 pt->read_pos = pt->write_pos;
3fe6190f
GH
128 if (pt->read_pos > pt->read_end)
129 {
130 pt->read_end = (unsigned char *) pt->read_pos;
131 pt->read_buf_size = pt->read_end - pt->read_buf;
132 }
61e452ba 133 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
134}
135
31703ab8 136static void
8aa011a1 137st_write (SCM port, const void *data, size_t size)
31703ab8
GH
138{
139 scm_port *pt = SCM_PTAB_ENTRY (port);
140 const char *input = (char *) data;
141
142 while (size > 0)
143 {
144 int space = pt->write_end - pt->write_pos;
145 int write_len = (size > space) ? space : size;
146
147 strncpy (pt->write_pos, input, write_len);
148 pt->write_pos += write_len;
149 size -= write_len;
150 input += write_len;
151 if (write_len == space)
152 st_flush (port);
153 }
154}
155
754c9491 156static void
affc96b5 157st_end_input (SCM port, int offset)
754c9491
JB
158{
159 scm_port *pt = SCM_PTAB_ENTRY (port);
7dcb364d 160
cd19d608
GH
161 if (pt->read_pos - pt->read_buf < offset)
162 scm_misc_error ("st_end_input", "negative position", SCM_EOL);
163
a3c8b9fc 164 pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
61e452ba 165 pt->rw_active = SCM_PORT_NEITHER;
754c9491
JB
166}
167
168static off_t
169st_seek (SCM port, off_t offset, int whence)
170{
171 scm_port *pt = SCM_PTAB_ENTRY (port);
172 off_t target;
173
7dcb364d
GH
174 if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
175 /* special case to avoid disturbing the unread-char buffer. */
754c9491 176 {
7dcb364d
GH
177 if (pt->read_buf == pt->putback_buf)
178 {
179 target = pt->saved_read_pos - pt->saved_read_buf
180 - (pt->read_end - pt->read_pos);
181 }
182 else
183 {
184 target = pt->read_pos - pt->read_buf;
185 }
754c9491 186 }
7dcb364d
GH
187 else
188 /* all other cases. */
754c9491 189 {
7dcb364d
GH
190 if (pt->rw_active == SCM_PORT_WRITE)
191 st_flush (port);
192
193 if (pt->rw_active == SCM_PORT_READ)
194 scm_end_input (port);
195
196 switch (whence)
197 {
198 case SEEK_CUR:
199 target = pt->read_pos - pt->read_buf + offset;
200 break;
201 case SEEK_END:
202 target = pt->read_end - pt->read_buf + offset;
203 break;
204 default: /* SEEK_SET */
205 target = offset;
206 break;
207 }
208
209 if (target < 0)
210 scm_misc_error ("st_seek", "negative offset", SCM_EOL);
211
212 if (target >= pt->write_buf_size)
3fe6190f 213 {
f1267706 214 if (!(SCM_UNPACK_CAR (port) & SCM_WRTNG))
3fe6190f 215 {
7dcb364d
GH
216 if (target > pt->write_buf_size)
217 {
218 scm_misc_error ("st_seek",
219 "seek past end of read-only strport",
220 SCM_EOL);
221 }
222 }
223 else
224 {
225 st_resize_port (pt, target + (target == pt->write_buf_size
226 ? SCM_WRITE_BLOCK
227 : 0));
3fe6190f
GH
228 }
229 }
7dcb364d
GH
230 pt->read_pos = pt->write_pos = pt->read_buf + target;
231 if (pt->read_pos > pt->read_end)
3fe6190f 232 {
7dcb364d
GH
233 pt->read_end = (unsigned char *) pt->read_pos;
234 pt->read_buf_size = pt->read_end - pt->read_buf;
3fe6190f 235 }
ee149d03 236 }
754c9491
JB
237 return target;
238}
239
240static void
affc96b5 241st_truncate (SCM port, off_t length)
754c9491
JB
242{
243 scm_port *pt = SCM_PTAB_ENTRY (port);
3fe6190f
GH
244
245 if (length > pt->write_buf_size)
246 st_resize_port (pt, length);
247
248 pt->read_buf_size = length;
249 pt->read_end = pt->read_buf + length;
250 if (pt->read_pos > pt->read_end)
251 pt->read_pos = pt->read_end;
754c9491 252
3fe6190f
GH
253 if (pt->write_pos > pt->read_end)
254 pt->write_pos = pt->read_end;
0f2d19dd
JB
255}
256
0f2d19dd 257SCM
1bbd0b84 258scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
0f2d19dd
JB
259{
260 SCM z;
754c9491
JB
261 scm_port *pt;
262 int str_len;
263
264 SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
0c95b57d 265 SCM_ASSERT (SCM_ROSTRINGP(str), str, SCM_ARG1, caller);
754c9491
JB
266 str_len = SCM_ROLENGTH (str);
267 if (SCM_INUM (pos) > str_len)
268 scm_out_of_range (caller, pos);
269 if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
270 scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
0f2d19dd
JB
271 SCM_NEWCELL (z);
272 SCM_DEFER_INTS;
273 pt = scm_add_to_port_table (z);
a6c64c3c 274 SCM_SETCAR (z, scm_tc16_strport | modes);
0f2d19dd 275 SCM_SETPTAB_ENTRY (z, pt);
754c9491 276 SCM_SETSTREAM (z, str);
ee149d03 277 pt->write_buf = pt->read_buf = SCM_ROCHARS (str);
754c9491
JB
278 pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
279 pt->write_buf_size = pt->read_buf_size = str_len;
280 pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
3fe6190f 281
0de97b83 282 pt->rw_random = 1;
3fe6190f 283
0f2d19dd 284 SCM_ALLOW_INTS;
3fe6190f
GH
285
286 /* ensure write_pos is writable. */
754c9491
JB
287 if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
288 st_flush (z);
0f2d19dd
JB
289 return z;
290}
291
3fe6190f
GH
292/* create a new string from a string port's buffer. */
293SCM scm_strport_to_string (SCM port)
294{
295 scm_port *pt = SCM_PTAB_ENTRY (port);
296
297 if (pt->rw_active == SCM_PORT_WRITE)
298 st_flush (port);
e684c60f 299 return scm_makfromstr (pt->read_buf, pt->read_buf_size, 0);
3fe6190f
GH
300}
301
3b3b36dd 302SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0,
1bbd0b84 303 (SCM proc),
b380b885
MD
304 "Calls the one-argument procedure @var{proc} with a newly created output\n"
305 "port. When the function returns, the string composed of the characters\n"
306 "written into the port is returned.")
1bbd0b84 307#define FUNC_NAME s_scm_call_with_output_string
0f2d19dd
JB
308{
309 SCM p;
754c9491
JB
310
311 p = scm_mkstrport (SCM_INUM0,
312 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
313 SCM_OPN | SCM_WRTNG,
1bbd0b84 314 FUNC_NAME);
0f2d19dd 315 scm_apply (proc, p, scm_listofnull);
3fe6190f
GH
316
317 return scm_strport_to_string (p);
0f2d19dd 318}
1bbd0b84 319#undef FUNC_NAME
0f2d19dd
JB
320
321
322
323/* Return a Scheme string obtained by printing a given object.
324 */
325
1cc91f1b 326
0f2d19dd 327SCM
1bbd0b84 328scm_strprint_obj (SCM obj)
0f2d19dd
JB
329{
330 SCM str;
331 SCM port;
332
3fe6190f
GH
333 str = scm_makstr (0, 0);
334 port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj");
87818069 335 scm_prin1 (obj, port, 1);
0f2d19dd 336 {
3fe6190f 337 return scm_strport_to_string (port);
0f2d19dd
JB
338 }
339}
340
341
342
343
3b3b36dd 344SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0,
1bbd0b84 345 (SCM str, SCM proc),
b380b885
MD
346 "Calls the one-argument procedure @var{proc} with a newly created input\n"
347 "port from which @var{string}'s contents may be read. The value yielded\n"
348 "by the @var{proc} is returned.")
1bbd0b84 349#define FUNC_NAME s_scm_call_with_input_string
0f2d19dd 350{
1bbd0b84 351 SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
0f2d19dd
JB
352 return scm_apply (proc, p, scm_listofnull);
353}
1bbd0b84 354#undef FUNC_NAME
0f2d19dd 355
1cc91f1b 356
cd07a097 357
a8aa30d8
MD
358/* Given a null-terminated string EXPR containing a Scheme expression
359 read it, and return it as an SCM value. */
360SCM
1bbd0b84 361scm_read_0str (char *expr)
a8aa30d8 362{
3fe6190f 363 SCM port = scm_mkstrport (SCM_INUM0,
a8aa30d8
MD
364 scm_makfrom0str (expr),
365 SCM_OPN | SCM_RDNG,
366 "scm_eval_0str");
367 SCM form;
368
369 /* Read expressions from that port; ignore the values. */
deca31e1 370 form = scm_read (port);
a8aa30d8
MD
371
372 scm_close_port (port);
373 return form;
374}
375
cd07a097 376/* Given a null-terminated string EXPR containing Scheme program text,
a8aa30d8
MD
377 evaluate it, and return the result of the last expression evaluated. */
378SCM
6e706938 379scm_eval_0str (const char *expr)
cd07a097 380{
b377f53e
JB
381 return scm_eval_string (scm_makfrom0str (expr));
382}
383
384
a1ec6916 385SCM_DEFINE (scm_eval_string, "eval-string", 1, 0, 0,
1bbd0b84 386 (SCM string),
b380b885
MD
387 "Evaluate @var{string} as the text representation of a Scheme form\n"
388 "or forms, and return whatever value they produce.")
1bbd0b84 389#define FUNC_NAME s_scm_eval_string
b377f53e 390{
3fe6190f 391 SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
cd07a097
JB
392 "scm_eval_0str");
393 SCM form;
b377f53e 394 SCM ans = SCM_UNSPECIFIED;
cd07a097
JB
395
396 /* Read expressions from that port; ignore the values. */
0c32d76c 397 while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
a8aa30d8 398 ans = scm_eval_x (form);
cd07a097 399
0d7368d7
JB
400 /* Don't close the port here; if we re-enter this function via a
401 continuation, then the next time we enter it, we'll get an error.
402 It's a string port anyway, so there's no advantage to closing it
403 early. */
404
a8aa30d8 405 return ans;
cd07a097 406}
1bbd0b84 407#undef FUNC_NAME
cd07a097 408
0b6881fa 409void scm_make_stptob (void); /* Called from ports.c */
cd07a097 410
0b6881fa
MD
411void
412scm_make_stptob ()
0f2d19dd 413{
affc96b5 414 long tc = scm_make_port_type ("string", stfill_buffer, st_write);
6c747373 415 scm_set_port_mark (tc, scm_markstream);
affc96b5
GH
416 scm_set_port_end_input (tc, st_end_input);
417 scm_set_port_flush (tc, st_flush);
6c747373 418 scm_set_port_seek (tc, st_seek);
affc96b5 419 scm_set_port_truncate (tc, st_truncate);
0f2d19dd
JB
420}
421
0f2d19dd
JB
422void
423scm_init_strports ()
0f2d19dd
JB
424{
425#include "strports.x"
426}
427
89e00824
ML
428
429/*
430 Local Variables:
431 c-file-style: "gnu"
432 End:
433*/