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