Mercurial > hg > xemacs-beta
diff src/lstream.c @ 412:697ef44129c6 r21-2-14
Import from CVS: tag r21-2-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:20:41 +0200 |
parents | de805c49cfc1 |
children | 41dbb7a9d5f2 |
line wrap: on
line diff
--- a/src/lstream.c Mon Aug 13 11:19:22 2007 +0200 +++ b/src/lstream.c Mon Aug 13 11:20:41 2007 +0200 @@ -26,6 +26,7 @@ #include <config.h> #include "lisp.h" +#include <limits.h> #include "buffer.h" #include "insdel.h" @@ -51,7 +52,7 @@ /* Functions are as follows: -Lstream *Lstream_new (Lstream_implementation *imp, const char *mode) +Lstream *Lstream_new (Lstream_implementation *imp, CONST char *mode) Allocate and return a new Lstream. This function is not really meant to be called directly; rather, each stream type should provide its own stream creation function, which @@ -94,12 +95,12 @@ void Lstream_fungetc (Lstream *stream, int c) Function equivalents of the above macros. -ssize_t Lstream_read (Lstream *stream, void *data, size_t size) +int Lstream_read (Lstream *stream, void *data, size_t size) Read SIZE bytes of DATA from the stream. Return the number of bytes read. 0 means EOF. -1 means an error occurred and no bytes were read. -ssize_t Lstream_write (Lstream *stream, void *data, size_t size) +int Lstream_write (Lstream *stream, void *data, size_t size) Write SIZE bytes of DATA to the stream. Return the number of bytes written. -1 means an error occurred and no bytes were written. @@ -133,10 +134,10 @@ #define MAX_READ_SIZE 512 static Lisp_Object -mark_lstream (Lisp_Object obj) +mark_lstream (Lisp_Object obj, void (*markobj) (Lisp_Object)) { Lstream *lstr = XLSTREAM (obj); - return lstr->imp->marker ? (lstr->imp->marker) (obj) : Qnil; + return lstr->imp->marker ? (lstr->imp->marker) (obj, markobj) : Qnil; } static void @@ -179,15 +180,15 @@ } static size_t -sizeof_lstream (const void *header) +sizeof_lstream (CONST void *header) { - const Lstream *lstr = (const Lstream *) header; + CONST Lstream *lstr = (CONST Lstream *) header; return sizeof (*lstr) + lstr->imp->size - 1; } DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("stream", lstream, mark_lstream, print_lstream, - finalize_lstream, 0, 0, 0, + finalize_lstream, 0, 0, sizeof_lstream, Lstream); void @@ -209,12 +210,12 @@ } } -static const Lstream_implementation *lstream_types[32]; +static CONST Lstream_implementation *lstream_types[32]; static Lisp_Object Vlstream_free_list[32]; static int lstream_type_count; Lstream * -Lstream_new (const Lstream_implementation *imp, const char *mode) +Lstream_new (CONST Lstream_implementation *imp, CONST char *mode) { Lstream *p; int i; @@ -281,7 +282,7 @@ #define Lstream_internal_error(reason, lstr) \ Lstream_signal_simple_error ("Internal error: " reason, lstr) -static void Lstream_signal_simple_error (const char *reason, Lstream *lstr) +static void Lstream_signal_simple_error (CONST char *reason, Lstream *lstr) { Lisp_Object obj; XSETLSTREAM (obj, lstr); @@ -301,11 +302,11 @@ int Lstream_flush_out (Lstream *lstr) { - ssize_t num_written; + int num_written; while (lstr->out_buffer_ind > 0) { - size_t size = lstr->out_buffer_ind; + int size = lstr->out_buffer_ind; if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) Lstream_internal_error ("lstream not open", lstr); if (! (lstr->flags & LSTREAM_FL_WRITE)) @@ -318,8 +319,8 @@ character at the end. We need to spit back that incomplete character. */ { - const unsigned char *data = lstr->out_buffer; - const unsigned char *dataend = data + size - 1; + CONST unsigned char *data = lstr->out_buffer; + CONST unsigned char *dataend = data + size - 1; assert (size > 0); /* safety check ... */ /* Optimize the most common case. */ if (!BYTE_ASCII_P (*dataend)) @@ -346,7 +347,7 @@ the attempt to write the data might have resulted in an EWOULDBLOCK error. */ return 0; - else if (num_written >= lstr->out_buffer_ind) + else if (num_written >= (int) lstr->out_buffer_ind) lstr->out_buffer_ind = 0; else if (num_written > 0) { @@ -388,36 +389,31 @@ if it's getting EWOULDBLOCK errors. We have to keep stocking them up until they can be written, so as to avoid losing data. */ -static size_t +static int Lstream_adding (Lstream *lstr, size_t num, int force) { - size_t size = num + lstr->out_buffer_ind; - - if (size <= lstr->out_buffer_size) - return num; - + /* Compute the size that the outbuffer needs to be after the + chars are added. */ + size_t size_needed = max (lstr->out_buffer_size, + num + lstr->out_buffer_ind); /* Maybe chop it down so that we don't buffer more characters than our advertised buffering size. */ - if ((size > lstr->buffering_size) && !force) - { - size = lstr->buffering_size; - /* There might be more data buffered than the buffering size. */ - if (size <= lstr->out_buffer_ind) - return 0; - } - - DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, size, unsigned char); - - return size - lstr->out_buffer_ind; + if (!force) + size_needed = min (lstr->buffering_size, size_needed); + DO_REALLOC (lstr->out_buffer, lstr->out_buffer_size, + size_needed, unsigned char); + /* There might be more data buffered than the buffering size, + so make sure we don't return a negative number here. */ + return max (0, min (num, size_needed - lstr->out_buffer_ind)); } /* Like Lstream_write(), but does not handle line-buffering correctly. */ -static ssize_t -Lstream_write_1 (Lstream *lstr, const void *data, size_t size) +static int +Lstream_write_1 (Lstream *lstr, CONST void *data, size_t size) { - const unsigned char *p = (const unsigned char *) data; - ssize_t off = 0; + CONST unsigned char *p = (CONST unsigned char *) data; + int off = 0; if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) Lstream_internal_error ("lstream not open", lstr); if (! (lstr->flags & LSTREAM_FL_WRITE)) @@ -428,7 +424,7 @@ while (1) { /* Figure out how much we can add to the buffer */ - size_t chunk = Lstream_adding (lstr, size, 0); + int chunk = Lstream_adding (lstr, size, 0); if (chunk == 0) { if (couldnt_write_last_time) @@ -471,30 +467,33 @@ /* If the stream is not line-buffered, then we can just call Lstream_write_1(), which writes in chunks. Otherwise, we repeatedly call Lstream_putc(), which knows how to handle - line buffering. Returns number of bytes written. */ + line buffering. */ -ssize_t -Lstream_write (Lstream *lstr, const void *data, size_t size) +int +Lstream_write (Lstream *lstr, CONST void *data, size_t size) { - size_t i; - const unsigned char *p = (const unsigned char *) data; + int i; + CONST unsigned char *p = (CONST unsigned char *) data; if (size == 0) return size; if (lstr->buffering != LSTREAM_LINE_BUFFERED) return Lstream_write_1 (lstr, data, size); - for (i = 0; i < size; i++) + for (i = 0; i < (int) size; i++) { if (Lstream_putc (lstr, p[i]) < 0) break; } - return i == 0 ? -1 : (ssize_t) i; + return i == 0 ? -1 : 0; } int Lstream_was_blocked_p (Lstream *lstr) { - return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; + if (lstr->imp->was_blocked_p) + return (lstr->imp->was_blocked_p) (lstr); + else + return 0; } static int @@ -512,16 +511,16 @@ /* Assuming the buffer is empty, fill it up again. */ -static ssize_t +static int Lstream_read_more (Lstream *lstr) { #if 0 - ssize_t size_needed = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); + int size_needed = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); #else /* If someone requested a larger buffer size, so be it! */ - ssize_t size_needed = max (1, lstr->buffering_size); + int size_needed = max (1, lstr->buffering_size); #endif - ssize_t size_gotten; + int size_gotten; DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, size_needed, unsigned char); @@ -531,11 +530,11 @@ return size_gotten < 0 ? -1 : size_gotten; } -ssize_t +int Lstream_read (Lstream *lstr, void *data, size_t size) { unsigned char *p = (unsigned char *) data; - size_t off = 0; + int off = 0; size_t chunk; int error_occurred = 0; @@ -547,7 +546,7 @@ if (chunk > 0) { /* The bytes come back in reverse order. */ - for (; off < chunk; off++) + for (; off < (int) chunk; off++) p[off] = lstr->unget_buffer[--lstr->unget_buffer_ind]; lstr->byte_count += chunk; size -= chunk; @@ -569,7 +568,7 @@ /* If we need some more, try to get some more from the stream's end */ if (size > 0) { - ssize_t retval = Lstream_read_more (lstr); + int retval = Lstream_read_more (lstr); if (retval < 0) error_occurred = 1; if (retval <= 0) @@ -583,7 +582,7 @@ /* It's quite possible for us to get passed an incomplete character at the end. We need to spit back that incomplete character. */ - const unsigned char *dataend = p + off - 1; + CONST unsigned char *dataend = p + off - 1; /* Optimize the most common case. */ if (!BYTE_ASCII_P (*dataend)) { @@ -593,7 +592,7 @@ VALIDATE_CHARPTR_BACKWARD (dataend); if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) { - size_t newoff = dataend - p; + int newoff = dataend - p; /* If not, chop the size down to ignore the last char and stash it away for next time. */ Lstream_unread (lstr, dataend, off - newoff); @@ -602,24 +601,25 @@ } } - return off == 0 && error_occurred ? -1 : (ssize_t) off; + return ((off == 0 && error_occurred) ? -1 : off); } void -Lstream_unread (Lstream *lstr, const void *data, size_t size) +Lstream_unread (Lstream *lstr, CONST void *data, size_t size) { - const unsigned char *p = (const unsigned char *) data; + int i; + unsigned char *p = (unsigned char *) data; /* Make sure buffer is big enough */ + DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, lstr->unget_buffer_ind + size, unsigned char); - lstr->byte_count -= size; - /* Bytes have to go on in reverse order -- they are reversed again when read back. */ - while (size--) - lstr->unget_buffer[lstr->unget_buffer_ind++] = p[size]; + for (i = size - 1; i >= 0; i--) + lstr->unget_buffer[lstr->unget_buffer_ind++] = p[i]; + lstr->byte_count -= size; } int @@ -646,11 +646,15 @@ static int Lstream_pseudo_close (Lstream *lstr) { + int rc; + if (!lstr->flags & LSTREAM_FL_IS_OPEN) Lstream_internal_error ("lstream is not open", lstr); /* don't check errors here -- best not to risk file descriptor loss */ - return Lstream_flush (lstr); + rc = Lstream_flush (lstr); + + return rc; } int @@ -725,7 +729,7 @@ Lstream_fputc (Lstream *lstr, int c) { unsigned char ch = (unsigned char) c; - ssize_t retval = Lstream_write_1 (lstr, &ch, 1); + int retval = Lstream_write_1 (lstr, &ch, 1); if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') return Lstream_flush_out (lstr); return retval < 0 ? -1 : 0; @@ -764,7 +768,7 @@ sizeof (struct stdio_stream)); static Lisp_Object -make_stdio_stream_1 (FILE *stream, int flags, const char *mode) +make_stdio_stream_1 (FILE *stream, int flags, CONST char *mode) { Lisp_Object obj; Lstream *lstr = Lstream_new (lstream_stdio, mode); @@ -788,42 +792,24 @@ return make_stdio_stream_1 (stream, flags, "w"); } -/* #### From reading the Unix 98 specification, it appears that if we - want stdio_reader() to be completely correct, we should check for - 0 < val < size and if so, check to see if an error has occurred. - If an error has occurred, but val is non-zero, we should go ahead - and act as if the read was successful, but remember in some fashion - or other, that an error has occurred, and report that on the next - call to stdio_reader instead of calling fread() again. - - Currently, in such a case, we end up calling fread() twice and we - assume that - - 1) this is not harmful, and - 2) the error will still be reported on the second read. - - This is probably reasonable, so I don't think we should change this - code (it could even be argued that the error might have fixed - itself, so we should do the fread() again. */ - -static ssize_t +static int stdio_reader (Lstream *stream, unsigned char *data, size_t size) { struct stdio_stream *str = STDIO_STREAM_DATA (stream); - size_t val = fread (data, 1, size, str->file); + size_t val = fread (data, 1, (size_t) size, str->file); if (!val && ferror (str->file)) return -1; - return val; + return (int) val; } -static ssize_t -stdio_writer (Lstream *stream, const unsigned char *data, size_t size) +static int +stdio_writer (Lstream *stream, CONST unsigned char *data, size_t size) { struct stdio_stream *str = STDIO_STREAM_DATA (stream); size_t val = fwrite (data, 1, size, str->file); if (!val && ferror (str->file)) return -1; - return val; + return (int) val; } static int @@ -851,6 +837,9 @@ if (stream->flags & LSTREAM_FL_WRITE) return fflush (str->file); else + /* call fpurge? Only exists on some systems. #### Why not add a + configure check for HAVE_FPURGE and utilize it on systems that + support it? --hniksic */ return 0; } @@ -864,6 +853,7 @@ if (stream->flags & LSTREAM_FL_WRITE) return fflush (str->file); else + /* call fpurge? Only exists on some systems. */ return 0; } @@ -896,7 +886,7 @@ ignored when writing); -1 for unlimited. */ static Lisp_Object make_filedesc_stream_1 (int filedesc, int offset, int count, int flags, - const char *mode) + CONST char *mode) { Lisp_Object obj; Lstream *lstr = Lstream_new (lstream_filedesc, mode); @@ -931,16 +921,14 @@ return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); } -static ssize_t +static int filedesc_reader (Lstream *stream, unsigned char *data, size_t size) { - ssize_t nread; + int nread; struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); if (str->end_pos >= 0) size = min (size, (size_t) (str->end_pos - str->current_pos)); - nread = str->allow_quit ? - read_allowing_quit (str->fd, data, size) : - read (str->fd, data, size); + nread = (str->allow_quit ? read_allowing_quit : read) (str->fd, data, size); if (nread > 0) str->current_pos += nread; return nread; @@ -960,11 +948,11 @@ return 0; } -static ssize_t -filedesc_writer (Lstream *stream, const unsigned char *data, size_t size) +static int +filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size) { struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); - ssize_t retval; + int retval; int need_newline = 0; /* This function would be simple if it were not for the blasted @@ -976,9 +964,9 @@ if (str->pty_flushing) { /* To make life easy, only send out one line at the most. */ - const unsigned char *ptr; + CONST unsigned char *ptr; - ptr = (const unsigned char *) memchr (data, '\n', size); + ptr = (CONST unsigned char *) memchr (data, '\n', size); if (ptr) need_newline = 1; else @@ -993,9 +981,8 @@ /**** start of non-PTY-crap ****/ if (size > 0) - retval = str->allow_quit ? - write_allowing_quit (str->fd, data, size) : - write (str->fd, data, size); + retval = ((str->allow_quit ? write_allowing_quit : write) + (str->fd, data, size)); else retval = 0; if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok) @@ -1018,10 +1005,8 @@ out for EWOULDBLOCK. */ if (str->chars_sans_newline >= str->pty_max_bytes) { - ssize_t retval2 = str->allow_quit ? - write_allowing_quit (str->fd, &str->eof_char, 1) : - write (str->fd, &str->eof_char, 1); - + int retval2 = ((str->allow_quit ? write_allowing_quit : write) + (str->fd, &str->eof_char, 1)); if (retval2 > 0) str->chars_sans_newline = 0; else if (retval2 < 0) @@ -1051,10 +1036,8 @@ if (need_newline) { Bufbyte nl = '\n'; - ssize_t retval2 = str->allow_quit ? - write_allowing_quit (str->fd, &nl, 1) : - write (str->fd, &nl, 1); - + int retval2 = ((str->allow_quit ? write_allowing_quit : write) + (str->fd, &nl, 1)); if (retval2 > 0) { str->chars_sans_newline = 0; @@ -1187,7 +1170,7 @@ return obj; } -static ssize_t +static int lisp_string_reader (Lstream *stream, unsigned char *data, size_t size) { struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); @@ -1231,7 +1214,7 @@ } static Lisp_Object -lisp_string_marker (Lisp_Object stream) +lisp_string_marker (Lisp_Object stream, void (*markobj) (Lisp_Object)) { struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (XLSTREAM (stream)); return str->obj; @@ -1244,7 +1227,7 @@ struct fixed_buffer_stream { - const unsigned char *inbuf; + CONST unsigned char *inbuf; unsigned char *outbuf; size_t size; size_t offset; @@ -1254,30 +1237,30 @@ sizeof (struct fixed_buffer_stream)); Lisp_Object -make_fixed_buffer_input_stream (const void *buf, size_t size) +make_fixed_buffer_input_stream (CONST unsigned char *buf, size_t size) { Lisp_Object obj; Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); - str->inbuf = (const unsigned char *) buf; + str->inbuf = buf; str->size = size; XSETLSTREAM (obj, lstr); return obj; } Lisp_Object -make_fixed_buffer_output_stream (void *buf, size_t size) +make_fixed_buffer_output_stream (unsigned char *buf, size_t size) { Lisp_Object obj; Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); - str->outbuf = (unsigned char *) buf; + str->outbuf = buf; str->size = size; XSETLSTREAM (obj, lstr); return obj; } -static ssize_t +static int fixed_buffer_reader (Lstream *stream, unsigned char *data, size_t size) { struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); @@ -1287,8 +1270,8 @@ return size; } -static ssize_t -fixed_buffer_writer (Lstream *stream, const unsigned char *data, size_t size) +static int +fixed_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size) { struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); if (str->offset == str->size) @@ -1311,7 +1294,7 @@ return 0; } -const unsigned char * +CONST unsigned char * fixed_buffer_input_stream_ptr (Lstream *stream) { assert (stream->imp == lstream_fixed_buffer); @@ -1349,8 +1332,8 @@ return obj; } -static ssize_t -resizing_buffer_writer (Lstream *stream, const unsigned char *data, size_t size) +static int +resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size) { struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); @@ -1411,8 +1394,8 @@ return obj; } -static ssize_t -dynarr_writer (Lstream *stream, const unsigned char *data, size_t size) +static int +dynarr_writer (Lstream *stream, CONST unsigned char *data, size_t size) { struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); Dynarr_add_many (str->dyn, data, size); @@ -1455,7 +1438,7 @@ static Lisp_Object make_lisp_buffer_stream_1 (struct buffer *buf, Bufpos start, Bufpos end, - int flags, const char *mode) + int flags, CONST char *mode) { Lisp_Object obj; Lstream *lstr; @@ -1535,7 +1518,7 @@ return lstr; } -static ssize_t +static int lisp_buffer_reader (Lstream *stream, unsigned char *data, size_t size) { struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); @@ -1593,8 +1576,8 @@ return data - orig_data; } -static ssize_t -lisp_buffer_writer (Lstream *stream, const unsigned char *data, size_t size) +static int +lisp_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size) { struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); Bufpos pos; @@ -1629,13 +1612,13 @@ } static Lisp_Object -lisp_buffer_marker (Lisp_Object stream) +lisp_buffer_marker (Lisp_Object stream, void (*markobj) (Lisp_Object)) { struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (XLSTREAM (stream)); - mark_object (str->start); - mark_object (str->end); + markobj (str->start); + markobj (str->end); return str->buffer; } @@ -1690,21 +1673,13 @@ } void -reinit_vars_of_lstream (void) +vars_of_lstream (void) { int i; for (i = 0; i < countof (Vlstream_free_list); i++) { Vlstream_free_list[i] = Qnil; - staticpro_nodump (&Vlstream_free_list[i]); + staticpro (&Vlstream_free_list[i]); } } - -void -vars_of_lstream (void) -{ - INIT_LRECORD_IMPLEMENTATION (lstream); - - reinit_vars_of_lstream (); -}