]> git.ipfire.org Git - thirdparty/glibc.git/blob - argp/argp-fmtstream.h
Update.
[thirdparty/glibc.git] / argp / argp-fmtstream.h
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>.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
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. */
25
26 #ifndef __ARGP_FMTSTREAM_H__
27 #define __ARGP_FMTSTREAM_H__
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
38 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
39 /* line_wrap_stream is available, so use that. */
40 #define ARGP_FMTSTREAM_USE_LINEWRAP
41 #endif
42
43 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
44 /* Just be a simple wrapper for line_wrap_stream; the semantics are
45 *slightly* different, as line_wrap_stream doesn't actually make a new
46 object, it just modifies the given stream (reversibly) to do
47 line-wrapping. Since we control who uses this code, it doesn't matter. */
48
49 #include <linewrap.h>
50
51 typedef FILE *argp_fmtstream_t;
52
53 #define argp_make_fmtstream line_wrap_stream
54 #define __argp_make_fmtstream line_wrap_stream
55 #define argp_fmtstream_free line_unwrap_stream
56 #define __argp_fmtstream_free line_unwrap_stream
57
58 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
59 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
60 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
61 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
62 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
63 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
64 #define __argp_fmtstream_printf fprintf
65 #define argp_fmtstream_printf fprintf
66
67 #define __argp_fmtstream_lmargin line_wrap_lmargin
68 #define argp_fmtstream_lmargin line_wrap_lmargin
69 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
70 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
71 #define __argp_fmtstream_rmargin line_wrap_rmargin
72 #define argp_fmtstream_rmargin line_wrap_rmargin
73 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
74 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
75 #define __argp_fmtstream_wmargin line_wrap_wmargin
76 #define argp_fmtstream_wmargin line_wrap_wmargin
77 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
78 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
79 #define __argp_fmtstream_point line_wrap_point
80 #define argp_fmtstream_point line_wrap_point
81
82 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
83 /* Guess we have to define our own version. */
84
85 #ifndef __const
86 #define __const const
87 #endif
88 \f
89 struct argp_fmtstream
90 {
91 FILE *stream; /* The stream we're outputting to. */
92
93 size_t lmargin, rmargin; /* Left and right margins. */
94 ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
95
96 /* Point in buffer to which we've processed for wrapping, but not output. */
97 size_t point_offs;
98 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
99 ssize_t point_col;
100
101 char *buf; /* Output buffer. */
102 char *p; /* Current end of text in BUF. */
103 char *end; /* Absolute end of BUF. */
104 };
105
106 typedef struct argp_fmtstream *argp_fmtstream_t;
107
108 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
109 written on it with LMARGIN spaces and limits them to RMARGIN columns
110 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
111 replacing the whitespace before them with a newline and WMARGIN spaces.
112 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
113 Returns NULL if there was an error. */
114 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
115 size_t __lmargin,
116 size_t __rmargin,
117 ssize_t __wmargin);
118 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
119 size_t __lmargin,
120 size_t __rmargin,
121 ssize_t __wmargin);
122
123 /* Flush __FS to its stream, and free it (but don't close the stream). */
124 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
125 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
126
127 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
128 __const char *__fmt, ...)
129 __attribute__ ((__format__ (printf, 2, 3)));
130 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
131 __const char *__fmt, ...)
132 __attribute__ ((__format__ (printf, 2, 3)));
133
134 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
135 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
136
137 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
138 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
139
140 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
141 __const char *__str, size_t __len);
142 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
143 __const char *__str, size_t __len);
144 \f
145 /* Access macros for various bits of state. */
146 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
147 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
148 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
149 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
150 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
151 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
152
153 /* Set __FS's left margin to LMARGIN and return the old value. */
154 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
155 size_t __lmargin);
156 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
157 size_t __lmargin);
158
159 /* Set __FS's right margin to __RMARGIN and return the old value. */
160 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
161 size_t __rmargin);
162 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
163 size_t __rmargin);
164
165 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
166 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
167 size_t __wmargin);
168 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
169 size_t __wmargin);
170
171 /* Return the column number of the current output point in __FS. */
172 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
173 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
174
175 /* Internal routines. */
176 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
177 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
178 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
179 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
180 \f
181 #ifdef __OPTIMIZE__
182 /* Inline versions of above routines. */
183
184 #if !_LIBC
185 #define __argp_fmtstream_putc argp_fmtstream_putc
186 #define __argp_fmtstream_puts argp_fmtstream_puts
187 #define __argp_fmtstream_write argp_fmtstream_write
188 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
189 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
190 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
191 #define __argp_fmtstream_point argp_fmtstream_point
192 #define __argp_fmtstream_update _argp_fmtstream_update
193 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
194 #endif
195
196 #ifndef ARGP_FS_EI
197 #define ARGP_FS_EI extern inline
198 #endif
199
200 ARGP_FS_EI size_t
201 __argp_fmtstream_write (argp_fmtstream_t __fs,
202 __const char *__str, size_t __len)
203 {
204 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
205 {
206 memcpy (__fs->p, __str, __len);
207 __fs->p += __len;
208 return __len;
209 }
210 else
211 return 0;
212 }
213
214 ARGP_FS_EI int
215 __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
216 {
217 size_t __len = strlen (__str);
218 if (__len)
219 {
220 size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
221 return __wrote == __len ? 0 : -1;
222 }
223 else
224 return 0;
225 }
226
227 ARGP_FS_EI int
228 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
229 {
230 if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
231 return *__fs->p++ = __ch;
232 else
233 return EOF;
234 }
235
236 /* Set __FS's left margin to __LMARGIN and return the old value. */
237 ARGP_FS_EI size_t
238 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
239 {
240 size_t __old;
241 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
242 __argp_fmtstream_update (__fs);
243 __old = __fs->lmargin;
244 __fs->lmargin = __lmargin;
245 return __old;
246 }
247
248 /* Set __FS's right margin to __RMARGIN and return the old value. */
249 ARGP_FS_EI size_t
250 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
251 {
252 size_t __old;
253 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
254 __argp_fmtstream_update (__fs);
255 __old = __fs->rmargin;
256 __fs->rmargin = __rmargin;
257 return __old;
258 }
259
260 /* Set FS's wrap margin to __WMARGIN and return the old value. */
261 ARGP_FS_EI size_t
262 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
263 {
264 size_t __old;
265 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
266 __argp_fmtstream_update (__fs);
267 __old = __fs->wmargin;
268 __fs->wmargin = __wmargin;
269 return __old;
270 }
271
272 /* Return the column number of the current output point in __FS. */
273 ARGP_FS_EI size_t
274 __argp_fmtstream_point (argp_fmtstream_t __fs)
275 {
276 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
277 __argp_fmtstream_update (__fs);
278 return __fs->point_col >= 0 ? __fs->point_col : 0;
279 }
280
281 #if !_LIBC
282 #undef __argp_fmtstream_putc
283 #undef __argp_fmtstream_puts
284 #undef __argp_fmtstream_write
285 #undef __argp_fmtstream_set_lmargin
286 #undef __argp_fmtstream_set_rmargin
287 #undef __argp_fmtstream_set_wmargin
288 #undef __argp_fmtstream_point
289 #undef __argp_fmtstream_update
290 #undef __argp_fmtstream_ensure
291 #endif
292
293 #endif /* __OPTIMIZE__ */
294
295 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
296
297 #endif /* __ARGP_FMTSTREAM_H__ */