mirror of
git://sourceware.org/git/glibc.git
synced 2024-11-21 01:12:26 +08:00
50304ef057
1998-07-16 10:23 Ulrich Drepper <drepper@cygnus.com> * argp/argp-fmtstream.c: Unify names of used global functions. * argp/argp-help.c: Likewise. * assert/assert-perr.c: Likewise * assert/assert.c: Likewise * dirent/scandir.c: Likewise * dirent/scandir64.c: Likewise * dirent/versionsort.c: Likewise * dirent/versionsort64.c: Likewise * gmon/bb_exit_func.c: Likewise. * gmon/gmon.c: Likewise. * grp/initgroups.c: Likewise. * iconv/gconv_conf.c: Likewise. * inet/getnameinfo.c: Likewise. * inet/getnetgrent_r.c: Likewise. * inet/inet_ntoa.c: Likewise. * inet/rcmd.c: Likewise. * inet/rexec.c: Likewise. * inet/ruserpass.c: Likewise. * io/fts.c: Likewise. * io/ftw.c: Likewise. * io/ftw64.c: Likewise. * io/getdirname.c: Likewise. * io/getwd.c: Likewise. * io/lockf.c: Likewise. * libio/iofdopen.c: Likewise. * libio/iopopen.c: Likewise. * login/utmp_daemon.c: Likewise. * malloc/mtrace.c: Likewise. * malloc/obstack.c * misc/daemon.c: Likewise. * misc/efgcvt_r.c: Likewise. * misc/err.c: Likewise. * misc/error.c: Likewise. * misc/fstab.c: Likewise. * misc/getpass.c: Likewise. * misc/getttyent.c: Likewise. * misc/mntent_r.c: Likewise. * misc/search.h: Likewise. * misc/syslog.c: Likewise. * nscd/nscd_getgr_r.c: Likewise. * nscd/nscd_getpw_r.c: Likewise. * posix/getpgrp.c: Likewise. * posix/wordexp.c: Likewise. * pwd/fgetpwent_r.c: Likewise. * pwd/getpw.c: Likewise. * resolv/herror.c: Likewise. * resolv/res_init.c: Likewise. * shadow/fgetspent_r.c: Likewise. * shadow/lckpwdf.c: Likewise. * signal/sigrelse.c: Likewise. * stdio-common/asprintf.c: Likewise. * stdio-common/dprintf.c: Likewise. * stdio-common/getw.c: Likewise. * stdio-common/putw.c: Likewise. * stdio-common/snprintf.c: Likewise. * stdio-common/sprintf.c: Likewise. * stdio-common/sscanf.c: Likewise. * stdlib/lrand48_r.c: Likewise. * stdlib/mrand48_r.c: Likewise. * string/argz-replace.c: Likewise. * string/envz.c: Likewise. * sunrpc/auth_des.c: Likewise. * sunrpc/auth_unix.c: Likewise. * sunrpc/bindrsvprt.c: Likewise. * sunrpc/clnt_gen.c: Likewise. * sunrpc/clnt_perr.c: Likewise. * sunrpc/clnt_simp.c: Likewise. * sunrpc/clnt_tcp.c: Likewise. * sunrpc/clnt_udp.c: Likewise. * sunrpc/get_myaddr.c: Likewise. * sunrpc/key_call.c: Likewise. * sunrpc/netname.c: Likewise. * sunrpc/openchild.c: Likewise. * sunrpc/pmap_rmt.c: Likewise. * sunrpc/rpc_dtable.c: Likewise. * sunrpc/rtime.c: Likewise. * sunrpc/svc_run.c: Likewise. * sunrpc/svc_simple.c: Likewise. * sunrpc/svc_tcp.c: Likewise. * sunrpc/svc_udp.c: Likewise. * sunrpc/svcauth_des.c: Likewise. * sunrpc/xdr_array.c: Likewise. * sunrpc/xdr_rec.c: Likewise. * sunrpc/xdr_ref.c: Likewise. * sunrpc/xdr_stdio.c: Likewise. * sysdeps/generic/abort.c: Likewise. * sysdeps/generic/dl-sysdep.c: Likewise. * sysdeps/generic/fstatfs64.c: Likewise. * sysdeps/generic/ftruncate64.c: Likewise. * sysdeps/generic/getrlimit64.c: Likewise. * sysdeps/generic/glob.c: Likewise. * sysdeps/generic/prof-freq.c: Likewise. * sysdeps/generic/putenv.c: Likewise. * sysdeps/generic/statfs64.c: Likewise. * sysdeps/generic/ttyname_r.c: Likewise. * sysdeps/generic/utmp_file.c: Likewise. * sysdeps/generic/vlimit.c: Likewise. * sysdeps/generic/vtimes.c: Likewise. * sysdeps/posix/cuserid.c: Likewise. * sysdeps/posix/euidaccess.c: Likewise. * sysdeps/posix/mkstemp.c: Likewise. * sysdeps/posix/mktemp.c: Likewise. * sysdeps/posix/pread.c: Likewise. * sysdeps/posix/pread64.c: Likewise. * sysdeps/posix/profil.c: Likewise. * sysdeps/posix/pwrite.c: Likewise. * sysdeps/posix/pwrite64.c: Likewise. * sysdeps/posix/sigblock.c: Likewise. * sysdeps/posix/sigpause.c: Likewise. * sysdeps/posix/ttyname.c: Likewise. * sysdeps/posix/ttyname_r.c: Likewise. * sysdeps/posix/waitid.c: Likewise. * sysdeps/unix/getlogin_r.c: Likewise. * sysdeps/unix/grantpt.c: Likewise. * sysdeps/unix/rewinddir.c: Likewise. * sysdeps/unix/sysv/linux/gethostid.c: Likewise. * sysdeps/unix/sysv/linux/getpt.c: Likewise. * sysdeps/unix/sysv/linux/if_index.c: Likewise. * sysdeps/unix/sysv/linux/ptsname.c: Likewise. * sysdeps/unix/sysv/linux/sendmsg.c: Likewise. * sysdeps/unix/sysv/linux/statvfs.c: Likewise. * sysdeps/unix/sysv/linux/ttyname.c: Likewise. * sysdeps/unix/sysv/linux/ttyname_r.c: Likewise. * sysdeps/unix/sysv/linux/ulimit.c: Likewise. * sysdeps/unix/sysv/linux/unlockpt.c: Likewise. * sysvipc/sys/shm.h: Likewise. * time/ctime_r.c: Likewise. * time/strptime.c: Likewise. * wcsmbs/mbrlen.c: Likewise. * wcsmbs/wcsdup.c: Likewise. * wcsmbs/wcsxfrm.c: Likewise. * wctype/wcfuncs.c: Likewise. * sysdeps/unix/sysv/linux/i386/socker.S: Change to honor NO_WEAK_ALIAS. * sysdeps/unix/sysv/linux/accept.S: Don't generate __ name. * sysdeps/unix/sysv/linux/bind.S: Likewise. * sysdeps/unix/sysv/linux/getsockname.S: Likewise. * sysdeps/unix/sysv/linux/listen.S: Likewise. * sysdeps/unix/sysv/linux/recvfrom.S: Likewise. * sysdeps/unix/sysv/linux/sendto.S: Likewise. * sysdeps/unix/sysv/linux/setsockopt.S: Likewise. * grp/fgetgrent_r.c: Use explicit locking of the stream. * elf/Makefile (rtld-routines): Add dl-environ. * sysdeps/generic/dl-environ.c: New file. * libio/Makefile [REENTRANT] (routines): Add iofputs_u. * libio/Versions: Add fputs_unlocked. * libio/iofputs_u.c: New file. * libio/stdio.h: Add prototype for fputs_unlocked. * sunrpc/rpc/auth.h: Use __PMT instead of __P in type definitions. * sunrpc/rpc/clnt.h: Likewise. * sunrpc/rpc/pmap_clnt.h: Likewise. * sunrpc/rpc/svc.h: Likewise. * sunrpc/rpc/xdr.h: Likewise. * sysdeps/i386/memchr.S: Correct for more strict gas. * sysdeps/i386/fpu/bits/mathinline.h: Likewise. * sysdeps/libm-i387/i686/s_fdim.S: Likewise. * sysdeps/libm-i387/i686/s_fdimf.S: Likewise. * sysdeps/libm-i387/i686/s_fdiml.S: Likewise. 1998-07-15 Andreas Jaeger <aj@arthur.rhein-neckar.de> * configure.in: Change message for binutils version from 2.8.1.0.17->2.8.1.0.23. 1998-07-15 Ulrich Drepper <drepper@cygnus.com> * sysdeps/unix/sysv/sysv4/solaris2/sparc/sysdep.h: Define LOC. Patch by John Tobey <jtobey@banta-im.com>.
387 lines
10 KiB
C
387 lines
10 KiB
C
/* Word-wrapping and line-truncating streams
|
||
Copyright (C) 1997, 1998 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.
|
||
|
||
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.
|
||
|
||
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. */
|
||
|
||
/* 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
|
||
|
||
#if defined _LIBC && defined USE_IN_LIBIO
|
||
# 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 = malloc (sizeof (struct argp_fmtstream));
|
||
if (fs)
|
||
{
|
||
fs->stream = stream;
|
||
|
||
fs->lmargin = lmargin;
|
||
fs->rmargin = rmargin;
|
||
fs->wmargin = wmargin;
|
||
fs->point_col = 0;
|
||
fs->point_offs = 0;
|
||
|
||
fs->buf = 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;
|
||
}
|
||
#ifdef weak_alias
|
||
weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
|
||
#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)
|
||
fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
|
||
free (fs->buf);
|
||
free (fs);
|
||
}
|
||
#ifdef weak_alias
|
||
weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
|
||
#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++)
|
||
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 (*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 (*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. */
|
||
do
|
||
++p;
|
||
while (p < nl && !isblank (*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 (*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)
|
||
/* 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;
|
||
}
|
||
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)
|
||
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);
|
||
|
||
wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
|
||
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 new_size = fs->end - fs->buf + amount;
|
||
char *new_buf = realloc (fs->buf, new_size);
|
||
|
||
if (! new_buf)
|
||
{
|
||
__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 size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */
|
||
|
||
do
|
||
{
|
||
va_list args;
|
||
|
||
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);
|
||
va_end (args);
|
||
}
|
||
while (out == -1);
|
||
|
||
fs->p += out;
|
||
|
||
return out;
|
||
}
|
||
#ifdef weak_alias
|
||
weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
|
||
#endif
|
||
|
||
#endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */
|