283
|
1 /* Word-wrapping and line-truncating streams.
|
|
2 Copyright (C) 1997, 2006-2008 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 #ifndef __attribute__
|
|
32 /* This feature is available in gcc versions 2.5 and later. */
|
|
33 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
|
|
34 # define __attribute__(Spec) /* empty */
|
|
35 # endif
|
|
36 /* The __-protected variants of `format' and `printf' attributes
|
|
37 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
|
|
38 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || __STRICT_ANSI__
|
|
39 # define __format__ format
|
|
40 # define __printf__ printf
|
|
41 # endif
|
|
42 #endif
|
|
43
|
|
44 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
|
|
45 || (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 struct argp_fmtstream
|
|
93 {
|
|
94 FILE *stream; /* The stream we're outputting to. */
|
|
95
|
|
96 size_t lmargin, rmargin; /* Left and right margins. */
|
|
97 ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
|
|
98
|
|
99 /* Point in buffer to which we've processed for wrapping, but not output. */
|
|
100 size_t point_offs;
|
|
101 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
|
|
102 ssize_t point_col;
|
|
103
|
|
104 char *buf; /* Output buffer. */
|
|
105 char *p; /* Current end of text in BUF. */
|
|
106 char *end; /* Absolute end of BUF. */
|
|
107 };
|
|
108
|
|
109 typedef struct argp_fmtstream *argp_fmtstream_t;
|
|
110
|
|
111 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
|
|
112 written on it with LMARGIN spaces and limits them to RMARGIN columns
|
|
113 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
|
|
114 replacing the whitespace before them with a newline and WMARGIN spaces.
|
|
115 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
|
|
116 Returns NULL if there was an error. */
|
|
117 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
|
|
118 size_t __lmargin,
|
|
119 size_t __rmargin,
|
|
120 ssize_t __wmargin);
|
|
121 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
|
|
122 size_t __lmargin,
|
|
123 size_t __rmargin,
|
|
124 ssize_t __wmargin);
|
|
125
|
|
126 /* Flush __FS to its stream, and free it (but don't close the stream). */
|
|
127 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
|
|
128 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
|
|
129
|
|
130 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
|
|
131 const char *__fmt, ...)
|
|
132 __attribute__ ((__format__ (printf, 2, 3)));
|
|
133 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
|
|
134 const char *__fmt, ...)
|
|
135 __attribute__ ((__format__ (printf, 2, 3)));
|
|
136
|
|
137 #if _LIBC || !defined __OPTIMIZE__
|
|
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 #endif
|
|
149
|
|
150 /* Access macros for various bits of state. */
|
|
151 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
|
|
152 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
|
|
153 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
|
|
154 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
|
|
155 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
|
|
156 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
|
|
157
|
|
158 #if _LIBC || !defined __OPTIMIZE__
|
|
159 /* Set __FS's left margin to LMARGIN and return the old value. */
|
|
160 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
|
|
161 size_t __lmargin);
|
|
162 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
|
|
163 size_t __lmargin);
|
|
164
|
|
165 /* Set __FS's right margin to __RMARGIN and return the old value. */
|
|
166 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
|
|
167 size_t __rmargin);
|
|
168 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
|
|
169 size_t __rmargin);
|
|
170
|
|
171 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
|
|
172 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
|
|
173 size_t __wmargin);
|
|
174 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
|
|
175 size_t __wmargin);
|
|
176
|
|
177 /* Return the column number of the current output point in __FS. */
|
|
178 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
|
|
179 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
|
|
180 #endif
|
|
181
|
|
182 /* Internal routines. */
|
|
183 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
|
|
184 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
|
|
185 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
|
|
186 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
|
|
187
|
|
188 #ifdef __OPTIMIZE__
|
|
189 /* Inline versions of above routines. */
|
|
190
|
|
191 #if !_LIBC
|
|
192 #define __argp_fmtstream_putc argp_fmtstream_putc
|
|
193 #define __argp_fmtstream_puts argp_fmtstream_puts
|
|
194 #define __argp_fmtstream_write argp_fmtstream_write
|
|
195 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
|
|
196 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
|
|
197 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
|
|
198 #define __argp_fmtstream_point argp_fmtstream_point
|
|
199 #define __argp_fmtstream_update _argp_fmtstream_update
|
|
200 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
|
|
201 #endif
|
|
202
|
|
203 #ifndef ARGP_FS_EI
|
|
204 # ifdef __GNUC__
|
|
205 /* GCC 4.3 and above with -std=c99 or -std=gnu99 implements ISO C99
|
|
206 inline semantics, unless -fgnu89-inline is used. It defines a macro
|
|
207 __GNUC_STDC_INLINE__ to indicate this situation or a macro
|
|
208 __GNUC_GNU_INLINE__ to indicate the opposite situation.
|
|
209
|
|
210 GCC 4.2 with -std=c99 or -std=gnu99 implements the GNU C inline
|
|
211 semantics but warns, unless -fgnu89-inline is used:
|
|
212 warning: C99 inline functions are not supported; using GNU89
|
|
213 warning: to disable this warning use -fgnu89-inline or the gnu_inline function attribute
|
|
214 It defines a macro __GNUC_GNU_INLINE__ to indicate this situation.
|
|
215
|
|
216 Whereas Apple GCC 4.0.1 build 5479 without -std=c99 or -std=gnu99
|
|
217 implements the GNU C inline semantics and defines the macro
|
|
218 __GNUC_GNU_INLINE__, but it does not warn and does not support
|
|
219 __attribute__ ((__gnu_inline__)).
|
|
220
|
|
221 All in all, these are the possible combinations. For every compiler,
|
|
222 we need to choose ARGP_FS_EI so that the corresponding table cell
|
|
223 contains an "ok".
|
|
224
|
|
225 \ ARGP_FS_EI inline extern extern
|
|
226 \ inline inline
|
|
227 CC \ __attribute__
|
|
228 ((gnu_inline))
|
|
229
|
|
230 gcc 4.3.0 error ok ok
|
|
231 gcc 4.3.0 -std=gnu99 -fgnu89-inline error ok ok
|
|
232 gcc 4.3.0 -std=gnu99 ok error ok
|
|
233
|
|
234 gcc 4.2.2 error ok ok
|
|
235 gcc 4.2.2 -std=gnu99 -fgnu89-inline error ok ok
|
|
236 gcc 4.2.2 -std=gnu99 error warning ok
|
|
237
|
|
238 gcc 4.1.2 error ok warning
|
|
239 gcc 4.1.2 -std=gnu99 error ok warning
|
|
240
|
|
241 Apple gcc 4.0.1 error ok warning
|
|
242 Apple gcc 4.0.1 -std=gnu99 ok error warning
|
|
243 */
|
|
244 # if defined __GNUC_STDC_INLINE__
|
|
245 # define ARGP_FS_EI inline
|
|
246 # elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
|
|
247 # define ARGP_FS_EI extern inline __attribute__ ((__gnu_inline__))
|
|
248 # else
|
|
249 # define ARGP_FS_EI extern inline
|
|
250 # endif
|
|
251 # else
|
|
252 /* With other compilers, assume the ISO C99 meaning of 'inline', if
|
|
253 the compiler supports 'inline' at all. */
|
|
254 # define ARGP_FS_EI inline
|
|
255 # endif
|
|
256 #endif
|
|
257
|
|
258 ARGP_FS_EI size_t
|
|
259 __argp_fmtstream_write (argp_fmtstream_t __fs,
|
|
260 const char *__str, size_t __len)
|
|
261 {
|
|
262 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
|
|
263 {
|
|
264 memcpy (__fs->p, __str, __len);
|
|
265 __fs->p += __len;
|
|
266 return __len;
|
|
267 }
|
|
268 else
|
|
269 return 0;
|
|
270 }
|
|
271
|
|
272 ARGP_FS_EI int
|
|
273 __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str)
|
|
274 {
|
|
275 size_t __len = strlen (__str);
|
|
276 if (__len)
|
|
277 {
|
|
278 size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
|
|
279 return __wrote == __len ? 0 : -1;
|
|
280 }
|
|
281 else
|
|
282 return 0;
|
|
283 }
|
|
284
|
|
285 ARGP_FS_EI int
|
|
286 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
|
|
287 {
|
|
288 if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
|
|
289 return *__fs->p++ = __ch;
|
|
290 else
|
|
291 return EOF;
|
|
292 }
|
|
293
|
|
294 /* Set __FS's left margin to __LMARGIN and return the old value. */
|
|
295 ARGP_FS_EI size_t
|
|
296 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
|
|
297 {
|
|
298 size_t __old;
|
|
299 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
|
|
300 __argp_fmtstream_update (__fs);
|
|
301 __old = __fs->lmargin;
|
|
302 __fs->lmargin = __lmargin;
|
|
303 return __old;
|
|
304 }
|
|
305
|
|
306 /* Set __FS's right margin to __RMARGIN and return the old value. */
|
|
307 ARGP_FS_EI size_t
|
|
308 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
|
|
309 {
|
|
310 size_t __old;
|
|
311 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
|
|
312 __argp_fmtstream_update (__fs);
|
|
313 __old = __fs->rmargin;
|
|
314 __fs->rmargin = __rmargin;
|
|
315 return __old;
|
|
316 }
|
|
317
|
|
318 /* Set FS's wrap margin to __WMARGIN and return the old value. */
|
|
319 ARGP_FS_EI size_t
|
|
320 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
|
|
321 {
|
|
322 size_t __old;
|
|
323 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
|
|
324 __argp_fmtstream_update (__fs);
|
|
325 __old = __fs->wmargin;
|
|
326 __fs->wmargin = __wmargin;
|
|
327 return __old;
|
|
328 }
|
|
329
|
|
330 /* Return the column number of the current output point in __FS. */
|
|
331 ARGP_FS_EI size_t
|
|
332 __argp_fmtstream_point (argp_fmtstream_t __fs)
|
|
333 {
|
|
334 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
|
|
335 __argp_fmtstream_update (__fs);
|
|
336 return __fs->point_col >= 0 ? __fs->point_col : 0;
|
|
337 }
|
|
338
|
|
339 #if !_LIBC
|
|
340 #undef __argp_fmtstream_putc
|
|
341 #undef __argp_fmtstream_puts
|
|
342 #undef __argp_fmtstream_write
|
|
343 #undef __argp_fmtstream_set_lmargin
|
|
344 #undef __argp_fmtstream_set_rmargin
|
|
345 #undef __argp_fmtstream_set_wmargin
|
|
346 #undef __argp_fmtstream_point
|
|
347 #undef __argp_fmtstream_update
|
|
348 #undef __argp_fmtstream_ensure
|
|
349 #endif
|
|
350
|
|
351 #endif /* __OPTIMIZE__ */
|
|
352
|
|
353 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
|
|
354
|
|
355 #endif /* argp-fmtstream.h */
|