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