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