Blame argp/argp-fmtstream.h

Packit Service 82fcde
/* Word-wrapping and line-truncating streams.
Packit Service 82fcde
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Written by Miles Bader <miles@gnu.ai.mit.edu>.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/* This package emulates glibc `line_wrap_stream' semantics for systems that
Packit Service 82fcde
   don't have that.  If the system does have it, it is just a wrapper for
Packit Service 82fcde
   that.  This header file is only used internally while compiling argp, and
Packit Service 82fcde
   shouldn't be installed.  */
Packit Service 82fcde
Packit Service 82fcde
#ifndef _ARGP_FMTSTREAM_H
Packit Service 82fcde
#define _ARGP_FMTSTREAM_H
Packit Service 82fcde
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
#if defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H)
Packit Service 82fcde
/* line_wrap_stream is available, so use that.  */
Packit Service 82fcde
#define ARGP_FMTSTREAM_USE_LINEWRAP
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
#ifdef ARGP_FMTSTREAM_USE_LINEWRAP
Packit Service 82fcde
/* Just be a simple wrapper for line_wrap_stream; the semantics are
Packit Service 82fcde
   *slightly* different, as line_wrap_stream doesn't actually make a new
Packit Service 82fcde
   object, it just modifies the given stream (reversibly) to do
Packit Service 82fcde
   line-wrapping.  Since we control who uses this code, it doesn't matter.  */
Packit Service 82fcde
Packit Service 82fcde
#include <linewrap.h>
Packit Service 82fcde
Packit Service 82fcde
typedef FILE *argp_fmtstream_t;
Packit Service 82fcde
Packit Service 82fcde
#define argp_make_fmtstream line_wrap_stream
Packit Service 82fcde
#define __argp_make_fmtstream line_wrap_stream
Packit Service 82fcde
#define argp_fmtstream_free line_unwrap_stream
Packit Service 82fcde
#define __argp_fmtstream_free line_unwrap_stream
Packit Service 82fcde
Packit Service 82fcde
#define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
Packit Service 82fcde
#define argp_fmtstream_putc(fs,ch) putc(ch,fs)
Packit Service 82fcde
#define __argp_fmtstream_puts(fs,str) fputs(str,fs)
Packit Service 82fcde
#define argp_fmtstream_puts(fs,str) fputs(str,fs)
Packit Service 82fcde
#define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
Packit Service 82fcde
#define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
Packit Service 82fcde
#define __argp_fmtstream_printf fprintf
Packit Service 82fcde
#define argp_fmtstream_printf fprintf
Packit Service 82fcde
Packit Service 82fcde
#define __argp_fmtstream_lmargin line_wrap_lmargin
Packit Service 82fcde
#define argp_fmtstream_lmargin line_wrap_lmargin
Packit Service 82fcde
#define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
Packit Service 82fcde
#define argp_fmtstream_set_lmargin line_wrap_set_lmargin
Packit Service 82fcde
#define __argp_fmtstream_rmargin line_wrap_rmargin
Packit Service 82fcde
#define argp_fmtstream_rmargin line_wrap_rmargin
Packit Service 82fcde
#define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
Packit Service 82fcde
#define argp_fmtstream_set_rmargin line_wrap_set_rmargin
Packit Service 82fcde
#define __argp_fmtstream_wmargin line_wrap_wmargin
Packit Service 82fcde
#define argp_fmtstream_wmargin line_wrap_wmargin
Packit Service 82fcde
#define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
Packit Service 82fcde
#define argp_fmtstream_set_wmargin line_wrap_set_wmargin
Packit Service 82fcde
#define __argp_fmtstream_point line_wrap_point
Packit Service 82fcde
#define argp_fmtstream_point line_wrap_point
Packit Service 82fcde
Packit Service 82fcde
#else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
Packit Service 82fcde
/* Guess we have to define our own version.  */
Packit Service 82fcde
Packit Service 82fcde

Packit Service 82fcde
struct argp_fmtstream
Packit Service 82fcde
{
Packit Service 82fcde
  FILE *stream;			/* The stream we're outputting to.  */
Packit Service 82fcde
Packit Service 82fcde
  size_t lmargin, rmargin;	/* Left and right margins.  */
Packit Service 82fcde
  ssize_t wmargin;		/* Margin to wrap to, or -1 to truncate.  */
Packit Service 82fcde
Packit Service 82fcde
  /* Point in buffer to which we've processed for wrapping, but not output.  */
Packit Service 82fcde
  size_t point_offs;
Packit Service 82fcde
  /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin.  */
Packit Service 82fcde
  ssize_t point_col;
Packit Service 82fcde
Packit Service 82fcde
  char *buf;			/* Output buffer.  */
Packit Service 82fcde
  char *p;			/* Current end of text in BUF. */
Packit Service 82fcde
  char *end;			/* Absolute end of BUF.  */
Packit Service 82fcde
};
Packit Service 82fcde
Packit Service 82fcde
typedef struct argp_fmtstream *argp_fmtstream_t;
Packit Service 82fcde
Packit Service 82fcde
__BEGIN_DECLS
Packit Service 82fcde
Packit Service 82fcde
/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
Packit Service 82fcde
   written on it with LMARGIN spaces and limits them to RMARGIN columns
