root/maint/gnulib/lib/argp-fmtstream.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. __argp_fmtstream_write
  2. __argp_fmtstream_puts
  3. __argp_fmtstream_putc
  4. __argp_fmtstream_set_lmargin
  5. __argp_fmtstream_set_rmargin
  6. __argp_fmtstream_set_wmargin
  7. __argp_fmtstream_point

   1 /* Word-wrapping and line-truncating streams.
   2    Copyright (C) 1997-2021 Free Software Foundation, Inc.
   3    This file is part of the GNU C Library.
   4    Written by Miles Bader <miles@gnu.ai.mit.edu>.
   5 
   6    This file is free software: you can redistribute it and/or modify
   7    it under the terms of the GNU Lesser General Public License as
   8    published by the Free Software Foundation; either version 3 of the
   9    License, or (at your option) any later version.
  10 
  11    This file is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU Lesser General Public License for more details.
  15 
  16    You should have received a copy of the GNU Lesser General Public License
  17    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  18 
  19 /* This package emulates glibc 'line_wrap_stream' semantics for systems that
  20    don't have that.  If the system does have it, it is just a wrapper for
  21    that.  This header file is only used internally while compiling argp, and
  22    shouldn't be installed.  */
  23 
  24 #ifndef _ARGP_FMTSTREAM_H
  25 #define _ARGP_FMTSTREAM_H
  26 
  27 #include <stdio.h>
  28 #include <string.h>
  29 #include <unistd.h>
  30 
  31 #if defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H)
  32 /* line_wrap_stream is available, so use that.  */
  33 #define ARGP_FMTSTREAM_USE_LINEWRAP
  34 #endif
  35 
  36 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
  37 /* Just be a simple wrapper for line_wrap_stream; the semantics are
  38    *slightly* different, as line_wrap_stream doesn't actually make a new
  39    object, it just modifies the given stream (reversibly) to do
  40    line-wrapping.  Since we control who uses this code, it doesn't matter.  */
  41 
  42 #include <linewrap.h>
  43 
  44 typedef FILE *argp_fmtstream_t;
  45 
  46 #define argp_make_fmtstream line_wrap_stream
  47 #define __argp_make_fmtstream line_wrap_stream
  48 #define argp_fmtstream_free line_unwrap_stream
  49 #define __argp_fmtstream_free line_unwrap_stream
  50 
  51 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
  52 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
  53 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
  54 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
  55 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
  56 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
  57 #define __argp_fmtstream_printf fprintf
  58 #define argp_fmtstream_printf fprintf
  59 
  60 #define __argp_fmtstream_lmargin line_wrap_lmargin
  61 #define argp_fmtstream_lmargin line_wrap_lmargin
  62 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
  63 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
  64 #define __argp_fmtstream_rmargin line_wrap_rmargin
  65 #define argp_fmtstream_rmargin line_wrap_rmargin
  66 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
  67 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
  68 #define __argp_fmtstream_wmargin line_wrap_wmargin
  69 #define argp_fmtstream_wmargin line_wrap_wmargin
  70 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
  71 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
  72 #define __argp_fmtstream_point line_wrap_point
  73 #define argp_fmtstream_point line_wrap_point
  74 
  75 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
  76 /* Guess we have to define our own version.  */
  77 
  78 
  79 struct argp_fmtstream
  80 {
  81   FILE *stream;                 /* The stream we're outputting to.  */
  82 
  83   size_t lmargin, rmargin;      /* Left and right margins.  */
  84   ssize_t wmargin;              /* Margin to wrap to, or -1 to truncate.  */
  85 
  86   /* Point in buffer to which we've processed for wrapping, but not output.  */
  87   size_t point_offs;
  88   /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin.  */
  89   ssize_t point_col;
  90 
  91   char *buf;                    /* Output buffer.  */
  92   char *p;                      /* Current end of text in BUF. */
  93   char *end;                    /* Absolute end of BUF.  */
  94 };
  95 
  96 typedef struct argp_fmtstream *argp_fmtstream_t;
  97 
  98 /* Flush __FS to its stream, and free it (but don't close the stream).  */
  99 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
 100 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
 101 
 102 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
 103    written on it with LMARGIN spaces and limits them to RMARGIN columns
 104    total.  If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
 105    replacing the whitespace before them with a newline and WMARGIN spaces.
 106    Otherwise, chars beyond RMARGIN are simply dropped until a newline.
 107    Returns NULL if there was an error.  */
 108 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
 109                                                size_t __lmargin,
 110                                                size_t __rmargin,
 111                                                ssize_t __wmargin)
 112   _GL_ATTRIBUTE_DEALLOC (__argp_fmtstream_free, 1);
 113 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
 114                                              size_t __lmargin,
 115                                              size_t __rmargin,
 116                                              ssize_t __wmargin)
 117   _GL_ATTRIBUTE_DEALLOC (argp_fmtstream_free, 1);
 118 
 119 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
 120                                         const char *__fmt, ...)
 121      _GL_ATTRIBUTE_FORMAT ((printf, 2, 3));
 122 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
 123                                       const char *__fmt, ...)
 124      _GL_ATTRIBUTE_FORMAT ((printf, 2, 3));
 125 
 126 #if _LIBC
 127 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
 128 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
 129 
 130 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
 131 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
 132 
 133 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
 134                                       const char *__str, size_t __len);
 135 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
 136                                     const char *__str, size_t __len);
 137 #endif
 138 
 139 /* Access macros for various bits of state.  */
 140 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
 141 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
 142 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
 143 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
 144 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
 145 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
 146 
 147 #if _LIBC
 148 /* Set __FS's left margin to LMARGIN and return the old value.  */
 149 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
 150                                           size_t __lmargin);
 151 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
 152                                             size_t __lmargin);
 153 
 154 /* Set __FS's right margin to __RMARGIN and return the old value.  */
 155 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
 156                                           size_t __rmargin);
 157 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
 158                                             size_t __rmargin);
 159 
 160 /* Set __FS's wrap margin to __WMARGIN and return the old value.  */
 161 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
 162                                           size_t __wmargin);
 163 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
 164                                             size_t __wmargin);
 165 
 166 /* Return the column number of the current output point in __FS.  */
 167 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
 168 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
 169 #endif
 170 
 171 /* Internal routines.  */
 172 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
 173 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
 174 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
 175 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
 176 
 177 #if !_LIBC || defined __OPTIMIZE__
 178 /* Inline versions of above routines.  */
 179 
 180 #if !_LIBC
 181 #define __argp_fmtstream_putc argp_fmtstream_putc
 182 #define __argp_fmtstream_puts argp_fmtstream_puts
 183 #define __argp_fmtstream_write argp_fmtstream_write
 184 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
 185 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
 186 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
 187 #define __argp_fmtstream_point argp_fmtstream_point
 188 #define __argp_fmtstream_update _argp_fmtstream_update
 189 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
 190 #ifndef _GL_INLINE_HEADER_BEGIN
 191  #error "Please include config.h first."
 192 #endif
 193 _GL_INLINE_HEADER_BEGIN
 194 #ifndef ARGP_FS_EI
 195 # define ARGP_FS_EI _GL_INLINE
 196 #endif
 197 #endif
 198 
 199 #ifndef ARGP_FS_EI
 200 #define ARGP_FS_EI extern inline
 201 #endif
 202 
 203 ARGP_FS_EI size_t
 204 __argp_fmtstream_write (argp_fmtstream_t __fs, const char *__str, size_t __len)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206   if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
 207     {
 208       memcpy (__fs->p, __str, __len);
 209       __fs->p += __len;
 210       return __len;
 211     }
 212   else
 213     return 0;
 214 }
 215 
 216 ARGP_FS_EI int
 217 __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219   size_t __len = strlen (__str);
 220   if (__len)
 221     {
 222       size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
 223       return __wrote == __len ? 0 : -1;
 224     }
 225   else
 226     return 0;
 227 }
 228 
 229 ARGP_FS_EI int
 230 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
     /* [previous][next][first][last][top][bottom][index][help] */
 231 {
 232   if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
 233     return *__fs->p++ = __ch;
 234   else
 235     return EOF;
 236 }
 237 
 238 /* Set __FS's left margin to __LMARGIN and return the old value.  */
 239 ARGP_FS_EI size_t
 240 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
     /* [previous][next][first][last][top][bottom][index][help] */
 241 {
 242   size_t __old;
 243   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
 244     __argp_fmtstream_update (__fs);
 245   __old = __fs->lmargin;
 246   __fs->lmargin = __lmargin;
 247   return __old;
 248 }
 249 
 250 /* Set __FS's right margin to __RMARGIN and return the old value.  */
 251 ARGP_FS_EI size_t
 252 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
     /* [previous][next][first][last][top][bottom][index][help] */
 253 {
 254   size_t __old;
 255   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
 256     __argp_fmtstream_update (__fs);
 257   __old = __fs->rmargin;
 258   __fs->rmargin = __rmargin;
 259   return __old;
 260 }
 261 
 262 /* Set FS's wrap margin to __WMARGIN and return the old value.  */
 263 ARGP_FS_EI size_t
 264 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266   size_t __old;
 267   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
 268     __argp_fmtstream_update (__fs);
 269   __old = __fs->wmargin;
 270   __fs->wmargin = __wmargin;
 271   return __old;
 272 }
 273 
 274 /* Return the column number of the current output point in __FS.  */
 275 ARGP_FS_EI size_t
 276 __argp_fmtstream_point (argp_fmtstream_t __fs)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
 279     __argp_fmtstream_update (__fs);
 280   return __fs->point_col >= 0 ? __fs->point_col : 0;
 281 }
 282 
 283 #if !_LIBC
 284 #undef __argp_fmtstream_putc
 285 #undef __argp_fmtstream_puts
 286 #undef __argp_fmtstream_write
 287 #undef __argp_fmtstream_set_lmargin
 288 #undef __argp_fmtstream_set_rmargin
 289 #undef __argp_fmtstream_set_wmargin
 290 #undef __argp_fmtstream_point
 291 #undef __argp_fmtstream_update
 292 #undef __argp_fmtstream_ensure
 293 _GL_INLINE_HEADER_END
 294 #endif
 295 
 296 #endif /* !_LIBC || __OPTIMIZE__ */
 297 
 298 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
 299 
 300 #endif /* argp-fmtstream.h */

/* [previous][next][first][last][top][bottom][index][help] */