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