WvStreams
argp-fmtstream.h
1/* Word-wrapping and line-truncating streams.
2 Copyright (C) 1997, 2003 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#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
32
33#if _LIBC || (defined (HAVE_FLOCKFILE) && defined(HAVE_PUTC_UNLOCKED) \
34 && defined (HAVE_FPUTS_UNLOCKED) && defined (HAVE_FWRITE_UNLOCKED) )
35/* Use locking funxtions */
36# define FLOCKFILE(f) flockfile(f)
37# define FUNLOCKFILE(f) funlockfile(f)
38# define PUTC_UNLOCKED(c, f) putc_unlocked((c), (f))
39# define FPUTS_UNLOCKED(s, f) fputs_unlocked((s), (f))
40# define FWRITE_UNLOCKED(b, s, n, f) fwrite_unlocked((b), (s), (n), (f))
41#else
42/* Disable stdio locking */
43# define FLOCKFILE(f)
44# define FUNLOCKFILE(f)
45#ifndef _WIN32
46# define PUTC_UNLOCKED(c, f) putc((c), (f))
47#else
48/* Mingw does something really dumb with putc (probably evaluating the 'stream'
49 * argument to it, which putc can technically do by the spec), which causes it
50 * to fatally abort. If we use fputc, this problem goes away
51 */
52# define PUTC_UNLOCKED(c, f) fputc((c), (f))
53#endif
54# define FPUTS_UNLOCKED(s, f) fputs((s), (f))
55# define FWRITE_UNLOCKED(b, s, n, f) fwrite((b), (s), (n), (f))
56#endif /* No thread safe i/o */
57
58#if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
59 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
60/* line_wrap_stream is available, so use that. */
61#define ARGP_FMTSTREAM_USE_LINEWRAP
62#endif
63
64#ifdef ARGP_FMTSTREAM_USE_LINEWRAP
65/* Just be a simple wrapper for line_wrap_stream; the semantics are
66 *slightly* different, as line_wrap_stream doesn't actually make a new
67 object, it just modifies the given stream (reversibly) to do
68 line-wrapping. Since we control who uses this code, it doesn't matter. */
69
70#include <linewrap.h>
71
72typedef FILE *argp_fmtstream_t;
73
74#define argp_make_fmtstream line_wrap_stream
75#define __argp_make_fmtstream line_wrap_stream
76#define argp_fmtstream_free line_unwrap_stream
77#define __argp_fmtstream_free line_unwrap_stream
78
79#define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
80#define argp_fmtstream_putc(fs,ch) putc(ch,fs)
81#define __argp_fmtstream_puts(fs,str) fputs(str,fs)
82#define argp_fmtstream_puts(fs,str) fputs(str,fs)
83#define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
84#define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
85#define __argp_fmtstream_printf fprintf
86#define argp_fmtstream_printf fprintf
87
88#define __argp_fmtstream_lmargin line_wrap_lmargin
89#define argp_fmtstream_lmargin line_wrap_lmargin
90#define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
91#define argp_fmtstream_set_lmargin line_wrap_set_lmargin
92#define __argp_fmtstream_rmargin line_wrap_rmargin
93#define argp_fmtstream_rmargin line_wrap_rmargin
94#define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
95#define argp_fmtstream_set_rmargin line_wrap_set_rmargin
96#define __argp_fmtstream_wmargin line_wrap_wmargin
97#define argp_fmtstream_wmargin line_wrap_wmargin
98#define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
99#define argp_fmtstream_set_wmargin line_wrap_set_wmargin
100#define __argp_fmtstream_point line_wrap_point
101#define argp_fmtstream_point line_wrap_point
102
103#else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
104/* Guess we have to define our own version. */
105
106#ifndef __const
107#define __const const
108#endif
109
110
112{
113 FILE *stream; /* The stream we're outputting to. */
114
115 size_t lmargin, rmargin; /* Left and right margins. */
116 ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */
117
118 /* Point in buffer to which we've processed for wrapping, but not output. */
119 size_t point_offs;
120 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
121 ssize_t point_col;
122
123 char *buf; /* Output buffer. */
124 char *p; /* Current end of text in BUF. */
125 char *end; /* Absolute end of BUF. */
126};
127
128typedef struct argp_fmtstream *argp_fmtstream_t;
129
130/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
131 written on it with LMARGIN spaces and limits them to RMARGIN columns
132 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
133 replacing the whitespace before them with a newline and WMARGIN spaces.
134 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
135 Returns NULL if there was an error. */
136extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
137 size_t __lmargin,
138 size_t __rmargin,
139 ssize_t __wmargin);
140extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
141 size_t __lmargin,
142 size_t __rmargin,
143 ssize_t __wmargin);
144
145/* Flush __FS to its stream, and free it (but don't close the stream). */
146extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
147extern void argp_fmtstream_free (argp_fmtstream_t __fs);
148
149extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
150 __const char *__fmt, ...)
151 PRINTF_STYLE(2,3);
152extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
153 __const char *__fmt, ...)
154 PRINTF_STYLE(2,3);
155
156extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
157extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
158
159extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
160extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
161
162extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
163 __const char *__str, size_t __len);
164extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
165 __const char *__str, size_t __len);
166
167/* Access macros for various bits of state. */
168#define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
169#define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
170#define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
171#define __argp_fmtstream_lmargin argp_fmtstream_lmargin
172#define __argp_fmtstream_rmargin argp_fmtstream_rmargin
173#define __argp_fmtstream_wmargin argp_fmtstream_wmargin
174
175/* Set __FS's left margin to LMARGIN and return the old value. */
176extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
177 size_t __lmargin);
178extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
179 size_t __lmargin);
180
181/* Set __FS's right margin to __RMARGIN and return the old value. */
182extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
183 size_t __rmargin);
184extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
185 size_t __rmargin);
186
187/* Set __FS's wrap margin to __WMARGIN and return the old value. */
188extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
189 size_t __wmargin);
190extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
191 size_t __wmargin);
192
193/* Return the column number of the current output point in __FS. */
194extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
195extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
196
197/* Internal routines. */
198extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
199extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
200extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
201extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
202
203#ifdef __OPTIMIZE__
204/* Inline versions of above routines. */
205
206#if !_LIBC
207#define __argp_fmtstream_putc argp_fmtstream_putc
208#define __argp_fmtstream_puts argp_fmtstream_puts
209#define __argp_fmtstream_write argp_fmtstream_write
210#define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
211#define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
212#define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
213#define __argp_fmtstream_point argp_fmtstream_point
214#define __argp_fmtstream_update _argp_fmtstream_update
215#define __argp_fmtstream_ensure _argp_fmtstream_ensure
216#endif
217
218#ifndef ARGP_FS_EI
219#define ARGP_FS_EI extern inline
220#endif
221
222ARGP_FS_EI size_t
223__argp_fmtstream_write (argp_fmtstream_t __fs,
224 __const char *__str, size_t __len)
225{
226 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
227 {
228 memcpy (__fs->p, __str, __len);
229 __fs->p += __len;
230 return __len;
231 }
232 else
233 return 0;
234}
235
236ARGP_FS_EI int
237__argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
238{
239 size_t __len = strlen (__str);
240 if (__len)
241 {
242 size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
243 return __wrote == __len ? 0 : -1;
244 }
245 else
246 return 0;
247}
248
249ARGP_FS_EI int
250__argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
251{
252 if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
253 return *__fs->p++ = __ch;
254 else
255 return EOF;
256}
257
258/* Set __FS's left margin to __LMARGIN and return the old value. */
259ARGP_FS_EI size_t
260__argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
261{
262 size_t __old;
263 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
264 __argp_fmtstream_update (__fs);
265 __old = __fs->lmargin;
266 __fs->lmargin = __lmargin;
267 return __old;
268}
269
270/* Set __FS's right margin to __RMARGIN and return the old value. */
271ARGP_FS_EI size_t
272__argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
273{
274 size_t __old;
275 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
276 __argp_fmtstream_update (__fs);
277 __old = __fs->rmargin;
278 __fs->rmargin = __rmargin;
279 return __old;
280}
281
282/* Set FS's wrap margin to __WMARGIN and return the old value. */
283ARGP_FS_EI size_t
284__argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
285{
286 size_t __old;
287 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
288 __argp_fmtstream_update (__fs);
289 __old = __fs->wmargin;
290 __fs->wmargin = __wmargin;
291 return __old;
292}
293
294/* Return the column number of the current output point in __FS. */
295ARGP_FS_EI size_t
296__argp_fmtstream_point (argp_fmtstream_t __fs)
297{
298 if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
299 __argp_fmtstream_update (__fs);
300 return __fs->point_col >= 0 ? __fs->point_col : 0;
301}
302
303#if !_LIBC
304#undef __argp_fmtstream_putc
305#undef __argp_fmtstream_puts
306#undef __argp_fmtstream_write
307#undef __argp_fmtstream_set_lmargin
308#undef __argp_fmtstream_set_rmargin
309#undef __argp_fmtstream_set_wmargin
310#undef __argp_fmtstream_point
311#undef __argp_fmtstream_update
312#undef __argp_fmtstream_ensure
313#endif
314
315#endif /* __OPTIMIZE__ */
316
317#endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
318
319#endif /* argp-fmtstream.h */