2N/A/* Word-wrapping and line-truncating streams
2N/A Copyright (C) 1997-1999, 2001-2003, 2005, 2009-2010 Free Software
2N/A Foundation, Inc.
2N/A This file is part of the GNU C Library.
2N/A Written by Miles Bader <miles@gnu.ai.mit.edu>.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A/* This package emulates glibc `line_wrap_stream' semantics for systems that
2N/A don't have that. */
2N/A
2N/A#ifdef HAVE_CONFIG_H
2N/A# include <config.h>
2N/A#endif
2N/A
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <errno.h>
2N/A#include <stdarg.h>
2N/A#include <ctype.h>
2N/A
2N/A#include "argp-fmtstream.h"
2N/A#include "argp-namefrob.h"
2N/A
2N/A#ifndef ARGP_FMTSTREAM_USE_LINEWRAP
2N/A
2N/A#ifndef isblank
2N/A#define isblank(ch) ((ch)==' ' || (ch)=='\t')
2N/A#endif
2N/A
2N/A#if defined _LIBC && defined USE_IN_LIBIO
2N/A# include <wchar.h>
2N/A# include <libio/libioP.h>
2N/A# define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
2N/A#endif
2N/A
2N/A#define INIT_BUF_SIZE 200
2N/A#define PRINTF_SIZE_GUESS 150
2N/A
2N/A/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
2N/A written on it with LMARGIN spaces and limits them to RMARGIN columns
2N/A total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
2N/A replacing the whitespace before them with a newline and WMARGIN spaces.
2N/A Otherwise, chars beyond RMARGIN are simply dropped until a newline.
2N/A Returns NULL if there was an error. */
2N/Aargp_fmtstream_t
2N/A__argp_make_fmtstream (FILE *stream,
2N/A size_t lmargin, size_t rmargin, ssize_t wmargin)
2N/A{
2N/A argp_fmtstream_t fs;
2N/A
2N/A fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
2N/A if (fs != NULL)
2N/A {
2N/A fs->stream = stream;
2N/A
2N/A fs->lmargin = lmargin;
2N/A fs->rmargin = rmargin;
2N/A fs->wmargin = wmargin;
2N/A fs->point_col = 0;
2N/A fs->point_offs = 0;
2N/A
2N/A fs->buf = (char *) malloc (INIT_BUF_SIZE);
2N/A if (! fs->buf)
2N/A {
2N/A free (fs);
2N/A fs = 0;
2N/A }
2N/A else
2N/A {
2N/A fs->p = fs->buf;
2N/A fs->end = fs->buf + INIT_BUF_SIZE;
2N/A }
2N/A }
2N/A
2N/A return fs;
2N/A}
2N/A#if 0
2N/A/* Not exported. */
2N/A#ifdef weak_alias
2N/Aweak_alias (__argp_make_fmtstream, argp_make_fmtstream)
2N/A#endif
2N/A#endif
2N/A
2N/A/* Flush FS to its stream, and free it (but don't close the stream). */
2N/Avoid
2N/A__argp_fmtstream_free (argp_fmtstream_t fs)
2N/A{
2N/A __argp_fmtstream_update (fs);
2N/A if (fs->p > fs->buf)
2N/A {
2N/A#ifdef USE_IN_LIBIO
2N/A __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
2N/A#else
2N/A fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
2N/A#endif
2N/A }
2N/A free (fs->buf);
2N/A free (fs);
2N/A}
2N/A#if 0
2N/A/* Not exported. */
2N/A#ifdef weak_alias
2N/Aweak_alias (__argp_fmtstream_free, argp_fmtstream_free)
2N/A#endif
2N/A#endif
2N/A
2N/A/* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
2N/A end of its buffer. This code is mostly from glibc stdio/linewrap.c. */
2N/Avoid
2N/A__argp_fmtstream_update (argp_fmtstream_t fs)
2N/A{
2N/A char *buf, *nl;
2N/A size_t len;
2N/A
2N/A /* Scan the buffer for newlines. */
2N/A buf = fs->buf + fs->point_offs;
2N/A while (buf < fs->p)
2N/A {
2N/A size_t r;
2N/A
2N/A if (fs->point_col == 0 && fs->lmargin != 0)
2N/A {
2N/A /* We are starting a new line. Print spaces to the left margin. */
2N/A const size_t pad = fs->lmargin;
2N/A if (fs->p + pad < fs->end)
2N/A {
2N/A /* We can fit in them in the buffer by moving the
2N/A buffer text up and filling in the beginning. */
2N/A memmove (buf + pad, buf, fs->p - buf);
2N/A fs->p += pad; /* Compensate for bigger buffer. */
2N/A memset (buf, ' ', pad); /* Fill in the spaces. */
2N/A buf += pad; /* Don't bother searching them. */
2N/A }
2N/A else
2N/A {
2N/A /* No buffer space for spaces. Must flush. */
2N/A size_t i;
2N/A for (i = 0; i < pad; i++)
2N/A {
2N/A#ifdef USE_IN_LIBIO
2N/A if (_IO_fwide (fs->stream, 0) > 0)
2N/A putwc_unlocked (L' ', fs->stream);
2N/A else
2N/A#endif
2N/A putc_unlocked (' ', fs->stream);
2N/A }
2N/A }
2N/A fs->point_col = pad;
2N/A }
2N/A
2N/A len = fs->p - buf;
2N/A nl = memchr (buf, '\n', len);
2N/A
2N/A if (fs->point_col < 0)
2N/A fs->point_col = 0;
2N/A
2N/A if (!nl)
2N/A {
2N/A /* The buffer ends in a partial line. */
2N/A
2N/A if (fs->point_col + len < fs->rmargin)
2N/A {
2N/A /* The remaining buffer text is a partial line and fits
2N/A within the maximum line width. Advance point for the
2N/A characters to be written and stop scanning. */
2N/A fs->point_col += len;
2N/A break;
2N/A }
2N/A else
2N/A /* Set the end-of-line pointer for the code below to
2N/A the end of the buffer. */
2N/A nl = fs->p;
2N/A }
2N/A else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
2N/A {
2N/A /* The buffer contains a full line that fits within the maximum
2N/A line width. Reset point and scan the next line. */
2N/A fs->point_col = 0;
2N/A buf = nl + 1;
2N/A continue;
2N/A }
2N/A
2N/A /* This line is too long. */
2N/A r = fs->rmargin - 1;
2N/A
2N/A if (fs->wmargin < 0)
2N/A {
2N/A /* Truncate the line by overwriting the excess with the
2N/A newline and anything after it in the buffer. */
2N/A if (nl < fs->p)
2N/A {
2N/A memmove (buf + (r - fs->point_col), nl, fs->p - nl);
2N/A fs->p -= buf + (r - fs->point_col) - nl;
2N/A /* Reset point for the next line and start scanning it. */
2N/A fs->point_col = 0;
2N/A buf += r + 1; /* Skip full line plus \n. */
2N/A }
2N/A else
2N/A {
2N/A /* The buffer ends with a partial line that is beyond the
2N/A maximum line width. Advance point for the characters
2N/A written, and discard those past the max from the buffer. */
2N/A fs->point_col += len;
2N/A fs->p -= fs->point_col - r;
2N/A break;
2N/A }
2N/A }
2N/A else
2N/A {
2N/A /* Do word wrap. Go to the column just past the maximum line
2N/A width and scan back for the beginning of the word there.
2N/A Then insert a line break. */
2N/A
2N/A char *p, *nextline;
2N/A int i;
2N/A
2N/A p = buf + (r + 1 - fs->point_col);
2N/A while (p >= buf && !isblank ((unsigned char) *p))
2N/A --p;
2N/A nextline = p + 1; /* This will begin the next line. */
2N/A
2N/A if (nextline > buf)
2N/A {
2N/A /* Swallow separating blanks. */
2N/A if (p >= buf)
2N/A do
2N/A --p;
2N/A while (p >= buf && isblank ((unsigned char) *p));
2N/A nl = p + 1; /* The newline will replace the first blank. */
2N/A }
2N/A else
2N/A {
2N/A /* A single word that is greater than the maximum line width.
2N/A Oh well. Put it on an overlong line by itself. */
2N/A p = buf + (r + 1 - fs->point_col);
2N/A /* Find the end of the long word. */
2N/A if (p < nl)
2N/A do
2N/A ++p;
2N/A while (p < nl && !isblank ((unsigned char) *p));
2N/A if (p == nl)
2N/A {
2N/A /* It already ends a line. No fussing required. */
2N/A fs->point_col = 0;
2N/A buf = nl + 1;
2N/A continue;
2N/A }
2N/A /* We will move the newline to replace the first blank. */
2N/A nl = p;
2N/A /* Swallow separating blanks. */
2N/A do
2N/A ++p;
2N/A while (isblank ((unsigned char) *p));
2N/A /* The next line will start here. */
2N/A nextline = p;
2N/A }
2N/A
2N/A /* Note: There are a bunch of tests below for
2N/A NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall
2N/A at the end of the buffer, and NEXTLINE is in fact empty (and so
2N/A we need not be careful to maintain its contents). */
2N/A
2N/A if ((nextline == buf + len + 1
2N/A ? fs->end - nl < fs->wmargin + 1
2N/A : nextline - (nl + 1) < fs->wmargin)
2N/A && fs->p > nextline)
2N/A {
2N/A /* The margin needs more blanks than we removed. */
2N/A if (fs->end - fs->p > fs->wmargin + 1)
2N/A /* Make some space for them. */
2N/A {
2N/A size_t mv = fs->p - nextline;
2N/A memmove (nl + 1 + fs->wmargin, nextline, mv);
2N/A nextline = nl + 1 + fs->wmargin;
2N/A len = nextline + mv - buf;
2N/A *nl++ = '\n';
2N/A }
2N/A else
2N/A /* Output the first line so we can use the space. */
2N/A {
2N/A#ifdef _LIBC
2N/A __fxprintf (fs->stream, "%.*s\n",
2N/A (int) (nl - fs->buf), fs->buf);
2N/A#else
2N/A if (nl > fs->buf)
2N/A fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
2N/A putc_unlocked ('\n', fs->stream);
2N/A#endif
2N/A
2N/A len += buf - fs->buf;
2N/A nl = buf = fs->buf;
2N/A }
2N/A }
2N/A else
2N/A /* We can fit the newline and blanks in before
2N/A the next word. */
2N/A *nl++ = '\n';
2N/A
2N/A if (nextline - nl >= fs->wmargin
2N/A || (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin))
2N/A /* Add blanks up to the wrap margin column. */
2N/A for (i = 0; i < fs->wmargin; ++i)
2N/A *nl++ = ' ';
2N/A else
2N/A for (i = 0; i < fs->wmargin; ++i)
2N/A#ifdef USE_IN_LIBIO
2N/A if (_IO_fwide (fs->stream, 0) > 0)
2N/A putwc_unlocked (L' ', fs->stream);
2N/A else
2N/A#endif
2N/A putc_unlocked (' ', fs->stream);
2N/A
2N/A /* Copy the tail of the original buffer into the current buffer
2N/A position. */
2N/A if (nl < nextline)
2N/A memmove (nl, nextline, buf + len - nextline);
2N/A len -= nextline - buf;
2N/A
2N/A /* Continue the scan on the remaining lines in the buffer. */
2N/A buf = nl;
2N/A
2N/A /* Restore bufp to include all the remaining text. */
2N/A fs->p = nl + len;
2N/A
2N/A /* Reset the counter of what has been output this line. If wmargin
2N/A is 0, we want to avoid the lmargin getting added, so we set
2N/A point_col to a magic value of -1 in that case. */
2N/A fs->point_col = fs->wmargin ? fs->wmargin : -1;
2N/A }
2N/A }
2N/A
2N/A /* Remember that we've scanned as far as the end of the buffer. */
2N/A fs->point_offs = fs->p - fs->buf;
2N/A}
2N/A
2N/A/* Ensure that FS has space for AMOUNT more bytes in its buffer, either by
2N/A growing the buffer, or by flushing it. True is returned iff we succeed. */
2N/Aint
2N/A__argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
2N/A{
2N/A if ((size_t) (fs->end - fs->p) < amount)
2N/A {
2N/A ssize_t wrote;
2N/A
2N/A /* Flush FS's buffer. */
2N/A __argp_fmtstream_update (fs);
2N/A
2N/A#ifdef _LIBC
2N/A __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
2N/A wrote = fs->p - fs->buf;
2N/A#else
2N/A wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
2N/A#endif
2N/A if (wrote == fs->p - fs->buf)
2N/A {
2N/A fs->p = fs->buf;
2N/A fs->point_offs = 0;
2N/A }
2N/A else
2N/A {
2N/A fs->p -= wrote;
2N/A fs->point_offs -= wrote;
2N/A memmove (fs->buf, fs->buf + wrote, fs->p - fs->buf);
2N/A return 0;
2N/A }
2N/A
2N/A if ((size_t) (fs->end - fs->buf) < amount)
2N/A /* Gotta grow the buffer. */
2N/A {
2N/A size_t old_size = fs->end - fs->buf;
2N/A size_t new_size = old_size + amount;
2N/A char *new_buf;
2N/A
2N/A if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
2N/A {
2N/A __set_errno (ENOMEM);
2N/A return 0;
2N/A }
2N/A
2N/A fs->buf = new_buf;
2N/A fs->end = new_buf + new_size;
2N/A fs->p = fs->buf;
2N/A }
2N/A }
2N/A
2N/A return 1;
2N/A}
2N/A
2N/Assize_t
2N/A__argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
2N/A{
2N/A int out;
2N/A size_t avail;
2N/A size_t size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */
2N/A
2N/A do
2N/A {
2N/A va_list args;
2N/A
2N/A if (! __argp_fmtstream_ensure (fs, size_guess))
2N/A return -1;
2N/A
2N/A va_start (args, fmt);
2N/A avail = fs->end - fs->p;
2N/A out = __vsnprintf (fs->p, avail, fmt, args);
2N/A va_end (args);
2N/A if ((size_t) out >= avail)
2N/A size_guess = out + 1;
2N/A }
2N/A while ((size_t) out >= avail);
2N/A
2N/A fs->p += out;
2N/A
2N/A return out;
2N/A}
2N/A#if 0
2N/A/* Not exported. */
2N/A#ifdef weak_alias
2N/Aweak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
2N/A#endif
2N/A#endif
2N/A
2N/A#endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */