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