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