Update copyright notices with scripts/update-copyrights
[jlayton/glibc.git] / argp / argp-fmtstream.h
1 /* Word-wrapping and line-truncating streams.
2    Copyright (C) 1997-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    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 /* This package emulates glibc `line_wrap_stream' semantics for systems that
21    don't have that.  If the system does have it, it is just a wrapper for
22    that.  This header file is only used internally while compiling argp, and
23    shouldn't be installed.  */
24
25 #ifndef _ARGP_FMTSTREAM_H
26 #define _ARGP_FMTSTREAM_H
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #ifndef __attribute__
33 /* This feature is available in gcc versions 2.5 and later.  */
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
35 #  define __attribute__(Spec) /* empty */
36 # endif
37 /* The __-protected variants of `format' and `printf' attributes
38    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
39 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || __STRICT_ANSI__
40 #  define __format__ format
41 #  define __printf__ printf
42 # endif
43 #endif
44
45 #if defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H)
46 /* line_wrap_stream is available, so use that.  */
47 #define ARGP_FMTSTREAM_USE_LINEWRAP
48 #endif
49
50 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
51 /* Just be a simple wrapper for line_wrap_stream; the semantics are
52    *slightly* different, as line_wrap_stream doesn't actually make a new
53    object, it just modifies the given stream (reversibly) to do
54    line-wrapping.  Since we control who uses this code, it doesn't matter.  */
55
56 #include <linewrap.h>
57
58 typedef FILE *argp_fmtstream_t;
59
60 #define argp_make_fmtstream line_wrap_stream
61 #define __argp_make_fmtstream line_wrap_stream
62 #define argp_fmtstream_free line_unwrap_stream
63 #define __argp_fmtstream_free line_unwrap_stream
64
65 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
66 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
67 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
68 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
69 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
70 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
71 #define __argp_fmtstream_printf fprintf
72 #define argp_fmtstream_printf fprintf
73
74 #define __argp_fmtstream_lmargin line_wrap_lmargin
75 #define argp_fmtstream_lmargin line_wrap_lmargin
76 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
77 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
78 #define __argp_fmtstream_rmargin line_wrap_rmargin
79 #define argp_fmtstream_rmargin line_wrap_rmargin
80 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
81 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
82 #define __argp_fmtstream_wmargin line_wrap_wmargin
83 #define argp_fmtstream_wmargin line_wrap_wmargin
84 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
85 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
86 #define __argp_fmtstream_point line_wrap_point
87 #define argp_fmtstream_point line_wrap_point
88
89 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
90 /* Guess we have to define our own version.  */
91
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      __attribute__ ((__format__ (printf, 2, 3)));
134 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
135                                       const char *__fmt, ...)
136      __attribute__ ((__format__ (printf, 2, 3)));
137
138 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
139 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
140
141 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
142 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str);
143
144 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
145                                       const char *__str, size_t __len);
146 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
147                                     const char *__str, size_t __len);
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 /* Set __FS's left margin to LMARGIN and return the old value.  */
158 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
159                                           size_t __lmargin);
160 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
161                                             size_t __lmargin);
162
163 /* Set __FS's right margin to __RMARGIN and return the old value.  */
164 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
165                                           size_t __rmargin);
166 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
167                                             size_t __rmargin);
168
169 /* Set __FS's wrap margin to __WMARGIN and return the old value.  */
170 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
171                                           size_t __wmargin);
172 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
173                                             size_t __wmargin);
174
175 /* Return the column number of the current output point in __FS.  */
176 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
177 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
178
179 /* Internal routines.  */
180 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
181 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
182 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
183 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
184 \f
185 #ifdef __OPTIMIZE__
186 /* Inline versions of above routines.  */
187
188 #if !_LIBC
189 #define __argp_fmtstream_putc argp_fmtstream_putc
190 #define __argp_fmtstream_puts argp_fmtstream_puts
191 #define __argp_fmtstream_write argp_fmtstream_write
192 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
193 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
194 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
195 #define __argp_fmtstream_point argp_fmtstream_point
196 #define __argp_fmtstream_update _argp_fmtstream_update
197 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
198 #endif
199
200 #ifndef ARGP_FS_EI
201 #define ARGP_FS_EI extern inline
202 #endif
203
204 ARGP_FS_EI size_t
205 __argp_fmtstream_write (argp_fmtstream_t __fs, const char *__str, size_t __len)
206 {
207   if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
208     {
209       memcpy (__fs->p, __str, __len);
210       __fs->p += __len;
211       return __len;
212     }
213   else
214     return 0;
215 }
216
217 ARGP_FS_EI int
218 __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str)
219 {
220   size_t __len = strlen (__str);
221   if (__len)
222     {
223       size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
224       return __wrote == __len ? 0 : -1;
225     }
226   else
227     return 0;
228 }
229
230 ARGP_FS_EI int
231 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
232 {
233   if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
234     return *__fs->p++ = __ch;
235   else
236     return EOF;
237 }
238
239 /* Set __FS's left margin to __LMARGIN and return the old value.  */
240 ARGP_FS_EI size_t
241 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
242 {
243   size_t __old;
244   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
245     __argp_fmtstream_update (__fs);
246   __old = __fs->lmargin;
247   __fs->lmargin = __lmargin;
248   return __old;
249 }
250
251 /* Set __FS's right margin to __RMARGIN and return the old value.  */
252 ARGP_FS_EI size_t
253 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
254 {
255   size_t __old;
256   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
257     __argp_fmtstream_update (__fs);
258   __old = __fs->rmargin;
259   __fs->rmargin = __rmargin;
260   return __old;
261 }
262
263 /* Set FS's wrap margin to __WMARGIN and return the old value.  */
264 ARGP_FS_EI size_t
265 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
266 {
267   size_t __old;
268   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
269     __argp_fmtstream_update (__fs);
270   __old = __fs->wmargin;
271   __fs->wmargin = __wmargin;
272   return __old;
273 }
274
275 /* Return the column number of the current output point in __FS.  */
276 ARGP_FS_EI size_t
277 __argp_fmtstream_point (argp_fmtstream_t __fs)
278 {
279   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
280     __argp_fmtstream_update (__fs);
281   return __fs->point_col >= 0 ? __fs->point_col : 0;
282 }
283
284 #if !_LIBC
285 #undef __argp_fmtstream_putc
286 #undef __argp_fmtstream_puts
287 #undef __argp_fmtstream_write
288 #undef __argp_fmtstream_set_lmargin
289 #undef __argp_fmtstream_set_rmargin
290 #undef __argp_fmtstream_set_wmargin
291 #undef __argp_fmtstream_point
292 #undef __argp_fmtstream_update
293 #undef __argp_fmtstream_ensure
294 #endif
295
296 #endif /* __OPTIMIZE__ */
297
298 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
299
300 #endif /* argp-fmtstream.h */