2N/A/* Word-wrapping and line-truncating streams.
2N/A Copyright (C) 1997, 2006-2010 Free Software 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. If the system does have it, it is just a wrapper for
2N/A that. This header file is only used internally while compiling argp, and
2N/A shouldn't be installed. */
2N/A
2N/A#ifndef _ARGP_FMTSTREAM_H
2N/A#define _ARGP_FMTSTREAM_H
2N/A
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A
2N/A#ifndef __attribute__
2N/A/* The __attribute__ feature is available in gcc versions 2.5 and later.
2N/A The __-protected variants of the attributes 'format' and 'printf' are
2N/A accepted by gcc versions 2.6.4 (effectively 2.7) and later.
2N/A We enable __attribute__ only if these are supported too, because
2N/A gnulib and libintl do '#define printf __printf__' when they override
2N/A the 'printf' function. */
2N/A# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
2N/A# define __attribute__(Spec) /* empty */
2N/A# endif
2N/A#endif
2N/A
2N/A#if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
2N/A || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
2N/A/* line_wrap_stream is available, so use that. */
2N/A#define ARGP_FMTSTREAM_USE_LINEWRAP
2N/A#endif
2N/A
2N/A#ifdef ARGP_FMTSTREAM_USE_LINEWRAP
2N/A/* Just be a simple wrapper for line_wrap_stream; the semantics are
2N/A *slightly* different, as line_wrap_stream doesn't actually make a new
2N/A object, it just modifies the given stream (reversibly) to do
2N/A line-wrapping. Since we control who uses this code, it doesn't matter. */
2N/A
2N/A#include <linewrap.h>
2N/A
2N/Atypedef FILE *argp_fmtstream_t;
2N/A
2N/A#define argp_make_fmtstream line_wrap_stream
2N/A#define __argp_make_fmtstream line_wrap_stream
2N/A#define argp_fmtstream_free line_unwrap_stream
2N/A#define __argp_fmtstream_free line_unwrap_stream
2N/A
2N/A#define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
2N/A#define argp_fmtstream_putc(fs,ch) putc(ch,fs)
2N/A#define __argp_fmtstream_puts(fs,str) fputs(str,fs)
2N/A#define argp_fmtstream_puts(fs,str) fputs(str,fs)
2N/A#define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
2N/A#define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
2N/A#define __argp_fmtstream_printf fprintf
2N/A#define argp_fmtstream_printf fprintf
2N/A
2N/A#define __argp_fmtstream_lmargin line_wrap_lmargin
2N/A#define argp_fmtstream_lmargin line_wrap_lmargin
2N/A#define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
2N/A#define argp_fmtstream_set_lmargin line_wrap_set_lmargin
2N/A#define __argp_fmtstream_rmargin line_wrap_rmargin
2N/A#define argp_fmtstream_rmargin line_wrap_rmargin
2N/A#define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
2N/A#define argp_fmtstream_set_rmargin line_wrap_set_rmargin
2N/A#define __argp_fmtstream_wmargin line_wrap_wmargin
2N/A#define argp_fmtstream_wmargin line_wrap_wmargin
2N/A#define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
2N/A#define argp_fmtstream_set_wmargin line_wrap_set_wmargin
2N/A#define __argp_fmtstream_point line_wrap_point
2N/A#define argp_fmtstream_point line_wrap_point
2N/A
2N/A#else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
2N/A/* Guess we have to define our own version. */
2N/A
2N/Astruct argp_fmtstream
2N/A{
2N/A FILE *stream; /* The stream we're outputting to. */
2N/A
2N/A size_t lmargin, rmargin; /* Left and right margins. */
2N/A ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
2N/A
2N/A /* Point in buffer to which we've processed for wrapping, but not output. */
2N/A size_t point_offs;
2N/A /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
2N/A ssize_t point_col;
2N/A
2N/A char *buf; /* Output buffer. */
2N/A char *p; /* Current end of text in BUF. */
2N/A char *end; /* Absolute end of BUF. */
2N/A};
2N/A
2N/Atypedef struct argp_fmtstream *argp_fmtstream_t;
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/Aextern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
2N/A size_t __lmargin,
2N/A size_t __rmargin,
2N/A ssize_t __wmargin);
2N/Aextern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
2N/A size_t __lmargin,
2N/A size_t __rmargin,
2N/A ssize_t __wmargin);
2N/A
2N/A/* Flush __FS to its stream, and free it (but don't close the stream). */
2N/Aextern void __argp_fmtstream_free (argp_fmtstream_t __fs);
2N/Aextern void argp_fmtstream_free (argp_fmtstream_t __fs);
2N/A
2N/Aextern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
2N/A const char *__fmt, ...)
2N/A __attribute__ ((__format__ (printf, 2, 3)));
2N/Aextern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
2N/A const char *__fmt, ...)
2N/A __attribute__ ((__format__ (printf, 2, 3)));
2N/A
2N/A#if _LIBC || !defined __OPTIMIZE__
2N/Aextern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
2N/Aextern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
2N/A
2N/Aextern int __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
2N/Aextern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
2N/A
2N/Aextern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
2N/A const char *__str, size_t __len);
2N/Aextern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
2N/A const char *__str, size_t __len);
2N/A#endif
2N/A
2N/A/* Access macros for various bits of state. */
2N/A#define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
2N/A#define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
2N/A#define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
2N/A#define __argp_fmtstream_lmargin argp_fmtstream_lmargin
2N/A#define __argp_fmtstream_rmargin argp_fmtstream_rmargin
2N/A#define __argp_fmtstream_wmargin argp_fmtstream_wmargin
2N/A
2N/A#if _LIBC || !defined __OPTIMIZE__
2N/A/* Set __FS's left margin to LMARGIN and return the old value. */
2N/Aextern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
2N/A size_t __lmargin);
2N/Aextern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
2N/A size_t __lmargin);
2N/A
2N/A/* Set __FS's right margin to __RMARGIN and return the old value. */
2N/Aextern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
2N/A size_t __rmargin);
2N/Aextern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
2N/A size_t __rmargin);
2N/A
2N/A/* Set __FS's wrap margin to __WMARGIN and return the old value. */
2N/Aextern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
2N/A size_t __wmargin);
2N/Aextern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
2N/A size_t __wmargin);
2N/A
2N/A/* Return the column number of the current output point in __FS. */
2N/Aextern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
2N/Aextern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
2N/A#endif
2N/A
2N/A/* Internal routines. */
2N/Aextern void _argp_fmtstream_update (argp_fmtstream_t __fs);
2N/Aextern void __argp_fmtstream_update (argp_fmtstream_t __fs);
2N/Aextern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
2N/Aextern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
2N/A
2N/A#ifdef __OPTIMIZE__
2N/A/* Inline versions of above routines. */
2N/A
2N/A#if !_LIBC
2N/A#define __argp_fmtstream_putc argp_fmtstream_putc
2N/A#define __argp_fmtstream_puts argp_fmtstream_puts
2N/A#define __argp_fmtstream_write argp_fmtstream_write
2N/A#define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
2N/A#define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
2N/A#define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
2N/A#define __argp_fmtstream_point argp_fmtstream_point
2N/A#define __argp_fmtstream_update _argp_fmtstream_update
2N/A#define __argp_fmtstream_ensure _argp_fmtstream_ensure
2N/A#endif
2N/A
2N/A#ifndef ARGP_FS_EI
2N/A# ifdef __GNUC__
2N/A /* GCC 4.3 and above with -std=c99 or -std=gnu99 implements ISO C99
2N/A inline semantics, unless -fgnu89-inline is used. It defines a macro
2N/A __GNUC_STDC_INLINE__ to indicate this situation or a macro
2N/A __GNUC_GNU_INLINE__ to indicate the opposite situation.
2N/A
2N/A GCC 4.2 with -std=c99 or -std=gnu99 implements the GNU C inline
2N/A semantics but warns, unless -fgnu89-inline is used:
2N/A warning: C99 inline functions are not supported; using GNU89
2N/A warning: to disable this warning use -fgnu89-inline or the gnu_inline function attribute
2N/A It defines a macro __GNUC_GNU_INLINE__ to indicate this situation.
2N/A
2N/A Whereas Apple GCC 4.0.1 build 5479 without -std=c99 or -std=gnu99
2N/A implements the GNU C inline semantics and defines the macro
2N/A __GNUC_GNU_INLINE__, but it does not warn and does not support
2N/A __attribute__ ((__gnu_inline__)).
2N/A
2N/A All in all, these are the possible combinations. For every compiler,
2N/A we need to choose ARGP_FS_EI so that the corresponding table cell
2N/A contains an "ok".
2N/A
2N/A \ ARGP_FS_EI inline extern extern
2N/A \ inline inline
2N/A CC \ __attribute__
2N/A ((gnu_inline))
2N/A
2N/A gcc 4.3.0 error ok ok
2N/A gcc 4.3.0 -std=gnu99 -fgnu89-inline error ok ok
2N/A gcc 4.3.0 -std=gnu99 ok error ok
2N/A
2N/A gcc 4.2.2 error ok ok
2N/A gcc 4.2.2 -std=gnu99 -fgnu89-inline error ok ok
2N/A gcc 4.2.2 -std=gnu99 error warning ok
2N/A
2N/A gcc 4.1.2 error ok warning
2N/A gcc 4.1.2 -std=gnu99 error ok warning
2N/A
2N/A Apple gcc 4.0.1 error ok warning
2N/A Apple gcc 4.0.1 -std=gnu99 ok error warning
2N/A */
2N/A# if defined __GNUC_STDC_INLINE__
2N/A# define ARGP_FS_EI inline
2N/A# elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
2N/A# define ARGP_FS_EI extern inline __attribute__ ((__gnu_inline__))
2N/A# else
2N/A# define ARGP_FS_EI extern inline
2N/A# endif
2N/A# else
2N/A /* With other compilers, assume the ISO C99 meaning of 'inline', if
2N/A the compiler supports 'inline' at all. */
2N/A# define ARGP_FS_EI inline
2N/A# endif
2N/A#endif
2N/A
2N/AARGP_FS_EI size_t
2N/A__argp_fmtstream_write (argp_fmtstream_t __fs,
2N/A const char *__str, size_t __len)
2N/A{
2N/A if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
2N/A {
2N/A memcpy (__fs->p, __str, __len);
2N/A __fs->p += __len;
2N/A return __len;
2N/A }
2N/A else
2N/A return 0;
2N/A}
2N/A
2N/AARGP_FS_EI int
2N/A__argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str)
2N/A{
2N/A size_t __len = strlen (__str);
2N/A if (__len)
2N/A {
2N/A size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
2N/A return __wrote == __len ? 0 : -1;
2N/A }
2N/A else
2N/A return 0;
2N/A}
2N/A
2N/AARGP_FS_EI int
2N/A__argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
2N/A{
2N/A if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
2N/A return *__fs->p++ = __ch;
2N/A else
2N/A return EOF;
2N/A}
2N/A
2N/A/* Set __FS's left margin to __LMARGIN and return the old value. */
2N/AARGP_FS_EI size_t
2N/A__argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
2N/A{
2N/A size_t __old;
2N/A if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
2N/A __argp_fmtstream_update (__fs);
2N/A __old = __fs->lmargin;
2N/A __fs->lmargin = __lmargin;
2N/A return __old;
2N/A}
2N/A
2N/A/* Set __FS's right margin to __RMARGIN and return the old value. */
2N/AARGP_FS_EI size_t
2N/A__argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
2N/A{
2N/A size_t __old;
2N/A if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
2N/A __argp_fmtstream_update (__fs);
2N/A __old = __fs->rmargin;
2N/A __fs->rmargin = __rmargin;
2N/A return __old;
2N/A}
2N/A
2N/A/* Set FS's wrap margin to __WMARGIN and return the old value. */
2N/AARGP_FS_EI size_t
2N/A__argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
2N/A{
2N/A size_t __old;
2N/A if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
2N/A __argp_fmtstream_update (__fs);
2N/A __old = __fs->wmargin;
2N/A __fs->wmargin = __wmargin;
2N/A return __old;
2N/A}
2N/A
2N/A/* Return the column number of the current output point in __FS. */
2N/AARGP_FS_EI size_t
2N/A__argp_fmtstream_point (argp_fmtstream_t __fs)
2N/A{
2N/A if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
2N/A __argp_fmtstream_update (__fs);
2N/A return __fs->point_col >= 0 ? __fs->point_col : 0;
2N/A}
2N/A
2N/A#if !_LIBC
2N/A#undef __argp_fmtstream_putc
2N/A#undef __argp_fmtstream_puts
2N/A#undef __argp_fmtstream_write
2N/A#undef __argp_fmtstream_set_lmargin
2N/A#undef __argp_fmtstream_set_rmargin
2N/A#undef __argp_fmtstream_set_wmargin
2N/A#undef __argp_fmtstream_point
2N/A#undef __argp_fmtstream_update
2N/A#undef __argp_fmtstream_ensure
2N/A#endif
2N/A
2N/A#endif /* __OPTIMIZE__ */
2N/A
2N/A#endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
2N/A
2N/A#endif /* argp-fmtstream.h */