comparison src/lstream.c @ 272:c5d627a313b1 r21-0b34

Import from CVS: tag r21-0b34
author cvs
date Mon, 13 Aug 2007 10:28:48 +0200
parents 727739f917cb
children ca9a9ec9c1c1
comparison
equal deleted inserted replaced
271:c7b7086b0a39 272:c5d627a313b1
24 24
25 /* Written by Ben Wing. */ 25 /* Written by Ben Wing. */
26 26
27 #include <config.h> 27 #include <config.h>
28 #include "lisp.h" 28 #include "lisp.h"
29 #include <limits.h>
29 30
30 #include "buffer.h" 31 #include "buffer.h"
31 #include "insdel.h" 32 #include "insdel.h"
32 #include "lstream.h" 33 #include "lstream.h"
33 34
92 int Lstream_fputc (Lstream *stream, int c) 93 int Lstream_fputc (Lstream *stream, int c)
93 int Lstream_fgetc (Lstream *stream) 94 int Lstream_fgetc (Lstream *stream)
94 void Lstream_fungetc (Lstream *stream, int c) 95 void Lstream_fungetc (Lstream *stream, int c)
95 Function equivalents of the above macros. 96 Function equivalents of the above macros.
96 97
97 int Lstream_read (Lstream *stream, void *data, int size) 98 int Lstream_read (Lstream *stream, void *data, size_t size)
98 Read SIZE bytes of DATA from the stream. Return the number of 99 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 read. 0 means EOF. -1 means an error occurred and no
100 bytes were read. 101 bytes were read.
101 102
102 int Lstream_write (Lstream *stream, void *data, int size) 103 int Lstream_write (Lstream *stream, void *data, size_t size)
103 Write SIZE bytes of DATA to the stream. Return the number of 104 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 bytes written. -1 means an error occurred and no bytes were
105 written. 106 written.
106 107
107 void Lstream_unread (Lstream *stream, void *data, int size) 108 void Lstream_unread (Lstream *stream, void *data, size_t size)
108 Push back SIZE bytes of DATA onto the input queue. The 109 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 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 same bytes back. Note that this will be the case even if
111 there is other pending unread data. 112 there is other pending unread data.
112 113
127 128
128 void Lstream_rewind (Lstream *stream) 129 void Lstream_rewind (Lstream *stream)
129 Rewind the stream to the beginning. 130 Rewind the stream to the beginning.
130 */ 131 */
131 132
132 #ifdef MULE
133 MAC_DEFINE (Emchar, MTlstream_emchar)
134 MAC_DEFINE (int, MTlstream_emcint)
135 #endif
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 133 #define DEFAULT_BLOCK_BUFFERING_SIZE 512
150 #define MAX_READ_SIZE 512 134 #define MAX_READ_SIZE 512
151 135
152 static Lisp_Object 136 static Lisp_Object
153 mark_lstream (Lisp_Object obj, void (*markobj) (Lisp_Object)) 137 mark_lstream (Lisp_Object obj, void (*markobj) (Lisp_Object))
154 { 138 {
155 Lstream *lstr = XLSTREAM (obj); 139 Lstream *lstr = XLSTREAM (obj);
156 if (lstr->imp->marker) 140 return lstr->imp->marker ? (lstr->imp->marker) (obj, markobj) : Qnil;
157 return (lstr->imp->marker) (obj, markobj);
158 else
159 return Qnil;
160 } 141 }
161 142
162 static void 143 static void
163 print_lstream (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag) 144 print_lstream (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
164 { 145 {
196 /* Just close. */ 177 /* Just close. */
197 Lstream_close (lstr); 178 Lstream_close (lstr);
198 } 179 }
199 } 180 }
200 181
201 static unsigned int 182 static size_t
202 sizeof_lstream (CONST void *header) 183 sizeof_lstream (CONST void *header)
203 { 184 {
204 CONST Lstream *lstr = (CONST Lstream *) header; 185 CONST Lstream *lstr = (CONST Lstream *) header;
205 return sizeof (*lstr) + lstr->imp->size - 1; 186 return sizeof (*lstr) + lstr->imp->size - 1;
206 } 187 }
207 188
189 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("stream", lstream,
190 mark_lstream, print_lstream,
191 finalize_lstream, 0, 0,
192 sizeof_lstream, Lstream);
193
208 void 194 void
209 Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering, 195 Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
210 int buffering_size) 196 int buffering_size)
211 { 197 {
212 lstr->buffering = buffering; 198 lstr->buffering = buffering;
276 262
277 void 263 void
278 Lstream_delete (Lstream *lstr) 264 Lstream_delete (Lstream *lstr)
279 { 265 {
280 int i; 266 int i;
281 Lisp_Object val = Qnil; 267 Lisp_Object val;
282 268
283 XSETLSTREAM (val, lstr); 269 XSETLSTREAM (val, lstr);
284 for (i = 0; i < lstream_type_count; i++) 270 for (i = 0; i < lstream_type_count; i++)
285 { 271 {
286 if (lstream_types[i] == lstr->imp) 272 if (lstream_types[i] == lstr->imp)
291 } 277 }
292 278
293 abort (); 279 abort ();
294 } 280 }
295 281
296 static void 282 #define Lstream_internal_error(reason, lstr) \
297 signal_simple_internal_error (CONST char *reason, Lisp_Object obj) 283 Lstream_signal_simple_error ("Internal error: " reason, lstr)
298 { 284
285 static void Lstream_signal_simple_error (CONST char *reason, Lstream *lstr)
286 {
287 Lisp_Object obj;
288 XSETLSTREAM (obj, lstr);
299 signal_simple_error (reason, obj); 289 signal_simple_error (reason, obj);
300 } 290 }
301 291
302 void 292 void
303 Lstream_reopen (Lstream *lstr) 293 Lstream_reopen (Lstream *lstr)
304 { 294 {
305 if (lstr->flags & LSTREAM_FL_IS_OPEN) 295 if (lstr->flags & LSTREAM_FL_IS_OPEN)
306 { 296 Lstream_internal_error ("lstream already open", lstr);
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; 297 lstr->flags |= LSTREAM_FL_IS_OPEN;
313 } 298 }
314 299
315 /* Attempt to flush out all of the buffered data for writing. */ 300 /* Attempt to flush out all of the buffered data for writing. */
316 301
321 306
322 while (lstr->out_buffer_ind > 0) 307 while (lstr->out_buffer_ind > 0)
323 { 308 {
324 int size = lstr->out_buffer_ind; 309 int size = lstr->out_buffer_ind;
325 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) 310 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
326 { 311 Lstream_internal_error ("lstream not open", lstr);
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)) 312 if (! (lstr->flags & LSTREAM_FL_WRITE))
333 { 313 Lstream_internal_error ("lstream not open for writing", lstr);
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) 314 if (!lstr->imp->writer)
340 { 315 Lstream_internal_error ("lstream has no writer", lstr);
341 Lisp_Object obj = Qnil;
342 XSETLSTREAM (obj, lstr);
343 signal_simple_internal_error
344 ("Internal error: lstream has no writer", obj);
345 }
346 316
347 if (lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS) 317 if (lstr->flags & LSTREAM_FL_NO_PARTIAL_CHARS)
348 /* It's quite possible for us to get passed an incomplete 318 /* It's quite possible for us to get passed an incomplete
349 character at the end. We need to spit back that 319 character at the end. We need to spit back that
350 incomplete character. */ 320 incomplete character. */
368 if (size == 0) 338 if (size == 0)
369 break; 339 break;
370 } 340 }
371 } 341 }
372 342
373 num_written = 343 num_written = (lstr->imp->writer) (lstr, lstr->out_buffer, size);
374 (lstr->imp->writer) (lstr, lstr->out_buffer, size);
375 if (num_written == 0) 344 if (num_written == 0)
376 /* If nothing got written, then just hold the data. This may 345 /* If nothing got written, then just hold the data. This may
377 occur, for example, if this stream does non-blocking I/O; 346 occur, for example, if this stream does non-blocking I/O;
378 the attempt to write the data might have resulted in an 347 the attempt to write the data might have resulted in an
379 EWOULDBLOCK error. */ 348 EWOULDBLOCK error. */
380 return 0; 349 return 0;
381 else if (num_written >= lstr->out_buffer_ind) 350 else if (num_written >= (int) lstr->out_buffer_ind)
382 lstr->out_buffer_ind = 0; 351 lstr->out_buffer_ind = 0;
383 else if (num_written > 0) 352 else if (num_written > 0)
384 { 353 {
385 memmove (lstr->out_buffer, lstr->out_buffer + num_written, 354 memmove (lstr->out_buffer, lstr->out_buffer + num_written,
386 lstr->out_buffer_ind - num_written); 355 lstr->out_buffer_ind - num_written);
419 that the stream writer might refuse to write any bytes now, e.g. 388 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 389 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. */ 390 up until they can be written, so as to avoid losing data. */
422 391
423 static int 392 static int
424 Lstream_adding (Lstream *lstr, int num, int force) 393 Lstream_adding (Lstream *lstr, size_t num, int force)
425 { 394 {
426 /* Compute the size that the outbuffer needs to be after the 395 /* Compute the size that the outbuffer needs to be after the
427 chars are added. */ 396 chars are added. */
428 int size_needed = max (lstr->out_buffer_size, 397 size_t size_needed = max (lstr->out_buffer_size,
429 num + lstr->out_buffer_ind); 398 num + lstr->out_buffer_ind);
430 /* Maybe chop it down so that we don't buffer more characters 399 /* Maybe chop it down so that we don't buffer more characters
431 than our advertised buffering size. */ 400 than our advertised buffering size. */
432 if (!force) 401 if (!force)
433 size_needed = min (lstr->buffering_size, size_needed); 402 size_needed = min (lstr->buffering_size, size_needed);
434 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, 403 DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size,
439 } 408 }
440 409
441 /* Like Lstream_write(), but does not handle line-buffering correctly. */ 410 /* Like Lstream_write(), but does not handle line-buffering correctly. */
442 411
443 static int 412 static int
444 Lstream_write_1 (Lstream *lstr, CONST void *data, int size) 413 Lstream_write_1 (Lstream *lstr, CONST void *data, size_t size)
445 { 414 {
446 CONST unsigned char *p = (CONST unsigned char *) data; 415 CONST unsigned char *p = (CONST unsigned char *) data;
447 int off = 0; 416 int off = 0;
448 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) 417 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
449 { 418 Lstream_internal_error ("lstream not open", lstr);
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)) 419 if (! (lstr->flags & LSTREAM_FL_WRITE))
456 { 420 Lstream_internal_error ("lstream not open for writing", lstr);
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 { 421 {
463 int couldnt_write_last_time = 0; 422 int couldnt_write_last_time = 0;
464 423
465 while (1) 424 while (1)
466 { 425 {
509 Lstream_write_1(), which writes in chunks. Otherwise, we 468 Lstream_write_1(), which writes in chunks. Otherwise, we
510 repeatedly call Lstream_putc(), which knows how to handle 469 repeatedly call Lstream_putc(), which knows how to handle
511 line buffering. */ 470 line buffering. */
512 471
513 int 472 int
514 Lstream_write (Lstream *lstr, CONST void *data, int size) 473 Lstream_write (Lstream *lstr, CONST void *data, size_t size)
515 { 474 {
516 int i; 475 int i;
517 CONST unsigned char *p = (CONST unsigned char *) data; 476 CONST unsigned char *p = (CONST unsigned char *) data;
518 477
519 assert (size >= 0);
520 if (size == 0) 478 if (size == 0)
521 return size; 479 return size;
522 if (lstr->buffering != LSTREAM_LINE_BUFFERED) 480 if (lstr->buffering != LSTREAM_LINE_BUFFERED)
523 return Lstream_write_1 (lstr, data, size); 481 return Lstream_write_1 (lstr, data, size);
524 for (i = 0; i < size; i++) 482 for (i = 0; i < (int) size; i++)
525 { 483 {
526 if (Lstream_putc (lstr, p[i]) < 0) 484 if (Lstream_putc (lstr, p[i]) < 0)
527 break; 485 break;
528 } 486 }
529 return i == 0 ? -1 : 0; 487 return i == 0 ? -1 : 0;
537 else 495 else
538 return 0; 496 return 0;
539 } 497 }
540 498
541 static int 499 static int
542 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, int size) 500 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, size_t size)
543 { 501 {
544 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) 502 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
545 { 503 Lstream_internal_error ("lstream not open", lstr);
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)) 504 if (! (lstr->flags & LSTREAM_FL_READ))
552 { 505 Lstream_internal_error ("lstream not open for reading", lstr);
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) 506 if (!lstr->imp->reader)
559 { 507 Lstream_internal_error ("lstream has no reader", lstr);
560 Lisp_Object obj = Qnil;
561 XSETLSTREAM (obj, lstr);
562 signal_simple_internal_error
563 ("Internal error: lstream has no reader", obj);
564 }
565 508
566 return (lstr->imp->reader) (lstr, buffer, size); 509 return (lstr->imp->reader) (lstr, buffer, size);
567 } 510 }
568 511
569 /* Assuming the buffer is empty, fill it up again. */ 512 /* Assuming the buffer is empty, fill it up again. */
586 lstr->in_buffer_ind = 0; 529 lstr->in_buffer_ind = 0;
587 return size_gotten < 0 ? -1 : size_gotten; 530 return size_gotten < 0 ? -1 : size_gotten;
588 } 531 }
589 532
590 int 533 int
591 Lstream_read (Lstream *lstr, void *data, int size) 534 Lstream_read (Lstream *lstr, void *data, size_t size)
592 { 535 {
593 unsigned char *p = (unsigned char *) data; 536 unsigned char *p = (unsigned char *) data;
594 int off = 0; 537 int off = 0;
595 int chunk; 538 size_t chunk;
596 int error_occurred = 0; 539 int error_occurred = 0;
597 540
598 assert (size >= 0);
599 if (size == 0) 541 if (size == 0)
600 return 0; 542 return 0;
601 543
602 /* First try to get some data from the unget buffer */ 544 /* First try to get some data from the unget buffer */
603 chunk = min (size, lstr->unget_buffer_ind); 545 chunk = min (size, lstr->unget_buffer_ind);
604 if (chunk > 0) 546 if (chunk > 0)
605 { 547 {
606 /* The bytes come back in reverse order. */ 548 /* The bytes come back in reverse order. */
607 for (; off < chunk; off++) 549 for (; off < (int) chunk; off++)
608 p[off] = lstr->unget_buffer[--lstr->unget_buffer_ind]; 550 p[off] = lstr->unget_buffer[--lstr->unget_buffer_ind];
609 lstr->byte_count += chunk; 551 lstr->byte_count += chunk;
610 size -= chunk; 552 size -= chunk;
611 } 553 }
612 554
661 603
662 return ((off == 0 && error_occurred) ? -1 : off); 604 return ((off == 0 && error_occurred) ? -1 : off);
663 } 605 }
664 606
665 void 607 void
666 Lstream_unread (Lstream *lstr, CONST void *data, int size) 608 Lstream_unread (Lstream *lstr, CONST void *data, size_t size)
667 { 609 {
668 int i; 610 int i;
669 unsigned char *p = (unsigned char *) data; 611 unsigned char *p = (unsigned char *) data;
670 612
671 /* Make sure buffer is big enough */ 613 /* Make sure buffer is big enough */
682 624
683 int 625 int
684 Lstream_rewind (Lstream *lstr) 626 Lstream_rewind (Lstream *lstr)
685 { 627 {
686 if (!lstr->imp->rewinder) 628 if (!lstr->imp->rewinder)
687 { 629 Lstream_internal_error ("lstream has no rewinder", lstr);
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) 630 if (Lstream_flush (lstr) < 0)
694 return -1; 631 return -1;
695 lstr->byte_count = 0; 632 lstr->byte_count = 0;
696 return (lstr->imp->rewinder) (lstr); 633 return (lstr->imp->rewinder) (lstr);
697 } 634 }
710 Lstream_pseudo_close (Lstream *lstr) 647 Lstream_pseudo_close (Lstream *lstr)
711 { 648 {
712 int rc; 649 int rc;
713 650
714 if (!lstr->flags & LSTREAM_FL_IS_OPEN) 651 if (!lstr->flags & LSTREAM_FL_IS_OPEN)
715 { 652 Lstream_internal_error ("lstream is not open", lstr);
716 Lisp_Object obj = Qnil;
717 XSETLSTREAM (obj, lstr);
718 signal_simple_internal_error
719 ("Internal error: lstream is not open", obj);
720 }
721 653
722 /* don't check errors here -- best not to risk file descriptor loss */ 654 /* don't check errors here -- best not to risk file descriptor loss */
723 rc = Lstream_flush (lstr); 655 rc = Lstream_flush (lstr);
724 656
725 return rc; 657 return rc;
836 sizeof (struct stdio_stream)); 768 sizeof (struct stdio_stream));
837 769
838 static Lisp_Object 770 static Lisp_Object
839 make_stdio_stream_1 (FILE *stream, int flags, CONST char *mode) 771 make_stdio_stream_1 (FILE *stream, int flags, CONST char *mode)
840 { 772 {
841 Lisp_Object obj = Qnil; 773 Lisp_Object obj;
842 Lstream *lstr = Lstream_new (lstream_stdio, mode); 774 Lstream *lstr = Lstream_new (lstream_stdio, mode);
843 struct stdio_stream *str = STDIO_STREAM_DATA (lstr); 775 struct stdio_stream *str = STDIO_STREAM_DATA (lstr);
844 str->file = stream; 776 str->file = stream;
845 str->closing = flags & LSTR_CLOSING; 777 str->closing = flags & LSTR_CLOSING;
846 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE; 778 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
859 { 791 {
860 return make_stdio_stream_1 (stream, flags, "w"); 792 return make_stdio_stream_1 (stream, flags, "w");
861 } 793 }
862 794
863 static int 795 static int
864 stdio_reader (Lstream *stream, unsigned char *data, int size) 796 stdio_reader (Lstream *stream, unsigned char *data, size_t size)
865 { 797 {
866 struct stdio_stream *str = STDIO_STREAM_DATA (stream); 798 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
867 size_t val = fread (data, 1, (size_t) size, str->file); 799 size_t val = fread (data, 1, (size_t) size, str->file);
868 if (!val && ferror (str->file)) 800 if (!val && ferror (str->file))
869 return -1; 801 return -1;
870 return (int) val; 802 return (int) val;
871 } 803 }
872 804
873 static int 805 static int
874 stdio_writer (Lstream *stream, CONST unsigned char *data, int size) 806 stdio_writer (Lstream *stream, CONST unsigned char *data, size_t size)
875 { 807 {
876 struct stdio_stream *str = STDIO_STREAM_DATA (stream); 808 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
877 size_t val = fwrite (data, 1, (size_t) size, str->file); 809 size_t val = fwrite (data, 1, size, str->file);
878 if (!val && ferror (str->file)) 810 if (!val && ferror (str->file))
879 return -1; 811 return -1;
880 return (int) val; 812 return (int) val;
881 } 813 }
882 814
888 } 820 }
889 821
890 static int 822 static int
891 stdio_seekable_p (Lstream *stream) 823 stdio_seekable_p (Lstream *stream)
892 { 824 {
825 struct stat lestat;
893 struct stdio_stream *str = STDIO_STREAM_DATA (stream); 826 struct stdio_stream *str = STDIO_STREAM_DATA (stream);
894 { 827
895 struct stat lestat; 828 if (fstat (fileno (str->file), &lestat) < 0)
896 829 return 0;
897 if (fstat (fileno (str->file), &lestat) < 0) 830 return S_ISREG (lestat.st_mode);
898 return 0;
899 return S_ISREG (lestat.st_mode);
900 }
901 } 831 }
902 832
903 static int 833 static int
904 stdio_flusher (Lstream *stream) 834 stdio_flusher (Lstream *stream)
905 { 835 {
956 ignored when writing); -1 for unlimited. */ 886 ignored when writing); -1 for unlimited. */
957 static Lisp_Object 887 static Lisp_Object
958 make_filedesc_stream_1 (int filedesc, int offset, int count, int flags, 888 make_filedesc_stream_1 (int filedesc, int offset, int count, int flags,
959 CONST char *mode) 889 CONST char *mode)
960 { 890 {
961 Lisp_Object obj = Qnil; 891 Lisp_Object obj;
962 Lstream *lstr = Lstream_new (lstream_filedesc, mode); 892 Lstream *lstr = Lstream_new (lstream_filedesc, mode);
963 struct filedesc_stream *fstr = FILEDESC_STREAM_DATA (lstr); 893 struct filedesc_stream *fstr = FILEDESC_STREAM_DATA (lstr);
964 fstr->fd = filedesc; 894 fstr->fd = filedesc;
965 fstr->closing = !!(flags & LSTR_CLOSING); 895 fstr->closing = !!(flags & LSTR_CLOSING);
966 fstr->allow_quit = !!(flags & LSTR_ALLOW_QUIT); 896 fstr->allow_quit = !!(flags & LSTR_ALLOW_QUIT);
967 fstr->blocked_ok = !!(flags & LSTR_BLOCKED_OK); 897 fstr->blocked_ok = !!(flags & LSTR_BLOCKED_OK);
968 fstr->pty_flushing = !!(flags & LSTR_PTY_FLUSHING); 898 fstr->pty_flushing = !!(flags & LSTR_PTY_FLUSHING);
969 fstr->blocking_error_p = 0; 899 fstr->blocking_error_p = 0;
970 fstr->chars_sans_newline = 0; 900 fstr->chars_sans_newline = 0;
971 fstr->starting_pos = lseek (filedesc, offset, SEEK_CUR); 901 fstr->starting_pos = lseek (filedesc, offset, SEEK_CUR);
972 if (fstr->starting_pos < 0) 902 fstr->current_pos = max (fstr->current_pos, 0);
973 fstr->current_pos = 0;
974 else
975 fstr->current_pos = fstr->starting_pos;
976 if (count < 0) 903 if (count < 0)
977 fstr->end_pos = -1; 904 fstr->end_pos = -1;
978 else 905 else
979 fstr->end_pos = fstr->starting_pos + count; 906 fstr->end_pos = fstr->starting_pos + count;
980 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE; 907 lstr->flags |= LSTREAM_FL_CLOSE_AT_DISKSAVE;
993 { 920 {
994 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); 921 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w");
995 } 922 }
996 923
997 static int 924 static int
998 filedesc_reader (Lstream *stream, unsigned char *data, int size) 925 filedesc_reader (Lstream *stream, unsigned char *data, size_t size)
999 { 926 {
927 int nread;
1000 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); 928 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
1001 if (str->end_pos >= 0) 929 if (str->end_pos >= 0)
1002 size = min (size, str->end_pos - str->current_pos); 930 size = min (size, (size_t) (str->end_pos - str->current_pos));
1003 size = (str->allow_quit ? read_allowing_quit : read) (str->fd, data, size); 931 nread = (str->allow_quit ? read_allowing_quit : read) (str->fd, data, size);
1004 if (size > 0) 932 if (nread > 0)
1005 str->current_pos += size; 933 str->current_pos += nread;
1006 return size; 934 return nread;
1007 } 935 }
1008 936
1009 static int 937 static int
1010 errno_would_block_p (int val) 938 errno_would_block_p (int val)
1011 { 939 {
1019 #endif 947 #endif
1020 return 0; 948 return 0;
1021 } 949 }
1022 950
1023 static int 951 static int
1024 filedesc_writer (Lstream *stream, CONST unsigned char *data, int size) 952 filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
1025 { 953 {
1026 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); 954 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
1027 int retval; 955 int retval;
1028 int need_newline = 0; 956 int need_newline = 0;
1029 957
1219 1147
1220 Lisp_Object 1148 Lisp_Object
1221 make_lisp_string_input_stream (Lisp_Object string, Bytecount offset, 1149 make_lisp_string_input_stream (Lisp_Object string, Bytecount offset,
1222 Bytecount len) 1150 Bytecount len)
1223 { 1151 {
1224 Lisp_Object obj = Qnil; 1152 Lisp_Object obj;
1225 Lstream *lstr; 1153 Lstream *lstr;
1226 struct lisp_string_stream *str; 1154 struct lisp_string_stream *str;
1227 1155
1228 CHECK_STRING (string); 1156 CHECK_STRING (string);
1229 if (len < 0) 1157 if (len < 0)
1241 XSETLSTREAM (obj, lstr); 1169 XSETLSTREAM (obj, lstr);
1242 return obj; 1170 return obj;
1243 } 1171 }
1244 1172
1245 static int 1173 static int
1246 lisp_string_reader (Lstream *stream, unsigned char *data, int size) 1174 lisp_string_reader (Lstream *stream, unsigned char *data, size_t size)
1247 { 1175 {
1248 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); 1176 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream);
1249 /* Don't lose if the string shrank past us ... */ 1177 /* Don't lose if the string shrank past us ... */
1250 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); 1178 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj));
1251 Bufbyte *strstart = XSTRING_DATA (str->obj); 1179 Bufbyte *strstart = XSTRING_DATA (str->obj);
1256 /* Being in the middle of a character is `normal' unless 1184 /* Being in the middle of a character is `normal' unless
1257 LSTREAM_NO_PARTIAL_CHARS - mrb */ 1185 LSTREAM_NO_PARTIAL_CHARS - mrb */
1258 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) 1186 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS)
1259 VALIDATE_CHARPTR_BACKWARD (start); 1187 VALIDATE_CHARPTR_BACKWARD (start);
1260 offset = start - strstart; 1188 offset = start - strstart;
1261 size = min (size, str->end - offset); 1189 size = min (size, (size_t) (str->end - offset));
1262 assert (size >= 0); /* paranoia */
1263 memcpy (data, start, size); 1190 memcpy (data, start, size);
1264 str->offset = offset + size; 1191 str->offset = offset + size;
1265 return size; 1192 return size;
1266 } 1193 }
1267 1194
1300 1227
1301 struct fixed_buffer_stream 1228 struct fixed_buffer_stream
1302 { 1229 {
1303 CONST unsigned char *inbuf; 1230 CONST unsigned char *inbuf;
1304 unsigned char *outbuf; 1231 unsigned char *outbuf;
1305 int size; 1232 size_t size;
1306 int offset; 1233 size_t offset;
1307 }; 1234 };
1308 1235
1309 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, 1236 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer,
1310 sizeof (struct fixed_buffer_stream)); 1237 sizeof (struct fixed_buffer_stream));
1311 1238
1312 Lisp_Object 1239 Lisp_Object
1313 make_fixed_buffer_input_stream (CONST unsigned char *buf, int size) 1240 make_fixed_buffer_input_stream (CONST unsigned char *buf, size_t size)
1314 { 1241 {
1315 Lisp_Object obj = Qnil; 1242 Lisp_Object obj;
1316 Lstream *lstr; 1243 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r");
1317 struct fixed_buffer_stream *str; 1244 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr);
1318
1319 lstr = Lstream_new (lstream_fixed_buffer, "r");
1320 str = FIXED_BUFFER_STREAM_DATA (lstr);
1321 str->inbuf = buf; 1245 str->inbuf = buf;
1322 str->size = size; 1246 str->size = size;
1323 XSETLSTREAM (obj, lstr); 1247 XSETLSTREAM (obj, lstr);
1324 return obj; 1248 return obj;
1325 } 1249 }
1326 1250
1327 Lisp_Object 1251 Lisp_Object
1328 make_fixed_buffer_output_stream (unsigned char *buf, int size) 1252 make_fixed_buffer_output_stream (unsigned char *buf, size_t size)
1329 { 1253 {
1330 Lisp_Object obj = Qnil; 1254 Lisp_Object obj;
1331 Lstream *lstr; 1255 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w");
1332 struct fixed_buffer_stream *str; 1256 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr);
1333
1334 lstr = Lstream_new (lstream_fixed_buffer, "w");
1335 str = FIXED_BUFFER_STREAM_DATA (lstr);
1336 str->outbuf = buf; 1257 str->outbuf = buf;
1337 str->size = size; 1258 str->size = size;
1338 XSETLSTREAM (obj, lstr); 1259 XSETLSTREAM (obj, lstr);
1339 return obj; 1260 return obj;
1340 } 1261 }
1341 1262
1342 static int 1263 static int
1343 fixed_buffer_reader (Lstream *stream, unsigned char *data, int size) 1264 fixed_buffer_reader (Lstream *stream, unsigned char *data, size_t size)
1344 { 1265 {
1345 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); 1266 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream);
1346 size = min (size, str->size - str->offset); 1267 size = min (size, str->size - str->offset);
1347 memcpy (data, str->inbuf + str->offset, size); 1268 memcpy (data, str->inbuf + str->offset, size);
1348 str->offset += size; 1269 str->offset += size;
1349 return size; 1270 return size;
1350 } 1271 }
1351 1272
1352 static int 1273 static int
1353 fixed_buffer_writer (Lstream *stream, CONST unsigned char *data, int size) 1274 fixed_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
1354 { 1275 {
1355 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); 1276 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream);
1356 if (str->offset == str->size) 1277 if (str->offset == str->size)
1357 { 1278 {
1358 /* If we're at the end, just throw away the data and pretend 1279 /* If we're at the end, just throw away the data and pretend
1393 LSTREAM_TYPE_DATA (stream, resizing_buffer) 1314 LSTREAM_TYPE_DATA (stream, resizing_buffer)
1394 1315
1395 struct resizing_buffer_stream 1316 struct resizing_buffer_stream
1396 { 1317 {
1397 unsigned char *buf; 1318 unsigned char *buf;
1398 int allocked; 1319 size_t allocked;
1399 int max_stored; 1320 int max_stored;
1400 int stored; 1321 int stored;
1401 }; 1322 };
1402 1323
1403 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, 1324 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer,
1404 sizeof (struct resizing_buffer_stream)); 1325 sizeof (struct resizing_buffer_stream));
1405 1326
1406 Lisp_Object 1327 Lisp_Object
1407 make_resizing_buffer_output_stream (void) 1328 make_resizing_buffer_output_stream (void)
1408 { 1329 {
1409 Lisp_Object obj = Qnil; 1330 Lisp_Object obj;
1410 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); 1331 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w"));
1411 return obj; 1332 return obj;
1412 } 1333 }
1413 1334
1414 static int 1335 static int
1415 resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, int size) 1336 resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
1416 { 1337 {
1417 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); 1338 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream);
1418 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); 1339 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char);
1419 memcpy (str->buf + str->stored, data, size); 1340 memcpy (str->buf + str->stored, data, size);
1420 str->stored += size; 1341 str->stored += size;
1465 sizeof (struct dynarr_stream)); 1386 sizeof (struct dynarr_stream));
1466 1387
1467 Lisp_Object 1388 Lisp_Object
1468 make_dynarr_output_stream (unsigned_char_dynarr *dyn) 1389 make_dynarr_output_stream (unsigned_char_dynarr *dyn)
1469 { 1390 {
1470 Lisp_Object obj = Qnil; 1391 Lisp_Object obj;
1471 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); 1392 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w"));
1472 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; 1393 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn;
1473 return obj; 1394 return obj;
1474 } 1395 }
1475 1396
1476 static int 1397 static int
1477 dynarr_writer (Lstream *stream, CONST unsigned char *data, int size) 1398 dynarr_writer (Lstream *stream, CONST unsigned char *data, size_t size)
1478 { 1399 {
1479 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); 1400 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream);
1480 Dynarr_add_many (str->dyn, data, size); 1401 Dynarr_add_many (str->dyn, data, size);
1481 return size; 1402 return size;
1482 } 1403 }
1517 1438
1518 static Lisp_Object 1439 static Lisp_Object
1519 make_lisp_buffer_stream_1 (struct buffer *buf, Bufpos start, Bufpos end, 1440 make_lisp_buffer_stream_1 (struct buffer *buf, Bufpos start, Bufpos end,
1520 int flags, CONST char *mode) 1441 int flags, CONST char *mode)
1521 { 1442 {
1522 Lisp_Object obj = Qnil; 1443 Lisp_Object obj;
1523 Lstream *lstr; 1444 Lstream *lstr;
1524 struct lisp_buffer_stream *str; 1445 struct lisp_buffer_stream *str;
1525 Bufpos bmin, bmax; 1446 Bufpos bmin, bmax;
1526 int reading = !strcmp (mode, "r"); 1447 int reading = !strcmp (mode, "r");
1527 1448
1555 1476
1556 lstr = Lstream_new (lstream_lisp_buffer, mode); 1477 lstr = Lstream_new (lstream_lisp_buffer, mode);
1557 str = LISP_BUFFER_STREAM_DATA (lstr); 1478 str = LISP_BUFFER_STREAM_DATA (lstr);
1558 { 1479 {
1559 Lisp_Object marker; 1480 Lisp_Object marker;
1560 Lisp_Object buffer = Qnil; 1481 Lisp_Object buffer;
1561 1482
1562 XSETBUFFER (buffer, buf); 1483 XSETBUFFER (buffer, buf);
1563 marker = Fmake_marker (); 1484 marker = Fmake_marker ();
1564 Fset_marker (marker, make_int (start), buffer); 1485 Fset_marker (marker, make_int (start), buffer);
1565 str->start = marker; 1486 str->start = marker;
1596 Lstream_set_character_mode (XLSTREAM (lstr)); 1517 Lstream_set_character_mode (XLSTREAM (lstr));
1597 return lstr; 1518 return lstr;
1598 } 1519 }
1599 1520
1600 static int 1521 static int
1601 lisp_buffer_reader (Lstream *stream, unsigned char *data, int size) 1522 lisp_buffer_reader (Lstream *stream, unsigned char *data, size_t size)
1602 { 1523 {
1603 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); 1524 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream);
1604 unsigned char *orig_data = data; 1525 unsigned char *orig_data = data;
1605 Bytind start; 1526 Bytind start;
1606 Bytind end; 1527 Bytind end;
1620 BI_BUF_ZV (buf)); 1541 BI_BUF_ZV (buf));
1621 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, 1542 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end,
1622 BI_BUF_ZV (buf)); 1543 BI_BUF_ZV (buf));
1623 } 1544 }
1624 1545
1625 size = min (size, end - start); 1546 size = min (size, (size_t) (end - start));
1626 end = start + size; 1547 end = start + size;
1627 /* We cannot return a partial character. */ 1548 /* We cannot return a partial character. */
1628 VALIDATE_BYTIND_BACKWARD (buf, end); 1549 VALIDATE_BYTIND_BACKWARD (buf, end);
1629 1550
1630 while (start < end) 1551 while (start < end)
1654 set_bi_marker_position (str->start, end); 1575 set_bi_marker_position (str->start, end);
1655 return data - orig_data; 1576 return data - orig_data;
1656 } 1577 }
1657 1578
1658 static int 1579 static int
1659 lisp_buffer_writer (Lstream *stream, CONST unsigned char *data, int size) 1580 lisp_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
1660 { 1581 {
1661 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); 1582 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream);
1662 Bufpos pos; 1583 Bufpos pos;
1663 struct buffer *buf = XBUFFER (str->buffer); 1584 struct buffer *buf = XBUFFER (str->buffer);
1664 1585