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