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