Mercurial > hg > xemacs-beta
annotate src/lstream.c @ 5118:e0db3c197671 ben-lisp-object
merge up to latest default branch, doesn't compile yet
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 21:18:49 -0600 |
parents | 3742ea8250b5 d674024a8674 |
children | 623d57b7fbe8 |
rev | line source |
---|---|
428 | 1 /* Generic stream implementation. |
2 Copyright (C) 1995 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
788 | 4 Copyright (C) 1996, 2001, 2002 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: Not in FSF. */ | |
24 | |
25 /* Written by Ben Wing. */ | |
26 | |
27 #include <config.h> | |
28 #include "lisp.h" | |
29 | |
30 #include "buffer.h" | |
31 #include "insdel.h" | |
32 #include "lstream.h" | |
33 | |
34 #include "sysfile.h" | |
35 | |
771 | 36 /* This module provides a generic buffering stream implementation. |
428 | 37 Conceptually, you send data to the stream or read data from the |
38 stream, not caring what's on the other end of the stream. The | |
39 other end could be another stream, a file descriptor, a stdio | |
40 stream, a fixed block of memory, a reallocating block of memory, | |
41 etc. The main purpose of the stream is to provide a standard | |
42 interface and to do buffering. Macros are defined to read | |
43 or write characters, so the calling functions do not have to | |
44 worry about blocking data together in order to achieve efficiency. | |
45 | |
771 | 46 Note that this object is called "stream" in Lisp but "lstream" |
428 | 47 in C. The reason for this is that "stream" is too generic a name |
48 for C; too much likelihood of conflict/confusion with C++, etc. */ | |
49 | |
50 #define DEFAULT_BLOCK_BUFFERING_SIZE 512 | |
51 #define MAX_READ_SIZE 512 | |
52 | |
53 static Lisp_Object | |
54 mark_lstream (Lisp_Object obj) | |
55 { | |
56 Lstream *lstr = XLSTREAM (obj); | |
57 return lstr->imp->marker ? (lstr->imp->marker) (obj) : Qnil; | |
58 } | |
59 | |
60 static void | |
2286 | 61 print_lstream (Lisp_Object obj, Lisp_Object printcharfun, |
62 int UNUSED (escapeflag)) | |
428 | 63 { |
64 Lstream *lstr = XLSTREAM (obj); | |
65 | |
800 | 66 write_fmt_string (printcharfun, |
67 "#<INTERNAL OBJECT (XEmacs bug?) (%s lstream) 0x%lx>", | |
68 lstr->imp->name, (long) lstr); | |
428 | 69 } |
70 | |
71 static void | |
72 finalize_lstream (void *header, int for_disksave) | |
73 { | |
74 /* WARNING WARNING WARNING. This function (and all finalize functions) | |
75 may get called more than once on the same object, and may get called | |
76 (at dump time) on objects that are not being released. */ | |
77 Lstream *lstr = (Lstream *) header; | |
78 | |
79 #if 0 /* this may cause weird Broken Pipes? */ | |
80 if (for_disksave) | |
81 { | |
82 Lstream_pseudo_close (lstr); | |
83 return; | |
84 } | |
85 #endif | |
86 if (lstr->flags & LSTREAM_FL_IS_OPEN) | |
87 { | |
88 if (for_disksave) | |
89 { | |
90 if (lstr->flags & LSTREAM_FL_CLOSE_AT_DISKSAVE) | |
91 Lstream_close (lstr); | |
92 } | |
93 else | |
94 /* Just close. */ | |
95 Lstream_close (lstr); | |
96 } | |
771 | 97 |
98 if (!for_disksave) | |
99 { | |
100 if (lstr->imp->finalizer) | |
101 (lstr->imp->finalizer) (lstr); | |
102 } | |
428 | 103 } |
104 | |
665 | 105 inline static Bytecount |
106 aligned_sizeof_lstream (Bytecount lstream_type_specific_size) | |
456 | 107 { |
826 | 108 return MAX_ALIGN_SIZE (offsetof (Lstream, data) + |
109 lstream_type_specific_size); | |
456 | 110 } |
111 | |
665 | 112 static Bytecount |
442 | 113 sizeof_lstream (const void *header) |
428 | 114 { |
456 | 115 return aligned_sizeof_lstream (((const Lstream *) header)->imp->size); |
428 | 116 } |
117 | |
1204 | 118 static const struct memory_description lstream_implementation_description_1[] |
119 = { | |
120 { XD_END } | |
121 }; | |
122 | |
123 const struct sized_memory_description lstream_implementation_description = { | |
124 sizeof (struct lstream_implementation), | |
125 lstream_implementation_description_1 | |
126 }; | |
127 | |
128 static const struct sized_memory_description lstream_extra_description_map[] = | |
129 { | |
130 { offsetof (Lstream, imp) }, | |
131 { offsetof (struct lstream_implementation, extra_description) }, | |
132 { -1 }, | |
133 }; | |
134 | |
135 static const struct memory_description lstream_description[] = | |
136 { | |
2367 | 137 { XD_BLOCK_PTR, offsetof (Lstream, imp), 1, |
2551 | 138 { &lstream_implementation_description } }, |
2367 | 139 { XD_BLOCK_ARRAY, offsetof (Lstream, data), 1, |
2551 | 140 { lstream_extra_description_map } }, |
1204 | 141 { XD_END } |
142 }; | |
143 | |
144 static const struct memory_description lstream_empty_extra_description_1[] = | |
145 { | |
146 { XD_END } | |
147 }; | |
148 | |
149 const struct sized_memory_description lstream_empty_extra_description = { | |
150 0, lstream_empty_extra_description_1 | |
151 }; | |
152 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
153 DEFINE_NODUMP_SIZABLE_LISP_OBJECT ("stream", lstream, |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
154 mark_lstream, print_lstream, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
155 finalize_lstream, 0, 0, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
156 lstream_description, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
157 sizeof_lstream, Lstream); |
428 | 158 |
771 | 159 |
160 /* Change the buffering of a stream. See lstream.h. By default the | |
161 buffering is STREAM_BLOCK_BUFFERED. */ | |
162 | |
428 | 163 void |
164 Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering, | |
165 int buffering_size) | |
166 { | |
167 lstr->buffering = buffering; | |
168 switch (buffering) | |
169 { | |
170 case LSTREAM_UNBUFFERED: | |
171 lstr->buffering_size = 0; break; | |
172 case LSTREAM_BLOCK_BUFFERED: | |
173 lstr->buffering_size = DEFAULT_BLOCK_BUFFERING_SIZE; break; | |
174 case LSTREAM_BLOCKN_BUFFERED: | |
175 lstr->buffering_size = buffering_size; break; | |
176 case LSTREAM_LINE_BUFFERED: | |
177 case LSTREAM_UNLIMITED: | |
178 lstr->buffering_size = INT_MAX; break; | |
179 } | |
180 } | |
181 | |
3263 | 182 #ifndef NEW_GC |
442 | 183 static const Lstream_implementation *lstream_types[32]; |
428 | 184 static Lisp_Object Vlstream_free_list[32]; |
185 static int lstream_type_count; | |
3263 | 186 #endif /* not NEW_GC */ |
428 | 187 |
771 | 188 /* Allocate and return a new Lstream. This function is not really |
189 meant to be called directly; rather, each stream type should | |
190 provide its own stream creation function, which creates the stream | |
191 and does any other necessary creation stuff (e.g. opening a | |
192 file). */ | |
193 | |
428 | 194 Lstream * |
442 | 195 Lstream_new (const Lstream_implementation *imp, const char *mode) |
428 | 196 { |
197 Lstream *p; | |
3263 | 198 #ifdef NEW_GC |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
199 p = XLSTREAM (alloc_sized_lrecord (aligned_sizeof_lstream (imp->size), |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
200 &lrecord_lstream)); |
3263 | 201 #else /* not NEW_GC */ |
428 | 202 int i; |
203 | |
204 for (i = 0; i < lstream_type_count; i++) | |
205 { | |
206 if (lstream_types[i] == imp) | |
207 break; | |
208 } | |
209 | |
210 if (i == lstream_type_count) | |
211 { | |
212 assert (lstream_type_count < countof (lstream_types)); | |
213 lstream_types[lstream_type_count] = imp; | |
214 Vlstream_free_list[lstream_type_count] = | |
456 | 215 make_lcrecord_list (aligned_sizeof_lstream (imp->size), |
428 | 216 &lrecord_lstream); |
217 lstream_type_count++; | |
218 } | |
219 | |
1204 | 220 p = XLSTREAM (alloc_managed_lcrecord (Vlstream_free_list[i])); |
3263 | 221 #endif /* not NEW_GC */ |
428 | 222 /* Zero it out, except the header. */ |
456 | 223 memset ((char *) p + sizeof (p->header), '\0', |
224 aligned_sizeof_lstream (imp->size) - sizeof (p->header)); | |
428 | 225 p->imp = imp; |
226 Lstream_set_buffering (p, LSTREAM_BLOCK_BUFFERED, 0); | |
227 p->flags = LSTREAM_FL_IS_OPEN; | |
228 | |
229 /* convert mode (one of "r", "w", "rc", "wc") to p->flags */ | |
230 assert (mode[0] == 'r' || mode[0] == 'w'); | |
231 assert (mode[1] == 'c' || mode[1] == '\0'); | |
232 p->flags |= (mode[0] == 'r' ? LSTREAM_FL_READ : LSTREAM_FL_WRITE); | |
233 if (mode[1] == 'c') | |
234 p->flags |= LSTREAM_FL_NO_PARTIAL_CHARS; | |
235 | |
236 return p; | |
237 } | |
238 | |
771 | 239 /* Set or unset "character mode" on the stream. The basic idea is that, |
240 assuming valid internal-format data is passing through the stream and | |
241 we're processing the data character by character, we don't want partial | |
242 characters at the end of the data. (No partial characters at the | |
243 beginning happens naturally if we eliminate partial characters at the | |
244 end and the stream is implemented correctly.) | |
245 | |
246 Character mode actually has two somewhat different meanings, depending | |
247 on whether this is a read stream or write stream. If a read stream, | |
248 character mode means that data returned from calling Lstream_read() on | |
249 the stream will contain only full characters. If a write stream, | |
250 character mode means that data passed to the write method in the stream | |
251 implementation will contain only full characters. It's important to | |
252 note the non-parallelism in who should set this mode on the stream: The | |
253 *CALLER* sets character mode on read streams it creates; the *STREAM | |
254 ITSELF* sets character mode on write streams, typically at creation | |
814 | 255 time. |
256 | |
257 (However, if a read stream always generates internal-format data, then | |
258 the callers will almost always want character mode, and it's allowed to | |
259 set this on behalf of the caller, as long as a flag can be provided at | |
260 creation time to disable this behavior.) */ | |
771 | 261 |
428 | 262 void |
263 Lstream_set_character_mode (Lstream *lstr) | |
264 { | |
265 lstr->flags |= LSTREAM_FL_NO_PARTIAL_CHARS; | |
266 } | |
267 | |
771 | 268 /* Unset character mode. See Lstream_set_character_mode(). */ |
269 | |
270 void | |
271 Lstream_unset_character_mode (Lstream *lstr) | |
272 { | |
273 lstr->flags &= ~LSTREAM_FL_NO_PARTIAL_CHARS; | |
274 } | |
275 | |
276 /* Close the stream (if it's open), and free all memory associated with the | |
277 stream. Put the stream on a free list; later calls to create a new | |
278 stream of this type may reuse this stream. Calling this is not strictly | |
279 necessary, but it is much more efficient than having the Lstream be | |
280 garbage-collected. Be VERY VERY SURE there are no pointers to this | |
281 object hanging around anywhere where they might be used! When streams | |
282 are chained together, be VERY CAREFUL of the order in which you delete | |
283 them! (e.g. if the streams are in a singly-linked list, delete the head | |
814 | 284 first; this will close (but check the documentation, e.g. of |
285 make_coding_input_stream()), and may send data down to the rest. Then | |
771 | 286 proceed to the rest, one by one. If the chains are in a doubly-linked |
287 list, close all the streams first (again, from the head to the tail), | |
288 disconnect the back links, then delete starting from the head. In | |
814 | 289 general, it's a good idea to close everything before deleting anything. |
771 | 290 |
291 NOTE: DO NOT CALL DURING GARBAGE COLLECTION (e.g. in a finalizer). You | |
292 will be aborted. See free_managed_lcrecord(). */ | |
293 | |
428 | 294 void |
295 Lstream_delete (Lstream *lstr) | |
296 { | |
3263 | 297 #ifndef NEW_GC |
428 | 298 int i; |
3263 | 299 #endif /* not NEW_GC */ |
793 | 300 Lisp_Object val = wrap_lstream (lstr); |
428 | 301 |
3263 | 302 #ifdef NEW_GC |
2720 | 303 free_lrecord (val); |
3263 | 304 #else /* not NEW_GC */ |
428 | 305 for (i = 0; i < lstream_type_count; i++) |
306 { | |
307 if (lstream_types[i] == lstr->imp) | |
308 { | |
309 free_managed_lcrecord (Vlstream_free_list[i], val); | |
310 return; | |
311 } | |
312 } | |
313 | |
2500 | 314 ABORT (); |
3263 | 315 #endif /* not NEW_GC */ |
428 | 316 } |
317 | |
318 #define Lstream_internal_error(reason, lstr) \ | |
563 | 319 signal_error (Qinternal_error, reason, wrap_lstream (lstr)) |
428 | 320 |
771 | 321 /* Reopen a closed stream. This enables I/O on it again. This is not |
322 meant to be called except from a wrapper routine that reinitializes | |
323 variables and such -- the close routine may well have freed some | |
324 necessary storage structures, for example. */ | |
325 | |
428 | 326 void |
327 Lstream_reopen (Lstream *lstr) | |
328 { | |
329 if (lstr->flags & LSTREAM_FL_IS_OPEN) | |
330 Lstream_internal_error ("lstream already open", lstr); | |
331 lstr->flags |= LSTREAM_FL_IS_OPEN; | |
332 } | |
333 | |
771 | 334 /* Try to write as much of DATA as possible to the stream. Return the |
335 number of bytes written. */ | |
428 | 336 |
771 | 337 static int |
338 Lstream_really_write (Lstream *lstr, const unsigned char *data, int size) | |
428 | 339 { |
665 | 340 Bytecount num_written; |
771 | 341 const unsigned char *orig_data = data; |
342 int error_occurred = 0; | |
428 | 343 |
771 | 344 while (size > 0) |
428 | 345 { |
346 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | |
347 Lstream_internal_error ("lstream not open", lstr); | |
348 if (! (lstr->flags & LSTREAM_FL_WRITE)) | |
349 Lstream_internal_error ("lstream not open for writing", lstr); | |
350 if (!lstr->imp->writer) | |
351 Lstream_internal_error ("lstream has no writer", lstr); | |
352 | |
353 if (lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | |
354 /* It's quite possible for us to get passed an incomplete | |
355 character at the end. We need to spit back that | |
356 incomplete character. */ | |
357 { | |
442 | 358 const unsigned char *dataend = data + size - 1; |
428 | 359 assert (size > 0); /* safety check ... */ |
360 /* Optimize the most common case. */ | |
826 | 361 if (!byte_ascii_p (*dataend)) |
428 | 362 { |
363 /* Go back to the beginning of the last (and possibly partial) | |
364 character, and bump forward to see if the character is | |
365 complete. */ | |
867 | 366 VALIDATE_IBYTEPTR_BACKWARD (dataend); |
826 | 367 if (dataend + rep_bytes_by_first_byte (*dataend) != data + size) |
428 | 368 /* If not, chop the size down to ignore the last char |
369 and stash it away for next time. */ | |
370 size = dataend - data; | |
371 /* If we don't even have one character to write, then just | |
372 skip out. */ | |
373 if (size == 0) | |
374 break; | |
375 } | |
376 } | |
377 | |
771 | 378 num_written = (lstr->imp->writer) (lstr, data, size); |
428 | 379 if (num_written == 0) |
380 /* If nothing got written, then just hold the data. This may | |
381 occur, for example, if this stream does non-blocking I/O; | |
382 the attempt to write the data might have resulted in an | |
383 EWOULDBLOCK error. */ | |
771 | 384 break; |
385 else if (num_written > size) | |
2500 | 386 ABORT (); |
428 | 387 else if (num_written > 0) |
388 { | |
771 | 389 data += num_written; |
390 size -= num_written; | |
428 | 391 } |
392 else | |
771 | 393 { |
394 /* If error, just hold the data, for similar reasons as above. */ | |
395 error_occurred = 1; | |
396 break; | |
397 } | |
428 | 398 } |
399 | |
2383 | 400 if (!error_occurred && lstr->imp->flusher) |
771 | 401 error_occurred = (lstr->imp->flusher) (lstr) < 0; |
402 | |
403 if (data == orig_data && error_occurred) | |
404 return -1; | |
405 | |
406 return data - orig_data; | |
407 } | |
408 | |
409 /* Attempt to flush out all of the buffered data for writing. Leaves | |
410 whatever wasn't flushed sitting in the stream's buffers. Return -1 if | |
411 nothing written and error occurred, 0 otherwise. */ | |
428 | 412 |
771 | 413 int |
414 Lstream_flush_out (Lstream *lstr) | |
415 { | |
416 Bytecount num_written = | |
417 Lstream_really_write (lstr, lstr->out_buffer, lstr->out_buffer_ind); | |
418 if (num_written == lstr->out_buffer_ind) | |
419 { | |
420 lstr->out_buffer_ind = 0; | |
421 return 0; | |
422 } | |
423 else if (num_written > 0) | |
424 { | |
425 memmove (lstr->out_buffer, lstr->out_buffer + num_written, | |
426 lstr->out_buffer_ind - num_written); | |
427 lstr->out_buffer_ind -= num_written; | |
428 return 0; | |
429 } | |
430 else return num_written; | |
428 | 431 } |
432 | |
771 | 433 /* Flush out any pending unwritten data in the stream. Clear any buffered |
434 input data. This differs from Lstream_flush_out() in that it also | |
435 clears any unflushable buffered data. Returns 0 on success, -1 on | |
436 error. */ | |
437 | |
428 | 438 int |
439 Lstream_flush (Lstream *lstr) | |
440 { | |
441 if (Lstream_flush_out (lstr) < 0) | |
442 return -1; | |
443 | |
444 /* clear out buffered data */ | |
445 lstr->in_buffer_current = lstr->in_buffer_ind = 0; | |
446 lstr->unget_buffer_ind = 0; | |
447 | |
448 return 0; | |
449 } | |
450 | |
451 /* We want to add NUM characters. This function ensures that the | |
452 buffer is large enough for this (per the buffering size specified | |
453 in the stream) and returns the number of characters we can | |
454 actually write. If FORCE is set, ignore the buffering size | |
455 and go ahead and make space for all the chars even if it exceeds | |
456 the buffering size. (This is used to deal with the possibility | |
457 that the stream writer might refuse to write any bytes now, e.g. | |
458 if it's getting EWOULDBLOCK errors. We have to keep stocking them | |
771 | 459 up until they can be written, so as to avoid losing data.) */ |
428 | 460 |
665 | 461 static Bytecount |
462 Lstream_adding (Lstream *lstr, Bytecount num, int force) | |
428 | 463 { |
665 | 464 Bytecount size = num + lstr->out_buffer_ind; |
430 | 465 |
466 if (size <= lstr->out_buffer_size) | |
467 return num; | |
468 | |
428 | 469 /* Maybe chop it down so that we don't buffer more characters |
470 than our advertised buffering size. */ | |
430 | 471 if ((size > lstr->buffering_size) && !force) |
472 { | |
473 size = lstr->buffering_size; | |
474 /* There might be more data buffered than the buffering size. */ | |
475 if (size <= lstr->out_buffer_ind) | |
476 return 0; | |
477 } | |
478 | |
479 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, size, unsigned char); | |
480 | |
481 return size - lstr->out_buffer_ind; | |
428 | 482 } |
483 | |
484 /* Like Lstream_write(), but does not handle line-buffering correctly. */ | |
485 | |
771 | 486 static int |
665 | 487 Lstream_write_1 (Lstream *lstr, const void *data, Bytecount size) |
428 | 488 { |
442 | 489 const unsigned char *p = (const unsigned char *) data; |
665 | 490 Bytecount off = 0; |
428 | 491 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
492 Lstream_internal_error ("lstream not open", lstr); | |
493 if (! (lstr->flags & LSTREAM_FL_WRITE)) | |
494 Lstream_internal_error ("lstream not open for writing", lstr); | |
771 | 495 |
496 if (lstr->buffering == LSTREAM_UNBUFFERED) | |
497 { | |
498 /* If there is buffered data, it means we ran into blocking | |
499 errors the previous time and had to buffer our remaining | |
500 data. Try to write it now. */ | |
501 if (lstr->out_buffer_ind > 0) | |
502 { | |
503 if (Lstream_flush_out (lstr) < 0) | |
504 return -1; | |
505 } | |
506 | |
507 /* If not still blocked, try to write the new data */ | |
508 if (lstr->out_buffer_ind == 0) | |
509 { | |
510 /* we don't need to loop because Lstream_really_write does that | |
511 for us. */ | |
512 Bytecount num_written = Lstream_really_write (lstr, p, size); | |
513 if (num_written < 0) | |
514 return -1; | |
515 off += num_written; | |
516 } | |
517 | |
518 /* squirrel away the rest of the data */ | |
519 if (off < size) | |
520 { | |
521 Lstream_adding (lstr, size - off, 1); | |
522 memcpy (lstr->out_buffer + lstr->out_buffer_ind, p + off, | |
523 size - off); | |
524 lstr->out_buffer_ind += size - off; | |
525 } | |
526 | |
527 lstr->byte_count += size; | |
528 return 0; | |
529 } | |
530 else | |
531 { | |
532 int couldnt_write_last_time = 0; | |
428 | 533 |
771 | 534 while (1) |
535 { | |
536 /* Figure out how much we can add to the buffer */ | |
537 Bytecount chunk = Lstream_adding (lstr, size, 0); | |
538 if (chunk == 0) | |
539 { | |
540 if (couldnt_write_last_time) | |
541 /* Ung, we ran out of space and tried to flush | |
542 the buffer, but it didn't work because the stream | |
543 writer is refusing to accept any data. So we | |
544 just have to squirrel away all the rest of the | |
545 stuff. */ | |
546 chunk = Lstream_adding (lstr, size, 1); | |
547 else | |
548 couldnt_write_last_time = 1; | |
549 } | |
550 /* Do it. */ | |
551 if (chunk > 0) | |
552 { | |
553 memcpy (lstr->out_buffer + lstr->out_buffer_ind, p + off, chunk); | |
554 lstr->out_buffer_ind += chunk; | |
555 lstr->byte_count += chunk; | |
556 size -= chunk; | |
557 off += chunk; | |
558 } | |
559 /* If the buffer is full and we have more to add, flush it out. */ | |
560 if (size > 0) | |
561 { | |
562 if (Lstream_flush_out (lstr) < 0) | |
563 { | |
564 if (off == 0) | |
565 return -1; | |
566 else | |
567 return 0; | |
568 } | |
569 } | |
570 else | |
571 break; | |
572 } | |
573 } | |
574 return 0; | |
428 | 575 } |
576 | |
771 | 577 /* Write SIZE bytes of DATA to the stream. Return value is 0 on success, |
578 -1 on error. -1 is only returned when no bytes could be written; if any | |
579 bytes could be written, then 0 is returned and any unwritten bytes are | |
580 buffered and the next call to Lstream_write() will try to write them | |
581 again. (This buffering happens even when the stream's buffering type is | |
582 LSTREAM_UNBUFFERED, and regardless of how much data is passed in or what | |
583 the stream's buffering size was set to. #### There should perhaps be a | |
584 way to control whether this happens.) */ | |
428 | 585 |
771 | 586 int |
665 | 587 Lstream_write (Lstream *lstr, const void *data, Bytecount size) |
428 | 588 { |
665 | 589 Bytecount i; |
442 | 590 const unsigned char *p = (const unsigned char *) data; |
428 | 591 |
771 | 592 /* If the stream is not line-buffered, then we can just call |
593 Lstream_write_1(), which writes in chunks. Otherwise, we repeatedly | |
594 call Lstream_putc(), which knows how to handle line buffering. | |
595 Returns 0 on success, -1 on failure. */ | |
596 | |
428 | 597 if (size == 0) |
771 | 598 return 0; |
428 | 599 if (lstr->buffering != LSTREAM_LINE_BUFFERED) |
600 return Lstream_write_1 (lstr, data, size); | |
601 for (i = 0; i < size; i++) | |
602 { | |
603 if (Lstream_putc (lstr, p[i]) < 0) | |
604 break; | |
605 } | |
771 | 606 return i == 0 ? -1 : 0; |
428 | 607 } |
608 | |
609 int | |
610 Lstream_was_blocked_p (Lstream *lstr) | |
611 { | |
612 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; | |
613 } | |
614 | |
665 | 615 static Bytecount |
462 | 616 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, |
665 | 617 Bytecount size) |
428 | 618 { |
619 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | |
620 Lstream_internal_error ("lstream not open", lstr); | |
621 if (! (lstr->flags & LSTREAM_FL_READ)) | |
622 Lstream_internal_error ("lstream not open for reading", lstr); | |
623 if (!lstr->imp->reader) | |
624 Lstream_internal_error ("lstream has no reader", lstr); | |
625 | |
626 return (lstr->imp->reader) (lstr, buffer, size); | |
627 } | |
628 | |
629 /* Assuming the buffer is empty, fill it up again. */ | |
630 | |
665 | 631 static Bytecount |
428 | 632 Lstream_read_more (Lstream *lstr) |
633 { | |
634 #if 0 | |
665 | 635 Bytecount size_needed |
462 | 636 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); |
428 | 637 #else |
638 /* If someone requested a larger buffer size, so be it! */ | |
665 | 639 Bytecount size_needed = |
462 | 640 max (1, lstr->buffering_size); |
428 | 641 #endif |
665 | 642 Bytecount size_gotten; |
428 | 643 |
644 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, | |
645 size_needed, unsigned char); | |
646 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); | |
647 lstr->in_buffer_current = max (0, size_gotten); | |
648 lstr->in_buffer_ind = 0; | |
649 return size_gotten < 0 ? -1 : size_gotten; | |
650 } | |
651 | |
771 | 652 /* Read SIZE bytes of DATA from the stream. Return the number of bytes |
653 read. 0 means EOF (#### sometimes; it may simply indicate we can't read | |
654 any data at other times, particularly if SIZE is too small. this needs | |
655 to be fixed!). -1 means an error occurred and no bytes were read. */ | |
656 | |
814 | 657 static Bytecount |
658 Lstream_read_1 (Lstream *lstr, void *data, Bytecount size, | |
659 int override_no_partial_chars) | |
428 | 660 { |
661 unsigned char *p = (unsigned char *) data; | |
665 | 662 Bytecount off = 0; |
663 Bytecount chunk; | |
428 | 664 int error_occurred = 0; |
665 | |
666 if (size == 0) | |
667 return 0; | |
668 | |
669 /* First try to get some data from the unget buffer */ | |
670 chunk = min (size, lstr->unget_buffer_ind); | |
671 if (chunk > 0) | |
672 { | |
673 /* The bytes come back in reverse order. */ | |
674 for (; off < chunk; off++) | |
675 p[off] = lstr->unget_buffer[--lstr->unget_buffer_ind]; | |
676 lstr->byte_count += chunk; | |
677 size -= chunk; | |
678 } | |
679 | |
680 while (size > 0) | |
681 { | |
771 | 682 /* If unbuffered, then simply read directly into output buffer. |
683 No need to copy. */ | |
684 if (lstr->buffering == LSTREAM_UNBUFFERED) | |
685 { | |
686 chunk = Lstream_raw_read (lstr, p + off, size); | |
687 if (chunk < 0) | |
688 error_occurred = 1; | |
689 if (chunk <= 0) | |
690 break; | |
691 lstr->byte_count += chunk; | |
428 | 692 size -= chunk; |
771 | 693 off += chunk; |
694 } | |
695 else | |
428 | 696 { |
771 | 697 /* Take whatever we can from the in buffer */ |
698 chunk = min (size, lstr->in_buffer_current - lstr->in_buffer_ind); | |
699 if (chunk > 0) | |
700 { | |
701 memcpy (p + off, lstr->in_buffer + lstr->in_buffer_ind, chunk); | |
702 lstr->in_buffer_ind += chunk; | |
703 lstr->byte_count += chunk; | |
704 size -= chunk; | |
705 off += chunk; | |
706 } | |
707 | |
708 /* If we need some more, try to get some more from the | |
709 stream's end */ | |
710 if (size > 0) | |
711 { | |
712 Bytecount retval = Lstream_read_more (lstr); | |
713 if (retval < 0) | |
714 error_occurred = 1; | |
715 if (retval <= 0) | |
716 break; | |
717 } | |
428 | 718 } |
719 } | |
720 | |
814 | 721 if ((lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS) && |
722 !override_no_partial_chars) | |
428 | 723 { |
724 /* It's quite possible for us to get passed an incomplete | |
725 character at the end. We need to spit back that | |
726 incomplete character. */ | |
867 | 727 Bytecount newoff = validate_ibyte_string_backward (p, off); |
771 | 728 if (newoff < off) |
428 | 729 { |
771 | 730 Lstream_unread (lstr, p + newoff, off - newoff); |
731 off = newoff; | |
428 | 732 } |
733 } | |
734 | |
462 | 735 return off == 0 && error_occurred ? -1 : off; |
428 | 736 } |
737 | |
814 | 738 Bytecount |
739 Lstream_read (Lstream *lstr, void *data, Bytecount size) | |
740 { | |
741 return Lstream_read_1 (lstr, data, size, 0); | |
742 } | |
743 | |
744 | |
771 | 745 /* Push back SIZE bytes of DATA onto the input queue. The next call |
746 to Lstream_read() with the same size will read the same bytes back. | |
747 Note that this will be the case even if there is other pending | |
748 unread data. */ | |
749 | |
428 | 750 void |
665 | 751 Lstream_unread (Lstream *lstr, const void *data, Bytecount size) |
428 | 752 { |
442 | 753 const unsigned char *p = (const unsigned char *) data; |
428 | 754 |
755 /* Make sure buffer is big enough */ | |
756 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, | |
757 lstr->unget_buffer_ind + size, unsigned char); | |
758 | |
759 lstr->byte_count -= size; | |
760 | |
761 /* Bytes have to go on in reverse order -- they are reversed | |
762 again when read back. */ | |
763 while (size--) | |
764 lstr->unget_buffer[lstr->unget_buffer_ind++] = p[size]; | |
765 } | |
766 | |
771 | 767 /* Rewind the stream to the beginning. */ |
768 | |
428 | 769 int |
770 Lstream_rewind (Lstream *lstr) | |
771 { | |
772 if (!lstr->imp->rewinder) | |
773 Lstream_internal_error ("lstream has no rewinder", lstr); | |
774 if (Lstream_flush (lstr) < 0) | |
775 return -1; | |
776 lstr->byte_count = 0; | |
777 return (lstr->imp->rewinder) (lstr); | |
778 } | |
779 | |
780 int | |
781 Lstream_seekable_p (Lstream *lstr) | |
782 { | |
783 if (!lstr->imp->rewinder) | |
784 return 0; | |
785 if (!lstr->imp->seekable_p) | |
786 return 1; | |
787 return (lstr->imp->seekable_p) (lstr); | |
788 } | |
789 | |
790 static int | |
791 Lstream_pseudo_close (Lstream *lstr) | |
792 { | |
1943 | 793 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
428 | 794 Lstream_internal_error ("lstream is not open", lstr); |
795 | |
796 /* don't check errors here -- best not to risk file descriptor loss */ | |
797 return Lstream_flush (lstr); | |
798 } | |
799 | |
771 | 800 /* Close the stream. All data will be flushed out. If the stream is |
801 already closed, nothing happens. Note that, even if all data has | |
802 already been flushed out, the act of closing a stream may generate more | |
803 data -- for example, if the stream implements some sort of conversion, | |
804 such as gzip, there may be special "end-data" that need to be written | |
805 out when the file is closed. */ | |
806 | |
428 | 807 int |
808 Lstream_close (Lstream *lstr) | |
809 { | |
810 int rc = 0; | |
811 | |
812 if (lstr->flags & LSTREAM_FL_IS_OPEN) | |
813 { | |
814 rc = Lstream_pseudo_close (lstr); | |
815 /* | |
816 * We used to return immediately if the closer method reported | |
817 * failure, leaving the stream open. But this is no good, for | |
818 * the following reasons. | |
819 * | |
820 * 1. The finalizer method used in GC makes no provision for | |
821 * failure, so we must not return without freeing buffer | |
822 * memory. | |
823 * | |
824 * 2. The closer method may have already freed some memory | |
825 * used for I/O in this stream. E.g. encoding_closer frees | |
826 * ENCODING_STREAM_DATA(stream)->runoff. If a writer method | |
827 * tries to use this buffer later, it will write into memory | |
828 * that may have been allocated elsewhere. Sometime later | |
829 * you will see a sign that says "Welcome to Crash City." | |
830 * | |
831 * 3. The closer can report failure if a flush fails in the | |
832 * other stream in a MULE encoding/decoding stream pair. | |
833 * The other stream in the pair is closed, but returning | |
834 * early leaves the current stream open. If we try to | |
835 * flush the current stream later, we will crash when the | |
836 * flusher notices that the other end stream is closed. | |
837 * | |
838 * So, we no longer abort the close if the closer method | |
839 * reports some kind of failure. We still report the failure | |
840 * to the caller. | |
841 */ | |
842 if (lstr->imp->closer) | |
843 if ((lstr->imp->closer) (lstr) < 0) | |
844 rc = -1; | |
845 } | |
846 | |
847 lstr->flags &= ~LSTREAM_FL_IS_OPEN; | |
848 lstr->byte_count = 0; | |
849 /* Note that Lstream_flush() reset all the buffer indices. That way, | |
850 the next call to Lstream_putc(), Lstream_getc(), or Lstream_ungetc() | |
851 on a closed stream will call into the function equivalents, which will | |
852 cause an error. */ | |
853 | |
854 /* We set the pointers to 0 so that we don't lose when this function | |
855 is called more than once on the same object */ | |
856 if (lstr->out_buffer) | |
857 { | |
1726 | 858 xfree (lstr->out_buffer, unsigned char *); |
428 | 859 lstr->out_buffer = 0; |
860 } | |
861 if (lstr->in_buffer) | |
862 { | |
1726 | 863 xfree (lstr->in_buffer, unsigned char *); |
428 | 864 lstr->in_buffer = 0; |
865 } | |
866 if (lstr->unget_buffer) | |
867 { | |
1726 | 868 xfree (lstr->unget_buffer, unsigned char *); |
428 | 869 lstr->unget_buffer = 0; |
870 } | |
871 | |
872 return rc; | |
873 } | |
874 | |
771 | 875 |
876 /* Function equivalent of Lstream_putc(). */ | |
877 | |
428 | 878 int |
879 Lstream_fputc (Lstream *lstr, int c) | |
880 { | |
881 unsigned char ch = (unsigned char) c; | |
771 | 882 int retval = Lstream_write_1 (lstr, &ch, 1); |
883 if (retval == 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') | |
428 | 884 return Lstream_flush_out (lstr); |
771 | 885 return retval; |
428 | 886 } |
887 | |
771 | 888 /* Function equivalent of Lstream_getc(). */ |
889 | |
428 | 890 int |
891 Lstream_fgetc (Lstream *lstr) | |
892 { | |
893 unsigned char ch; | |
814 | 894 if (Lstream_read_1 (lstr, &ch, 1, 1) <= 0) |
428 | 895 return -1; |
896 return ch; | |
897 } | |
898 | |
771 | 899 /* Function equivalent of Lstream_ungetc(). */ |
900 | |
428 | 901 void |
902 Lstream_fungetc (Lstream *lstr, int c) | |
903 { | |
904 unsigned char ch = (unsigned char) c; | |
905 Lstream_unread (lstr, &ch, 1); | |
906 } | |
907 | |
908 | |
909 /************************ some stream implementations *********************/ | |
910 | |
911 /*********** a stdio stream ***********/ | |
912 | |
913 struct stdio_stream | |
914 { | |
915 FILE *file; | |
916 int closing; | |
917 }; | |
918 | |
919 #define STDIO_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, stdio) | |
920 | |
771 | 921 DEFINE_LSTREAM_IMPLEMENTATION ("stdio", stdio); |
428 | 922 |
923 static Lisp_Object | |
442 | 924 make_stdio_stream_1 (FILE *stream, int flags, const char *mode) |
428 | 925 { |
926 Lstream *lstr = Lstream_new (lstream_stdio, mode); | |
927 struct stdio_stream *str = STDIO_STREAM_DATA (lstr); | |
928 str->file = stream; | |
929 str->closing = flags & LSTR_CLOSING; | |
930 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE; | |
793 | 931 return wrap_lstream (lstr); |
428 | 932 } |
933 | |
934 Lisp_Object | |
935 make_stdio_input_stream (FILE *stream, int flags) | |
936 { | |
937 return make_stdio_stream_1 (stream, flags, "r"); | |
938 } | |
939 | |
940 Lisp_Object | |
941 make_stdio_output_stream (FILE *stream, int flags) | |
942 { | |
943 return make_stdio_stream_1 (stream, flags, "w"); | |
944 } | |
945 | |
946 /* #### From reading the Unix 98 specification, it appears that if we | |
947 want stdio_reader() to be completely correct, we should check for | |
948 0 < val < size and if so, check to see if an error has occurred. | |
949 If an error has occurred, but val is non-zero, we should go ahead | |
950 and act as if the read was successful, but remember in some fashion | |
951 or other, that an error has occurred, and report that on the next | |
771 | 952 call to stdio_reader instead of calling retry_fread() again. |
428 | 953 |
771 | 954 Currently, in such a case, we end up calling retry_fread() twice and we |
428 | 955 assume that |
956 | |
957 1) this is not harmful, and | |
958 2) the error will still be reported on the second read. | |
959 | |
960 This is probably reasonable, so I don't think we should change this | |
961 code (it could even be argued that the error might have fixed | |
771 | 962 itself, so we should do the retry_fread() again. */ |
428 | 963 |
665 | 964 static Bytecount |
965 stdio_reader (Lstream *stream, unsigned char *data, Bytecount size) | |
428 | 966 { |
967 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
771 | 968 Bytecount val = retry_fread (data, 1, size, str->file); |
969 if (!val) | |
970 { | |
971 if (ferror (str->file)) | |
972 return LSTREAM_ERROR; | |
973 if (feof (str->file)) | |
974 return 0; /* LSTREAM_EOF; */ | |
975 } | |
428 | 976 return val; |
977 } | |
978 | |
665 | 979 static Bytecount |
462 | 980 stdio_writer (Lstream *stream, const unsigned char *data, |
665 | 981 Bytecount size) |
428 | 982 { |
983 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
771 | 984 Bytecount val = retry_fwrite (data, 1, size, str->file); |
428 | 985 if (!val && ferror (str->file)) |
771 | 986 return LSTREAM_ERROR; |
428 | 987 return val; |
988 } | |
989 | |
990 static int | |
991 stdio_rewinder (Lstream *stream) | |
992 { | |
993 rewind (STDIO_STREAM_DATA (stream)->file); | |
994 return 0; | |
995 } | |
996 | |
997 static int | |
998 stdio_seekable_p (Lstream *stream) | |
999 { | |
1000 struct stat lestat; | |
1001 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
1002 | |
771 | 1003 if (qxe_fstat (fileno (str->file), &lestat) < 0) |
428 | 1004 return 0; |
1005 return S_ISREG (lestat.st_mode); | |
1006 } | |
1007 | |
1008 static int | |
1009 stdio_flusher (Lstream *stream) | |
1010 { | |
1011 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
1012 if (stream->flags & LSTREAM_FL_WRITE) | |
1013 return fflush (str->file); | |
1014 else | |
1015 return 0; | |
1016 } | |
1017 | |
1018 static int | |
1019 stdio_closer (Lstream *stream) | |
1020 { | |
1021 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | |
1022 if (str->closing) | |
771 | 1023 return retry_fclose (str->file); |
428 | 1024 else |
1025 if (stream->flags & LSTREAM_FL_WRITE) | |
1026 return fflush (str->file); | |
1027 else | |
1028 return 0; | |
1029 } | |
1030 | |
1031 /*********** a file descriptor ***********/ | |
1032 | |
1033 struct filedesc_stream | |
1034 { | |
1035 int fd; | |
1036 int pty_max_bytes; | |
867 | 1037 Ibyte eof_char; |
428 | 1038 int starting_pos; |
1039 int current_pos; | |
1040 int end_pos; | |
1041 int chars_sans_newline; | |
1042 unsigned int closing :1; | |
1043 unsigned int allow_quit :1; | |
1044 unsigned int blocked_ok :1; | |
1045 unsigned int pty_flushing :1; | |
1046 unsigned int blocking_error_p :1; | |
1047 }; | |
1048 | |
1049 #define FILEDESC_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, filedesc) | |
1050 | |
771 | 1051 DEFINE_LSTREAM_IMPLEMENTATION ("filedesc", filedesc); |
428 | 1052 |
1053 /* Make a stream that reads from or writes to a file descriptor FILEDESC. | |
1054 OFFSET is the offset from the *current* file pointer that the reading | |
1055 should start at. COUNT is the number of bytes to be read (it is | |
1056 ignored when writing); -1 for unlimited. */ | |
1057 static Lisp_Object | |
1058 make_filedesc_stream_1 (int filedesc, int offset, int count, int flags, | |
442 | 1059 const char *mode) |
428 | 1060 { |
1061 Lstream *lstr = Lstream_new (lstream_filedesc, mode); | |
1062 struct filedesc_stream *fstr = FILEDESC_STREAM_DATA (lstr); | |
1063 fstr->fd = filedesc; | |
1064 fstr->closing = !!(flags & LSTR_CLOSING); | |
1065 fstr->allow_quit = !!(flags & LSTR_ALLOW_QUIT); | |
1066 fstr->blocked_ok = !!(flags & LSTR_BLOCKED_OK); | |
1067 fstr->pty_flushing = !!(flags & LSTR_PTY_FLUSHING); | |
1068 fstr->blocking_error_p = 0; | |
1069 fstr->chars_sans_newline = 0; | |
1070 fstr->starting_pos = lseek (filedesc, offset, SEEK_CUR); | |
1071 fstr->current_pos = max (fstr->starting_pos, 0); | |
1072 if (count < 0) | |
1073 fstr->end_pos = -1; | |
1074 else | |
1075 fstr->end_pos = fstr->starting_pos + count; | |
1076 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE; | |
793 | 1077 return wrap_lstream (lstr); |
428 | 1078 } |
1079 | |
814 | 1080 /* Flags: |
1081 | |
1082 LSTR_CLOSING | |
1083 If set, close the descriptor or FILE * when the stream is closed. | |
1084 | |
1085 LSTR_ALLOW_QUIT | |
1086 If set, allow quitting out of the actual I/O. | |
1087 | |
1088 LSTR_PTY_FLUSHING | |
1089 If set and filedesc_stream_set_pty_flushing() has been called | |
1090 on the stream, do not send more than pty_max_bytes on a single | |
1091 line without flushing the data out using the eof_char. | |
1092 | |
1093 LSTR_BLOCKED_OK | |
1094 If set, an EWOULDBLOCK error is not treated as an error but | |
1095 simply causes the write function to return 0 as the number | |
1096 of bytes written out. | |
1097 */ | |
1098 | |
428 | 1099 Lisp_Object |
1100 make_filedesc_input_stream (int filedesc, int offset, int count, int flags) | |
1101 { | |
1102 return make_filedesc_stream_1 (filedesc, offset, count, flags, "r"); | |
1103 } | |
1104 | |
1105 Lisp_Object | |
1106 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) | |
1107 { | |
1108 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); | |
1109 } | |
1110 | |
665 | 1111 static Bytecount |
1112 filedesc_reader (Lstream *stream, unsigned char *data, Bytecount size) | |
428 | 1113 { |
665 | 1114 Bytecount nread; |
428 | 1115 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
1116 if (str->end_pos >= 0) | |
665 | 1117 size = min (size, (Bytecount) (str->end_pos - str->current_pos)); |
430 | 1118 nread = str->allow_quit ? |
1119 read_allowing_quit (str->fd, data, size) : | |
771 | 1120 retry_read (str->fd, data, size); |
428 | 1121 if (nread > 0) |
1122 str->current_pos += nread; | |
771 | 1123 if (nread == 0) |
1124 return 0; /* LSTREAM_EOF; */ | |
1125 if (nread < 0) | |
1126 return LSTREAM_ERROR; | |
428 | 1127 return nread; |
1128 } | |
1129 | |
1130 static int | |
1131 errno_would_block_p (int val) | |
1132 { | |
1133 #ifdef EWOULDBLOCK | |
1134 if (val == EWOULDBLOCK) | |
1135 return 1; | |
1136 #endif | |
1137 #ifdef EAGAIN | |
1138 if (val == EAGAIN) | |
1139 return 1; | |
1140 #endif | |
1141 return 0; | |
1142 } | |
1143 | |
665 | 1144 static Bytecount |
462 | 1145 filedesc_writer (Lstream *stream, const unsigned char *data, |
665 | 1146 Bytecount size) |
428 | 1147 { |
1148 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
665 | 1149 Bytecount retval; |
428 | 1150 int need_newline = 0; |
1151 | |
1152 /* This function would be simple if it were not for the blasted | |
1153 PTY max-bytes stuff. Why the hell can't they just have written | |
1154 the PTY drivers right so this problem doesn't exist? | |
1155 | |
1156 Maybe all the PTY crap here should be moved into another stream | |
1157 that does nothing but periodically insert EOF's as necessary. */ | |
1158 if (str->pty_flushing) | |
1159 { | |
1160 /* To make life easy, only send out one line at the most. */ | |
442 | 1161 const unsigned char *ptr; |
428 | 1162 |
442 | 1163 ptr = (const unsigned char *) memchr (data, '\n', size); |
428 | 1164 if (ptr) |
1165 need_newline = 1; | |
1166 else | |
1167 ptr = data + size; | |
1168 if (ptr - data >= str->pty_max_bytes - str->chars_sans_newline) | |
1169 { | |
1170 ptr = data + str->pty_max_bytes - str->chars_sans_newline; | |
1171 need_newline = 0; | |
1172 } | |
1173 size = ptr - data; | |
1174 } | |
1175 | |
1176 /**** start of non-PTY-crap ****/ | |
1177 if (size > 0) | |
430 | 1178 retval = str->allow_quit ? |
1179 write_allowing_quit (str->fd, data, size) : | |
771 | 1180 retry_write (str->fd, data, size); |
428 | 1181 else |
1182 retval = 0; | |
1183 if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok) | |
1184 { | |
1185 str->blocking_error_p = 1; | |
1186 return 0; | |
1187 } | |
1188 str->blocking_error_p = 0; | |
1189 if (retval < 0) | |
771 | 1190 return LSTREAM_ERROR; |
428 | 1191 /**** end non-PTY-crap ****/ |
1192 | |
1193 if (str->pty_flushing) | |
1194 { | |
1195 str->chars_sans_newline += retval; | |
1196 /* Note that a newline was not among the bytes written out. | |
1197 Add to the number of non-newline bytes written out, | |
1198 and flush with an EOF if necessary. Be careful to | |
1199 keep track of write errors as we go along and look | |
1200 out for EWOULDBLOCK. */ | |
1201 if (str->chars_sans_newline >= str->pty_max_bytes) | |
1202 { | |
665 | 1203 Bytecount retval2 = str->allow_quit ? |
430 | 1204 write_allowing_quit (str->fd, &str->eof_char, 1) : |
771 | 1205 retry_write (str->fd, &str->eof_char, 1); |
430 | 1206 |
428 | 1207 if (retval2 > 0) |
1208 str->chars_sans_newline = 0; | |
1209 else if (retval2 < 0) | |
1210 { | |
1211 /* Error writing the EOF char. If nothing got written, | |
1212 then treat this as an error -- either return an error | |
1213 condition or set the blocking-error flag. */ | |
1214 if (retval == 0) | |
1215 { | |
1216 if (errno_would_block_p (errno) && str->blocked_ok) | |
1217 { | |
1218 str->blocking_error_p = 1; | |
1219 return 0; | |
1220 } | |
1221 else | |
771 | 1222 return LSTREAM_ERROR; |
428 | 1223 } |
1224 else | |
1225 return retval; | |
1226 } | |
1227 } | |
1228 } | |
1229 | |
1230 /* The need_newline flag is necessary because otherwise when the | |
1231 first byte is a newline, we'd get stuck never writing anything | |
1232 in pty-flushing mode. */ | |
1233 if (need_newline) | |
1234 { | |
867 | 1235 Ibyte nl = '\n'; |
665 | 1236 Bytecount retval2 = str->allow_quit ? |
430 | 1237 write_allowing_quit (str->fd, &nl, 1) : |
771 | 1238 retry_write (str->fd, &nl, 1); |
430 | 1239 |
428 | 1240 if (retval2 > 0) |
1241 { | |
1242 str->chars_sans_newline = 0; | |
1243 retval++; | |
1244 } | |
1245 else if (retval2 < 0) | |
1246 { | |
1247 /* Error writing the newline char. If nothing got written, | |
1248 then treat this as an error -- either return an error | |
1249 condition or set the blocking-error flag. */ | |
1250 if (retval == 0) | |
1251 { | |
1252 if (errno_would_block_p (errno) && str->blocked_ok) | |
1253 { | |
1254 str->blocking_error_p = 1; | |
1255 return 0; | |
1256 } | |
1257 else | |
771 | 1258 return LSTREAM_ERROR; |
428 | 1259 } |
1260 else | |
1261 return retval; | |
1262 } | |
1263 } | |
1264 | |
1265 return retval; | |
1266 } | |
1267 | |
1268 static int | |
1269 filedesc_rewinder (Lstream *stream) | |
1270 { | |
1271 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1272 if (str->starting_pos < 0 || | |
1273 lseek (FILEDESC_STREAM_DATA (stream)->fd, str->starting_pos, | |
1274 SEEK_SET) == -1) | |
1275 return -1; | |
1276 else | |
1277 { | |
1278 str->current_pos = str->starting_pos; | |
1279 return 0; | |
1280 } | |
1281 } | |
1282 | |
1283 static int | |
1284 filedesc_seekable_p (Lstream *stream) | |
1285 { | |
1286 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1287 if (str->starting_pos < 0) | |
1288 return 0; | |
1289 else | |
1290 { | |
1291 struct stat lestat; | |
1292 | |
771 | 1293 if (qxe_fstat (str->fd, &lestat) < 0) |
428 | 1294 return 0; |
1295 return S_ISREG (lestat.st_mode); | |
1296 } | |
1297 } | |
1298 | |
1299 static int | |
1300 filedesc_closer (Lstream *stream) | |
1301 { | |
1302 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1303 if (str->closing) | |
771 | 1304 return retry_close (str->fd); |
428 | 1305 else |
1306 return 0; | |
1307 } | |
1308 | |
1309 static int | |
1310 filedesc_was_blocked_p (Lstream *stream) | |
1311 { | |
1312 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1313 return str->blocking_error_p; | |
1314 } | |
1315 | |
1316 void | |
1317 filedesc_stream_set_pty_flushing (Lstream *stream, int pty_max_bytes, | |
867 | 1318 Ibyte eof_char) |
428 | 1319 { |
1320 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1321 str->pty_max_bytes = pty_max_bytes; | |
1322 str->eof_char = eof_char; | |
1323 str->pty_flushing = 1; | |
1324 } | |
1325 | |
1326 int | |
1327 filedesc_stream_fd (Lstream *stream) | |
1328 { | |
1329 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | |
1330 return str->fd; | |
1331 } | |
1332 | |
1333 /*********** read from a Lisp string ***********/ | |
1334 | |
1335 #define LISP_STRING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, lisp_string) | |
1336 | |
1337 struct lisp_string_stream | |
1338 { | |
1339 Lisp_Object obj; | |
1340 Bytecount init_offset; | |
1341 Bytecount offset, end; | |
1342 }; | |
1343 | |
1204 | 1344 static const struct memory_description lisp_string_lstream_description[] = { |
1345 { XD_LISP_OBJECT, offsetof (struct lisp_string_stream, obj) }, | |
1346 { XD_END } | |
1347 }; | |
1348 | |
1349 DEFINE_LSTREAM_IMPLEMENTATION_WITH_DATA ("lisp-string", lisp_string); | |
428 | 1350 |
1351 Lisp_Object | |
1352 make_lisp_string_input_stream (Lisp_Object string, Bytecount offset, | |
1353 Bytecount len) | |
1354 { | |
1355 Lstream *lstr; | |
1356 struct lisp_string_stream *str; | |
1357 | |
1358 CHECK_STRING (string); | |
1359 if (len < 0) | |
1360 len = XSTRING_LENGTH (string) - offset; | |
1361 assert (offset >= 0); | |
1362 assert (len >= 0); | |
1363 assert (offset + len <= XSTRING_LENGTH (string)); | |
1364 | |
1365 lstr = Lstream_new (lstream_lisp_string, "r"); | |
1366 str = LISP_STRING_STREAM_DATA (lstr); | |
1367 str->offset = offset; | |
1368 str->end = offset + len; | |
1369 str->init_offset = offset; | |
1370 str->obj = string; | |
793 | 1371 return wrap_lstream (lstr); |
428 | 1372 } |
1373 | |
665 | 1374 static Bytecount |
462 | 1375 lisp_string_reader (Lstream *stream, unsigned char *data, |
665 | 1376 Bytecount size) |
428 | 1377 { |
1378 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | |
1379 /* Don't lose if the string shrank past us ... */ | |
1380 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); | |
867 | 1381 Ibyte *strstart = XSTRING_DATA (str->obj); |
1382 Ibyte *start = strstart + offset; | |
428 | 1383 |
1384 /* ... or if someone changed the string and we ended up in the | |
1385 middle of a character. */ | |
1386 /* Being in the middle of a character is `normal' unless | |
1387 LSTREAM_NO_PARTIAL_CHARS - mrb */ | |
1388 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | |
867 | 1389 VALIDATE_IBYTEPTR_BACKWARD (start); |
428 | 1390 offset = start - strstart; |
665 | 1391 size = min (size, (Bytecount) (str->end - offset)); |
428 | 1392 memcpy (data, start, size); |
1393 str->offset = offset + size; | |
1394 return size; | |
1395 } | |
1396 | |
1397 static int | |
1398 lisp_string_rewinder (Lstream *stream) | |
1399 { | |
1400 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | |
1401 int pos = str->init_offset; | |
1402 if (pos > str->end) | |
1403 pos = str->end; | |
1404 /* Don't lose if the string shrank past us ... */ | |
1405 pos = min (pos, XSTRING_LENGTH (str->obj)); | |
1406 /* ... or if someone changed the string and we ended up in the | |
1407 middle of a character. */ | |
1408 { | |
867 | 1409 Ibyte *strstart = XSTRING_DATA (str->obj); |
1410 Ibyte *start = strstart + pos; | |
1411 VALIDATE_IBYTEPTR_BACKWARD (start); | |
428 | 1412 pos = start - strstart; |
1413 } | |
1414 str->offset = pos; | |
1415 return 0; | |
1416 } | |
1417 | |
1418 static Lisp_Object | |
1419 lisp_string_marker (Lisp_Object stream) | |
1420 { | |
1421 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (XLSTREAM (stream)); | |
1422 return str->obj; | |
1423 } | |
1424 | |
1425 /*********** a fixed buffer ***********/ | |
1426 | |
1427 #define FIXED_BUFFER_STREAM_DATA(stream) \ | |
1428 LSTREAM_TYPE_DATA (stream, fixed_buffer) | |
1429 | |
1430 struct fixed_buffer_stream | |
1431 { | |
442 | 1432 const unsigned char *inbuf; |
428 | 1433 unsigned char *outbuf; |
665 | 1434 Bytecount size; |
1435 Bytecount offset; | |
428 | 1436 }; |
1437 | |
771 | 1438 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", fixed_buffer); |
428 | 1439 |
1440 Lisp_Object | |
665 | 1441 make_fixed_buffer_input_stream (const void *buf, Bytecount size) |
428 | 1442 { |
1443 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); | |
1444 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | |
440 | 1445 str->inbuf = (const unsigned char *) buf; |
428 | 1446 str->size = size; |
793 | 1447 return wrap_lstream (lstr); |
428 | 1448 } |
1449 | |
1450 Lisp_Object | |
665 | 1451 make_fixed_buffer_output_stream (void *buf, Bytecount size) |
428 | 1452 { |
1453 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); | |
1454 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | |
440 | 1455 str->outbuf = (unsigned char *) buf; |
428 | 1456 str->size = size; |
793 | 1457 return wrap_lstream (lstr); |
428 | 1458 } |
1459 | |
665 | 1460 static Bytecount |
462 | 1461 fixed_buffer_reader (Lstream *stream, unsigned char *data, |
665 | 1462 Bytecount size) |
428 | 1463 { |
1464 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | |
1465 size = min (size, str->size - str->offset); | |
1466 memcpy (data, str->inbuf + str->offset, size); | |
1467 str->offset += size; | |
1468 return size; | |
1469 } | |
1470 | |
665 | 1471 static Bytecount |
462 | 1472 fixed_buffer_writer (Lstream *stream, const unsigned char *data, |
665 | 1473 Bytecount size) |
428 | 1474 { |
1475 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | |
1476 if (str->offset == str->size) | |
1477 { | |
1478 /* If we're at the end, just throw away the data and pretend | |
1479 we wrote all of it. If we return 0, then the lstream routines | |
1480 will try again and again to write it out. */ | |
1481 return size; | |
1482 } | |
1483 size = min (size, str->size - str->offset); | |
1484 memcpy (str->outbuf + str->offset, data, size); | |
1485 str->offset += size; | |
1486 return size; | |
1487 } | |
1488 | |
1489 static int | |
1490 fixed_buffer_rewinder (Lstream *stream) | |
1491 { | |
1492 FIXED_BUFFER_STREAM_DATA (stream)->offset = 0; | |
1493 return 0; | |
1494 } | |
1495 | |
442 | 1496 const unsigned char * |
428 | 1497 fixed_buffer_input_stream_ptr (Lstream *stream) |
1498 { | |
1499 assert (stream->imp == lstream_fixed_buffer); | |
1500 return FIXED_BUFFER_STREAM_DATA (stream)->inbuf; | |
1501 } | |
1502 | |
1503 unsigned char * | |
1504 fixed_buffer_output_stream_ptr (Lstream *stream) | |
1505 { | |
1506 assert (stream->imp == lstream_fixed_buffer); | |
1507 return FIXED_BUFFER_STREAM_DATA (stream)->outbuf; | |
1508 } | |
1509 | |
1510 /*********** write to a resizing buffer ***********/ | |
1511 | |
1512 #define RESIZING_BUFFER_STREAM_DATA(stream) \ | |
1513 LSTREAM_TYPE_DATA (stream, resizing_buffer) | |
1514 | |
1515 struct resizing_buffer_stream | |
1516 { | |
1517 unsigned char *buf; | |
665 | 1518 Bytecount allocked; |
428 | 1519 int max_stored; |
1520 int stored; | |
1521 }; | |
1522 | |
771 | 1523 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", resizing_buffer); |
428 | 1524 |
1525 Lisp_Object | |
1526 make_resizing_buffer_output_stream (void) | |
1527 { | |
793 | 1528 return wrap_lstream (Lstream_new (lstream_resizing_buffer, "w")); |
428 | 1529 } |
1530 | |
665 | 1531 static Bytecount |
462 | 1532 resizing_buffer_writer (Lstream *stream, const unsigned char *data, |
665 | 1533 Bytecount size) |
428 | 1534 { |
1535 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | |
1536 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); | |
1537 memcpy (str->buf + str->stored, data, size); | |
1538 str->stored += size; | |
1539 str->max_stored = max (str->max_stored, str->stored); | |
1540 return size; | |
1541 } | |
1542 | |
1543 static int | |
1544 resizing_buffer_rewinder (Lstream *stream) | |
1545 { | |
1546 RESIZING_BUFFER_STREAM_DATA (stream)->stored = 0; | |
1547 return 0; | |
1548 } | |
1549 | |
1550 static int | |
1551 resizing_buffer_closer (Lstream *stream) | |
1552 { | |
1553 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | |
1554 if (str->buf) | |
1555 { | |
1726 | 1556 xfree (str->buf, unsigned char *); |
428 | 1557 str->buf = 0; |
1558 } | |
1559 return 0; | |
1560 } | |
1561 | |
1562 unsigned char * | |
1563 resizing_buffer_stream_ptr (Lstream *stream) | |
1564 { | |
1565 return RESIZING_BUFFER_STREAM_DATA (stream)->buf; | |
1566 } | |
1567 | |
788 | 1568 Lisp_Object |
1569 resizing_buffer_to_lisp_string (Lstream *stream) | |
1570 { | |
1571 return make_string (resizing_buffer_stream_ptr (stream), | |
1572 Lstream_byte_count (stream)); | |
1573 } | |
1574 | |
428 | 1575 /*********** write to an unsigned-char dynarr ***********/ |
1576 | |
1577 /* Note: If you have a dynarr whose type is not unsigned_char_dynarr | |
1578 but which is really just an unsigned_char_dynarr (e.g. its type | |
867 | 1579 is Ibyte or Extbyte), just cast to unsigned_char_dynarr. */ |
428 | 1580 |
1581 #define DYNARR_STREAM_DATA(stream) \ | |
1582 LSTREAM_TYPE_DATA (stream, dynarr) | |
1583 | |
1584 struct dynarr_stream | |
1585 { | |
1586 unsigned_char_dynarr *dyn; | |
1587 }; | |
1588 | |
771 | 1589 DEFINE_LSTREAM_IMPLEMENTATION ("dynarr", dynarr); |
428 | 1590 |
1591 Lisp_Object | |
1592 make_dynarr_output_stream (unsigned_char_dynarr *dyn) | |
1593 { | |
793 | 1594 Lisp_Object obj = wrap_lstream (Lstream_new (lstream_dynarr, "w")); |
1595 | |
428 | 1596 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; |
1597 return obj; | |
1598 } | |
1599 | |
665 | 1600 static Bytecount |
462 | 1601 dynarr_writer (Lstream *stream, const unsigned char *data, |
665 | 1602 Bytecount size) |
428 | 1603 { |
1604 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); | |
1605 Dynarr_add_many (str->dyn, data, size); | |
1606 return size; | |
1607 } | |
1608 | |
1609 static int | |
1610 dynarr_rewinder (Lstream *stream) | |
1611 { | |
1612 Dynarr_reset (DYNARR_STREAM_DATA (stream)->dyn); | |
1613 return 0; | |
1614 } | |
1615 | |
1616 static int | |
2286 | 1617 dynarr_closer (Lstream *UNUSED (stream)) |
428 | 1618 { |
1619 return 0; | |
1620 } | |
1621 | |
1622 /************ read from or write to a Lisp buffer ************/ | |
1623 | |
1624 /* Note: Lisp-buffer read streams never return partial characters, | |
1625 and Lisp-buffer write streams expect to never get partial | |
1626 characters. */ | |
1627 | |
1628 #define LISP_BUFFER_STREAM_DATA(stream) \ | |
1629 LSTREAM_TYPE_DATA (stream, lisp_buffer) | |
1630 | |
1631 struct lisp_buffer_stream | |
1632 { | |
1633 Lisp_Object buffer; | |
1634 Lisp_Object orig_start; | |
1635 /* we use markers to properly deal with insertion/deletion */ | |
1636 Lisp_Object start, end; | |
1637 int flags; | |
1638 }; | |
1639 | |
1204 | 1640 static const struct memory_description lisp_buffer_lstream_description[] = { |
1641 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, buffer) }, | |
1642 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, orig_start) }, | |
1643 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, start) }, | |
1644 { XD_LISP_OBJECT, offsetof (struct lisp_buffer_stream, end) }, | |
1645 { XD_END } | |
1646 }; | |
1647 | |
1648 DEFINE_LSTREAM_IMPLEMENTATION_WITH_DATA ("lisp-buffer", lisp_buffer); | |
428 | 1649 |
1650 static Lisp_Object | |
665 | 1651 make_lisp_buffer_stream_1 (struct buffer *buf, Charbpos start, Charbpos end, |
2367 | 1652 int flags, const Ascbyte *mode) |
428 | 1653 { |
1654 Lstream *lstr; | |
1655 struct lisp_buffer_stream *str; | |
665 | 1656 Charbpos bmin, bmax; |
428 | 1657 int reading = !strcmp (mode, "r"); |
1658 | |
1659 /* Make sure the luser didn't pass "w" in. */ | |
1660 if (!strcmp (mode, "w")) | |
2500 | 1661 ABORT (); |
428 | 1662 |
1663 if (flags & LSTR_IGNORE_ACCESSIBLE) | |
1664 { | |
1665 bmin = BUF_BEG (buf); | |
1666 bmax = BUF_Z (buf); | |
1667 } | |
1668 else | |
1669 { | |
1670 bmin = BUF_BEGV (buf); | |
1671 bmax = BUF_ZV (buf); | |
1672 } | |
1673 | |
1674 if (start == -1) | |
1675 start = bmin; | |
1676 if (end == -1) | |
1677 end = bmax; | |
1678 assert (bmin <= start); | |
1679 assert (start <= bmax); | |
1680 if (reading) | |
1681 { | |
1682 assert (bmin <= end); | |
1683 assert (end <= bmax); | |
1684 assert (start <= end); | |
1685 } | |
1686 | |
1687 lstr = Lstream_new (lstream_lisp_buffer, mode); | |
1688 str = LISP_BUFFER_STREAM_DATA (lstr); | |
1689 { | |
1690 Lisp_Object marker; | |
793 | 1691 Lisp_Object buffer = wrap_buffer (buf); |
428 | 1692 |
1693 marker = Fmake_marker (); | |
1694 Fset_marker (marker, make_int (start), buffer); | |
1695 str->start = marker; | |
1696 marker = Fmake_marker (); | |
1697 Fset_marker (marker, make_int (start), buffer); | |
1698 str->orig_start = marker; | |
1699 if (reading) | |
1700 { | |
1701 marker = Fmake_marker (); | |
1702 Fset_marker (marker, make_int (end), buffer); | |
1703 str->end = marker; | |
1704 } | |
1705 else | |
1706 str->end = Qnil; | |
1707 str->buffer = buffer; | |
1708 } | |
1709 str->flags = flags; | |
793 | 1710 return wrap_lstream (lstr); |
428 | 1711 } |
1712 | |
1713 Lisp_Object | |
826 | 1714 make_lisp_buffer_input_stream (struct buffer *buf, Charbpos start, |
1715 Charbpos end, int flags) | |
428 | 1716 { |
1717 return make_lisp_buffer_stream_1 (buf, start, end, flags, "r"); | |
1718 } | |
1719 | |
1720 Lisp_Object | |
665 | 1721 make_lisp_buffer_output_stream (struct buffer *buf, Charbpos pos, int flags) |
428 | 1722 { |
1723 Lisp_Object lstr = make_lisp_buffer_stream_1 (buf, pos, 0, flags, "wc"); | |
1724 | |
1725 Lstream_set_character_mode (XLSTREAM (lstr)); | |
1726 return lstr; | |
1727 } | |
1728 | |
665 | 1729 static Bytecount |
867 | 1730 lisp_buffer_reader (Lstream *stream, Ibyte *data, Bytecount size) |
428 | 1731 { |
1732 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | |
665 | 1733 Bytebpos start; |
1734 Bytebpos end; | |
428 | 1735 struct buffer *buf = XBUFFER (str->buffer); |
826 | 1736 Bytecount src_used; |
428 | 1737 |
1738 if (!BUFFER_LIVE_P (buf)) | |
1739 return 0; /* Fut. */ | |
1740 | |
826 | 1741 start = byte_marker_position (str->start); |
1742 end = byte_marker_position (str->end); | |
428 | 1743 if (!(str->flags & LSTR_IGNORE_ACCESSIBLE)) |
1744 { | |
826 | 1745 start = bytebpos_clip_to_bounds (BYTE_BUF_BEGV (buf), start, |
1746 BYTE_BUF_ZV (buf)); | |
1747 end = bytebpos_clip_to_bounds (BYTE_BUF_BEGV (buf), end, | |
1748 BYTE_BUF_ZV (buf)); | |
428 | 1749 } |
1750 | |
826 | 1751 size = copy_buffer_text_out (buf, start, end - start, data, size, |
1752 FORMAT_DEFAULT, Qnil, &src_used); | |
1753 end = start + src_used; | |
428 | 1754 |
1755 if (EQ (buf->selective_display, Qt) && str->flags & LSTR_SELECTIVE) | |
1756 { | |
1757 /* What a kludge. What a kludge. What a kludge. */ | |
867 | 1758 Ibyte *p; |
840 | 1759 for (p = data; p < data + src_used; p++) |
428 | 1760 if (*p == '\r') |
1761 *p = '\n'; | |
1762 } | |
1763 | |
826 | 1764 set_byte_marker_position (str->start, end); |
1765 return size; | |
428 | 1766 } |
1767 | |
665 | 1768 static Bytecount |
867 | 1769 lisp_buffer_writer (Lstream *stream, const Ibyte *data, |
665 | 1770 Bytecount size) |
428 | 1771 { |
1772 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | |
665 | 1773 Charbpos pos; |
428 | 1774 struct buffer *buf = XBUFFER (str->buffer); |
1775 | |
1776 if (!BUFFER_LIVE_P (buf)) | |
1777 return 0; /* Fut. */ | |
1778 | |
1779 pos = marker_position (str->start); | |
1780 pos += buffer_insert_raw_string_1 (buf, pos, data, size, 0); | |
1781 set_marker_position (str->start, pos); | |
1782 return size; | |
1783 } | |
1784 | |
1785 static int | |
1786 lisp_buffer_rewinder (Lstream *stream) | |
1787 { | |
1788 struct lisp_buffer_stream *str = | |
1789 LISP_BUFFER_STREAM_DATA (stream); | |
1790 struct buffer *buf = XBUFFER (str->buffer); | |
1791 long pos = marker_position (str->orig_start); | |
1792 if (!BUFFER_LIVE_P (buf)) | |
1793 return -1; /* Fut. */ | |
1794 if (pos > BUF_ZV (buf)) | |
1795 pos = BUF_ZV (buf); | |
1796 if (pos < marker_position (str->orig_start)) | |
1797 pos = marker_position (str->orig_start); | |
1798 if (MARKERP (str->end) && pos > marker_position (str->end)) | |
1799 pos = marker_position (str->end); | |
1800 set_marker_position (str->start, pos); | |
1801 return 0; | |
1802 } | |
1803 | |
1804 static Lisp_Object | |
1805 lisp_buffer_marker (Lisp_Object stream) | |
1806 { | |
1807 struct lisp_buffer_stream *str = | |
1808 LISP_BUFFER_STREAM_DATA (XLSTREAM (stream)); | |
1809 | |
1204 | 1810 mark_object (str->orig_start); |
428 | 1811 mark_object (str->start); |
1812 mark_object (str->end); | |
1813 return str->buffer; | |
1814 } | |
1815 | |
665 | 1816 Charbpos |
428 | 1817 lisp_buffer_stream_startpos (Lstream *stream) |
1818 { | |
1819 return marker_position (LISP_BUFFER_STREAM_DATA (stream)->start); | |
1820 } | |
1821 | |
1822 | |
1823 /************************************************************************/ | |
1824 /* initialization */ | |
1825 /************************************************************************/ | |
1826 | |
1827 void | |
1828 lstream_type_create (void) | |
1829 { | |
1830 LSTREAM_HAS_METHOD (stdio, reader); | |
1831 LSTREAM_HAS_METHOD (stdio, writer); | |
1832 LSTREAM_HAS_METHOD (stdio, rewinder); | |
1833 LSTREAM_HAS_METHOD (stdio, seekable_p); | |
1834 LSTREAM_HAS_METHOD (stdio, flusher); | |
1835 LSTREAM_HAS_METHOD (stdio, closer); | |
1836 | |
1837 LSTREAM_HAS_METHOD (filedesc, reader); | |
1838 LSTREAM_HAS_METHOD (filedesc, writer); | |
1839 LSTREAM_HAS_METHOD (filedesc, was_blocked_p); | |
1840 LSTREAM_HAS_METHOD (filedesc, rewinder); | |
1841 LSTREAM_HAS_METHOD (filedesc, seekable_p); | |
1842 LSTREAM_HAS_METHOD (filedesc, closer); | |
1843 | |
1844 LSTREAM_HAS_METHOD (lisp_string, reader); | |
1845 LSTREAM_HAS_METHOD (lisp_string, rewinder); | |
1846 LSTREAM_HAS_METHOD (lisp_string, marker); | |
1847 | |
1848 LSTREAM_HAS_METHOD (fixed_buffer, reader); | |
1849 LSTREAM_HAS_METHOD (fixed_buffer, writer); | |
1850 LSTREAM_HAS_METHOD (fixed_buffer, rewinder); | |
1851 | |
1852 LSTREAM_HAS_METHOD (resizing_buffer, writer); | |
1853 LSTREAM_HAS_METHOD (resizing_buffer, rewinder); | |
1854 LSTREAM_HAS_METHOD (resizing_buffer, closer); | |
1855 | |
1856 LSTREAM_HAS_METHOD (dynarr, writer); | |
1857 LSTREAM_HAS_METHOD (dynarr, rewinder); | |
1858 LSTREAM_HAS_METHOD (dynarr, closer); | |
1859 | |
1860 LSTREAM_HAS_METHOD (lisp_buffer, reader); | |
1861 LSTREAM_HAS_METHOD (lisp_buffer, writer); | |
1862 LSTREAM_HAS_METHOD (lisp_buffer, rewinder); | |
1863 LSTREAM_HAS_METHOD (lisp_buffer, marker); | |
1864 } | |
1865 | |
3263 | 1866 #ifndef NEW_GC |
428 | 1867 void |
1868 reinit_vars_of_lstream (void) | |
1869 { | |
1870 int i; | |
1871 | |
1872 for (i = 0; i < countof (Vlstream_free_list); i++) | |
1873 { | |
1874 Vlstream_free_list[i] = Qnil; | |
1875 staticpro_nodump (&Vlstream_free_list[i]); | |
1876 } | |
1877 } | |
3263 | 1878 #endif /* not NEW_GC */ |
428 | 1879 |
1880 void | |
1881 vars_of_lstream (void) | |
1882 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3024
diff
changeset
|
1883 INIT_LISP_OBJECT (lstream); |
428 | 1884 } |