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 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 2, or (at your option)
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* This package emulates glibc `line_wrap_stream' semantics for systems that
21 don't have that. If the system does have it, it is just a wrapper for
22 that. This header file is only used internally while compiling argp, and
23 shouldn't be installed. */
25 #ifndef _ARGP_FMTSTREAM_H
26 #define _ARGP_FMTSTREAM_H
36 #include <unlocked-io.h>
39 # ifdef HAVE_FLOCKFILE
40 # define __flockfile flockfile
41 # define __funlockfile funlockfile
44 # define __funlockfile
49 /* This feature is available in gcc versions 2.5 and later. */
50 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
51 # define __attribute__(Spec) /* empty */
53 /* The __-protected variants of `format' and `printf' attributes
54 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
55 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
56 # define __format__ format
57 # define __printf__ printf
61 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
62 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
63 /* line_wrap_stream is available, so use that. */
64 #define ARGP_FMTSTREAM_USE_LINEWRAP
67 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
68 /* Just be a simple wrapper for line_wrap_stream; the semantics are
69 *slightly* different, as line_wrap_stream doesn't actually make a new
70 object, it just modifies the given stream (reversibly) to do
71 line-wrapping. Since we control who uses this code, it doesn't matter. */
75 typedef FILE *argp_fmtstream_t
;
77 #define argp_make_fmtstream line_wrap_stream
78 #define __argp_make_fmtstream line_wrap_stream
79 #define argp_fmtstream_free line_unwrap_stream
80 #define __argp_fmtstream_free line_unwrap_stream
82 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
83 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
84 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
85 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
86 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
87 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
88 #define __argp_fmtstream_printf fprintf
89 #define argp_fmtstream_printf fprintf
91 #define __argp_fmtstream_lmargin line_wrap_lmargin
92 #define argp_fmtstream_lmargin line_wrap_lmargin
93 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
94 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
95 #define __argp_fmtstream_rmargin line_wrap_rmargin
96 #define argp_fmtstream_rmargin line_wrap_rmargin
97 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
98 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
99 #define __argp_fmtstream_wmargin line_wrap_wmargin
100 #define argp_fmtstream_wmargin line_wrap_wmargin
101 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
102 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
103 #define __argp_fmtstream_point line_wrap_point
104 #define argp_fmtstream_point line_wrap_point
106 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
107 /* Guess we have to define our own version. */
110 #define __const const
113 struct argp_fmtstream
115 FILE *stream
; /* The stream we're outputting to. */
117 size_t lmargin
, rmargin
; /* Left and right margins. */
118 ssize_t wmargin
; /* Margin to wrap to, or -1 to truncate. */
120 /* Point in buffer to which we've processed for wrapping, but not output. */
122 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
125 char *buf
; /* Output buffer. */
126 char *p
; /* Current end of text in BUF. */
127 char *end
; /* Absolute end of BUF. */
130 typedef struct argp_fmtstream
*argp_fmtstream_t
;
132 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
133 written on it with LMARGIN spaces and limits them to RMARGIN columns
134 total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
135 replacing the whitespace before them with a newline and WMARGIN spaces.
136 Otherwise, chars beyond RMARGIN are simply dropped until a newline.
137 Returns NULL if there was an error. */
138 extern argp_fmtstream_t
__argp_make_fmtstream (FILE *__stream
,
142 extern argp_fmtstream_t
argp_make_fmtstream (FILE *__stream
,
147 /* Flush __FS to its stream, and free it (but don't close the stream). */
148 extern void __argp_fmtstream_free (argp_fmtstream_t __fs
);
149 extern void argp_fmtstream_free (argp_fmtstream_t __fs
);
151 extern ssize_t
__argp_fmtstream_printf (argp_fmtstream_t __fs
,
152 __const
char *__fmt
, ...)
153 __attribute__ ((__format__ (printf
, 2, 3)));
154 extern ssize_t
argp_fmtstream_printf (argp_fmtstream_t __fs
,
155 __const
char *__fmt
, ...)
156 __attribute__ ((__format__ (printf
, 2, 3)));
158 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
);
159 extern int argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
);
161 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
);
162 extern int argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
);
164 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs
,
165 __const
char *__str
, size_t __len
);
166 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs
,
167 __const
char *__str
, size_t __len
);
169 /* Access macros for various bits of state. */
170 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
171 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
172 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
173 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
174 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
175 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
177 /* Set __FS's left margin to LMARGIN and return the old value. */
178 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
,
180 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
,
183 /* Set __FS's right margin to __RMARGIN and return the old value. */
184 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
,
186 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
,
189 /* Set __FS's wrap margin to __WMARGIN and return the old value. */
190 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
,
192 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
,
195 /* Return the column number of the current output point in __FS. */
196 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs
);
197 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs
);
199 /* Internal routines. */
200 extern void _argp_fmtstream_update (argp_fmtstream_t __fs
);
201 extern void __argp_fmtstream_update (argp_fmtstream_t __fs
);
202 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs
, size_t __amount
);
203 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs
, size_t __amount
);
206 /* Inline versions of above routines. */
209 #define __argp_fmtstream_putc argp_fmtstream_putc
210 #define __argp_fmtstream_puts argp_fmtstream_puts
211 #define __argp_fmtstream_write argp_fmtstream_write
212 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
213 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
214 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
215 #define __argp_fmtstream_point argp_fmtstream_point
216 #define __argp_fmtstream_update _argp_fmtstream_update
217 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
221 #define ARGP_FS_EI extern inline
225 __argp_fmtstream_write (argp_fmtstream_t __fs
,
226 __const
char *__str
, size_t __len
)
228 if (__fs
->p
+ __len
<= __fs
->end
|| __argp_fmtstream_ensure (__fs
, __len
))
230 memcpy (__fs
->p
, __str
, __len
);
239 __argp_fmtstream_puts (argp_fmtstream_t __fs
, __const
char *__str
)
241 size_t __len
= strlen (__str
);
244 size_t __wrote
= __argp_fmtstream_write (__fs
, __str
, __len
);
245 return __wrote
== __len
? 0 : -1;
252 __argp_fmtstream_putc (argp_fmtstream_t __fs
, int __ch
)
254 if (__fs
->p
< __fs
->end
|| __argp_fmtstream_ensure (__fs
, 1))
255 return *__fs
->p
++ = __ch
;
260 /* Set __FS's left margin to __LMARGIN and return the old value. */
262 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs
, size_t __lmargin
)
265 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
266 __argp_fmtstream_update (__fs
);
267 __old
= __fs
->lmargin
;
268 __fs
->lmargin
= __lmargin
;
272 /* Set __FS's right margin to __RMARGIN and return the old value. */
274 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs
, size_t __rmargin
)
277 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
278 __argp_fmtstream_update (__fs
);
279 __old
= __fs
->rmargin
;
280 __fs
->rmargin
= __rmargin
;
284 /* Set FS's wrap margin to __WMARGIN and return the old value. */
286 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs
, size_t __wmargin
)
289 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
290 __argp_fmtstream_update (__fs
);
291 __old
= __fs
->wmargin
;
292 __fs
->wmargin
= __wmargin
;
296 /* Return the column number of the current output point in __FS. */
298 __argp_fmtstream_point (argp_fmtstream_t __fs
)
300 if ((size_t) (__fs
->p
- __fs
->buf
) > __fs
->point_offs
)
301 __argp_fmtstream_update (__fs
);
302 return __fs
->point_col
>= 0 ? __fs
->point_col
: 0;
306 #undef __argp_fmtstream_putc
307 #undef __argp_fmtstream_puts
308 #undef __argp_fmtstream_write
309 #undef __argp_fmtstream_set_lmargin
310 #undef __argp_fmtstream_set_rmargin
311 #undef __argp_fmtstream_set_wmargin
312 #undef __argp_fmtstream_point
313 #undef __argp_fmtstream_update
314 #undef __argp_fmtstream_ensure
317 #endif /* __OPTIMIZE__ */
319 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
321 #endif /* argp-fmtstream.h */