import -ko -b 1.1.3 XEmacs XEmacs-21_2 r21-2-35
[chise/xemacs-chise.git.1] / src / lstream.c
index 9ecce37..29427db 100644 (file)
@@ -26,7 +26,6 @@ Boston, MA 02111-1307, USA.  */
 
 #include <config.h>
 #include "lisp.h"
-#include <limits.h>
 
 #include "buffer.h"
 #include "insdel.h"
@@ -52,7 +51,7 @@ Boston, MA 02111-1307, USA.  */
 
 /* 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
@@ -180,9 +179,9 @@ finalize_lstream (void *header, int for_disksave)
 }
 
 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;
 }
 
@@ -210,12 +209,12 @@ Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
     }
 }
 
-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;
@@ -282,7 +281,7 @@ Lstream_delete (Lstream *lstr)
 #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);
@@ -319,8 +318,8 @@ Lstream_flush_out (Lstream *lstr)
           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))
@@ -392,27 +391,32 @@ Lstream_flush (Lstream *lstr)
 static size_t
 Lstream_adding (Lstream *lstr, size_t num, int force)
 {
-  /* 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);
+  size_t size = num + lstr->out_buffer_ind;
+
+  if (size <= lstr->out_buffer_size)
+    return num;
+
   /* Maybe chop it down so that we don't buffer more characters
      than our advertised buffering size. */
-  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));
+  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;
 }
 
 /* Like Lstream_write(), but does not handle line-buffering correctly. */
 
 static ssize_t
-Lstream_write_1 (Lstream *lstr, CONST void *data, size_t size)
+Lstream_write_1 (Lstream *lstr, const void *data, size_t size)
 {
-  CONST unsigned char *p = (CONST unsigned char *) data;
+  const unsigned char *p = (const unsigned char *) data;
   ssize_t off = 0;
   if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
     Lstream_internal_error ("lstream not open", lstr);
@@ -467,13 +471,13 @@ Lstream_write_1 (Lstream *lstr, CONST void *data, size_t size)
 /* 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. */
+   line buffering.  Returns number of bytes written. */
 
 ssize_t
-Lstream_write (Lstream *lstr, CONST void *data, size_t size)
+Lstream_write (Lstream *lstr, const void *data, size_t size)
 {
   size_t i;
-  CONST unsigned char *p = (CONST unsigned char *) data;
+  const unsigned char *p = (const unsigned char *) data;
 
   if (size == 0)
     return size;
@@ -484,7 +488,7 @@ Lstream_write (Lstream *lstr, CONST void *data, size_t size)
       if (Lstream_putc (lstr, p[i]) < 0)
        break;
     }
-  return i == 0 ? -1 : 0;
+  return i == 0 ? -1 : (ssize_t) i;
 }
 
 int
@@ -579,7 +583,7 @@ Lstream_read (Lstream *lstr, void *data, size_t size)
       /* 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))
        {
@@ -602,9 +606,9 @@ Lstream_read (Lstream *lstr, void *data, size_t size)
 }
 
 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;
+  const unsigned char *p = (const unsigned char *) data;
 
   /* Make sure buffer is big enough */
   DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size,
@@ -760,7 +764,7 @@ DEFINE_LSTREAM_IMPLEMENTATION ("stdio", lstream_stdio,
                               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);
@@ -813,7 +817,7 @@ stdio_reader (Lstream *stream, unsigned char *data, size_t size)
 }
 
 static ssize_t
-stdio_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+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);
@@ -892,7 +896,7 @@ DEFINE_LSTREAM_IMPLEMENTATION ("filedesc", lstream_filedesc,
    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);
@@ -934,7 +938,9 @@ filedesc_reader (Lstream *stream, unsigned char *data, size_t size)
   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 : read) (str->fd, data, size);
+  nread = str->allow_quit ?
+    read_allowing_quit (str->fd, data, size) :
+    read (str->fd, data, size);
   if (nread > 0)
     str->current_pos += nread;
   return nread;
@@ -955,7 +961,7 @@ errno_would_block_p (int val)
 }
 
 static ssize_t
-filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+filedesc_writer (Lstream *stream, const unsigned char *data, size_t size)
 {
   struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
   ssize_t retval;
@@ -970,9 +976,9 @@ filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
   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
@@ -987,8 +993,9 @@ filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
 
   /**** start of non-PTY-crap ****/
   if (size > 0)
-    retval = ((str->allow_quit ? write_allowing_quit : write)
-             (str->fd, data, size));
+    retval = str->allow_quit ?
+      write_allowing_quit (str->fd, data, size) :
+      write (str->fd, data, size);
   else
     retval = 0;
   if (retval < 0 && errno_would_block_p (errno) && str->blocked_ok)
@@ -1011,8 +1018,10 @@ filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
         out for EWOULDBLOCK. */
       if (str->chars_sans_newline >= str->pty_max_bytes)
        {
-         ssize_t retval2 = ((str->allow_quit ? write_allowing_quit : write)
-                            (str->fd, &str->eof_char, 1));
+         ssize_t retval2 = str->allow_quit ?
+           write_allowing_quit (str->fd, &str->eof_char, 1) :
+           write (str->fd, &str->eof_char, 1);
+
          if (retval2 > 0)
            str->chars_sans_newline = 0;
          else if (retval2 < 0)
@@ -1042,8 +1051,10 @@ filedesc_writer (Lstream *stream, CONST unsigned char *data, size_t size)
   if (need_newline)
     {
       Bufbyte nl = '\n';
-      ssize_t retval2 = ((str->allow_quit ? write_allowing_quit : write)
-                        (str->fd, &nl, 1));
+      ssize_t retval2 = str->allow_quit ?
+       write_allowing_quit (str->fd, &nl, 1) :
+       write (str->fd, &nl, 1);
+
       if (retval2 > 0)
         {
           str->chars_sans_newline = 0;
@@ -1233,7 +1244,7 @@ lisp_string_marker (Lisp_Object stream)
 
 struct fixed_buffer_stream
 {
-  CONST unsigned char *inbuf;
+  const unsigned char *inbuf;
   unsigned char *outbuf;
   size_t size;
   size_t offset;
@@ -1243,24 +1254,24 @@ DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer,
                               sizeof (struct fixed_buffer_stream));
 
 Lisp_Object
-make_fixed_buffer_input_stream (CONST unsigned char *buf, size_t size)
+make_fixed_buffer_input_stream (const void *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 = buf;
+  str->inbuf = (const unsigned char *) buf;
   str->size = size;
   XSETLSTREAM (obj, lstr);
   return obj;
 }
 
 Lisp_Object
-make_fixed_buffer_output_stream (unsigned char *buf, size_t size)
+make_fixed_buffer_output_stream (void *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 = buf;
+  str->outbuf = (unsigned char *) buf;
   str->size = size;
   XSETLSTREAM (obj, lstr);
   return obj;
@@ -1277,7 +1288,7 @@ fixed_buffer_reader (Lstream *stream, unsigned char *data, size_t size)
 }
 
 static ssize_t
-fixed_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+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)
@@ -1300,7 +1311,7 @@ fixed_buffer_rewinder (Lstream *stream)
   return 0;
 }
 
-CONST unsigned char *
+const unsigned char *
 fixed_buffer_input_stream_ptr (Lstream *stream)
 {
   assert (stream->imp == lstream_fixed_buffer);
@@ -1339,7 +1350,7 @@ make_resizing_buffer_output_stream (void)
 }
 
 static ssize_t
-resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+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);
@@ -1401,7 +1412,7 @@ make_dynarr_output_stream (unsigned_char_dynarr *dyn)
 }
 
 static ssize_t
-dynarr_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+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);
@@ -1444,7 +1455,7 @@ DEFINE_LSTREAM_IMPLEMENTATION ("lisp-buffer", lstream_lisp_buffer,
 
 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;
@@ -1583,7 +1594,7 @@ lisp_buffer_reader (Lstream *stream, unsigned char *data, size_t size)
 }
 
 static ssize_t
-lisp_buffer_writer (Lstream *stream, CONST unsigned char *data, size_t size)
+lisp_buffer_writer (Lstream *stream, const unsigned char *data, size_t size)
 {
   struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream);
   Bufpos pos;
@@ -1693,5 +1704,7 @@ reinit_vars_of_lstream (void)
 void
 vars_of_lstream (void)
 {
+  INIT_LRECORD_IMPLEMENTATION (lstream);
+
   reinit_vars_of_lstream ();
 }