Packit Service 82fcde
   total.  If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
Packit Service 82fcde
   replacing the whitespace before them with a newline and WMARGIN spaces.
Packit Service 82fcde
   Otherwise, chars beyond RMARGIN are simply dropped until a newline.
Packit Service 82fcde
   Returns NULL if there was an error.  */
Packit Service 82fcde
extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
Packit Service 82fcde
					       size_t __lmargin,
Packit Service 82fcde
					       size_t __rmargin,
Packit Service 82fcde
					       ssize_t __wmargin);
Packit Service 82fcde
extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
Packit Service 82fcde
					     size_t __lmargin,
Packit Service 82fcde
					     size_t __rmargin,
Packit Service 82fcde
					     ssize_t __wmargin);
Packit Service 82fcde
Packit Service 82fcde
/* Flush __FS to its stream, and free it (but don't close the stream).  */
Packit Service 82fcde
extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
Packit Service 82fcde
extern void argp_fmtstream_free (argp_fmtstream_t __fs);
Packit Service 82fcde
Packit Service 82fcde
extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
Packit Service 82fcde
					const char *__fmt, ...)
Packit Service 82fcde
     __attribute__ ((__format__ (printf, 2, 3)));
Packit Service 82fcde
extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
Packit Service 82fcde
				      const char *__fmt, ...)
Packit Service 82fcde
     __attribute__ ((__format__ (printf, 2, 3)));
Packit Service 82fcde
Packit Service 82fcde
extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
Packit Service 82fcde
extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
Packit Service 82fcde
Packit Service 82fcde
extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
Packit Service 82fcde
extern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
Packit Service 82fcde
Packit Service 82fcde
extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
Packit Service 82fcde
				      const char *__str, size_t __len);
Packit Service 82fcde
extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
Packit Service 82fcde
				    const char *__str, size_t __len);
Packit Service 82fcde

Packit Service 82fcde
/* Access macros for various bits of state.  */
Packit Service 82fcde
#define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
Packit Service 82fcde
#define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
Packit Service 82fcde
#define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
Packit Service 82fcde
#define __argp_fmtstream_lmargin argp_fmtstream_lmargin
Packit Service 82fcde
#define __argp_fmtstream_rmargin argp_fmtstream_rmargin
Packit Service 82fcde
#define __argp_fmtstream_wmargin argp_fmtstream_wmargin
Packit Service 82fcde
Packit Service 82fcde
/* Set __FS's left margin to LMARGIN and return the old value.  */
Packit Service 82fcde
extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
Packit Service 82fcde
					  size_t __lmargin);
Packit Service 82fcde
extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
Packit Service 82fcde
					    size_t __lmargin);
Packit Service 82fcde
Packit Service 82fcde
/* Set __FS's right margin to __RMARGIN and return the old value.  */
Packit Service 82fcde
extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
Packit Service 82fcde
					  size_t __rmargin);
Packit Service 82fcde
extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
Packit Service 82fcde
					    size_t __rmargin);
Packit Service 82fcde
Packit Service 82fcde
/* Set __FS's wrap margin to __WMARGIN and return the old value.  */
Packit Service 82fcde
extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
Packit Service 82fcde
					  size_t __wmargin);
Packit Service 82fcde
extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
Packit Service 82fcde
					    size_t __wmargin);
Packit Service 82fcde
Packit Service 82fcde
/* Return the column number of the current output point in __FS.  */
Packit Service 82fcde
extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
Packit Service 82fcde
extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
Packit Service 82fcde
Packit Service 82fcde
/* Internal routines.  */
Packit Service 82fcde
extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
Packit Service 82fcde
extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
Packit Service 82fcde
extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
Packit Service 82fcde
extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
Packit Service 82fcde

