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