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