Use O_CLOEXEC in tzfile handling
[glibc.git] / argp / argp-fmtstream.h
blobaa8842132fbb11b850a1ba61e27e5b24979329af
1 /* Word-wrapping and line-truncating streams.
2 Copyright (C) 1997, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
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. */
26 #ifndef _ARGP_FMTSTREAM_H
27 #define _ARGP_FMTSTREAM_H
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
33 #ifndef __attribute__
34 /* This feature is available in gcc versions 2.5 and later. */
35 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
36 # define __attribute__(Spec) /* empty */
37 # endif
38 /* The __-protected variants of `format' and `printf' attributes
39 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
40 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || __STRICT_ANSI__
41 # define __format__ format
42 # define __printf__ printf
43 # endif
44 #endif
46 #if defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H)
47 /* line_wrap_stream is available, so use that. */
48 #define ARGP_FMTSTREAM_USE_LINEWRAP
49 #endif
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. */
57 #include <linewrap.h>
59 typedef FILE *argp_fmtstream_t;
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
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
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
90 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
91 /* Guess we have to define our own version. */
93 #ifndef __const
94 #define __const const
95 #endif
97 struct argp_fmtstream
99 FILE *stream; /* The stream we're outputting to. */
101 size_t lmargin, rmargin; /* Left and right margins. */
102 ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
104 /* Point in buffer to which we've processed for wrapping, but not output. */
105 size_t point_offs;
106 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
107 ssize_t point_col;
109 char *buf; /* Output buffer. */
110 char *p; /* Current end of text in BUF. */
111 char *end; /* Absolute end of BUF. */
114 typedef struct argp_fmtstream *argp_fmtstream_t;
116 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
117 written on it with LMARGIN spaces and limits them to RMARGIN columns
118 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
119 replacing the whitespace before them with a newline and WMARGIN spaces.
120 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
121 Returns NULL if there was an error. */
122 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
123 size_t __lmargin,
124 size_t __rmargin,
125 ssize_t __wmargin);
126 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
127 size_t __lmargin,
128 size_t __rmargin,
129 ssize_t __wmargin);
131 /* Flush __FS to its stream, and free it (but don't close the stream). */
132 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
133 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
135 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
136 __const char *__fmt, ...)
137 __attribute__ ((__format__ (printf, 2, 3)));
138 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
139 __const char *__fmt, ...)
140 __attribute__ ((__format__ (printf, 2, 3)));
142 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
143 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
145 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
146 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
148 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
149 __const char *__str, size_t __len);
150 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
151 __const char *__str, size_t __len);
153 /* Access macros for various bits of state. */
154 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
155 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
156 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
157 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
158 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
159 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
161 /* Set __FS's left margin to LMARGIN and return the old value. */
162 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
163 size_t __lmargin);
164 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
165 size_t __lmargin);
167 /* Set __FS's right margin to __RMARGIN and return the old value. */
168 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
169 size_t __rmargin);
170 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
171 size_t __rmargin);
173 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
174 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
175 size_t __wmargin);
176 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
177 size_t __wmargin);
179 /* Return the column number of the current output point in __FS. */
180 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
181 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
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);
189 #ifdef __OPTIMIZE__
190 /* Inline versions of above routines. */
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 #endif
204 #ifndef ARGP_FS_EI
205 #define ARGP_FS_EI extern inline
206 #endif
208 ARGP_FS_EI size_t
209 __argp_fmtstream_write (argp_fmtstream_t __fs,
210 __const char *__str, size_t __len)
212 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
214 memcpy (__fs->p, __str, __len);
215 __fs->p += __len;
216 return __len;
218 else
219 return 0;
222 ARGP_FS_EI int
223 __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
225 size_t __len = strlen (__str);
226 if (__len)
228 size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
229 return __wrote == __len ? 0 : -1;
231 else
232 return 0;
235 ARGP_FS_EI int
236 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
238 if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
239 return *__fs->p++ = __ch;
240 else
241 return EOF;
244 /* Set __FS's left margin to __LMARGIN and return the old value. */
245 ARGP_FS_EI size_t
246 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
248 size_t __old;
249 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
250 __argp_fmtstream_update (__fs);
251 __old = __fs->lmargin;
252 __fs->lmargin = __lmargin;
253 return __old;
256 /* Set __FS's right margin to __RMARGIN and return the old value. */
257 ARGP_FS_EI size_t
258 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
260 size_t __old;
261 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
262 __argp_fmtstream_update (__fs);
263 __old = __fs->rmargin;
264 __fs->rmargin = __rmargin;
265 return __old;
268 /* Set FS's wrap margin to __WMARGIN and return the old value. */
269 ARGP_FS_EI size_t
270 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
272 size_t __old;
273 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
274 __argp_fmtstream_update (__fs);
275 __old = __fs->wmargin;
276 __fs->wmargin = __wmargin;
277 return __old;
280 /* Return the column number of the current output point in __FS. */
281 ARGP_FS_EI size_t
282 __argp_fmtstream_point (argp_fmtstream_t __fs)
284 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
285 __argp_fmtstream_update (__fs);
286 return __fs->point_col >= 0 ? __fs->point_col : 0;
289 #if !_LIBC
290 #undef __argp_fmtstream_putc
291 #undef __argp_fmtstream_puts
292 #undef __argp_fmtstream_write
293 #undef __argp_fmtstream_set_lmargin
294 #undef __argp_fmtstream_set_rmargin
295 #undef __argp_fmtstream_set_wmargin
296 #undef __argp_fmtstream_point
297 #undef __argp_fmtstream_update
298 #undef __argp_fmtstream_ensure
299 #endif
301 #endif /* __OPTIMIZE__ */
303 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
305 #endif /* argp-fmtstream.h */