Packit Service 82fcde
#ifdef __OPTIMIZE__
Packit Service 82fcde
/* Inline versions of above routines.  */
Packit Service 82fcde
Packit Service 82fcde
#if !_LIBC
Packit Service 82fcde
#define __argp_fmtstream_putc argp_fmtstream_putc
Packit Service 82fcde
#define __argp_fmtstream_puts argp_fmtstream_puts
Packit Service 82fcde
#define __argp_fmtstream_write argp_fmtstream_write
Packit Service 82fcde
#define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
Packit Service 82fcde
#define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
Packit Service 82fcde
#define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
Packit Service 82fcde
#define __argp_fmtstream_point argp_fmtstream_point
Packit Service 82fcde
#define __argp_fmtstream_update _argp_fmtstream_update
Packit Service 82fcde
#define __argp_fmtstream_ensure _argp_fmtstream_ensure
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
#ifndef ARGP_FS_EI
Packit Service 82fcde
#define ARGP_FS_EI extern inline
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
ARGP_FS_EI size_t
Packit Service 82fcde
__argp_fmtstream_write (argp_fmtstream_t __fs, const char *__str, size_t __len)
Packit Service 82fcde
{
Packit Service 82fcde
  if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
Packit Service 82fcde
    {
Packit Service 82fcde
      memcpy (__fs->p, __str, __len);
Packit Service 82fcde
      __fs->p += __len;
Packit Service 82fcde
      return __len;
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
ARGP_FS_EI int
Packit Service 82fcde
__argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t __len = strlen (__str);
Packit Service 82fcde
  if (__len)
Packit Service 82fcde
    {
Packit Service 82fcde
      size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
Packit Service 82fcde
      return __wrote == __len ? 0 : -1;
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
ARGP_FS_EI int
Packit Service 82fcde
__argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
Packit Service 82fcde
{
Packit Service 82fcde
  if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
Packit Service 82fcde
    return *__fs->p++ = __ch;
Packit Service 82fcde
  else
Packit Service 82fcde
    return EOF;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Set __FS's left margin to __LMARGIN and return the old value.  */
Packit Service 82fcde
ARGP_FS_EI size_t
Packit Service 82fcde
__argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t __old;
Packit Service 82fcde
  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
Packit Service 82fcde
    __argp_fmtstream_update (__fs);
Packit Service 82fcde
  __old = __fs->lmargin;
Packit Service 82fcde
  __fs->lmargin = __lmargin;
Packit Service 82fcde
  return __old;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Set __FS's right margin to __RMARGIN and return the old value.  */
Packit Service 82fcde
ARGP_FS_EI size_t
Packit Service 82fcde
__argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t __old;
Packit Service 82fcde
  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
Packit Service 82fcde
    __argp_fmtstream_update (__fs);
Packit Service 82fcde
  __old = __fs->rmargin;
Packit Service 82fcde
  __fs->rmargin = __rmargin;
Packit Service 82fcde
  return __old;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Set FS's wrap margin to __WMARGIN and return the old value.  */
Packit Service 82fcde
ARGP_FS_EI size_t
Packit Service 82fcde
__argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t __old;
Packit Service 82fcde
  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
Packit Service 82fcde
    __argp_fmtstream_update (__fs);
Packit Service 82fcde
  __old = __fs->wmargin;
Packit Service 82fcde
  __fs->wmargin = __wmargin;
Packit Service 82fcde
  return __old;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Return the column number of the current output point in __FS.  */
Packit Service 82fcde
ARGP_FS_EI size_t
Packit Service 82fcde
__argp_fmtstream_point (argp_fmtstream_t __fs)
Packit Service 82fcde
{
Packit Service 82fcde
  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
Packit Service 82fcde
    __argp_fmtstream_update (__fs);
Packit Service 82fcde
  return __fs->point_col >= 0 ? __fs->point_col : 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#if !_LIBC
Packit Service 82fcde
#undef __argp_fmtstream_putc
Packit Service 82fcde
#undef __argp_fmtstream_puts
Packit Service 82fcde
#undef __argp_fmtstream_write
Packit Service 82fcde
#undef __argp_fmtstream_set_lmargin
Packit Service 82fcde
#undef __argp_fmtstream_set_rmargin
Packit Service 82fcde
#undef __argp_fmtstream_set_wmargin
Packit Service 82fcde
#undef __argp_fmtstream_point
Packit Service 82fcde
#undef __argp_fmtstream_update
Packit Service 82fcde
#undef __argp_fmtstream_ensure
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
#endif /* __OPTIMIZE__ */
Packit Service 82fcde
Packit Service 82fcde
__END_DECLS
Packit Service 82fcde
Packit Service 82fcde
#endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
Packit Service 82fcde
Packit Service 82fcde
#endif /* argp-fmtstream.h */