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. */ |
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 |
69 | static int |
70 | stfill_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 | 82 | static void |
3fe6190f | 83 | st_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 |
105 | static void |
106 | st_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 | ||
123 | static void | |
283a1a0e | 124 | st_read_flush (SCM port, int offset) |
754c9491 JB |
125 | { |
126 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
127 | ||
128 | pt->write_pos = (unsigned char *) pt->read_pos; | |
129 | pt->rw_active = 0; | |
130 | } | |
131 | ||
132 | static off_t | |
133 | st_seek (SCM port, off_t offset, int whence) | |
134 | { | |
135 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
136 | off_t target; | |
137 | ||
3fe6190f | 138 | /* we can assume at this point that pt->write_pos == pt->read_pos. */ |
754c9491 JB |
139 | switch (whence) |
140 | { | |
141 | case SEEK_CUR: | |
3fe6190f | 142 | target = pt->read_pos - pt->read_buf + offset; |
754c9491 JB |
143 | break; |
144 | case SEEK_END: | |
3fe6190f | 145 | target = pt->read_end - pt->read_buf + offset; |
754c9491 JB |
146 | break; |
147 | default: /* SEEK_SET */ | |
148 | target = offset; | |
149 | break; | |
150 | } | |
151 | if (target < 0) | |
3fe6190f GH |
152 | scm_misc_error ("st_seek", "negative offset", SCM_EOL); |
153 | if (target >= pt->write_buf_size) | |
754c9491 | 154 | { |
3fe6190f GH |
155 | if (!(SCM_CAR (port) & SCM_WRTNG)) |
156 | { | |
157 | if (target > pt->write_buf_size) | |
158 | { | |
159 | scm_misc_error ("st_seek", "seek past end of read-only strport", | |
160 | SCM_EOL); | |
161 | } | |
162 | } | |
163 | else | |
164 | { | |
165 | st_resize_port (pt, target + (target == pt->write_buf_size | |
166 | ? SCM_WRITE_BLOCK | |
167 | : 0)); | |
168 | } | |
ee149d03 | 169 | } |
754c9491 | 170 | pt->read_pos = pt->write_pos = pt->read_buf + target; |
3fe6190f GH |
171 | if (pt->read_pos > pt->read_end) |
172 | { | |
173 | pt->read_end = (unsigned char *) pt->read_pos; | |
174 | pt->read_buf_size = pt->read_end - pt->read_buf; | |
175 | } | |
754c9491 JB |
176 | return target; |
177 | } | |
178 | ||
179 | static void | |
180 | st_ftruncate (SCM port, off_t length) | |
181 | { | |
182 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
3fe6190f GH |
183 | |
184 | if (length > pt->write_buf_size) | |
185 | st_resize_port (pt, length); | |
186 | ||
187 | pt->read_buf_size = length; | |
188 | pt->read_end = pt->read_buf + length; | |
189 | if (pt->read_pos > pt->read_end) | |
190 | pt->read_pos = pt->read_end; | |
754c9491 | 191 | |
3fe6190f GH |
192 | if (pt->write_pos > pt->read_end) |
193 | pt->write_pos = pt->read_end; | |
0f2d19dd JB |
194 | } |
195 | ||
0f2d19dd JB |
196 | SCM |
197 | scm_mkstrport (pos, str, modes, caller) | |
198 | SCM pos; | |
199 | SCM str; | |
200 | long modes; | |
3eeba8d4 | 201 | const char * caller; |
0f2d19dd JB |
202 | { |
203 | SCM z; | |
754c9491 JB |
204 | scm_port *pt; |
205 | int str_len; | |
206 | ||
207 | SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller); | |
208 | SCM_ASSERT (SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, caller); | |
209 | str_len = SCM_ROLENGTH (str); | |
210 | if (SCM_INUM (pos) > str_len) | |
211 | scm_out_of_range (caller, pos); | |
212 | if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG))) | |
213 | scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL); | |
0f2d19dd JB |
214 | SCM_NEWCELL (z); |
215 | SCM_DEFER_INTS; | |
216 | pt = scm_add_to_port_table (z); | |
a6c64c3c | 217 | SCM_SETCAR (z, scm_tc16_strport | modes); |
0f2d19dd | 218 | SCM_SETPTAB_ENTRY (z, pt); |
754c9491 | 219 | SCM_SETSTREAM (z, str); |
ee149d03 | 220 | pt->write_buf = pt->read_buf = SCM_ROCHARS (str); |
754c9491 JB |
221 | pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos); |
222 | pt->write_buf_size = pt->read_buf_size = str_len; | |
223 | pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size; | |
3fe6190f GH |
224 | |
225 | /* doesn't check (modes & SCM_RDNG), since the read_buf must be | |
226 | maintained even for output-only ports. */ | |
227 | pt->rw_random = modes & SCM_WRTNG; | |
228 | ||
0f2d19dd | 229 | SCM_ALLOW_INTS; |
3fe6190f GH |
230 | |
231 | /* ensure write_pos is writable. */ | |
754c9491 JB |
232 | if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end) |
233 | st_flush (z); | |
0f2d19dd JB |
234 | return z; |
235 | } | |
236 | ||
3fe6190f GH |
237 | /* create a new string from a string port's buffer. */ |
238 | SCM scm_strport_to_string (SCM port) | |
239 | { | |
240 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
241 | ||
242 | if (pt->rw_active == SCM_PORT_WRITE) | |
243 | st_flush (port); | |
244 | return scm_makfromstr (SCM_CHARS (SCM_STREAM (port)), | |
245 | pt->read_buf_size, 0); | |
246 | } | |
247 | ||
0f2d19dd | 248 | SCM_PROC(s_call_with_output_string, "call-with-output-string", 1, 0, 0, scm_call_with_output_string); |
1cc91f1b | 249 | |
0f2d19dd JB |
250 | SCM |
251 | scm_call_with_output_string (proc) | |
252 | SCM proc; | |
0f2d19dd JB |
253 | { |
254 | SCM p; | |
754c9491 JB |
255 | |
256 | p = scm_mkstrport (SCM_INUM0, | |
257 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
258 | SCM_OPN | SCM_WRTNG, | |
259 | s_call_with_output_string); | |
0f2d19dd | 260 | scm_apply (proc, p, scm_listofnull); |
3fe6190f GH |
261 | |
262 | return scm_strport_to_string (p); | |
0f2d19dd JB |
263 | } |
264 | ||
265 | ||
266 | ||
267 | /* Return a Scheme string obtained by printing a given object. | |
268 | */ | |
269 | ||
1cc91f1b | 270 | |
0f2d19dd JB |
271 | SCM |
272 | scm_strprint_obj (obj) | |
273 | SCM obj; | |
0f2d19dd JB |
274 | { |
275 | SCM str; | |
276 | SCM port; | |
277 | ||
3fe6190f GH |
278 | str = scm_makstr (0, 0); |
279 | port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj"); | |
87818069 | 280 | scm_prin1 (obj, port, 1); |
0f2d19dd | 281 | { |
3fe6190f | 282 | return scm_strport_to_string (port); |
0f2d19dd JB |
283 | } |
284 | } | |
285 | ||
286 | ||
287 | ||
288 | ||
289 | SCM_PROC(s_call_with_input_string, "call-with-input-string", 2, 0, 0, scm_call_with_input_string); | |
1cc91f1b | 290 | |
0f2d19dd JB |
291 | SCM |
292 | scm_call_with_input_string (str, proc) | |
293 | SCM str; | |
294 | SCM proc; | |
0f2d19dd JB |
295 | { |
296 | SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, s_call_with_input_string); | |
297 | return scm_apply (proc, p, scm_listofnull); | |
298 | } | |
299 | ||
1cc91f1b | 300 | |
cd07a097 | 301 | |
a8aa30d8 MD |
302 | /* Given a null-terminated string EXPR containing a Scheme expression |
303 | read it, and return it as an SCM value. */ | |
304 | SCM | |
305 | scm_read_0str (expr) | |
306 | char *expr; | |
307 | { | |
3fe6190f | 308 | SCM port = scm_mkstrport (SCM_INUM0, |
a8aa30d8 MD |
309 | scm_makfrom0str (expr), |
310 | SCM_OPN | SCM_RDNG, | |
311 | "scm_eval_0str"); | |
312 | SCM form; | |
313 | ||
314 | /* Read expressions from that port; ignore the values. */ | |
deca31e1 | 315 | form = scm_read (port); |
a8aa30d8 MD |
316 | |
317 | scm_close_port (port); | |
318 | return form; | |
319 | } | |
320 | ||
cd07a097 | 321 | /* Given a null-terminated string EXPR containing Scheme program text, |
a8aa30d8 MD |
322 | evaluate it, and return the result of the last expression evaluated. */ |
323 | SCM | |
cd07a097 JB |
324 | scm_eval_0str (expr) |
325 | char *expr; | |
326 | { | |
b377f53e JB |
327 | return scm_eval_string (scm_makfrom0str (expr)); |
328 | } | |
329 | ||
330 | ||
331 | SCM_PROC (s_eval_string, "eval-string", 1, 0, 0, scm_eval_string); | |
332 | ||
333 | SCM | |
334 | scm_eval_string (string) | |
335 | SCM string; | |
336 | { | |
3fe6190f | 337 | SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG, |
cd07a097 JB |
338 | "scm_eval_0str"); |
339 | SCM form; | |
b377f53e | 340 | SCM ans = SCM_UNSPECIFIED; |
cd07a097 JB |
341 | |
342 | /* Read expressions from that port; ignore the values. */ | |
0c32d76c | 343 | while (!SCM_EOF_OBJECT_P (form = scm_read (port))) |
a8aa30d8 | 344 | ans = scm_eval_x (form); |
cd07a097 | 345 | |
0d7368d7 JB |
346 | /* Don't close the port here; if we re-enter this function via a |
347 | continuation, then the next time we enter it, we'll get an error. | |
348 | It's a string port anyway, so there's no advantage to closing it | |
349 | early. */ | |
350 | ||
a8aa30d8 | 351 | return ans; |
cd07a097 JB |
352 | } |
353 | ||
0b6881fa | 354 | void scm_make_stptob (void); /* Called from ports.c */ |
cd07a097 | 355 | |
0b6881fa MD |
356 | void |
357 | scm_make_stptob () | |
0f2d19dd | 358 | { |
0b6881fa MD |
359 | long tc = scm_make_port_type ("string", stfill_buffer, st_flush); |
360 | scm_set_ptob_mark (tc, scm_markstream); | |
361 | scm_set_ptob_flush_input (tc, st_read_flush); | |
362 | scm_set_ptob_seek (tc, st_seek); | |
363 | scm_set_ptob_truncate (tc, st_ftruncate); | |
0f2d19dd JB |
364 | } |
365 | ||
0f2d19dd JB |
366 | void |
367 | scm_init_strports () | |
0f2d19dd JB |
368 | { |
369 | #include "strports.x" | |
370 | } | |
371 |