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