Import upstream version 1.28
[debian/tar] / gnu / argp-fmtstream.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Word-wrapping and line-truncating streams.
4    Copyright (C) 1997, 2006-2014 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6    Written by Miles Bader <miles@gnu.ai.mit.edu>.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* This package emulates glibc 'line_wrap_stream' semantics for systems that
22    don't have that.  If the system does have it, it is just a wrapper for
23    that.  This header file is only used internally while compiling argp, and
24    shouldn't be installed.  */
25
26 #ifndef _ARGP_FMTSTREAM_H
27 #define _ARGP_FMTSTREAM_H
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 /* The __attribute__ feature is available in gcc versions 2.5 and later.
34    The __-protected variants of the attributes 'format' and 'printf' are
35    accepted by gcc versions 2.6.4 (effectively 2.7) and later.
36    We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
37    gnulib and libintl do '#define printf __printf__' when they override
38    the 'printf' function.  */
39 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
40 # define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
41 #else
42 # define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
43 #endif
44
45 #if    (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
46     || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
47 /* line_wrap_stream is available, so use that.  */
48 #define ARGP_FMTSTREAM_USE_LINEWRAP
49 #endif
50
51 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
52 /* Just be a simple wrapper for line_wrap_stream; the semantics are
53    *slightly* different, as line_wrap_stream doesn't actually make a new
54    object, it just modifies the given stream (reversibly) to do
55    line-wrapping.  Since we control who uses this code, it doesn't matter.  */
56
57 #include <linewrap.h>
58
59 typedef FILE *argp_fmtstream_t;
60
61 #define argp_make_fmtstream line_wrap_stream
62 #define __argp_make_fmtstream line_wrap_stream
63 #define argp_fmtstream_free line_unwrap_stream
64 #define __argp_fmtstream_free line_unwrap_stream
65
66 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
67 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
68 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
69 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
70 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
71 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
72 #define __argp_fmtstream_printf fprintf
73 #define argp_fmtstream_printf fprintf
74
75 #define __argp_fmtstream_lmargin line_wrap_lmargin
76 #define argp_fmtstream_lmargin line_wrap_lmargin
77 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
78 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
79 #define __argp_fmtstream_rmargin line_wrap_rmargin
80 #define argp_fmtstream_rmargin line_wrap_rmargin
81 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
82 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
83 #define __argp_fmtstream_wmargin line_wrap_wmargin
84 #define argp_fmtstream_wmargin line_wrap_wmargin
85 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
86 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
87 #define __argp_fmtstream_point line_wrap_point
88 #define argp_fmtstream_point line_wrap_point
89
90 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
91 /* Guess we have to define our own version.  */
92 \f
93 struct argp_fmtstream
94 {
95   FILE *stream;                 /* The stream we're outputting to.  */
96
97   size_t lmargin, rmargin;      /* Left and right margins.  */
98   ssize_t wmargin;              /* Margin to wrap to, or -1 to truncate.  */
99
100   /* Point in buffer to which we've processed for wrapping, but not output.  */
101   size_t point_offs;
102   /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin.  */
103   ssize_t point_col;
104
105   char *buf;                    /* Output buffer.  */
106   char *p;                      /* Current end of text in BUF. */
107   char *end;                    /* Absolute end of BUF.  */
108 };
109
110 typedef struct argp_fmtstream *argp_fmtstream_t;
111
112 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
113    written on it with LMARGIN spaces and limits them to RMARGIN columns
114    total.  If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
115    replacing the whitespace before them with a newline and WMARGIN spaces.
116    Otherwise, chars beyond RMARGIN are simply dropped until a newline.
117    Returns NULL if there was an error.  */
118 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
119                                                size_t __lmargin,
120                                                size_t __rmargin,
121                                                ssize_t __wmargin);
122 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
123                                              size_t __lmargin,
124                                              size_t __rmargin,
125                                              ssize_t __wmargin);
126
127 /* Flush __FS to its stream, and free it (but don't close the stream).  */
128 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
129 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
130
131 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
132                                         const char *__fmt, ...)
133      _GL_ATTRIBUTE_FORMAT ((printf, 2, 3));
134 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
135                                       const char *__fmt, ...)
136      _GL_ATTRIBUTE_FORMAT ((printf, 2, 3));
137
138 #if _LIBC
139 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
140 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
141
142 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
143 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
144
145 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
146                                       const char *__str, size_t __len);
147 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
148                                     const char *__str, size_t __len);
149 #endif
150 \f
151 /* Access macros for various bits of state.  */
152 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
153 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
154 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
155 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
156 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
157 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
158
159 #if _LIBC
160 /* Set __FS's left margin to LMARGIN and return the old value.  */
161 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
162                                           size_t __lmargin);
163 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
164                                             size_t __lmargin);
165
166 /* Set __FS's right margin to __RMARGIN and return the old value.  */
167 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
168                                           size_t __rmargin);
169 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
170                                             size_t __rmargin);
171
172 /* Set __FS's wrap margin to __WMARGIN and return the old value.  */
173 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
174                                           size_t __wmargin);
175 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
176                                             size_t __wmargin);
177
178 /* Return the column number of the current output point in __FS.  */
179 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
180 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
181 #endif
182
183 /* Internal routines.  */
184 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
185 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
186 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
187 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
188 \f
189 #if !_LIBC || defined __OPTIMIZE__
190 /* Inline versions of above routines.  */
191
192 #if !_LIBC
193 #define __argp_fmtstream_putc argp_fmtstream_putc
194 #define __argp_fmtstream_puts argp_fmtstream_puts
195 #define __argp_fmtstream_write argp_fmtstream_write
196 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
197 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
198 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
199 #define __argp_fmtstream_point argp_fmtstream_point
200 #define __argp_fmtstream_update _argp_fmtstream_update
201 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
202 #ifndef _GL_INLINE_HEADER_BEGIN
203  #error "Please include config.h first."
204 #endif
205 _GL_INLINE_HEADER_BEGIN
206 #ifndef ARGP_FS_EI
207 # define ARGP_FS_EI _GL_INLINE
208 #endif
209 #endif
210
211 #ifndef ARGP_FS_EI
212 # ifdef __GNUC__
213    /* GCC 4.3 and above with -std=c99 or -std=gnu99 implements ISO C99
214       inline semantics, unless -fgnu89-inline is used.  It defines a macro
215       __GNUC_STDC_INLINE__ to indicate this situation or a macro
216       __GNUC_GNU_INLINE__ to indicate the opposite situation.
217
218       GCC 4.2 with -std=c99 or -std=gnu99 implements the GNU C inline
219       semantics but warns, unless -fgnu89-inline is used:
220         warning: C99 inline functions are not supported; using GNU89
221         warning: to disable this warning use -fgnu89-inline or the gnu_inline function attribute
222       It defines a macro __GNUC_GNU_INLINE__ to indicate this situation.
223
224       Whereas Apple GCC 4.0.1 build 5479 without -std=c99 or -std=gnu99
225       implements the GNU C inline semantics and defines the macro
226       __GNUC_GNU_INLINE__, but it does not warn and does not support
227       __attribute__ ((__gnu_inline__)).
228
229       All in all, these are the possible combinations.  For every compiler,
230       we need to choose ARGP_FS_EI so that the corresponding table cell
231       contains an "ok".
232
233         \    ARGP_FS_EI                      inline   extern    extern
234           \                                           inline    inline
235       CC    \                                                   __attribute__
236                                                                 ((gnu_inline))
237
238       gcc 4.3.0                              error    ok        ok
239       gcc 4.3.0 -std=gnu99 -fgnu89-inline    error    ok        ok
240       gcc 4.3.0 -std=gnu99                   ok       error     ok
241
242       gcc 4.2.2                              error    ok        ok
243       gcc 4.2.2 -std=gnu99 -fgnu89-inline    error    ok        ok
244       gcc 4.2.2 -std=gnu99                   error    warning   ok
245
246       gcc 4.1.2                              error    ok        warning
247       gcc 4.1.2 -std=gnu99                   error    ok        warning
248
249       Apple gcc 4.0.1                        error    ok        warning
250       Apple gcc 4.0.1 -std=gnu99             ok       error     warning
251     */
252 #  if defined __GNUC_STDC_INLINE__
253 #   define ARGP_FS_EI inline
254 #  elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
255 #   define ARGP_FS_EI extern inline __attribute__ ((__gnu_inline__))
256 #  else
257 #   define ARGP_FS_EI extern inline
258 #  endif
259 # else
260    /* With other compilers, assume the ISO C99 meaning of 'inline', if
261       the compiler supports 'inline' at all.  */
262 #  define ARGP_FS_EI inline
263 # endif
264 #endif
265
266 ARGP_FS_EI size_t
267 __argp_fmtstream_write (argp_fmtstream_t __fs,
268                         const char *__str, size_t __len)
269 {
270   if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
271     {
272       memcpy (__fs->p, __str, __len);
273       __fs->p += __len;
274       return __len;
275     }
276   else
277     return 0;
278 }
279
280 ARGP_FS_EI int
281 __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str)
282 {
283   size_t __len = strlen (__str);
284   if (__len)
285     {
286       size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
287       return __wrote == __len ? 0 : -1;
288     }
289   else
290     return 0;
291 }
292
293 ARGP_FS_EI int
294 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
295 {
296   if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
297     return *__fs->p++ = __ch;
298   else
299     return EOF;
300 }
301
302 /* Set __FS's left margin to __LMARGIN and return the old value.  */
303 ARGP_FS_EI size_t
304 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
305 {
306   size_t __old;
307   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
308     __argp_fmtstream_update (__fs);
309   __old = __fs->lmargin;
310   __fs->lmargin = __lmargin;
311   return __old;
312 }
313
314 /* Set __FS's right margin to __RMARGIN and return the old value.  */
315 ARGP_FS_EI size_t
316 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
317 {
318   size_t __old;
319   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
320     __argp_fmtstream_update (__fs);
321   __old = __fs->rmargin;
322   __fs->rmargin = __rmargin;
323   return __old;
324 }
325
326 /* Set FS's wrap margin to __WMARGIN and return the old value.  */
327 ARGP_FS_EI size_t
328 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
329 {
330   size_t __old;
331   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
332     __argp_fmtstream_update (__fs);
333   __old = __fs->wmargin;
334   __fs->wmargin = __wmargin;
335   return __old;
336 }
337
338 /* Return the column number of the current output point in __FS.  */
339 ARGP_FS_EI size_t
340 __argp_fmtstream_point (argp_fmtstream_t __fs)
341 {
342   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
343     __argp_fmtstream_update (__fs);
344   return __fs->point_col >= 0 ? __fs->point_col : 0;
345 }
346
347 #if !_LIBC
348 #undef __argp_fmtstream_putc
349 #undef __argp_fmtstream_puts
350 #undef __argp_fmtstream_write
351 #undef __argp_fmtstream_set_lmargin
352 #undef __argp_fmtstream_set_rmargin
353 #undef __argp_fmtstream_set_wmargin
354 #undef __argp_fmtstream_point
355 #undef __argp_fmtstream_update
356 #undef __argp_fmtstream_ensure
357 _GL_INLINE_HEADER_END
358 #endif
359
360 #endif /* !_LIBC || __OPTIMIZE__ */
361
362 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
363
364 #endif /* argp-fmtstream.h */