* lib/dosname.h, lib/glthread/lock.c, lib/glthread/lock.h, lib/glthread/threadlib.c, lib/windows-mutex.c, lib/windows-mutex.h, lib/windows-once.c, lib/windows-once.h, lib/windows-recmutex.c, lib/windows-recmutex.h, lib/windows-rwlock.c, lib/windows-rwlock.h, m4/host-cpu-c-abi.m4, m4/lib-ld.m4, m4/lib-link.m4, m4/lib-prefix.m4, m4/lock.m4, m4/longlong.m4, m4/pthread_rwlock_rdlock.m4, tools/config.rpath: Delete. * lib/alloca.in.h, lib/argmatch.c, lib/argmatch.h, lib/arg-nonnull.h, lib/argp-ba.c, lib/argp-eexst.c, lib/argp-fmtstream.c, lib/argp-fmtstream.h, lib/argp-fs-xinl.c, lib/argp.h, lib/argp-help.c, lib/argp-namefrob.h, lib/argp-parse.c, lib/argp-pin.c, lib/argp-pv.c, lib/argp-pvh.c, lib/argp-xinl.c, lib/asnprintf.c, lib/basename-lgpl.c, lib/c-ctype.h, lib/c++defs.h, lib/cdefs.h, lib/closeout.c, lib/closeout.h, lib/close-stream.c, lib/c-strcasecmp.c, lib/c-strcaseeq.h, lib/c-strcase.h, lib/c-strncasecmp.c, lib/dirname.h, lib/dirname-lgpl.c, lib/errno.in.h, lib/error.c, lib/error.h, lib/exitfail.c, lib/exitfail.h, lib/fcntl.in.h, lib/filename.h, lib/float.c, lib/float+.h, lib/float.in.h, lib/fpending.c, lib/fpending.h, lib/getopt1.c, lib/getopt.c, lib/getopt-cdefs.in.h, lib/getopt-core.h, lib/getopt-ext.h, lib/getopt.in.h, lib/getopt_int.h, lib/getopt-pfx-core.h, lib/getopt-pfx-ext.h, lib/getprogname.c, lib/getprogname.h, lib/gettext.h, lib/gettimeofday.c, lib/hard-locale.c, lib/hard-locale.h, lib/intprops.h, lib/isatty.c, lib/itold.c, lib/libc-config.h, lib/limits.in.h, lib/localcharset.c, lib/localcharset.h, lib/localtime-buffer.c, lib/localtime-buffer.h, lib/lstat.c, lib/Makefile.am, lib/malloca.c, lib/malloca.h, lib/malloc.c, lib/mbrtowc.c, lib/mbsinit.c, lib/memchr.c, lib/memchr.valgrind, lib/mempcpy.c, lib/minmax.h, lib/mkdir.c, lib/mkstemp.c, lib/mkstemps.c, lib/msvc-inval.c, lib/msvc-inval.h, lib/msvc-nothrow.c, lib/msvc-nothrow.h, lib/_Noreturn.h, lib/pathmax.h, lib/printf-args.c, lib/printf-args.h, lib/printf-parse.c, lib/printf-parse.h, lib/progname.c, lib/progname.h, lib/quotearg.c, lib/quotearg.h, lib/quote.h, lib/rawmemchr.c, lib/rawmemchr.valgrind, lib/secure_getenv.c, lib/size_max.h, lib/sleep.c, lib/stat.c, lib/stat-time.h, lib/stat-w32.c, lib/stat-w32.h, lib/stdalign.in.h, lib/stdbool.in.h, lib/stddef.in.h, lib/stdint.in.h, lib/stdio-impl.h, lib/stdio.in.h, lib/stdlib.in.h, lib/stpcpy.c, lib/strcasecmp.c, lib/strchrnul.c, lib/strchrnul.valgrind, lib/streq.h, lib/strerror.c, lib/strerror-override.c, lib/strerror-override.h, lib/string.in.h, lib/strings.in.h, lib/stripslash.c, lib/strncasecmp.c, lib/strndup.c, lib/strnlen.c, lib/strverscmp.c, lib/sysexits.in.h, lib/sys_stat.in.h, lib/sys_time.in.h, lib/sys_types.in.h, lib/sys_wait.in.h, lib/tempname.c, lib/tempname.h, lib/time.in.h, lib/unistd.in.h, lib/vasnprintf.c, lib/vasnprintf.h, lib/verify.h, lib/vsnprintf.c, lib/warn-on-use.h, lib/wchar.in.h, lib/wctype.in.h, lib/windows-initguard.h, lib/xalloc-die.c, lib/xalloc.h, lib/xalloc-oversized.h, lib/xmalloc.c, lib/xsize.h, m4/00gnulib.m4, m4/absolute-header.m4, m4/alloca.m4, m4/argp.m4, m4/codeset.m4, m4/dirname.m4, m4/double-slash-root.m4, m4/eealloc.m4, m4/errno_h.m4, m4/error.m4, m4/exponentd.m4, m4/extensions.m4, m4/extern-inline.m4, m4/fcntl_h.m4, m4/fcntl-o.m4, m4/float_h.m4, m4/fpending.m4, m4/getopt.m4, m4/getprogname.m4, m4/gettimeofday.m4, m4/gnulib-cache.m4, m4/gnulib-common.m4, m4/gnulib-comp.m4, m4/gnulib-tool.m4, m4/include_next.m4, m4/__inline.m4, m4/intmax_t.m4, m4/inttypes_h.m4, m4/isatty.m4, m4/largefile.m4, m4/limits-h.m4, m4/localcharset.m4, m4/locale-fr.m4, m4/locale-ja.m4, m4/locale-zh.m4, m4/localtime-buffer.m4, m4/lstat.m4, m4/malloca.m4, m4/malloc.m4, m4/math_h.m4, m4/mbrtowc.m4, m4/mbsinit.m4, m4/mbstate_t.m4, m4/memchr.m4, m4/mempcpy.m4, m4/minmax.m4, m4/mkdir.m4, m4/mkstemp.m4, m4/mkstemps.m4, m4/mmap-anon.m4, m4/msvc-inval.m4, m4/msvc-nothrow.m4, m4/multiarch.m4, m4/nocrash.m4, m4/off_t.m4, m4/pathmax.m4, m4/printf.m4, m4/quotearg.m4, m4/quote.m4, m4/rawmemchr.m4, m4/secure_getenv.m4, m4/size_max.m4, m4/sleep.m4, m4/ssize_t.m4, m4/stat.m4, m4/stat-time.m4, m4/stdalign.m4, m4/stdbool.m4, m4/stddef_h.m4, m4/std-gnu11.m4, m4/stdint_h.m4, m4/stdint.m4, m4/stdio_h.m4, m4/stdlib_h.m4, m4/stpcpy.m4, m4/strcase.m4, m4/strchrnul.m4, m4/strerror.m4, m4/string_h.m4, m4/strings_h.m4, m4/strndup.m4, m4/strnlen.m4, m4/strverscmp.m4, m4/sysexits.m4, m4/sys_socket_h.m4, m4/sys_stat_h.m4, m4/sys_time_h.m4, m4/sys_types_h.m4, m4/sys_wait_h.m4, m4/tempname.m4, m4/threadlib.m4, m4/time_h.m4, m4/unistd_h.m4, m4/vasnprintf.m4, m4/vsnprintf.m4, m4/warn-on-use.m4, m4/wchar_h.m4, m4/wchar_t.m4, m4/wctype_h.m4, m4/wint_t.m4, m4/xalloc.m4, m4/xsize.m4: Update. * lib/inttypes.in.h, lib/lc-charset-dispatch.c, lib/lc-charset-dispatch.h, lib/locale.in.h, lib/mbrtowc-impl.h, lib/mbrtowc-impl-utf8.h, lib/mbtowc-lock.c, lib/mbtowc-lock.h, lib/setlocale-lock.c, lib/setlocale_null.c, lib/setlocale_null.h, m4/inttypes.m4, m4/locale_h.m4, m4/setlocale_null.m4, m4/visibility.m4, m4/zzgnulib.m4: New files.
434 lines
13 KiB
C
434 lines
13 KiB
C
/* Word-wrapping and line-truncating streams
|
||
Copyright (C) 1997-2020 Free Software Foundation, Inc.
|
||
This file is part of the GNU C Library.
|
||
Written by Miles Bader <miles@gnu.ai.mit.edu>.
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU General Public License as published by
|
||
the Free Software Foundation; either version 3 of the License, or
|
||
(at your option) any later version.
|
||
|
||
This program 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 General Public License for more details.
|
||
|
||
You should have received a copy of the GNU General Public License
|
||
along with this program. If not, see <https://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>
|
||
#endif
|
||
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <errno.h>
|
||
#include <stdarg.h>
|
||
#include <ctype.h>
|
||
|
||
#include "argp-fmtstream.h"
|
||
#include "argp-namefrob.h"
|
||
|
||
#ifndef ARGP_FMTSTREAM_USE_LINEWRAP
|
||
|
||
#ifndef isblank
|
||
#define isblank(ch) ((ch)==' ' || (ch)=='\t')
|
||
#endif
|
||
|
||
#ifdef _LIBC
|
||
# include <wchar.h>
|
||
# include <libio/libioP.h>
|
||
# define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
|
||
#endif
|
||
|
||
#define INIT_BUF_SIZE 200
|
||
#define PRINTF_SIZE_GUESS 150
|
||
|
||
/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
|
||
written on it with LMARGIN spaces and limits them to RMARGIN columns
|
||
total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
|
||
replacing the whitespace before them with a newline and WMARGIN spaces.
|
||
Otherwise, chars beyond RMARGIN are simply dropped until a newline.
|
||
Returns NULL if there was an error. */
|
||
argp_fmtstream_t
|
||
__argp_make_fmtstream (FILE *stream,
|
||
size_t lmargin, size_t rmargin, ssize_t wmargin)
|
||
{
|
||
argp_fmtstream_t fs;
|
||
|
||
fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
|
||
if (fs != NULL)
|
||
{
|
||
fs->stream = stream;
|
||
|
||
fs->lmargin = lmargin;
|
||
fs->rmargin = rmargin;
|
||
fs->wmargin = wmargin;
|
||
fs->point_col = 0;
|
||
fs->point_offs = 0;
|
||
|
||
fs->buf = (char *) malloc (INIT_BUF_SIZE);
|
||
if (! fs->buf)
|
||
{
|
||
free (fs);
|
||
fs = 0;
|
||
}
|
||
else
|
||
{
|
||
fs->p = fs->buf;
|
||
fs->end = fs->buf + INIT_BUF_SIZE;
|
||
}
|
||
}
|
||
|
||
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
|
||
__argp_fmtstream_free (argp_fmtstream_t fs)
|
||
{
|
||
__argp_fmtstream_update (fs);
|
||
if (fs->p > fs->buf)
|
||
{
|
||
#ifdef _LIBC
|
||
__fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
|
||
#else
|
||
fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
|
||
#endif
|
||
}
|
||
free (fs->buf);
|
||
free (fs);
|
||
}
|
||
#if 0
|
||
/* Not exported. */
|
||
#ifdef weak_alias
|
||
weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
|
||
#endif
|
||
#endif
|
||
|
||
/* 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. */
|
||
void
|
||
__argp_fmtstream_update (argp_fmtstream_t fs)
|
||
{
|
||
char *buf, *nl;
|
||
size_t len;
|
||
|
||
/* Scan the buffer for newlines. */
|
||
buf = fs->buf + fs->point_offs;
|
||
while (buf < fs->p)
|
||
{
|
||
size_t r;
|
||
|
||
if (fs->point_col == 0 && fs->lmargin != 0)
|
||
{
|
||
/* We are starting a new line. Print spaces to the left margin. */
|
||
const size_t pad = fs->lmargin;
|
||
if (fs->p + pad < fs->end)
|
||
{
|
||
/* We can fit in them in the buffer by moving the
|
||
buffer text up and filling in the beginning. */
|
||
memmove (buf + pad, buf, fs->p - buf);
|
||
fs->p += pad; /* Compensate for bigger buffer. */
|
||
memset (buf, ' ', pad); /* Fill in the spaces. */
|
||
buf += pad; /* Don't bother searching them. */
|
||
}
|
||
else
|
||
{
|
||
/* No buffer space for spaces. Must flush. */
|
||
size_t i;
|
||
for (i = 0; i < pad; i++)
|
||
{
|
||
#ifdef _LIBC
|
||
if (_IO_fwide (fs->stream, 0) > 0)
|
||
putwc_unlocked (L' ', fs->stream);
|
||
else
|
||
#endif
|
||
putc_unlocked (' ', fs->stream);
|
||
}
|
||
}
|
||
fs->point_col = pad;
|
||
}
|
||
|
||
len = fs->p - buf;
|
||
nl = memchr (buf, '\n', len);
|
||
|
||
if (fs->point_col < 0)
|
||
fs->point_col = 0;
|
||
|
||
if (!nl)
|
||
{
|
||
/* The buffer ends in a partial line. */
|
||
|
||
if (fs->point_col + len < fs->rmargin)
|
||
{
|
||
/* The remaining buffer text is a partial line and fits
|
||
within the maximum line width. Advance point for the
|
||
characters to be written and stop scanning. */
|
||
fs->point_col += len;
|
||
break;
|
||
}
|
||
else
|
||
/* Set the end-of-line pointer for the code below to
|
||
the end of the buffer. */
|
||
nl = fs->p;
|
||
}
|
||
else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
|
||
{
|
||
/* The buffer contains a full line that fits within the maximum
|
||
line width. Reset point and scan the next line. */
|
||
fs->point_col = 0;
|
||
buf = nl + 1;
|
||
continue;
|
||
}
|
||
|
||
/* This line is too long. */
|
||
r = fs->rmargin - 1;
|
||
|
||
if (fs->wmargin < 0)
|
||
{
|
||
/* Truncate the line by overwriting the excess with the
|
||
newline and anything after it in the buffer. */
|
||
if (nl < fs->p)
|
||
{
|
||
memmove (buf + (r - fs->point_col), nl, fs->p - nl);
|
||
fs->p -= buf + (r - fs->point_col) - nl;
|
||
/* Reset point for the next line and start scanning it. */
|
||
fs->point_col = 0;
|
||
buf += r + 1; /* Skip full line plus \n. */
|
||
}
|
||
else
|
||
{
|
||
/* The buffer ends with a partial line that is beyond the
|
||
maximum line width. Advance point for the characters
|
||
written, and discard those past the max from the buffer. */
|
||
fs->point_col += len;
|
||
fs->p -= fs->point_col - r;
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
/* Do word wrap. Go to the column just past the maximum line
|
||
width and scan back for the beginning of the word there.
|
||
Then insert a line break. */
|
||
|
||
char *p, *nextline;
|
||
int i;
|
||
|
||
p = buf + (r + 1 - fs->point_col);
|
||
while (p >= buf && !isblank ((unsigned char) *p))
|
||
--p;
|
||
nextline = p + 1; /* This will begin the next line. */
|
||
|
||
if (nextline > buf)
|
||
{
|
||
/* Swallow separating blanks. */
|
||
if (p >= buf)
|
||
do
|
||
--p;
|
||
while (p >= buf && isblank ((unsigned char) *p));
|
||
nl = p + 1; /* The newline will replace the first blank. */
|
||
}
|
||
else
|
||
{
|
||
/* A single word that is greater than the maximum line width.
|
||
Oh well. Put it on an overlong line by itself. */
|
||
p = buf + (r + 1 - fs->point_col);
|
||
/* Find the end of the long word. */
|
||
if (p < nl)
|
||
do
|
||
++p;
|
||
while (p < nl && !isblank ((unsigned char) *p));
|
||
if (p == nl)
|
||
{
|
||
/* It already ends a line. No fussing required. */
|
||
fs->point_col = 0;
|
||
buf = nl + 1;
|
||
continue;
|
||
}
|
||
/* We will move the newline to replace the first blank. */
|
||
nl = p;
|
||
/* Swallow separating blanks. */
|
||
do
|
||
++p;
|
||
while (isblank ((unsigned char) *p));
|
||
/* The next line will start here. */
|
||
nextline = p;
|
||
}
|
||
|
||
/* Note: There are a bunch of tests below for
|
||
NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall
|
||
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)
|
||
&& 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. */
|
||
*nl++ = '\n';
|
||
|
||
if (nextline - nl >= fs->wmargin
|
||
|| (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin))
|
||
/* Add blanks up to the wrap margin column. */
|
||
for (i = 0; i < fs->wmargin; ++i)
|
||
*nl++ = ' ';
|
||
else
|
||
for (i = 0; i < fs->wmargin; ++i)
|
||
#ifdef _LIBC
|
||
if (_IO_fwide (fs->stream, 0) > 0)
|
||
putwc_unlocked (L' ', fs->stream);
|
||
else
|
||
#endif
|
||
putc_unlocked (' ', fs->stream);
|
||
|
||
/* Copy the tail of the original buffer into the current buffer
|
||
position. */
|
||
if (nl < nextline)
|
||
memmove (nl, nextline, buf + len - nextline);
|
||
len -= nextline - buf;
|
||
|
||
/* Continue the scan on the remaining lines in the buffer. */
|
||
buf = nl;
|
||
|
||
/* Restore bufp to include all the remaining text. */
|
||
fs->p = nl + len;
|
||
|
||
/* Reset the counter of what has been output this line. If wmargin
|
||
is 0, we want to avoid the lmargin getting added, so we set
|
||
point_col to a magic value of -1 in that case. */
|
||
fs->point_col = fs->wmargin ? fs->wmargin : -1;
|
||
}
|
||
}
|
||
|
||
/* Remember that we've scanned as far as the end of the buffer. */
|
||
fs->point_offs = fs->p - fs->buf;
|
||
}
|
||
|
||
/* Ensure that FS has space for AMOUNT more bytes in its buffer, either by
|
||
growing the buffer, or by flushing it. True is returned iff we succeed. */
|
||
int
|
||
__argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
|
||
{
|
||
if ((size_t) (fs->end - fs->p) < amount)
|
||
{
|
||
ssize_t wrote;
|
||
|
||
/* 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;
|
||
fs->point_offs = 0;
|
||
}
|
||
else
|
||
{
|
||
fs->p -= wrote;
|
||
fs->point_offs -= wrote;
|
||
memmove (fs->buf, fs->buf + wrote, fs->p - fs->buf);
|
||
return 0;
|
||
}
|
||
|
||
if ((size_t) (fs->end - fs->buf) < amount)
|
||
/* Gotta grow the buffer. */
|
||
{
|
||
size_t old_size = fs->end - fs->buf;
|
||
size_t new_size = old_size + amount;
|
||
char *new_buf;
|
||
|
||
if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
|
||
{
|
||
__set_errno (ENOMEM);
|
||
return 0;
|
||
}
|
||
|
||
fs->buf = new_buf;
|
||
fs->end = new_buf + new_size;
|
||
fs->p = fs->buf;
|
||
}
|
||
}
|
||
|
||
return 1;
|
||
}
|
||
|
||
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
|
||
{
|
||
va_list args;
|
||
|
||
if (! __argp_fmtstream_ensure (fs, size_guess))
|
||
return -1;
|
||
|
||
va_start (args, fmt);
|
||
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 ((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 */
|