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