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