]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - argp/argp-fmtstream.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / argp / argp-fmtstream.c
index 05e9e21f06f463f9849337e854f0b30d41349f6a..982db4e7c8690fe1623c3ed4dc403a75e41eec91 100644 (file)
@@ -1,28 +1,27 @@
 /* Word-wrapping and line-truncating streams
-   Copyright (C) 1997, 1998 Free Software Foundation, Inc.
+   Copyright (C) 1997-2016 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Written by Miles Bader <miles@gnu.ai.mit.edu>.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 /* This package emulates glibc `line_wrap_stream' semantics for systems that
    don't have that.  */
 
 #ifdef HAVE_CONFIG_H
-#include <config.h>
+# include <config.h>
 #endif
 
 #include <stdlib.h>
@@ -40,7 +39,8 @@
 #define isblank(ch) ((ch)==' ' || (ch)=='\t')
 #endif
 
-#if defined _LIBC && defined USE_IN_LIBIO
+#ifdef _LIBC
+# include <wchar.h>
 # include <libio/libioP.h>
 # define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
 #endif
@@ -58,8 +58,10 @@ argp_fmtstream_t
 __argp_make_fmtstream (FILE *stream,
                       size_t lmargin, size_t rmargin, ssize_t wmargin)
 {
-  argp_fmtstream_t fs = malloc (sizeof (struct argp_fmtstream));
-  if (fs)
+  argp_fmtstream_t fs;
+
+  fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
+  if (fs != NULL)
     {
       fs->stream = stream;
 
@@ -69,7 +71,7 @@ __argp_make_fmtstream (FILE *stream,
       fs->point_col = 0;
       fs->point_offs = 0;
 
-      fs->buf = malloc (INIT_BUF_SIZE);
+      fs->buf = (char *) malloc (INIT_BUF_SIZE);
       if (! fs->buf)
        {
          free (fs);
@@ -84,9 +86,12 @@ __argp_make_fmtstream (FILE *stream,
 
   return fs;
 }
+#if 0
+/* Not exported.  */
 #ifdef weak_alias
 weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
 #endif
+#endif
 
 /* Flush FS to its stream, and free it (but don't close the stream).  */
 void
@@ -94,13 +99,18 @@ __argp_fmtstream_free (argp_fmtstream_t fs)
 {
   __argp_fmtstream_update (fs);
   if (fs->p > fs->buf)
-    fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
+    {
+      __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
+    }
   free (fs->buf);
   free (fs);
 }
+#if 0
+/* Not exported.  */
 #ifdef weak_alias
 weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
 #endif
+#endif
 \f
 /* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
    end of its buffer.  This code is mostly from glibc stdio/linewrap.c.  */
@@ -134,7 +144,12 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              /* No buffer space for spaces.  Must flush.  */
              size_t i;
              for (i = 0; i < pad; i++)
-               putc_unlocked (' ', fs->stream);
+               {
+                 if (_IO_fwide (fs->stream, 0) > 0)
+                   putwc_unlocked (L' ', fs->stream);
+                 else
+                   putc_unlocked (' ', fs->stream);
+               }
            }
          fs->point_col = pad;
        }
@@ -213,10 +228,10 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
          if (nextline > buf)
            {
              /* Swallow separating blanks.  */
-             if (p > buf)
+             if (p >= buf)
                do
                  --p;
-               while (p > buf && isblank (*p));
+               while (p >= buf && isblank (*p));
              nl = p + 1;       /* The newline will replace the first blank. */
            }
          else
@@ -250,28 +265,37 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
             at the end of the buffer, and NEXTLINE is in fact empty (and so
             we need not be careful to maintain its contents).  */
 
-         if (nextline == buf + len + 1
-             ? fs->end - nl < fs->wmargin + 1
-             : nextline - (nl + 1) < fs->wmargin)
-           /* The margin needs more blanks than we removed.  */
-           if (fs->end - fs->p > fs->wmargin + 1)
-             /* Make some space for them.  */
-             {
-               size_t mv = fs->p - nextline;
-               memmove (nl + 1 + fs->wmargin, nextline, mv);
-               nextline = nl + 1 + fs->wmargin;
-               len = nextline + mv - buf;
-               *nl++ = '\n';
-             }
-           else
-             /* Output the first line so we can use the space.  */
-             {
-               if (nl > fs->buf)
-                 fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
-               putc_unlocked ('\n', fs->stream);
-               len += buf - fs->buf;
-               nl = buf = fs->buf;
-             }
+         if ((nextline == buf + len + 1
+              ? fs->end - nl < fs->wmargin + 1
+              : nextline - (nl + 1) < fs->wmargin)
+             && fs->p > nextline)
+           {
+             /* The margin needs more blanks than we removed.  */
+             if (fs->end - fs->p > fs->wmargin + 1)
+               /* Make some space for them.  */
+               {
+                 size_t mv = fs->p - nextline;
+                 memmove (nl + 1 + fs->wmargin, nextline, mv);
+                 nextline = nl + 1 + fs->wmargin;
+                 len = nextline + mv - buf;
+                 *nl++ = '\n';
+               }
+             else
+               /* Output the first line so we can use the space.  */
+               {
+#ifdef _LIBC
+                 __fxprintf (fs->stream, "%.*s\n",
+                             (int) (nl - fs->buf), fs->buf);
+#else
+                 if (nl > fs->buf)
+                   fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
+                 putc_unlocked ('\n', fs->stream);
+#endif
+
+                 len += buf - fs->buf;
+                 nl = buf = fs->buf;
+               }
+           }
          else
            /* We can fit the newline and blanks in before
               the next word.  */
@@ -284,7 +308,10 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              *nl++ = ' ';
          else
            for (i = 0; i < fs->wmargin; ++i)
-             putc_unlocked (' ', fs->stream);
+             if (_IO_fwide (fs->stream, 0) > 0)
+               putwc_unlocked (L' ', fs->stream);
+             else
+               putc_unlocked (' ', fs->stream);
 
          /* Copy the tail of the original buffer into the current buffer
             position.  */
@@ -321,7 +348,12 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
       /* Flush FS's buffer.  */
       __argp_fmtstream_update (fs);
 
+#ifdef _LIBC
+      __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
+      wrote = fs->p - fs->buf;
+#else
       wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
+#endif
       if (wrote == fs->p - fs->buf)
        {
          fs->p = fs->buf;
@@ -338,10 +370,11 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
       if ((size_t) (fs->end - fs->buf) < amount)
        /* Gotta grow the buffer.  */
        {
-         size_t new_size = fs->end - fs->buf + amount;
-         char *new_buf = realloc (fs->buf, new_size);
+         size_t old_size = fs->end - fs->buf;
+         size_t new_size = old_size + amount;
+         char *new_buf;
 
-         if (! new_buf)
+         if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
            {
              __set_errno (ENOMEM);
              return 0;
@@ -360,6 +393,7 @@ ssize_t
 __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
 {
   int out;
+  size_t avail;
   size_t size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */
 
   do
@@ -368,20 +402,25 @@ __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
 
       if (! __argp_fmtstream_ensure (fs, size_guess))
        return -1;
-      size_guess += size_guess;
 
       va_start (args, fmt);
-      out = __vsnprintf (fs->p, fs->end - fs->p, fmt, args);
+      avail = fs->end - fs->p;
+      out = __vsnprintf (fs->p, avail, fmt, args);
       va_end (args);
+      if ((size_t) out >= avail)
+       size_guess = out + 1;
     }
-  while (out == -1);
+  while ((size_t) out >= avail);
 
   fs->p += out;
 
   return out;
 }
+#if 0
+/* Not exported.  */
 #ifdef weak_alias
 weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
 #endif
+#endif
 
 #endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */