(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / argp / argp-fmtstream.h
blob5d0d5eac92a783b8a48df5c3a677405ebac9fe22
1 /* Word-wrapping and line-truncating streams.
2 Copyright (C) 1997 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 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
37 #ifndef __attribute__
38 /* This feature is available in gcc versions 2.5 and later. */
39 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
40 # define __attribute__(Spec) /* empty */
41 # endif
42 /* The __-protected variants of `format' and `printf' attributes
43 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
44 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || __STRICT_ANSI__
45 # define __format__ format
46 # define __printf__ printf
47 # endif
48 #endif
50 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
51 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
52 /* line_wrap_stream is available, so use that. */
53 #define ARGP_FMTSTREAM_USE_LINEWRAP
54 #endif
56 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
57 /* Just be a simple wrapper for line_wrap_stream; the semantics are
58 *slightly* different, as line_wrap_stream doesn't actually make a new
59 object, it just modifies the given stream (reversibly) to do
60 line-wrapping. Since we control who uses this code, it doesn't matter. */
62 #include <linewrap.h>
64 typedef FILE *argp_fmtstream_t;
66 #define argp_make_fmtstream line_wrap_stream
67 #define __argp_make_fmtstream line_wrap_stream
68 #define argp_fmtstream_free line_unwrap_stream
69 #define __argp_fmtstream_free line_unwrap_stream
71 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
72 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
73 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
74 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
75 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
76 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
77 #define __argp_fmtstream_printf fprintf
78 #define argp_fmtstream_printf fprintf
80 #define __argp_fmtstream_lmargin line_wrap_lmargin
81 #define argp_fmtstream_lmargin line_wrap_lmargin
82 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
83 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
84 #define __argp_fmtstream_rmargin line_wrap_rmargin
85 #define argp_fmtstream_rmargin line_wrap_rmargin
86 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
87 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
88 #define __argp_fmtstream_wmargin line_wrap_wmargin
89 #define argp_fmtstream_wmargin line_wrap_wmargin
90 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
91 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
92 #define __argp_fmtstream_point line_wrap_point
93 #define argp_fmtstream_point line_wrap_point
95 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
96 /* Guess we have to define our own version. */
98 #ifndef __const
99 #define __const const
100 #endif
102 struct argp_fmtstream
104 FILE *stream; /* The stream we're outputting to. */
106 size_t lmargin, rmargin; /* Left and right margins. */
107 ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
109 /* Point in buffer to which we've processed for wrapping, but not output. */
110 size_t point_offs;
111 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
112 ssize_t point_col;
114 char *buf; /* Output buffer. */
115 char *p; /* Current end of text in BUF. */
116 char *end; /* Absolute end of BUF. */
119 typedef struct argp_fmtstream *argp_fmtstream_t;
121 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
122 written on it with LMARGIN spaces and limits them to RMARGIN columns
123 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
124 replacing the whitespace before them with a newline and WMARGIN spaces.
125 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
126 Returns NULL if there was an error. */
127 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
128 size_t __lmargin,
129 size_t __rmargin,
130 ssize_t __wmargin);
131 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
132 size_t __lmargin,
133 size_t __rmargin,
134 ssize_t __wmargin);
136 /* Flush __FS to its stream, and free it (but don't close the stream). */
137 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
138 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
140 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
141 __const char *__fmt, ...)
142 __attribute__ ((__format__ (printf, 2, 3)));
143 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
144 __const char *__fmt, ...)
145 __attribute__ ((__format__ (printf, 2, 3)));
147 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
148 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
150 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
151 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
153 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
154 __const char *__str, size_t __len);
155 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
156 __const char *__str, size_t __len);
158 /* Access macros for various bits of state. */
159 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
160 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
161 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
162 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
163 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
164 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
166 /* Set __FS's left margin to LMARGIN and return the old value. */
167 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
168 size_t __lmargin);
169 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
170 size_t __lmargin);
172 /* Set __FS's right margin to __RMARGIN and return the old value. */
173 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
174 size_t __rmargin);
175 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
176 size_t __rmargin);
178 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
179 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
180 size_t __wmargin);
181 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
182 size_t __wmargin);
184 /* Return the column number of the current output point in __FS. */
185 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
186 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
188 /* Internal routines. */
189 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
190 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
191 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
192 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
194 #ifdef __OPTIMIZE__
195 /* Inline versions of above routines. */
197 #if !_LIBC
198 #define __argp_fmtstream_putc argp_fmtstream_putc
199 #define __argp_fmtstream_puts argp_fmtstream_puts
200 #define __argp_fmtstream_write argp_fmtstream_write
201 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
202 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
203 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
204 #define __argp_fmtstream_point argp_fmtstream_point
205 #define __argp_fmtstream_update _argp_fmtstream_update
206 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
207 #endif
209 #ifndef ARGP_FS_EI
210 #define ARGP_FS_EI extern inline
211 #endif
213 ARGP_FS_EI size_t
214 __argp_fmtstream_write (argp_fmtstream_t __fs,
215 __const char *__str, size_t __len)
217 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
219 memcpy (__fs->p, __str, __len);
220 __fs->p += __len;
221 return __len;
223 else
224 return 0;
227 ARGP_FS_EI int
228 __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
230 size_t __len = strlen (__str);
231 if (__len)
233 size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
234 return __wrote == __len ? 0 : -1;
236 else
237 return 0;
240 ARGP_FS_EI int
241 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
243 if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
244 return *__fs->p++ = __ch;
245 else
246 return EOF;
249 /* Set __FS's left margin to __LMARGIN and return the old value. */
250 ARGP_FS_EI size_t
251 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
253 size_t __old;
254 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
255 __argp_fmtstream_update (__fs);
256 __old = __fs->lmargin;
257 __fs->lmargin = __lmargin;
258 return __old;
261 /* Set __FS's right margin to __RMARGIN and return the old value. */
262 ARGP_FS_EI size_t
263 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
265 size_t __old;
266 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
267 __argp_fmtstream_update (__fs);
268 __old = __fs->rmargin;
269 __fs->rmargin = __rmargin;
270 return __old;
273 /* Set FS's wrap margin to __WMARGIN and return the old value. */
274 ARGP_FS_EI size_t
275 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
277 size_t __old;
278 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
279 __argp_fmtstream_update (__fs);
280 __old = __fs->wmargin;
281 __fs->wmargin = __wmargin;
282 return __old;
285 /* Return the column number of the current output point in __FS. */
286 ARGP_FS_EI size_t
287 __argp_fmtstream_point (argp_fmtstream_t __fs)
289 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
290 __argp_fmtstream_update (__fs);
291 return __fs->point_col >= 0 ? __fs->point_col : 0;
294 #if !_LIBC
295 #undef __argp_fmtstream_putc
296 #undef __argp_fmtstream_puts
297 #undef __argp_fmtstream_write
298 #undef __argp_fmtstream_set_lmargin
299 #undef __argp_fmtstream_set_rmargin
300 #undef __argp_fmtstream_set_wmargin
301 #undef __argp_fmtstream_point
302 #undef __argp_fmtstream_update
303 #undef __argp_fmtstream_ensure
304 #endif
306 #endif /* __OPTIMIZE__ */
308 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
310 #endif /* argp-fmtstream.h */