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